Skip to content

Commit df84e6f

Browse files
author
Ruidy
committed
docs: add examples
1 parent b64bbb9 commit df84e6f

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,34 @@
99

1010
It is mostly a port from the `underscore.js` library based on generics brought by `go1.18`.
1111

12+
## Usage
13+
14+
Please check out the [examples](examples) to see how to use the library.
15+
16+
```go
17+
package main
18+
19+
import (
20+
"fmt"
21+
u "github.com/rjNemo/underscore"
22+
)
23+
24+
func main() {
25+
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
26+
// filter even numbers from the slice
27+
isEven := func(n int) bool { return n%2 == 0 }
28+
evens := u.Filter(numbers, isEven)
29+
// square every number in the slice
30+
toSquare := func(n int) int { return n * n }
31+
squares := u.Map(evens, toSquare)
32+
// reduce to the sum
33+
sum := func(n, acc int) int { return n + acc }
34+
res := u.Reduce(squares, sum, 0)
35+
36+
fmt.Println(res) // 110
37+
}
38+
```
39+
1240
## Getting Started
1341

1442
These instructions will get you a copy of the project up and running on your local machine for development and testing

examples/filterMapReduce.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
u "github.com/rjNemo/underscore"
7+
)
8+
9+
func main() {
10+
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
11+
// filter even numbers from the slice
12+
isEven := func(n int) bool { return n%2 == 0 }
13+
evens := u.Filter(numbers, isEven)
14+
// square every numbers numbers in the slice
15+
toSquare := func(n int) int { return n * n }
16+
squares := u.Map(evens, toSquare)
17+
// reduce to the sum
18+
sum := func(n, acc int) int { return n + acc }
19+
res := u.Reduce(squares, sum, 0)
20+
21+
fmt.Println(res)
22+
}

0 commit comments

Comments
 (0)