underscore/examples/chaining.go
Ruidy 2d05b6c518
add unit test pipeline (#8)
* add unit test pipeline

* edit triggers

* change version

* change version

* other try

* other try

* fix: redeclared function name

Co-authored-by: Ruidy <rnemausat@newstore.com>
2022-01-02 03:28:12 +01:00

19 lines
408 B
Go

package examples
import (
"fmt"
u "github.com/rjNemo/underscore"
)
func chaining() {
sum := u.NewChain([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}).
// filter even numbers from the slice
Filter(func(n int) bool { return n%2 == 0 }).
// square every number in the slice
Map(func(n int) int { return n * n }).
// reduce to the sum
Reduce(func(n, acc int) int { return n + acc }, 0)
fmt.Println(sum)
}