-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchain.go
57 lines (44 loc) · 1.01 KB
/
chain.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
package it
import (
"iter"
"slices"
"github.com/gomoni/it/islices"
)
type Chain[T any] iter.Seq[T]
func NewChain[T any](seq iter.Seq[T]) Chain[T] {
return Chain[T](seq)
}
func (ch Chain[T]) Seq() iter.Seq[T] {
return iter.Seq[T](ch)
}
func (g Chain[T]) Filter(filterFunc islices.FilterFunc[T]) Chain[T] {
return Chain[T](islices.Filter(g.Seq(), filterFunc))
}
func (g Chain[T]) Collect() []T {
return slices.Collect(g.Seq())
}
type Mappable[T, V any] struct {
seq iter.Seq[T]
none V
}
func NewMappable[T, V any](seq iter.Seq[T]) Mappable[T, V] {
return Mappable[T, V]{
seq: seq,
}
}
func (g Mappable[T, V]) Seq() iter.Seq[T] {
return g.seq
}
func (g Mappable[T, V]) Filter(filterFunc islices.FilterFunc[T]) Mappable[T, V] {
return Mappable[T, V]{
seq: islices.Filter(g.seq, filterFunc),
}
}
func (g Mappable[T, V]) Map(mapFunc islices.MapFunc[T, V]) Mappable[V, T] {
return Mappable[V, T]{
seq: islices.Map(g.seq, mapFunc),
}
}
func (g Mappable[T, V]) Collect() []T {
return slices.Collect(g.Seq())
}