-
Notifications
You must be signed in to change notification settings - Fork 6
/
slice.go
41 lines (37 loc) · 962 Bytes
/
slice.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
package go2linq
import (
"iter"
)
// SliceAll returns an iterator over values of all elements of the [slice].
// If 's' is nil, empty iterator is returned.
//
// [slice]: https://go.dev/ref/spec#Slice_types
func SliceAll[S ~[]E, E any](s S) iter.Seq[E] {
return func(yield func(E) bool) {
for _, e := range s {
if !yield(e) {
return
}
}
}
}
// VarAll returns an iterator over all elements of the [variadic] [slice].
//
// [variadic]: https://go.dev/ref/spec#Function_types
// [slice]: https://go.dev/ref/spec#Slice_types
func VarAll[E any](s ...E) iter.Seq[E] {
return SliceAll(s)
}
// SliceAll2 returns an iterator over pairs of index and value of all elements of the [slice].
// If 's' is nil, empty iterator is returned.
//
// [slice]: https://go.dev/ref/spec#Slice_types
func SliceAll2[S ~[]E, E any](s S) iter.Seq2[int, E] {
return func(yield func(int, E) bool) {
for i, e := range s {
if !yield(i, e) {
return
}
}
}
}