stream.Contains

Check if the given element is found in the stream.

Function signature

    func Contains(item interface{}) (found bool,err *errors.Error)

Arguments

Name Type Description
item Same type of elements in the stream Item to be searched in the stream

Output

Name Type Description
found bool Returns true if element is found and false if not
err *errors.Error Returns nil when the operation was fine and error when something didn’t work

Errors

Type Message
err.items-nil It can not be checked if an element is in a nil stream
err.invalid-argument It can not be checked if an array of non-pointers contains a nil value
err.invalid-argument The stream contains elements of type %s and the passed argument has type %s

Examples

package main

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

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

func main() {
	found, _ := koazee.Stream().
		With(numbers).
		Contains(5)
	if found {
		fmt.Println("The element was found!")
	}
}
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() {
	searched:=newPrimate("Sarah", 7, "Gibbon", female)
	found,_:=koazee.StreamOf(primates).
		Contains(searched)
	if found{
		fmt.Printf("%s is in the list of monkeys invited to the party\n", searched.name)
	}

}