-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
170 lines (155 loc) · 2.8 KB
/
functions.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
161
162
163
164
165
166
167
168
169
170
package fpgo
import "sort"
// mapping T to U
func Map[T, U any](convert Pi1[*T, *U]) F {
return func(s *Shell) *Shell {
res := []U{}
for i, item := range s.V.([]T) {
res = append(res, *convert(i, &item))
}
return &Shell{res}
}
}
// filtering T
func Filter[T any](f Pi1[*T, bool]) F {
return func(s *Shell) *Shell {
res := []T{}
for i, item := range s.V.([]T) {
if f(i, &item) {
res = append(res, item)
}
}
return &Shell{res}
}
}
// slicing T as
func Slice[T any](idxes ...int) F {
return func(s *Shell) *Shell {
var a, b int
var l = len(idxes)
switch l {
case 0:
a = 0
b = l
case 1:
a = idxes[0]
b = l
default:
a = idxes[0]
b = idxes[1]
}
if a < 0 {
a += l
}
if b < 0 {
b += l
}
res := make([]T, b-a)
copy(res, s.V.([]T)[a:b])
return &Shell{res}
}
}
// (newValue, prevValue) -> processedPrevValue
func Reduce[T, U any](reducer Pi2[*T, *U, *U]) F {
return func(s *Shell) *Shell {
res := new(U)
for i, item := range s.V.([]T) {
res = reducer(i, &item, res)
}
return &Shell{res}
}
}
// sort element a, b
func Sort[T any](sorter P2[*T, *T, bool]) F {
return func(s *Shell) *Shell {
ts := s.V.([]T)
sort.Slice(ts, func(i, j int) bool {
return sorter(&ts[i], &ts[j])
})
return s
}
}
// deep copying an array
func ArrayCopy[T any]() F {
return func(s *Shell) *Shell {
t := s.V.([]T)
res := make([]T, len(t))
copy(res, t[:])
return &Shell{res}
}
}
// use pure value
func Func[T, U any](f P1[T, U]) F {
return func(s *Shell) *Shell {
u := f(s.V.(T))
return &Shell{u}
}
}
// true if satisfies at least 1
func Some[T any](f Pi1[*T, bool]) F {
return func(s *Shell) *Shell {
for i, item := range s.V.([]T) {
if f(i, &item) {
return &Shell{true}
}
}
return &Shell{false}
}
}
// true if satisfies all
func Every[T any](f Pi1[*T, bool]) F {
return func(s *Shell) *Shell {
for i, item := range s.V.([]T) {
if !f(i, &item) {
return &Shell{false}
}
}
return &Shell{true}
}
}
// (index, element, array)
func ForEach[T any](f Pi2V[*T, []T]) F {
return func(s *Shell) *Shell {
t := s.V.([]T)
for i, item := range t {
f(i, &item, t)
}
return s
}
}
func Reverse[T any]() F {
return func(s *Shell) *Shell {
t := s.V.([]T)
l := len(t)
res := make([]T, l)
for i, item := range t {
res[l-i-1] = item
}
return &Shell{res}
}
}
// []T -> T
func At[T any](i int) F {
return func(s *Shell) *Shell {
t := s.V.([]T)
l := len(t)
i = (l + i) % l
return &Shell{t[i]}
}
}
// append all elements
func Append[T any](elements []T) F {
return func(s *Shell) *Shell {
t := s.V.([]T)
t = append(t, elements...)
return &Shell{t}
}
}
// mapping a single element
func MapOne[T, U any](f P1[*T, *U]) F {
return func(s *Shell) *Shell {
t := s.V.(T)
u := f(&t)
return &Shell{*u}
}
}