Skip to content

Operation foreach

Iván Corrales Solera edited this page Dec 2, 2018 · 1 revision

stream.ForEach

It does something over all the elements in the stream.

Signature

func (s stream.Stream) Filter(fn func(interface{}) ) (out stream.Stream)

Arguments

Name Type Description
fn func This function must receive an argument of type the same that the elements in the stream and it doesn’t return any value

Output

N/A

Errors

Type Description
err.items-nil The stream is nil
err.invalid-argument The forEach operation requires a function as argument
err.invalid-argument The provided function must retrieve 1 argument
err.invalid-argument The provided function can not return any value
err.invalid-argument The type of the argument in the provided function must be the same than the elements in the stream

Example

package main

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

var stream = koazee.StreamOf([]int{1, 2, 3, 4, 5})

func main() {
  stream.ForEach(func(val int) {fmt.Printf("Double of %d is %d\n",val,2*val) }).Do()
}

Download

Benchmark

Iterating over a stream of int and printing the value

Test Identifier Stream Len Speed
BenchmarkForEachNumbers10Print-4 10 445 ns/op
BenchmarkForEachNumbers100Print-4 100 647 ns/op
BenchmarkForEachNumbers1000Print-4 1000 2980 ns/op
BenchmarkForEachNumbers5000Print-4 5000 7260 ns/op