underscore/docs/content/chaining/chaining.md
Ruidy d19fa7b8c9
docsite (#19)
* change documentation theme and structure

* add config files

* fix links

* add build doc command

Co-authored-by: Ruidy <rnemausat@newstore.com>
2022-03-23 10:32:56 -04:00

746 B

title date
Of 2021-12-31T13:11:41-04:00

Calling Of will cause all future method calls to return wrapped objects. When you've finished the computation, call Value to retrieve the final value.

Methods not returning a collection such as Reduce, All, Any, will break the chain and return Value instantly.

package main

import (
	"fmt"
	"github.com/rjNemo/underscore/chain"
)

func main() {
	sum := chain.Of([]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) // 120
}