-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathskip.go
101 lines (85 loc) · 2.3 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
package flinx
// Skip bypasses a specified number of elements in a collection and then returns
// the remaining elements.
func Skip[T any](q Query[T], count int) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
next := q.Iterate()
n := count
return func() (item T, 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 SkipWhile[T any](predicates ...func(T) bool) func(q Query[T]) Query[T] {
predicate := Predicates(predicates...)
return func(q Query[T]) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
next := q.Iterate()
ready := false
return func() (item T, ok bool) {
for !ready {
item, ok = next()
if !ok {
return
}
ready = !predicate(item)
if ready {
return
}
}
return next()
}
},
}
}
}
// 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 SkipWhileIndexed[T any](predicates ...func(int, T) bool) func(q Query[T]) Query[T] {
predicate := PredicatesIndexed(predicates...)
return func(q Query[T]) Query[T] {
return Query[T]{
Iterate: func() Iterator[T] {
next := q.Iterate()
ready := false
index := 0
return func() (item T, ok bool) {
for !ready {
item, ok = next()
if !ok {
return
}
ready = !predicate(index, item)
if ready {
return
}
index++
}
return next()
}
},
}
}
}