-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcurrying.go
185 lines (146 loc) · 3.96 KB
/
currying.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package loncha
import (
"golang.org/x/exp/constraints"
)
type curryParam[T any] struct {
Default T
}
// OptCurry ... functional option
type OptCurry[T any] func(*curryParam[T]) OptCurry[T]
func Default[T any](t T) OptCurry[T] {
return func(p *curryParam[T]) OptCurry[T] {
prev := p.Default
p.Default = t
return Default(prev)
}
}
func (p *curryParam[T]) Options(opts ...OptCurry[T]) (prevs []OptCurry[T]) {
for _, opt := range opts {
prevs = append(prevs, opt(p))
}
return
}
func NewOpt[T any](opts ...OptCurry[T]) *curryParam[T] {
p := curryParam[T]{}
p.Options(opts...)
return &p
}
// FilterFunc ... function generated by Filterlize()
type FilterFunc[T any] func([]T) []T
type GetFunc[T any] func([]T) T
// Filterable ... generate filter function for slice
func Filterable[T any](fns ...CondFunc2[T]) FilterFunc[T] {
return innerfilterlable(true, fns...)
}
// Deletable ... generate deleting function by fns Condition for slice
func Deletable[T comparable](fns ...CondFunc2[T]) FilterFunc[T] {
return innerfilterlable(false, fns...)
}
// Selectable ... generate deleting function by fns Condition for slice
func Selectable[T any](fns ...CondFunc2[T]) FilterFunc[T] {
return Filterable(fns...)
}
// Gettable ... generate deleting function by fns Condition for slice
func Gettable[T any](fns ...CondFunc2[T]) GetFunc[T] {
return func(datas []T) T {
result := Filterable(fns...)(datas)
if len(result) > 0 {
return result[0]
}
return *new(T)
}
}
func innerfilterlable[T any](keep bool, fns ...CondFunc2[T]) FilterFunc[T] {
return func(srcs []T) (dsts []T) {
dsts = srcs
innerFilter2(&dsts, keep, fns...)
return
}
}
// InjecterFunc ... condition function for Injectable
type InjecterFunc[T any, R any] func([]T) R
// Injectable ... generate Inject functions
func Injectable[T any, V any](injectFn InjectFn[T, V], opts ...OptCurry[V]) InjecterFunc[T, V] {
return func(src []T) (result V) {
return Inject(src, injectFn, opts...)
}
}
// Reducable ... alias of Injectable
func Reducable[T any, V any](injectFn InjectFn[T, V], opts ...OptCurry[V]) InjecterFunc[T, V] {
return Injectable(injectFn, opts...)
}
// Containable ... generate function of slice contain.
func Containable[T comparable](fn CondFunc2[T]) func([]T) bool {
return func(srcs []T) bool {
for _, src := range srcs {
if fn(&src) {
return true
}
}
return false
}
}
// Every ... Determines whether all the members of an array satisfy the specified test.
func Every[T any](fn CondFunc2[T]) func(...T) bool {
return func(srcs ...T) bool {
result := true
for _, src := range srcs {
if result && !fn(&src) {
result = false
}
}
return result
}
}
// EveryWithIndex ... Determines whether all the members of an array satisfy the specified test.
func EveryWithIndex[T comparable](fn CondFuncWithIndex[T]) func(...T) bool {
return func(srcs ...T) bool {
result := true
for i, src := range srcs {
if result && !fn(i, &src) {
result = false
}
}
return result
}
}
// Convertable ... generate function of slice conversion.
func Convertable[S, D any](fn ConvFunc[S, D]) func([]S) []D {
return func(srcs []S) (dsts []D) {
dsts = []D{}
for _, src := range srcs {
if d, removed := fn(src); !removed {
dsts = append(dsts, d)
}
}
return
}
}
// Number ... Number constraints
type Number interface {
constraints.Integer | constraints.Float
}
func max[T Number](s T, d T) T {
if s < d {
return d
}
return s
}
func min[T Number](s T, d T) T {
return -max(-s, -d)
}
// Zipper ... return tuple operation result using fn with double slice,
func Zipper[T, S, R any](fn func(R, T, S) R, start R) func([]T, []S) R {
return func(tlist []T, slist []S) (result R) {
result = start
for i := range tlist[:min(len(tlist), len(slist))] {
result = fn(result, tlist[i], slist[i])
}
return
}
}
// ToMap ... function for Zipper . return map from double array.
func ToMap[K, V constraints.Ordered](r map[K]V, k K, v V) map[K]V {
r[k] = v
return r
}