-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseq.go
504 lines (411 loc) · 9.35 KB
/
seq.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package monadgo
import (
"fmt"
"reflect"
"strings"
)
type sequence interface {
Any
// toSeq converts internal value to seq.
toSeq() seq
}
type seq struct {
x interface{}
t reflect.Type
v reflect.Value
len int
cap int
empty bool
}
var emptySeq = seq{
x: nothings,
t: typeNothings,
v: nothingsValue,
len: 0,
cap: 0,
empty: true,
}
func seqFromValue(v reflect.Value) seq {
if !v.IsValid() {
return emptySeq
}
if v.Type().Implements(typeSeq) {
return v.Interface().(sequence).toSeq()
}
if v.Kind() == reflect.Slice {
return seq{
x: v.Interface(),
t: v.Type(),
v: v,
len: v.Len(),
cap: v.Cap(),
}
}
if v.Kind() == reflect.Array {
// clone to a slice if x is array, or some slice operation on array will panic.
z := makeSlice(v.Type().Elem(), v.Len(), v.Len())
reflect.Copy(z, v)
return seq{
x: z.Interface(),
t: z.Type(),
v: z,
len: z.Len(),
cap: z.Cap(),
}
}
if v.Kind() == reflect.Map {
return newMap(v).toSeq()
}
z := makeSlice(v.Type(), 1, 1)
z.Index(0).Set(v)
return seqFromValue(z)
}
func seqOf(x interface{}) seq {
if x == nil {
return emptySeq
}
switch v := x.(type) {
case reflect.Value:
return seqFromValue(v)
default:
return seqFromValue(reflect.ValueOf(x))
}
}
// ----------------------------------------------------------------------------
// Get returns internal value.
func (s seq) Get() interface{} {
return s.x
}
func (s seq) String() string {
return fmt.Sprintf("%v", s.x)
}
func (s seq) rv() reflect.Value {
return s.v
}
func (s seq) toSeq() seq {
return s
}
// Size returns the size.
func (s seq) Size() int {
return s.len
}
// Len returns the length.
func (s seq) Len() int {
return s.len
}
// Cap returns the capacity.
func (s seq) Cap() int {
return s.cap
}
// Map applies function f to all elements in seq s.
// f: func(T) X
// returns a Traversable with element type X.
func (s seq) Map(f interface{}) Traversable {
if s.empty {
return emptySeq
}
fw := funcOf(f)
ret := makeSlice(fw.out[0])
for i := 0; i < s.len; i++ {
ret = appendSlice(ret, fw.call(s.v.Index(i)))
}
return seqFromValue(ret)
}
// FlatMap applies f to all elements and builds a new Travesable from result.
// f: func(T) X, X can be Go slice, or map.
// returns a new Traversable with element type X.
func (s seq) FlatMap(f interface{}) Traversable {
if s.empty {
return emptySeq
}
fw := funcOf(f)
var elm reflect.Type
if fw.out[0].Kind() == reflect.Slice {
elm = fw.out[0].Elem()
} else if fw.out[0].Kind() == reflect.Map {
elm = typePair
} else {
// collect other type into a slice.
// it is invalid in Scala.
elm = fw.out[0]
}
ret := makeSlice(elm, 0, 0)
for i := 0; i < s.len; i++ {
result := fw.call(s.v.Index(i))
ret = mergeSlice(ret, seqFromValue(result).v)
}
return SliceOf(ret)
}
// Forall tests whether a predicate holds for all elements.
// f: func(T) bool
func (s seq) Forall(f interface{}) bool {
if s.empty {
return true
}
fw := funcOf(f)
for i := 0; i < s.len; i++ {
if !fw.call(s.v.Index(i)).Bool() {
return false
}
}
return true
}
// Foreach applies f to all element.
// f: func(T)
func (s seq) Foreach(f interface{}) {
if s.len <= 0 {
return
}
fw := funcOf(f)
for i := 0; i < s.len; i++ {
fw.call(s.v.Index(i))
}
}
// Fold folds the elements using specified associative binary operator.
// z: func() T or value of type T.
// f: func(T, T) T
// returns value with type T
func (s seq) Fold(z, f interface{}) interface{} {
z = checkAndInvoke(z)
if s.len <= 0 {
return z
}
fw := foldOf(f)
zval := reflect.ValueOf(z)
for i := 0; i < s.len; i++ {
zval = fw.call(zval, s.v.Index(i))
}
return zval.Interface()
}
// Head returns the first element.
func (s seq) Head() interface{} {
if s.len <= 0 {
return nil
}
return s.v.Index(0).Interface()
}
// HeadOption returns None if this is empty, otherwise return Some of first element.
func (s seq) HeadOption() Option {
if s.len <= 0 {
return None
}
return OptionOf(s.v.Index(0))
}
// Tail returns all elements except the first.
func (s seq) Tail() Traversable {
if s.len < 1 {
return seqFromValue(s.v.Slice(0, 0))
}
return seqFromValue(s.v.Slice(1, s.len))
}
// Reduce reduces the elements of this using the specified associative binary operator.
// f: func(T, T) T
// returns value with type T.
func (s seq) Reduce(f interface{}) interface{} {
if s.len <= 0 {
panic("empty list can not reduce")
}
if s.len == 1 {
return s.v.Index(0).Interface()
}
fw := foldOf(f)
zval := s.v.Index(0)
for i := 1; i < s.len; i++ {
zval = fw.call(zval, s.v.Index(i))
}
return zval.Interface()
}
// Scan computes a prefix scan of the elements of the collection.
// z: func() T or value with type T.
// f: func(T, T) T
// returns a new Traversable with first element z.
func (s seq) Scan(z, f interface{}) Traversable {
z = checkAndInvoke(z)
zval := oneToSlice(reflect.ValueOf(z))
if s.len <= 0 {
return seqFromValue(zval)
}
fw := foldOf(f)
for i := 0; i < s.len; i++ {
zval = appendSlice(zval, fw.call(zval.Index(i), s.v.Index(i)))
}
return seqFromValue(zval)
}
// GroupBy returns Map with K -> Go slice. Key is the result of f. Collect elements into a slice with same resulting key value.
// f: func(T) K
// returns Map(K -> Go slice)
func (s seq) GroupBy(f interface{}) Map {
if s.len <= 0 {
panic("can not group by on empty slice")
}
fw := funcOf(f)
m := makeMap(fw.out[0], s.t, -1)
for i := 0; i < s.len; i++ {
k := fw.call(s.v.Index(i))
m.SetMapIndex(k, appendSlice(m.MapIndex(k), s.v.Index(i)))
}
return newMap(m)
}
// Take returns the first n elements.
func (s seq) Take(n int) Traversable {
if n >= s.len {
n = s.len
}
return seqFromValue(s.v.Slice(0, n))
}
// TakeWhile takes longest prefix of elements that satisfy a predicate.
// f: func(T) bool
func (s seq) TakeWhile(f interface{}) Traversable {
n := 0
fw := funcOf(f)
for i := 0; i < s.len; i++ {
if !fw.call(s.v.Index(i)).Bool() {
break
}
n = i
}
if n > 0 {
n++
}
return seqFromValue(s.v.Slice(0, n))
}
// Drop returns all elements except first n ones.
func (s seq) Drop(n int) Traversable {
if n > s.len {
n = s.len
}
return seqFromValue(s.v.Slice(n, s.len))
}
// Exists tests whether a predicate holds for at least one element of this sequence.
// f: func(T) bool
func (s seq) Exists(f interface{}) bool {
if s.len <= 0 {
return false
}
fw := funcOf(f)
for i := 0; i < s.len; i++ {
if fw.call(s.v.Index(i)).Bool() {
return true
}
}
return false
}
// Find returns the first element satisfying f,
// otherwise return None.
// f: func(T) bool
func (s seq) Find(f interface{}) Option {
if s.len <= 0 {
return None
}
fw := funcOf(f)
for i := 0; i < s.len; i++ {
x := s.v.Index(i)
if fw.call(x).Bool() {
return SomeOf(x)
}
}
return None
}
// Filter retuns all elements satisfying f.
// f: func(T) bool
func (s seq) Filter(f interface{}) Traversable {
ret := reflect.MakeSlice(s.t, 0, 0)
fw := funcOf(f)
for i := 0; i < s.len; i++ {
x := s.v.Index(i)
if fw.call(x).Bool() {
ret = appendSlice(ret, x)
}
}
return seqFromValue(ret)
}
// IndexWhere finds index of the first element satisfying f after or at some start index.
// f: func(T) bool
// returns -1 if no elment satisfying f.
func (s seq) IndexWhere(f interface{}, start int) int {
if s.len <= 0 {
return -1
}
if start < 0 {
start = 0
}
fw := funcOf(f)
for i := start; i < s.len; i++ {
if fw.call(s.v.Index(i)).Bool() {
return i
}
}
return -1
}
// LastIndexWhere finds index of last element satisfying f.
// f: func(T) bool
// returns -1 if no elment satisfying f.
func (s seq) LastIndexWhere(f interface{}, end int) int {
if s.len <= 0 {
return -1
}
if end >= s.len {
end = s.len - 1
}
fw := funcOf(f)
for i := end; i >= 0; i-- {
if fw.call(s.v.Index(i)).Bool() {
return i
}
}
return -1
}
// MkString displays all elements in a string using start, end, and separator sep.
func (s seq) MkString(start, sep, end string) string {
sb := new(strings.Builder)
sb.WriteString(start)
for i := 0; i < s.len; i++ {
sb.WriteString(fmt.Sprintf("%v", s.v.Index(i).Interface()))
sb.WriteString(sep)
}
sb.WriteString(end)
return sb.String()
}
// Reverse returns new list with elements in reversed order.
func (s seq) Reverse() Traversable {
ret := reflect.MakeSlice(s.t, s.len, s.len)
for i := 0; i < s.len; i++ {
ret.Index(i).Set(s.v.Index(s.len - 1 - i))
}
return seqFromValue(ret)
}
// Split splits this into a unsatisfying and satisfying pair according to f.
// f: func(T) bool
func (s seq) Split(f interface{}) Tuple2 {
if s.len <= 0 {
return Tuple2Of(nothings, nothings)
}
left := reflect.MakeSlice(s.t, 0, 0)
right := reflect.MakeSlice(s.t, 0, 0)
fw := funcOf(f)
for i := 0; i < s.len; i++ {
x := s.v.Index(i)
if fw.call(x).Bool() {
right = appendSlice(right, x)
} else {
left = appendSlice(left, x)
}
}
return Tuple2Of(left.Interface(), right.Interface())
}
// Collect returns elements satisfying pf.
// pf is a partial function consisting of Condition func(T) bool and Action func(T) X.
// returns a new Traversable[X]
func (s seq) Collect(pf PartialFunc) Traversable {
ret := makeSlice(pf.action.out[0], 0, 0)
if s.len <= 0 {
return seqFromValue(ret)
}
for i := 0; i < s.len; i++ {
result := pf.Call(s.v.Index(i))
if result != nothingValue {
ret = appendSlice(ret, result)
}
}
return seqFromValue(ret)
}