Skip to content

Operation add

Iván Corrales Solera edited this page Dec 2, 2018 · 8 revisions

stream.Add

It adds a new element in the stream (the element will be added in the last position)

Signature

func (s stream.Stream) Add(element interface{}) (out stream)

Arguments

Name Type Description
element interface{} New value to be added into the stream

Output

Name Type Description
out stream.Stream It returns the stream

Errors

Type Description
err.items-nil The stream is nil
err.invalid-argument A nil value can not be added in a stream of non-pointers values
err.invalid-argument Element type must be the same than the elements in the stream

Example

package main

import (
	"fmt"
	"github.com/wesovilabs/koazee"
)

type Food struct {
	Name    string
	Healthy bool
}

var foodSlice = []*Food{
	{"Vegetables", true},
	{"Nuts", true},
	{"Sweets", false},
}

var stream = koazee.StreamOf(foodSlice)

func main() {
	stream = stream.Add(&Food{"Fruit", true}).Do()
	for _, f := range stream.Out().Val().([]*Food) {
		fmt.Println(f.Name)
	}
}

Download

Benchmark

Adding a new element in a stream of strings

Test Identifier Stream Len Speed
BenchmarkAddString10-4 10 718 ns/op
BenchmarkAddString100-4 100 1276 ns/op
BenchmarkAddString1000-4 1000 7561 ns/op
BenchmarkAddString5000-4 5000 44036 ns/op