-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples_test.go
More file actions
218 lines (171 loc) · 3.96 KB
/
Copy pathexamples_test.go
File metadata and controls
218 lines (171 loc) · 3.96 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package fn
import (
"errors"
"fmt"
"math"
)
func ExampleAny() {
negativeInts := []int{-1, -2, -3}
anyPositive := Any(negativeInts, func(item int, index int) bool {
return item > 0
})
fmt.Println(anyPositive)
// Output: false
}
func ExampleAnyNonZero() {
nonZeroStrings := []string{"one", "", "", "four"}
fmt.Println(AnyNonZero(nonZeroStrings))
zeroStrings := []string{"", ""}
fmt.Println(AnyNonZero(zeroStrings))
// Output:
// true
// false
}
func ExampleAll() {
positiveInts := []int{1, 2, 3}
allPositive := All(positiveInts, func(item int, index int) bool {
return item > 0
})
fmt.Println(allPositive)
// Output: true
}
func ExampleAllNonZero() {
nonZeroInts := []string{"one", "two", "three"}
fmt.Println(AllNonZero(nonZeroInts))
zeroInts := []string{"", "", ""}
fmt.Println(AllNonZero(zeroInts))
// Output:
// true
// false
}
func ExampleChunk() {
ints := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
fmt.Println(Chunk(ints, 3))
// Output: [[1 2 3] [4 5 6] [7 8 9]]
}
func ExampleChunkWhile() {
ints := []int{1, 2, 4, 5, 7}
fmt.Println(ChunkWhile(ints, func(eltBefore, eltAfter int) bool {
return eltBefore+1 == eltAfter
}))
// Output: [[1 2] [4 5] [7]]
}
func ExampleCompactZero() {
ints := []int{0, 1, 2, 3, 0, 5}
fmt.Println(CompactZero(ints))
// Output: [1 2 3 5]
}
func ExampleCompactNil() {
errs := []error{nil, errors.New("example-error 1"), nil, errors.New("example-error 2")}
fmt.Println(CompactNil(errs))
// Output: [example-error 1 example-error 2]
}
func ExampleEach() {
Each([]string{"a", "b", "c"}, func(item string, index int) {
fmt.Println(item, index)
})
// Output:
// a 0
// b 1
// c 2
}
func ExampleFirst() {
sequence := []int{-1, 0, 1, 2}
firstPositive, index := First(sequence, func(item int, index int) bool {
return item > 0
})
ten, tenIndex := First(sequence, func(item int, index int) bool {
return item == 10
})
fmt.Printf("value %d at index %d\n", firstPositive, index)
fmt.Printf("value %d at index %d\n", ten, tenIndex)
// Output:
// value 1 at index 2
// value 0 at index -1
}
func ExampleFlatten() {
items := [][]string{{"one"}, {"two", "three"}, {"four"}}
fmt.Println(Flatten(items))
// Output: [one two three four]
}
func ExampleMap() {
ints := []int{1, 2, 3}
doubled := Map(ints, func(item int, index int) int {
return item * 2
})
fmt.Println(doubled)
// Output: [2 4 6]
}
func ExampleMust() {
canFail := func(shouldFail bool) (string, error) {
if shouldFail {
return "", fmt.Errorf("error")
}
return "success", nil
}
value := Must(canFail(false))
fmt.Println(value)
// Output: success
}
func ExampleMax() {
fmt.Println(Max(1, 2, 100, -1))
// Output: 100
}
func ExampleMin() {
fmt.Println(Min(-100, -300, 3, 100))
// Output: -300
}
func ExamplePartition() {
values := Seq(0, 10, 1)
evens, odds := Partition(values, func(item, index int) bool {
return item%2 == 0
})
fmt.Println(evens)
fmt.Println(odds)
// Output:
// [0 2 4 6 8]
// [1 3 5 7 9]
}
func ExampleReduce() {
combined := Reduce([]string{"a", "b", "c"}, func(result, item string, index int) string {
return result + item
}, "")
fmt.Println(combined)
// Output: abc
}
func ExampleSelect() {
allInts := []int{1, 2, 3, 4, 5, 6}
isEven := func(number int, index int) bool {
return number%2 == 0
}
evenNumbers := Select(allInts, isEven)
fmt.Println(evenNumbers)
// Output: [2 4 6]
}
func ExampleSeq() {
seq := Seq(0, 50, 5)
fmt.Println(seq)
// Output: [0 5 10 15 20 25 30 35 40 45]
}
func ExampleReject() {
allInts := []int{1, 2, 3, 4, 5, 6}
isEven := func(number int, index int) bool {
return number%2 == 0
}
oddNumbers := Reject(allInts, isEven)
fmt.Println(oddNumbers)
// Output: [1 3 5]
}
func ExampleSum() {
fmt.Println(Sum([]int{1, 2, 3}))
fmt.Println(Sum([]float64{math.Pi, math.Pi}))
// Output:
// 6
// 6.283185307179586
}
func ExampleZip() {
keys := []string{"one", "two", "three"}
values := []int{1, 2, 3}
fmt.Println(Zip(keys, values))
// Output: map[one:1 three:3 two:2]
}