-
Notifications
You must be signed in to change notification settings - Fork 11
/
stream.go
57 lines (49 loc) · 2.16 KB
/
stream.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 stream
import (
"reflect"
"github.com/youthlin/stream/optional"
"github.com/youthlin/stream/types"
)
// Stream is a interface which holds all supported operates.
// It has stateless operates(Filter, Map, FlatMap, Peek),
// stateful operates(Distinct, Sorted, Limit, Skip),
// and the left methods are terminal operates.
type Stream interface {
// stateless operate 无状态操作
Filter(types.Predicate) Stream // 过滤
Map(types.Function) Stream // 转换
FlatMap(func(t types.T) Stream) Stream // 打平
Peek(types.Consumer) Stream // peek 每个元素
// stateful operate 有状态操作
Distinct(types.IntFunction) Stream // 去重
Sorted(types.Comparator) Stream // 排序
Limit(int64) Stream // 限制个数
Skip(int64) Stream // 跳过个数
// terminal operate 终止操作
// 遍历
ForEach(types.Consumer)
// return []T 转为切片
ToSlice() []types.T
// return []X which X is the type of some
ToElementSlice(some types.T) types.R
// return []X which X is same as the `typ` representation
ToSliceOf(typ reflect.Type) types.R
// 测试是否所有元素满足条件
AllMatch(types.Predicate) bool
// 测试是否没有元素满足条件
NoneMatch(types.Predicate) bool
// 测试是否有任意元素满足条件
AnyMatch(types.Predicate) bool
// Reduce return optional.Empty if no element. calculate result by (T, T) -> T from first element, panic if reduction is nil
Reduce(accumulator types.BinaryOperator) optional.Optional
// type of initValue is same as element. (T, T) -> T
ReduceFrom(initValue types.T, accumulator types.BinaryOperator) types.T
// type of initValue is different from element. (R, T) -> R
ReduceWith(initValue types.R, accumulator func(acc types.R, e types.T) types.R) types.R
// ReduceBy use `buildInitValue` to build the initValue, which parameter is a int64 means element size, or -1 if unknown size.
// Then use `accumulator` to add each element to previous result
ReduceBy(buildInitValue func(sizeMayNegative int64) types.R, accumulator func(acc types.R, e types.T) types.R) types.R
FindFirst() optional.Optional
// 返回元素个数
Count() int64
}