-
Notifications
You must be signed in to change notification settings - Fork 31
Operation reduce
Iván Corrales Solera edited this page Dec 2, 2018
·
1 revision
It reduces the stream to a single value by executing a provided function for each value of the stream.
func (s stream.Stream) Map(fn func{interface{},interface{}}interfac€{}) (out *stream.Output)
Arguments
Name | Type | Description |
---|---|---|
fn | func | This function must receive two arguments, the first argument is an accumulator and the second a element of the same type that the elements in the stream. The output of this function will have the same type than the accumulator |
Output
Name | Type | Description |
---|---|---|
out | *stream.Output | It's a pointer to the below structure |
type Output struct {
value reflect.Value
error *errors.Error
}
Type | Description |
---|---|
err.items-nil | The stream is nil |
err.invalid-argument | The reduce operation requires a function as argument |
err.invalid-argument | The provided function must retrieve 2 arguments |
err.invalid-argument | The provided function must return 1 value |
err.invalid-argument | The type of the second argument in the provided function must be the same of the elements in the stream |
err.invalid-argument | The type of the first argument and the output in the provided function must be the same |
package main
import (
"fmt"
"github.com/wesovilabs/koazee"
)
var stream = koazee.StreamOf([]int{1, 2, 3, 4, 5})
func main() {
out := stream.Reduce(func(acc, val int) int { return acc + val })
fmt.Println(out.Int())
}
Sum the len of all the string elements in a tream
Test Identifier | Stream Len | Speed |
---|---|---|
BenchmarkReduceString10SumLen-4 | 10 | 355 ns/op |
BenchmarkReduceString100SumLen-4 | 100 | 723 ns/op |
BenchmarkReduceString1000SumLen-4 | 1000 | 4254 ns/op |
BenchmarkReduceString5000SumLen-4 | 5000 | 21286 ns/op |
Sum all the elments in a stream of ints
Test Identifier | Stream Len | Speed |
---|---|---|
BenchmarkReduceInt10Sum-4 | 10 | 324 ns/op |
BenchmarkReduceInt100Sum-4 | 199 | 629 ns/op |
BenchmarkReduceInt1000Sum-4 | 1999 | 3782 ns/op |
BenchmarkReduceInt5000Sum-4 | 5000 | 9533 ns/op |
|
@2018 Koazee by Iván Corrales Solera [email protected]