From a0944476b963e9d7598c54b1140197e30c61592e Mon Sep 17 00:00:00 2001 From: Ruidy Date: Sat, 1 Jan 2022 14:13:37 -0400 Subject: [PATCH] docs: update examples --- README.md | 17 ++++++++++------- examples/filterMapReduce.go | 9 +++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index e0d68e5..99fbc59 100644 --- a/README.md +++ b/README.md @@ -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 } diff --git a/examples/filterMapReduce.go b/examples/filterMapReduce.go index 76d33b0..a1c5fe1 100644 --- a/examples/filterMapReduce.go +++ b/examples/filterMapReduce.go @@ -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) }