mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 10:36:43 +00:00
* change documentation theme and structure * add config files * fix links * add build doc command Co-authored-by: Ruidy <rnemausat@newstore.com>
31 lines
No EOL
746 B
Markdown
31 lines
No EOL
746 B
Markdown
---
|
|
title: "Of"
|
|
date: 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.
|
|
|
|
```go
|
|
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
|
|
}
|
|
``` |