-
Notifications
You must be signed in to change notification settings - Fork 56
/
iterators.go
92 lines (75 loc) · 1.47 KB
/
iterators.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
package plush
import (
"fmt"
"reflect"
)
// Iterator type can be implemented and used by the `for` command to build loops in templates
type Iterator interface {
Next() interface{}
}
type ranger struct {
pos int
end int
}
func (r *ranger) Next() interface{} {
if r.pos < r.end {
r.pos++
return r.pos
}
return nil
}
func rangeHelper(a, b int) Iterator {
return &ranger{pos: a - 1, end: b}
}
func betweenHelper(a, b int) Iterator {
return &ranger{pos: a, end: b - 1}
}
func untilHelper(a int) Iterator {
return &ranger{pos: -1, end: a - 1}
}
func GroupByHelper(size int, underlying interface{}) (*groupBy, error) {
if size <= 0 {
return nil, fmt.Errorf("size must be greater than zero")
}
u := reflect.Indirect(reflect.ValueOf(underlying))
group := []reflect.Value{}
switch u.Kind() {
case reflect.Array, reflect.Slice:
if u.Len() == size {
return &groupBy{
group: []reflect.Value{u},
}, nil
}
groupSize := u.Len() / size
if u.Len()%size != 0 {
groupSize++
}
pos := 0
for pos < u.Len() {
e := pos + groupSize
if e > u.Len() {
e = u.Len()
}
group = append(group, u.Slice(pos, e))
pos += groupSize
}
default:
return nil, fmt.Errorf("can not use %T in groupBy", underlying)
}
g := &groupBy{
group: group,
}
return g, nil
}
type groupBy struct {
pos int
group []reflect.Value
}
func (g *groupBy) Next() interface{} {
if g.pos >= len(g.group) {
return nil
}
v := g.group[g.pos]
g.pos++
return v.Interface()
}