stream.ForEach

Do something over all the elements in the stream.

Function signature

    func ForEach(fn interface{})

Arguments

Name Type Description
item 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

None

Errors


</tbody>

Type Message
err.items-nil A nil stream can not be used to perform ForEach operation
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 %s

Examples

package main

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

var numbers = []int{1, 3, 5, 7, 9}

func main() {
	koazee.Stream().
		With(numbers).
		ForEach(func(n int) {
			fmt.Printf("The number %d is in the list\n", n)
		}).Out()

}
package main

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

type genre int

const (
	female genre = iota
	male
)

type primate struct {
	name   string
	age    int
	family string
	genre  genre
}

func newPrimate(name string, age int, family string, genre genre) *primate {
	return &primate{
		name:   name,
		age:    age,
		family: family,
		genre:  genre,
	}
}

var primates = []*primate{
	newPrimate("John", 15, "Capuchin", male),
	newPrimate("Laura", 12, "Squirrel monkey", female),
	newPrimate("Benjamin", 23, "Spider monkey", male),
	newPrimate("George", 19, "Golden Lion Tamarin", male),
	newPrimate("Jane", 33, "Orangutan", female),
	newPrimate("Sarah", 7, "Gibbon", female),
}

func main() {
	koazee.StreamOf(primates).
		ForEach(func(primate *primate) {
			fmt.Printf("I am %s\n", primate.name)
		}).Out()
}