forked from ahmetb/go-linq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skip.go
134 lines (115 loc) · 3.28 KB
/
skip.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
package linq
// Skip bypasses a specified number of elements in a collection and then returns
// the remaining elements.
func (q Query) Skip(count int) Query {
return Query{
Iterate: func() Iterator {
next := q.Iterate()
n := count
return func() (item interface{}, ok bool) {
for ; n > 0; n-- {
item, ok = next()
if !ok {
return
}
}
return next()
}
},
}
}
// SkipWhile bypasses elements in a collection as long as a specified condition
// is true and then returns the remaining elements.
//
// This method tests each element by using predicate and skips the element if
// the result is true. After the predicate function returns false for an
// element, that element and the remaining elements in source are returned and
// there are no more invocations of predicate.
func (q Query) SkipWhile(predicate func(interface{}) bool) Query {
return Query{
Iterate: func() Iterator {
next := q.Iterate()
ready := false
return func() (item interface{}, ok bool) {
for !ready {
item, ok = next()
if !ok {
return
}
ready = !predicate(item)
if ready {
return
}
}
return next()
}
},
}
}
// SkipWhileT is the typed version of SkipWhile.
//
// - predicateFn is of type "func(TSource)bool"
//
// NOTE: SkipWhile has better performance than SkipWhileT.
func (q Query) SkipWhileT(predicateFn interface{}) Query {
predicateGenericFunc, err := newGenericFunc(
"SkipWhileT", "predicateFn", predicateFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(bool))),
)
if err != nil {
panic(err)
}
predicateFunc := func(item interface{}) bool {
return predicateGenericFunc.Call(item).(bool)
}
return q.SkipWhile(predicateFunc)
}
// SkipWhileIndexed bypasses elements in a collection as long as a specified
// condition is true and then returns the remaining elements. The element's
// index is used in the logic of the predicate function.
//
// This method tests each element by using predicate and skips the element if
// the result is true. After the predicate function returns false for an
// element, that element and the remaining elements in source are returned and
// there are no more invocations of predicate.
func (q Query) SkipWhileIndexed(predicate func(int, interface{}) bool) Query {
return Query{
Iterate: func() Iterator {
next := q.Iterate()
ready := false
index := 0
return func() (item interface{}, ok bool) {
for !ready {
item, ok = next()
if !ok {
return
}
ready = !predicate(index, item)
if ready {
return
}
index++
}
return next()
}
},
}
}
// SkipWhileIndexedT is the typed version of SkipWhileIndexed.
//
// - predicateFn is of type "func(int,TSource)bool"
//
// NOTE: SkipWhileIndexed has better performance than SkipWhileIndexedT.
func (q Query) SkipWhileIndexedT(predicateFn interface{}) Query {
predicateGenericFunc, err := newGenericFunc(
"SkipWhileIndexedT", "predicateFn", predicateFn,
simpleParamValidator(newElemTypeSlice(new(int), new(genericType)), newElemTypeSlice(new(bool))),
)
if err != nil {
panic(err)
}
predicateFunc := func(index int, item interface{}) bool {
return predicateGenericFunc.Call(index, item).(bool)
}
return q.SkipWhileIndexed(predicateFunc)
}