-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
160 lines (134 loc) · 3.47 KB
/
example_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
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
package gollection_test
import (
"fmt"
"github.com/azihsoyn/gollection"
)
func Example_distinct() {
arr := []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10}
res, err := gollection.New(arr).Distinct().Result()
fmt.Println(res, err)
// Output: [1 2 3 4 5 6 7 8 9 10] <nil>
}
func Example_filter() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
res, err := gollection.New(arr).Filter(func(v int) bool {
return v > 5
}).Result()
fmt.Println(res, err)
// Output: [6 7 8 9 10] <nil>
}
func Example_flatMap() {
arr := [][]int{
{1, 2, 3},
{4, 5},
{6, 7, 8, 9, 10},
}
res, err := gollection.New(arr).FlatMap(func(v int) int {
return v * 2
}).Result()
fmt.Println(res, err)
// Output: [2 4 6 8 10 12 14 16 18 20] <nil>
}
func Example_flatten() {
arr := [][]int{
{1, 2, 3},
{4, 5},
{6, 7, 8, 9, 10},
}
res, err := gollection.New(arr).Flatten().Result()
fmt.Println(res, err)
// Output: [1 2 3 4 5 6 7 8 9 10] <nil>
}
func Example_fold() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
res, err := gollection.New(arr).Fold(100, func(v1, v2 int) int {
return v1 + v2
}).Result()
fmt.Println(res, err)
// Output: 155 <nil>
}
func Example_map() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
res, err := gollection.New(arr).Map(func(v int) int {
return v * 2
}).Result()
fmt.Println(res, err)
// Output: [2 4 6 8 10 12 14 16 18 20] <nil>
}
func Example_reduce() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
res, err := gollection.New(arr).Reduce(func(v1, v2 int) int {
return v1 + v2
}).Result()
fmt.Println(res, err)
// Output: 55 <nil>
}
func Example_skip() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
res, err := gollection.New(arr).Skip(3).Result()
fmt.Println(res, err)
res, err = gollection.New(arr).Skip(30).Result()
fmt.Println(res, err)
// Output: [4 5 6 7 8 9 10] <nil>
// [] <nil>
}
func Example_sort() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
res, err := gollection.New(arr).SortBy(func(v1, v2 int) bool {
return v1 < v2
}).Result()
fmt.Println(arr)
fmt.Println(res, err)
// Output: [1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1]
// [1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10] <nil>
}
func Example_take() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
res, err := gollection.New(arr).Take(3).Result()
fmt.Println(res, err)
res, err = gollection.New(arr).Take(30).Result()
fmt.Println(res, err)
// Output: [1 2 3] <nil>
// [1 2 3 4 5 6 7 8 9 10] <nil>
}
func Example_customType() {
type User struct {
ID int
Name string
}
in := []User{
{ID: 1, Name: "aaa"},
{ID: 2, Name: "bbb"},
{ID: 3, Name: "ccc"},
{ID: 4, Name: "ddd"},
{ID: 5, Name: "eee"},
{ID: 6, Name: "fff"},
{ID: 7, Name: "ggg"},
}
res, err := gollection.New(in).Filter(func(v User) bool {
return v.ID > 5
}).Result()
fmt.Printf("%#v %#v\n", res.([]User), err)
// Output: []gollection_test.User{gollection_test.User{ID:6, Name:"fff"}, gollection_test.User{ID:7, Name:"ggg"}} <nil>
}
func Example_customTypeWithResultAs() {
type User struct {
ID int
Name string
}
in := []User{
{ID: 1, Name: "aaa"},
{ID: 2, Name: "bbb"},
{ID: 3, Name: "ccc"},
{ID: 4, Name: "ddd"},
{ID: 5, Name: "eee"},
{ID: 6, Name: "fff"},
{ID: 7, Name: "ggg"},
}
var res []User
err := gollection.New(in).Filter(func(v User) bool {
return v.ID > 5
}).ResultAs(&res)
fmt.Printf("%#v %#v\n", res, err)
// Output: []gollection_test.User{gollection_test.User{ID:6, Name:"fff"}, gollection_test.User{ID:7, Name:"ggg"}} <nil>
}