Skip to content

Commit 8cd0d36

Browse files
committed
Add example for word count
Signed-off-by: Dinesh <dineshudt17@gmail.com>
1 parent f564a11 commit 8cd0d36

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

reduce_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ func TestReduce(t *testing.T) {
3333
assert.NoError(t, err)
3434
assert.Equal(t, expected, out)
3535
}
36+
{
37+
in := []string{"one", "two", "two", "three", "three", "three"}
38+
out := map[string]int{}
39+
40+
err := godash.Reduce(in, &out, func(acc map[string]int, element string) map[string]int {
41+
if _, present := acc[element]; present {
42+
acc[element] = acc[element] + 1
43+
} else {
44+
acc[element] = 1
45+
}
46+
return acc
47+
})
48+
49+
expected := map[string]int{"one": 1, "two": 2, "three": 3}
50+
assert.NoError(t, err)
51+
assert.Equal(t, expected, out)
52+
}
3653
})
3754

3855
t.Run("support structs", func(t *testing.T) {

0 commit comments

Comments
 (0)