Repository navigation
Operation filter
Iván Corrales Solera edited this page Dec 2, 2018
·
2 revisions
It discard those elements in the stream that do not match with the given filter.
func (s stream.Stream) Filter(fn func(interface{})bool ) (out stream.Stream)Arguments
| Name | Type | Description |
|---|---|---|
| fn | func | This function must receive 1 arguments, that must have the same type that current elements in the stream. The output of this function will be a bool, being true if the element matches the filter or false if not |
Output
| Name | Type | Description |
|---|---|---|
| out | stream.Stream | It returns the stream |
| Type | Description |
|---|---|
| err.items-nil | The stream is nil |
| err.invalid-argument | The filter operation requires a function as argument |
| err.invalid-argument | The provided function must retrieve 1 argument |
| err.invalid-argument | The provided function must return 1 value |
| err.invalid-argument | The type of the argument in the provided function must be the same than the elements in the stream |
| err.invalid-argument | The type of the output in the provided function must be bool |
package main
import (
"fmt"
"github.com/wesovilabs/koazee"
)
type Food struct {
Name string
Healthy bool
}
var foodSlice = []*Food{
{"Vegetables", true},
{"Nuts", true},
{"Sweets", false},
}
var stream = koazee.StreamOf(foodSlice)
func main() {
stream = stream.Filter(func(f *Food) bool {
return f.Healthy
}).Do()
for _, f := range stream.Out().Val().([]*Food) {
fmt.Println(f.Name)
}
}Removing from the stream those elements that they're not even
| Test Identifier | Stream Len | Speed |
|---|---|---|
| BenchmarkFilterNumbers10LenIsEven-4 | 10 | 850 ns/op |
| BenchmarkFilterNumbers100LenIsEven-4 | 100 | 1785 ns/op |
| BenchmarkFilterNumbers1000LenIsEven-4 | 1000 | 17190 ns/op |
| BenchmarkFilterNumbers5000LenIsEven-4 | 5000 | 26437 ns/op |
@2018 Koazee by Iván Corrales Solera developer@wesovilabs.com