Skip to content

Operation indexOf

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

stream.IndexOf

It returns the index of the given element in the stream

Signature

func (s stream.Stream) IndexOf(element interface{}) (index int,err error)

Arguments

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

Output

Name Type Description
out *stream.Output It's a pointer to the below structure
type Output struct {
  value reflect.Value
  error *errors.Error
}

Errors

Type Description
err.items-nil The stream is nil
err.invalid-argument A nil value can not be dropped from 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"
)

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

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

Download

Benchmark

IndexOf the first element in the stream

Test Identifier Stream Len Speed
BenchmarkIndexOfString10FirstElement-4 10 275 ns/op
BenchmarkIndexOfString100FirstElement-4 100 277 ns/op
BenchmarkIndexOfString1000FirstElement-4 1000 280 ns/op
BenchmarkIndexOfString5000FirstElement-4 5000 276 ns/op

IndexOf the last element in the stream

Test Identifier Stream Len Speed
BenchmarkIndexOfString10LastElement-4 10 273 ns/op
BenchmarkIndexOfString100LastElement-4 100 327 ns/op
BenchmarkIndexOfString1000LastElement-4 1000 764 ns/op
BenchmarkIndexOfString5000LastElement-4 5000 3687 ns/op

IndexOf a non found element

Test Identifier Stream Len Speed
BenchmarkIndexOfString10NotFound-4 10 266 ns/op
BenchmarkIndexOfString100NotFound-4 100 337 ns/op
BenchmarkIndexOfString1000NotFound-4 1000 1248 ns/op
BenchmarkIndexOfString5000NotFound-4 5000 7288 ns/op