Function signature
func Filter(fn interface{}) SArguments
| Name | Type | Description |
|---|---|---|
| fn | func | This function must receive an argument of type the same that the elements in the stream and the output must be bool |
Output
| Name | Type | Description |
|---|---|---|
| stream | koazee.S | It returns the stream modified by the current operation |
Errors
| Type | Message |
|---|---|
| err.items-nil | A nil stream can not be filtered |
| 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 %s |
| err.invalid-argument | The type of the output in the provided function must be bool |
Examples
package main
import (
"fmt"
"github.com/wesovilabs/koazee"
)
var numbers = []int{1, 3, 5, 7, 9}
func main() {
elements := koazee.Stream().
With(numbers).
Filter(func(val int) bool {
return val > 5
}).Out().Val().([]int)
for _, element := range elements {
fmt.Printf("Number %d elements is in the list\n", element)
}
}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() {
filteredPrimates := koazee.StreamOf(primates).
Filter(func(primate *primate) bool {
return primate.age > 10 && primate.genre == female
}).Out().Val().([]*primate)
for _, primate := range filteredPrimates {
fmt.Printf("%s is a female and is %d years\n", primate.name, primate.age)
}
}