-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.go
214 lines (188 loc) · 4.25 KB
/
map.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
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
package support
// base handler
type (
// MMapFunction [K comparable, T any] map[K]T
MMapFunction[K comparable, T any] func(b map[K]T) map[K]T
// MFilterFunction map filter function
MFilterFunction[K comparable, T any] func(b map[K]T) map[K]T
// MReduceFunction map reduce function
MReduceFunction[K comparable, T any] func(b map[K]T) any
)
// Mapper Mapper[Tan]
//
// @Description: Mapper type interface
type Mapper[K comparable, T any] interface {
// Map
//
// @Description: map data
// @param i
// @return Operator
Map(handler MMapFunction[K, T]) Mapper[K, T]
// Filter
//
// @Description:
// @param func(int, T) T
// @return Mapper[T]
Filter(handler MFilterFunction[K, T]) Mapper[K, T]
// Reduce
//
// @Description:
// @param func(k int, v T) (r bool)
// @return Mapper[T]
Reduce(handler MReduceFunction[K, T]) any
// Get list
//
// @Description:
// @return []T
Get() map[K]T
}
// _s Mapper type implement
type _m[K comparable, T any] map[K]T
// Mapper[[]string] is implements Mapper interface ?
//
// build mapper
var _ Mapper[string, string] = (_m[string, string])(nil)
// NewM NewM[T map[any]any | []string]
//
// @Description: new Mapper
// @param i data
// @return Operator[T]
func NewM[K comparable, T any](i map[K]T) Mapper[K, T] {
return _m[K, T](i)
}
// Map
//
// @Description: (fn func(b Mapper[T]) []T) used MMapF generate handle
// @receiver c
// @param i
// @return Operator
func (c _m[K, T]) Map(fn MMapFunction[K, T]) Mapper[K, T] {
return NewM(fn(c))
}
// MMapF bb[T any, C any]
//
// @Description: map map handle
// @param fn
// @param initialize
// @return func(b Mapper[T]) any
func MMapF[K comparable, T any](fn func(key K, val T) T) MMapFunction[K, T] {
return func(i map[K]T) map[K]T {
r := &mMapP[K, T]{fn: fn}
return r.mapPF()(i)
}
}
// mMapP mReduceP Reduce parameter build
// @Description:
type mMapP[K comparable, T any] struct {
fn func(K, T) T
}
// mapPF [T any, C any]
//
// @Description: mapPF parameter function
// @param callback
// @param initialize
// @return func([]T) C
func (s *mMapP[K, T]) mapPF() func(map[K]T) map[K]T {
return func(d map[K]T) map[K]T {
// build map
ts := make(map[K]T, len(d))
for k, v := range d {
ts[k] = s.fn(k, v)
}
return ts
}
}
// Filter Map
//
// @Description:
// @receiver c
// @param i
// @return Operator
func (c _m[K, T]) Filter(fn MFilterFunction[K, T]) Mapper[K, T] {
return NewM(fn(c))
}
// MFilterF [T any, C any]
//
// @Description: map filter handle
// @param fn
// @param initialize
// @return func(b Mapper[T]) any
func MFilterF[K comparable, T any](fn func(K, T) bool) MFilterFunction[K, T] {
return func(b map[K]T) map[K]T {
r := &mFilterP[K, T]{fn: fn}
return r.filterPF()(b)
}
}
// mFilterP mReduceP parameter build
// @Description:
type mFilterP[K comparable, T any] struct {
fn func(K, T) bool
}
// filterPF [T any, C any]
//
// @Description: filterPF method of mFilterP
// @param callback
// @param initialize
// @return func([]T) C
func (s *mFilterP[K, T]) filterPF() func(d map[K]T) map[K]T {
return func(d map[K]T) map[K]T {
// build map
ts := make(map[K]T, len(d))
for k, v := range d {
if s.fn(k, v) {
ts[k] = v
}
}
return ts
}
}
// Reduce
//
// @Description: Reduce Of _s
// @receiver c
// @param i
// @return Operator
func (c _m[K, T]) Reduce(fn MReduceFunction[K, T]) any {
return fn(c)
}
// MReduceF
//
// @Description: map reduce handle
// @param fn
// @param initialize
// @return func(b Mapper[T]) any
func MReduceF[T any, K comparable, V any](fn func(carry V, key K, item T) V, initialize V) MReduceFunction[K, T] {
return func(b map[K]T) any {
r := mReduceP[K, T, V]{fn: fn, initialize: initialize}
return r.reducePF()(b)
}
}
// mReduceP parameter build
// @Description:
type mReduceP[K comparable, T any, C any] struct {
fn func(carry C, key K, item T) C
initialize C
}
// reducePF reducePF[T any, C any]
//
// @Description: Reduce parameter function
// @param callback
// @param initialize
// @return func([]T) C
func (s *mReduceP[K, T, C]) reducePF() func(map[K]T) C {
return func(ts map[K]T) C {
for k, v := range ts {
s.initialize = s.fn(s.initialize, k, v)
}
return s.initialize
}
}
// Get To
//
// @Description:
// @receiver c
// @param i
// @return Operator
func (c _m[K, T]) Get() map[K]T {
return c
}