Skip to content

Commit b9cff70

Browse files
committed
Update README.md
1 parent 204d699 commit b9cff70

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

README.md

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22

33
Inspired from [Lodash](https://github.com/lodash/lodash) for golang
44

5-
# Functions
5+
## Why?
66

7-
## Map
7+
I did not like most map/reduce implementations that returned an `interface{}` which had to be typecasted. This library follows the concept of how `json.Marshal` works. Create an output variable **outside** the functions and pass a **pointer reference** to it, so it can be **set**.
8+
This library heavily makes use of `reflect` package and hence will have an **impact on performance**. Use it with care. All functions have **validations** on how mapper function/predicate functions should be written. So even if we lose out on compile time validation, the library still **does not panic** if it does not know how to handle an argument passed to it.
9+
10+
## Available Functions
11+
12+
1. [Map](#Map)
13+
2. [Filter](#Filter)
14+
15+
## Usages
16+
17+
### Map
818

919
Map applies a mapper function on each element of an input and sets it in output.
1020

@@ -13,9 +23,9 @@ _Primitive types_
1323
```go
1424
func main() {
1525
input := []int{1, 2, 3, 4, 5}
16-
output := make([]int, 0)
26+
var output []int
1727

18-
_ = godash.Map(input, &output, func(el int) int {
28+
godash.Map(input, &output, func(el int) int {
1929
return el * el
2030
})
2131

@@ -36,12 +46,45 @@ func main() {
3646
{Name: "John", Age: 22},
3747
{Name: "Doe", Age: 23},
3848
}
39-
output := make([]string, 0)
49+
var output []string
4050

41-
_ = godash.Map(input, &output, func(person Person) string {
51+
godash.Map(input, &output, func(person Person) string {
4252
return person.Name
4353
})
4454

4555
fmt.Println(output) // prints John Doe
4656
}
4757
```
58+
59+
### Filter
60+
61+
Filter out elements that fail the predicate
62+
63+
```go
64+
func main() {
65+
input := []int{1, 2, 3, 4, 5}
66+
var output []int
67+
68+
godash.Filter(input, &output, func(el int) bool {
69+
return bool % 2 == 0
70+
})
71+
72+
fmt.Println(output) // prints 2 4
73+
}
74+
```
75+
76+
```go
77+
func main() {
78+
input := []Person{
79+
{Name: "John", Age: 20},
80+
{Name: "Doe", Age: 30},
81+
}
82+
var output []string
83+
84+
godash.Filter(input, &output, func(person Person) string {
85+
return person.Age > 25
86+
})
87+
88+
fmt.Println(output) // prints {John 20}
89+
}
90+
```

0 commit comments

Comments
 (0)