-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
53 lines (46 loc) · 1.3 KB
/
collector.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
package stream
import (
"github.com/go-park/stream/internal/helper"
"github.com/go-park/stream/support/function"
"github.com/go-park/stream/support/optional"
"golang.org/x/exp/constraints"
)
func ToList[T, V any](s Stream[T], conv function.Func[T, V]) []V {
helper.RequireCanButNonNil(conv)
list := make([]V, 0)
s.ForEach(func(t T) {
value := conv.Apply(t)
list = append(list, value)
})
return list
}
func ToMap[T, V any, R comparable](s Stream[T], source function.Func[T, R], after function.Func[T, V]) map[R]V {
helper.RequireCanButNonNil(source)
helper.RequireCanButNonNil(after)
hash := make(map[R]V)
s.ForEach(func(t T) {
key := source.Apply(t)
value := after.Apply(t)
hash[key] = value
})
return hash
}
func Distinct[T comparable](s Stream[T]) Stream[T] {
helper.RequireCanButNonNil(s)
s = s.Distinct(func(t, u T) bool {
return t == u
})
return s
}
func Sort[T constraints.Ordered](s Stream[T]) Stream[T] {
helper.RequireCanButNonNil(s)
return s.Sort(func(t, u T) bool { return t < u })
}
func Max[T constraints.Ordered](s Stream[T]) optional.Value[T] {
helper.RequireCanButNonNil(s)
return s.Max(func(t, u T) bool { return t < u })
}
func Min[T constraints.Ordered](s Stream[T]) optional.Value[T] {
helper.RequireCanButNonNil(s)
return s.Min(func(t, u T) bool { return t < u })
}