-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
50 lines (35 loc) · 1.03 KB
/
examples_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
package listy_test
import (
"fmt"
"github.com/xdg/listy"
)
func Example() {
strings := []string{"foo", "bar", "bar", "baz"}
ints := []int{3, 1, 4, 1, 5, 9, 2, 6, 5}
// Wrap slices in listy types to "box" them
xs := listy.S(strings)
ys := listy.I(ints)
// Call methods on boxed types. Chain them as needed.
if xs.Contains("baz") {
fmt.Println("Has 'baz'")
}
if ys.Contains(9) {
fmt.Println("Has '9'")
}
uxs := xs.Uniq()
uys := ys.Uniq()
mxs := uxs.Reverse().Map(func(s string) string { return fmt.Sprintf("'%s'", s) })
mys := uys.Reverse().Map(func(i int) int { return -1 * i })
// Unbox them to get built-in slice types back
fmt.Println("Unique xs:", uxs.Unbox())
fmt.Println("Unique ys:", uys.Unbox())
fmt.Println("Reverse mapped unique xs:", mxs.Unbox())
fmt.Println("Reverse mapped unique ys:", mys.Unbox())
// Output:
// Has 'baz'
// Has '9'
// Unique xs: [foo bar baz]
// Unique ys: [3 1 4 5 9 2 6]
// Reverse mapped unique xs: ['baz' 'bar' 'foo']
// Reverse mapped unique ys: [-6 -2 -9 -5 -4 -1 -3]
}