-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslices.go
45 lines (37 loc) · 929 Bytes
/
slices.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
package gogcoll
// Slice wraps a slice so that you can iterate or sequence over it
type Slice[T any] []T
// Iter implements the Iterable interface for a Slice
func (s Slice[T]) Iter() Iterator[T] {
return s
}
// Each implements the Iterator interface for Slice
func (s Slice[T]) Each(f Proc1[T]) {
for _, v := range s {
f(v)
}
}
// Seq implements the Seqable interface for Slice
func (s Slice[T]) Seq() Seq[T] {
return sliceSeq(s)
}
// sliceSeq takes a slice and returns a Seq over that slice.
func sliceSeq[T any](sl []T) Seq[T] {
current := -1
next := func() T {
current += 1
if current < len(sl) {
return sl[current]
}
return zero[T]()
}
hasNext := func() bool {
return current+1 < len(sl)
}
return createFunctionSequence(next, hasNext)
}
// Append wraps the built in append, so that you can pass it around as a
// function object
func Append[T any](vs []T, v T) []T {
return append(vs, v)
}