Skip to content

Operation set

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

stream.Set

It replaces the element in the given index by the provided value

Signature

func (s stream.Stream) Set(index int, value interface{}) (out stream)

Arguments

Name Type Description
index int Index
value 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.Set(0, &Food{"Fruit", true}).Do()
	for _, f := range stream.Out().Val().([]*Food) {
		fmt.Println(f.Name)
	}
}

Download

Benchmark

Repleace element in position 0

Test Identifier Stream Len Speed
BenchmarkSetString10-4 10 771 ns/op
BenchmarkSetString100-4 100 1451 ns/op
BenchmarkSetString1000-4 1000 6717 ns/op
BenchmarkSetString5000-4 5000 32890 ns/op