Skip to content

Operation contains

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

stream.Contains

It checks if the given element is found in the stream.

Signature

func (s stream.Stream) Contains(element interfaca{})  (found bool,err *errors.Error)

Arguments

Name Type Description
element interface{} Element to be found in the stream

Output

Name Type Description
found bool Returns true if element is found and false if not
err *errors.Error Error in case of something didn't work

Errors

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

Example

package main

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

var stream = koazee.StreamOf([]string{"Dog","Cat","Monkey","Rabbit","Turtle"})

func main() {
  found,_:=stream.Contains("Monkey")
  fmt.Println(found)
}

Download

Benchmark

The searched element is in the first position of the stream of strings

Test Identifier Stream Len Speed
BenchmarkContainsString10FirstElement-4 10 284 ns/op
BenchmarkContainsString100FirstElement-4 100 331 ns/op
BenchmarkContainsString1000FirstElement-4 1000 337 ns/op
BenchmarkContainsString5000FirstElement-4 5000 346 ns/op

The searched element is in the last position of the stream of strings

Test Identifier Stream Len Speed
BenchmarkContainsString10LastElement-4 10 327 ns/op
BenchmarkContainsString100LastElement-4 100 347 ns/op
BenchmarkContainsString1000LastElement-4 1000 1060 ns/op
BenchmarkContainsString5000LastElement-4 5000 4930 ns/op

The searched element doesn't exist in the stream of strings

Test Identifier Stream Len Speed
BenchmarkContainsString10NotFound-4 10 246 ns/op
BenchmarkContainsString100NotFound-4 100 515 ns/op
BenchmarkContainsString1000NotFound-4 1000 1650 ns/op
BenchmarkContainsString5000NotFound-4 5000 8905 ns/op