Skip to content

Commit 479e99e

Browse files
authored
Merge pull request #10 from thecasualcoder/documentation-and-examples
Add documentation and examples
2 parents cf513bc + 5d7dd13 commit 479e99e

File tree

5 files changed

+104
-23
lines changed

5 files changed

+104
-23
lines changed

README.md

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ This library heavily makes use of `reflect` package and hence will have an **imp
2020

2121
### Map
2222

23-
Map applies a mapper function on each element of an input and sets it in output.
24-
25-
_Primitive types_
23+
Map applies a mapper function on each element of an input and sets it in output.
24+
For more [docs](https://godoc.org/github.com/thecasualcoder/godash#Map).
2625

2726
```go
2827
func main() {
@@ -37,8 +36,6 @@ func main() {
3736
}
3837
```
3938

40-
_Struct type_
41-
4239
```go
4340
type Person struct {
4441
Name string
@@ -62,7 +59,8 @@ func main() {
6259

6360
### Filter
6461

65-
Filter out elements that fail the predicate
62+
Filter out elements that fail the predicate.
63+
For more [docs](https://godoc.org/github.com/thecasualcoder/godash#Filter).
6664

6765
```go
6866
func main() {
@@ -95,31 +93,38 @@ func main() {
9593

9694
### Reduce
9795

98-
Reduce reduces the given collection using given reduce function
99-
100-
_Primitive types_
96+
Reduce can accept a reducer and apply the reducer on each element of the input slice while providing an accumulator to save the reduce output
97+
For more [docs](https://godoc.org/github.com/thecasualcoder/godash#Reduce).
10198

10299
```go
103100
func main() {
104-
input := []int{1, 2, 3, 4, 5}
105-
var output int
106-
107-
godash.Reduce(input, &output, func(sum, element int) int {
108-
return sum + element
101+
input := []string{"count", "words", "and", "print", "words", "count"}
102+
accumulator := map[string]int{}
103+
104+
_ = godash.Reduce(input, &accumulator, func(acc map[string]int, element string) map[string]int {
105+
if _, present := acc[element]; present {
106+
acc[element] = acc[element] + 1
107+
} else {
108+
acc[element] = 1
109+
}
110+
return acc
109111
})
110112

111-
fmt.Println(output) // prints 15
112-
}
113-
```
113+
bytes, _ := json.MarshalIndent(accumulator, "", " ")
114+
fmt.Println(string(bytes))
114115

115-
_Struct type_
116+
// Output:
117+
//{
118+
// "and": 1,
119+
// "count": 2,
120+
// "print": 1,
121+
// "words": 2
122+
//}
116123

117-
```go
118-
type Person struct {
119-
Name string
120-
Age Int
121124
}
125+
```
122126

127+
```go
123128
func main() {
124129
input := []Person{
125130
{Name: "John", Age: 22},

filter.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,20 @@ import (
55
"reflect"
66
)
77

8+
// Filter out elements that fail the predicate.
9+
//
10+
// Input of type slice is supported as of now.
11+
// Output is a slice in which filtered-in elements are stored.
12+
// PredicateFn function is applied on each element of input to determine to filter or not
13+
//
14+
// Validations:
15+
//
16+
// 1. Input and Output's slice should be of same type
17+
// 2. Predicate function can take one argment and return one argument
18+
// 3. Predicate's return argument is always boolean.
19+
// 4. Predicate's input argument should be input/output slice's element type.
20+
//
21+
// Validation errors are returned to the caller.
822
func Filter(in, out, predicateFn interface{}) error {
923
input := reflect.ValueOf(in)
1024

filter_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package godash_test
22

33
import (
4+
"fmt"
45
"github.com/stretchr/testify/assert"
56
"github.com/thecasualcoder/godash"
67
"testing"
@@ -101,3 +102,21 @@ func TestFilter(t *testing.T) {
101102
}
102103
})
103104
}
105+
106+
func ExampleFilter() {
107+
input := []string{
108+
"rhythm",
109+
"of",
110+
"life",
111+
}
112+
var output []string
113+
114+
_ = godash.Filter(input, &output, func(in string) bool {
115+
return len(in) > 3
116+
})
117+
118+
fmt.Println(output)
119+
120+
// Output:
121+
// [rhythm life]
122+
}

reduce.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,23 @@ import (
55
"reflect"
66
)
77

8-
// Reduce reduces the given collection using given reduce function
8+
// Reduce can accept a reducer and apply the reducer on each element
9+
// of the input slice while providing an accumulator to save the reduce output.
10+
//
11+
// Input of type slice is supported as of now.
12+
// Output is the accumulator.
13+
// ReduceFn is the reducer function.
14+
//
15+
// Whatever ReduceFn returns is fed as accumulator for the next iteration.
16+
// Reduction happens from left-to-right.
17+
//
18+
// Reduce does the following validations:
19+
//
20+
// 1. Reducer function should accept exactly 2 arguments and return 1 argument
21+
// 2. Reducer function's second argument should be the same type as input slice's element type
22+
// 3. Reducer function's return type should be the same as that of the accumulator
23+
//
24+
// Validation errors are returned to the caller.
925
func Reduce(in, out, reduceFn interface{}) error {
1026
input := reflect.ValueOf(in)
1127
output := reflect.ValueOf(out)

reduce_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package godash_test
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"github.com/stretchr/testify/assert"
57
"github.com/thecasualcoder/godash"
68
"strconv"
@@ -178,3 +180,28 @@ func TestReduce(t *testing.T) {
178180
}
179181
})
180182
}
183+
184+
func ExampleReduce() {
185+
input := []string{"count", "words", "and", "print", "words", "count"}
186+
accumulator := map[string]int{}
187+
188+
_ = godash.Reduce(input, &accumulator, func(acc map[string]int, element string) map[string]int {
189+
if _, present := acc[element]; present {
190+
acc[element] = acc[element] + 1
191+
} else {
192+
acc[element] = 1
193+
}
194+
return acc
195+
})
196+
197+
bytes, _ := json.MarshalIndent(accumulator, "", " ")
198+
fmt.Println(string(bytes))
199+
200+
// Output:
201+
//{
202+
// "and": 1,
203+
// "count": 2,
204+
// "print": 1,
205+
// "words": 2
206+
//}
207+
}

0 commit comments

Comments
 (0)