/
fp_test.go
84 lines (74 loc) · 1.65 KB
/
fp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package fp_test
import (
"fmt"
"strings"
"testing"
)
import (
"github.com/xdave/fp"
)
func TestMapWrongTypePanics(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Errorf("Did not panic as expected")
}
}()
input := "someOddType"
fp.Map(input, func() {})
}
func TestReduceWrongTypePanics(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Errorf("Did not panic as expected")
}
}()
input := "someOddType"
fp.Reduce(input, func() {})
}
func ExampleMapSliceString() {
input := []string{"Hello", "World"}
fmt.Println(fp.Map(input, func(item string, i int) string {
return strings.ToUpper(item)
}))
// Output: [HELLO WORLD]
}
func ExampleMapSliceInt() {
input := []int{1, 2, 3, 4, 5}
fmt.Println(fp.Map(input, func(item, i int) int {
return item * 5
}))
// Output: [5 10 15 20 25]
}
func ExampleMapMapStringInt() {
input := map[string]int{"two": 1, "four": 2, "six": 3, "eight": 4}
result := fp.Map(input, func(item int, i string) int {
return item * 2
}).(map[string]int)
fmt.Println(result["two"], result["four"], result["six"], result["eight"])
// Output: 2 4 6 8
}
func ExampleReduceSlice() {
input := []float64{1.0, 2.0, 3.0, 4.0, 5.0}
fmt.Println(fp.Reduce(input, func(a, b float64) float64 {
return a + b
}))
// Output: 15
}
func ExampleReduceMap() {
input := map[string]int{"foo": 5, "bar": 6, "baz": 7}
fmt.Println(fp.Reduce(input, func(a, b int) int {
return a * b
}))
// Output: 210
}
func ExampleMapReduceSlice() {
input := []int{1, 2, 3, 4}
fmt.Println(fp.Reduce(fp.Map(input, func(item, i int) int {
return item * 5
}), func(a, b int) int {
return a + b
}))
// Output: 50
}