-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtemplate.go
More file actions
704 lines (675 loc) · 16.1 KB
/
template.go
File metadata and controls
704 lines (675 loc) · 16.1 KB
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
// Code generated by go generate; DO NOT EDIT.
package main
var hasgoTemplates = map[string]string{
"abs.go": `
import (
"math"
)
// Abs returns the absolute value of all elements in the slice.
// Can be generated for number-types.
func (s SliceType) Abs() (out SliceType) {
for _, v := range s {
out = append(out, ElementType(math.Abs(float64(v))))
}
return
}
`,
"all.go": `
// All returns true if all elements of the slice satisfy the predicate.
// Can be generated for any type.
func (s SliceType) All(f func(ElementType) bool) bool {
if f == nil {
return false
}
for _, v := range s {
if !f(v) {
return false
}
}
return true
}
`,
"any.go": `
// Any returns true if any of the elements satisfy the predicate.
// Can be generated for any type.
func (s SliceType) Any(f func(ElementType) bool) bool {
if f == nil {
return false
}
for _, v := range s {
if f(v) {
return true
}
}
return false
}
`,
"average.go": `
// Average returns the average of all elements in the slice.
// Can be generated for all number types.
func (s SliceType) Average() float64 {
var sum ElementType
if len(s) == 0 {
return float64(0)
}
for _, i := range s {
sum += i
}
return float64(sum) / float64(len(s))
}
`,
"break.go": `
// Break returns a tuple of any elements that do not satisfy the predicate up until the first time it passes, followed
// by the rest of the elements.
// Can be generated on any type.
func (s SliceType) Break(f func(ElementType) bool) (before SliceType, after SliceType) {
if f == nil {
return before, s
}
passed := false
for _, v := range s {
if passed || f(v) {
after = append(after, v)
passed = true
} else {
before = append(before, v)
}
}
return
}
`,
"delete.go": `
// Delete returns a slice with the first matching element
// removed from the slice.
// Can be generated for any type.
func (s SliceType) Delete(e ElementType) (out SliceType) {
deleted := false
for _, v := range s {
if deleted || v != e {
out = append(out, v)
} else {
deleted = true
}
}
return
}
`,
"drop.go": `
// Drop returns a new Slice with the elements after the provided key.
// Can be generated for any type.
func (s SliceType) Drop(i int) (out SliceType) {
for i < len(s) {
out = append(out, s[i])
i++
}
return
}
`,
"dropwhile.go": `
// DropWhile returns a new slice containing all elements after the predicate fails for the first time.
// Can be generated for any type.
func (s SliceType) DropWhile(f func(ElementType) bool) (out SliceType) {
if f == nil {
return s
}
failed := false
for _, v := range s {
if failed {
out = append(out, v)
continue
}
if !f(v) {
out = append(out, v)
failed = true
}
}
return
}
`,
"elem.go": `
// Elem returns true if the slice contains the element
// Can be generated for any type.
func (s SliceType) Elem(el ElementType) bool {
for _, e := range s {
if e == el {
return true
}
}
return false
}
`,
"filter.go": `
// Filter returns a slice containing only the elements that match the predicate.
// Can be generated for any type.
func (s SliceType) Filter(f func(ElementType) bool) (out SliceType) {
for _, v := range s {
if f(v) {
out = append(out, v)
}
}
return
}
`,
"foldl.go": `
// Foldl reduces a list by iteratively applying f from left->right. Thus, for an empty slice, the result is the default zero-value.
func (s SliceType) Foldl(z ElementType, f func(e1, e2 ElementType) ElementType) (out ElementType) {
if len(s) == 0 {
return
}
out = f(z, s[0])
for _, v := range s[1:] {
out = f(out, v)
}
return
}
`,
"foldl1.go": `
// Foldl1 reduces a list by iteratively applying f from left->right. Thus, for an empty slice, the result is the default zero-value.
func (s SliceType) Foldl1(f func(e1, e2 ElementType) ElementType) (out ElementType) {
if len(s) == 0 {
return
}
out = s[0]
for _, v := range s[1:] {
out = f(out, v)
}
return
}
`,
"foldr.go": `
// Foldr reduces a list by iteratively applying f from right -> left. Thus, for an empty slice, the result is the default zero-value.
func (s SliceType) Foldr(e ElementType, f func(e1, e2 ElementType) ElementType) (out ElementType) {
if len(s) == 0 {
return
}
end := len(s) - 1
out = f(s[end], e)
for i := end - 1; i >= 0; i-- {
out = f(s[i], out)
}
return
}
`,
"foldr1.go": `
// Foldr1 reduces a list by iteratively applying f from right -> left. Thus, for an empty slice, the result is the default zero-value.
func (s SliceType) Foldr1(f func(e1, e2 ElementType) ElementType) (out ElementType) {
if len(s) == 0 {
return
}
end := len(s) - 1
out = s[end]
for i := end - 1; i >= 0; i-- {
out = f(s[i], out)
}
return
}
`,
"group.go": `
// Group returns a list of lists where each list contains only equal elements and the concatenation of the
// result is equal to the argument.
// Can be generated for any type.
func (s SliceType) Group() (out SliceSliceType) {
current := SliceType{}
for i, v := range s {
current = append(current, v)
if i == len(s)-1 || v != s[i+1] {
out = append(out, current)
current = SliceType{}
}
}
return
}
`,
"head.go": `
// Head returns the first element in the slice.
// If no element is found, returns the zero-value of the type.
// Can be generated for any type.
func (s SliceType) Head() (out ElementType) {
if len(s) > 0 {
out = s[0]
}
return
}
`,
"init.go": `
// Init takes n-1 elements from a slice, where n = len(list).
// Can be generated for any type.
func (s SliceType) Init() (out SliceType) {
if len(s) == 0 {
return
}
slicecopy := append([]ElementType(nil), s...)
return slicecopy[:len(s)-1]
}
`,
"inits.go": `
// Inits returns all inits of a sequence, in order of small to large, as if it were called recursively.
// Can be generated for any type.
func (s SliceType) Inits() (out SliceSliceType) {
out = append(out, make(SliceType, 0))
for i := range s {
init := make(SliceType, i+1)
for n := 0; n <= i; n++ {
init[n] = s[n]
}
out = append(out, init)
}
return
}
`,
"intercalate.go": `
// Intercalate inserts the method receiver slice into the function slice at each step.
// Can be generated for any type.
func (s SliceType) Intercalate(ss SliceSliceType) (out SliceType) {
for i, slice := range ss {
for _, el := range slice {
out = append(out, el)
}
if i == len(ss)-1 {
break
}
for _, el := range s {
out = append(out, el)
}
}
return out
}
`,
"intersperse.go": `
// Intersperse inserts the receiving value between each element of the method receiver.
// Can be generated for any type.
func (s SliceType) Intersperse(value ElementType) (out SliceType) {
for i, el := range s {
out = append(out, el)
if i == len(s)-1 {
break
}
out = append(out, value)
}
return
}
`,
"isprefixof.go": `
// IsPrefixOf returns true if the current sliceType is a prefix of the passed one.
// Can be generated for any time.
func (s SliceType) IsPrefixOf(in SliceType) bool {
if len(s) > len(in) {
return false
}
for i, v := range s {
if in[i] != v {
return false
}
}
return true
}
`,
"last.go": `
// Last returns the last element in the slice
// If no element is found, returns the zero-value of the type
// Can be generated for any type.
func (s SliceType) Last() (out ElementType) {
if len(s) > 0 {
out = s[len(s)-1]
}
return
}
`,
"length.go": `
// Length returns the length (len) of a slice.
// Can be generated for any type.
func (s SliceType) Length() int {
return len(s)
}
`,
"map.go": `
// Map return a new slice with the map operation applied to each element.
// Can be generated for any type.
func (s SliceType) Map(f func(ElementType) ElementType) (out SliceType) {
if f == nil {
return s
}
for _, v := range s {
out = append(out, f(v))
}
return
}
`,
"maximum.go": `
// Maximum returns the maximum in a slice.
// Can be generated for number types.
func (s SliceType) Maximum() (out ElementType) {
if len(s) == 0 {
return
}
for _, i := range s {
if i > out {
out = i
}
}
return
}
`,
"maximumby.go": `
// MaximumBy returns the maximum elements according to a custom comparator.
// Can be generated for any type.
func (s SliceType) MaximumBy(f func(e1, e2 ElementType) ElementType) (max ElementType) {
if len(s) == 0 {
return
}
max = s[0]
for _, el := range s[1:] {
max = f(max, el)
}
return
}
`,
"minimum.go": `
// Minimum returns the minimum of a slice.
// Can be generated for number types.
func (s SliceType) Minimum() ElementType {
if len(s) == 0 {
return 0 // bit strange?
}
min := s[0]
for _, i := range s {
if i < min {
min = i
}
}
return min
}
`,
"modes.go": `
// Modes returns the elements with highest frequency in the slice.
// Can be generated for any type.
func (s SliceType) Modes() (out SliceType) {
if len(s) == 0 {
return
}
counts := make(map[ElementType]int)
for _, v := range s {
counts[v]++
}
var max int
for _, v := range counts {
if v > max {
max = v
}
}
for k, v := range counts {
if v == max {
out = append(out, k)
}
}
return
}
`,
"nub.go": `
// Nub returns a slice containing only the unique elements of the receiver.
// The order of the elements is preserved.
// Can be generated for any type.
func (s SliceType) Nub() (out SliceType) {
if len(s) == 0 {
return
}
contains := make(map[ElementType]struct{})
for _, v := range s {
if _, ok := contains[v]; !ok {
contains[v] = struct{}{}
out = append(out, v)
}
}
return
}
`,
"null.go": `
// Null returns true the slice is empty.
// Can be generated for any type.
func (s SliceType) Null() bool {
return len(s) == 0
}
`,
"product.go": `
// Product returns the product of all elements in the slice.
// Can be generated for any number type.
func (s SliceType) Product() ElementType {
var prod ElementType
for _, v := range s {
prod += v
}
return prod
}
`,
"reverse.go": `
// Reverse returns the reversed slice.
// Can be generated for any type.
func (s SliceType) Reverse() (out SliceType) {
for i := len(s) - 1; i >= 0; i-- {
out = append(out, s[i])
}
return
}
`,
"scanl.go": `
// Scanl reduces a list by iteratively applying f from left->right and then returns each iteration in a slice.
func (s SliceType) Scanl(e ElementType, f func(e1, e2 ElementType) ElementType) (out SliceType) {
if len(s) == 0 {
return
}
out = append(out, e)
last := e
for _, v := range s {
last = f(last, v)
out = append(out, last)
}
return
}
`,
"sort.go": `
import (
"sort"
)
// Sort is a wrapper around go sort function.
// Can be generated for any type.
func (s SliceType) Sort() SliceType {
out := make(SliceType, len(s))
copy(out, s)
sort.Slice(out, func(i, j int) bool {
return out[i] < out[j]
})
return out
}
`,
"span.go": `
// Span returns a tuple of any elements that satisfy the predicate up until the first failure, followed by
// the rest of the elements.
// Can be generated for any type.
func (s SliceType) Span(f func(ElementType) bool) (before SliceType, after SliceType) {
if f == nil {
return before, s
}
failed := false
for _, v := range s {
if failed || !f(v) {
after = append(after, v)
failed = true
} else {
before = append(before, v)
}
}
return
}
`,
"splitat.go": `
// SplitAt splits the slice at the given index, returning before and after as a tuple.
// Can be generated for any type.
func (s SliceType) SplitAt(i int) (before, after SliceType) {
for k, v := range s {
if k < i {
before = append(before, v)
} else {
after = append(after, v)
}
}
return
}
`,
"sum.go": `
// Sum returns the sum of all elements in the slice.
// Can be generated for any number type.
func (s SliceType) Sum() ElementType {
var sum ElementType
for _, v := range s {
sum += v
}
return sum
}
`,
"tail.go": `
// Tail takes [1 -> n] elements from a slice, where n = len(list)
// Returns an empty slice if there are less than 2 elements in slice
// Can be generated for any type.
func (s SliceType) Tail() (out SliceType) {
if len(s) <= 1 {
return
}
slicecopy := append([]ElementType(nil), s...)
return slicecopy[1:]
}
`,
"tails.go": `
// Tails returns all tails of a sequence, in order of large to small, as if it were called recursively.
// Can be generated for any type.
func (s SliceType) Tails() (out SliceSliceType) {
slicecopy := append([]ElementType(nil), s...)
for range s {
out = append(out, slicecopy)
slicecopy = slicecopy[1:]
}
out = append(out, make(SliceType, 0))
return
}
`,
"take.go": `
// Take takes the first n elements of the slice, or the entire slice if n > len(slice).
// Can be generated for any type.
func (s SliceType) Take(n uint64) (out SliceType) {
if len(s) == 0 {
return
}
out = make(SliceType, len(s))
copy(out, s)
if n < uint64(len(s)) {
return out[:n]
}
return
}
`,
"takewhile.go": `
// TakeWhile continues appending to the output as long as the predicate is satisfied.
// Can be generated for any type.
func (s SliceType) TakeWhile(p func(ElementType) bool) (out SliceType) {
if len(s) == 0 {
return
}
for _, e := range s {
if !p(e) {
return
}
out = append(out, e)
}
return
}
`,
"uncons.go": `
// Uncons decomposes a slice into the head and tail component.
// Can be generated for any type.
func (s SliceType) Uncons() (head ElementType, tail SliceType) {
return s.Head(), s.Tail()
}
`,
"unlines.go": `
import (
"fmt"
)
// Unlines joins together the string representation of the slice with newlines after each element.
// Can be generated for any type.
func (s SliceType) Unlines() (out string) {
for i, v := range s {
out += fmt.Sprintf("%v", v)
if i != len(s)-1 {
out += "\n"
}
}
return
}
`,
"unwords.go": `
import (
"fmt"
)
// Unwords joins together the string representation of the slice with newlines after each element.
// Can be generated for any type.
func (s SliceType) Unwords() (out string) {
for i, v := range s {
out += fmt.Sprintf("%v", v)
if i != len(s)-1 {
out += " "
}
}
return
}
`,
}
const (
ForNumbers = "ForNumbers"
ForStrings = "ForStrings"
ForStructs = "ForStructs"
)
var funcDomains = map[string][]string{
"abs.go": {ForNumbers},
"all.go": {ForNumbers, ForStrings, ForStructs},
"any.go": {ForNumbers, ForStrings, ForStructs},
"average.go": {ForNumbers},
"break.go": {ForNumbers, ForStrings, ForStructs},
"delete.go": {ForNumbers, ForStrings, ForStructs},
"drop.go": {ForNumbers, ForStrings, ForStructs},
"dropwhile.go": {ForNumbers, ForStrings, ForStructs},
"elem.go": {ForNumbers, ForStrings, ForStructs},
"filter.go": {ForNumbers, ForStrings, ForStructs},
"foldl.go": {ForNumbers, ForStrings, ForStructs},
"foldl1.go": {ForNumbers, ForStrings, ForStructs},
"foldr.go": {ForNumbers, ForStrings, ForStructs},
"foldr1.go": {ForNumbers, ForStrings, ForStructs},
"group.go": {ForNumbers, ForStrings, ForStructs},
"head.go": {ForNumbers, ForStrings, ForStructs},
"init.go": {ForNumbers, ForStrings, ForStructs},
"inits.go": {ForNumbers, ForStrings, ForStructs},
"intercalate.go": {ForNumbers, ForStrings, ForStructs},
"intersperse.go": {ForNumbers, ForStrings, ForStructs},
"isprefixof.go": {ForNumbers, ForStrings, ForStructs},
"last.go": {ForNumbers, ForStrings, ForStructs},
"length.go": {ForNumbers, ForStrings, ForStructs},
"map.go": {ForNumbers, ForStrings, ForStructs},
"maximum.go": {ForNumbers},
"maximumby.go": {ForNumbers, ForStrings, ForStructs},
"minimum.go": {ForNumbers},
"modes.go": {ForNumbers, ForStrings, ForStructs},
"nub.go": {ForNumbers, ForStrings, ForStructs},
"null.go": {ForNumbers, ForStrings, ForStructs},
"product.go": {ForNumbers},
"reverse.go": {ForNumbers, ForStrings, ForStructs},
"scanl.go": {ForNumbers, ForStrings, ForStructs},
"sort.go": {ForNumbers, ForStrings},
"span.go": {ForNumbers, ForStrings, ForStructs},
"splitat.go": {ForNumbers, ForStrings, ForStructs},
"sum.go": {ForNumbers, ForStrings},
"tail.go": {ForNumbers, ForStrings, ForStructs},
"tails.go": {ForNumbers, ForStrings, ForStructs},
"take.go": {ForNumbers, ForStrings, ForStructs},
"takewhile.go": {ForNumbers, ForStrings, ForStructs},
"uncons.go": {ForNumbers, ForStrings, ForStructs},
"unlines.go": {ForNumbers, ForStrings, ForStructs},
"unwords.go": {ForNumbers, ForStrings, ForStructs},
}