docs: update examples

This commit is contained in:
Ruidy 2022-01-01 14:13:37 -04:00
parent f05fb70292
commit a0944476b9
2 changed files with 13 additions and 13 deletions

View file

@ -11,6 +11,12 @@ It is mostly a port from the `underscore.js` library based on generics brought b
## Usage
Install the library using
```shell
go get github.com/rjNemo/underscore
```
Please check out the [examples](examples) to see how to use the library.
```go
@ -24,14 +30,11 @@ import (
func main() {
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
// filter even numbers from the slice
isEven := func(n int) bool { return n%2 == 0 }
evens := u.Filter(numbers, isEven)
evens := u.Filter(numbers, func(n int) bool { return n%2 == 0 })
// square every number in the slice
toSquare := func(n int) int { return n * n }
squares := u.Map(evens, toSquare)
// reduce to the sum
sum := func(n, acc int) int { return n + acc }
res := u.Reduce(squares, sum, 0)
squares := u.Map(evens, func(n int) int { return n * n })
// reduce to the sum
res := u.Reduce(squares, func(n, acc int) int { return n + acc }, 0)
fmt.Println(res) // 120
}

View file

@ -9,14 +9,11 @@ import (
func main() {
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
// filter even numbers from the slice
isEven := func(n int) bool { return n%2 == 0 }
evens := u.Filter(numbers, isEven)
evens := u.Filter(numbers, func(n int) bool { return n%2 == 0 })
// square every number in the slice
toSquare := func(n int) int { return n * n }
squares := u.Map(evens, toSquare)
squares := u.Map(evens, func(n int) int { return n * n })
// reduce to the sum
sum := func(n, acc int) int { return n + acc }
res := u.Reduce(squares, sum, 0)
res := u.Reduce(squares, func(n, acc int) int { return n + acc }, 0)
fmt.Println(res)
}