Skip to content

Operation take

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

stream.Take

It returns a stream with elements between the given indexes (both included)

Signature

func (s stream.Stream) Take(firstIndex,lastIndex int ) (out stream.Stream)

Arguments

Name Type Description
firstIndex int First index to be taken
lastIndex int Last index to be taken

Output

Name Type Description
out stream.Stream It returns the stream

Errors

Type Description
err.items-nil The stream is nil
err.invalid-index The indexes are not valid

Example

package main

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

type Person struct {
	FirstName string
}

var people = []*Person{
	{"Cat"},
	{"Dog"},
	{"Rat"},
	{"Canary"},
	{"Gibbon"},
}

var stream = koazee.StreamOf(people)

func main() {
	stream = stream.Take(1, 3).Do()
	for _, f := range stream.Out().Val().([]*Person) {
		fmt.Println(f.FirstName)
	}
}

Download

Benchmark

Taking the elements between positions 1 and len-1

Test Identifier Stream Len Speed
BenchmarkTakeNumbers1000Between1and9-4 10 326 ns/op
BenchmarkTakeNumbers1000Between1and99-4 100 323 ns/op
BenchmarkTakeNumbers1000Between1and999-4 1000 335 ns/op
BenchmarkTakeNumbers2500etween1and2499-4 5000 321 ns/op