-
Notifications
You must be signed in to change notification settings - Fork 15
/
slice_mapping.go
76 lines (65 loc) · 2.63 KB
/
slice_mapping.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package stream
// SliceMappingStream Need to convert the type of source elements.
// - E elements type
// - MapE map elements type
// - ReduceE reduce elements type
type SliceMappingStream[E any, MapE any, ReduceE any] struct {
SliceStream[E]
}
// NewSliceByMapping new stream instance, Need to convert the type of source elements.
//
// - E elements type
// - MapE map elements type
// - ReduceE reduce elements type
func NewSliceByMapping[E any, MapE any, ReduceE any](source []E) SliceMappingStream[E, MapE, ReduceE] {
return SliceMappingStream[E, MapE, ReduceE]{SliceStream: NewSlice(source)}
}
// Map Returns a stream consisting of the results of applying the given function to the elements of this stream.
//
// Support Parallel.
func (stream SliceMappingStream[E, MapE, ReduceE]) Map(mapper func(E) MapE) SliceMappingStream[MapE, MapE, ReduceE] {
if stream.source == nil {
return NewSliceByMapping[MapE, MapE, ReduceE](nil)
}
terminal := func(index int, v E) (isReturn bool, isComplete bool, ret MapE) {
return true, false, mapper(v)
}
ret := pipelineRun(stream.Pipeline, wrapTerminal(stream.stages, terminal))
return NewSliceByMapping[MapE, MapE, ReduceE](ret)
}
// Reduce Returns a source consisting of the elements of this stream.
func (stream SliceMappingStream[E, MapE, ReduceE]) Reduce(result ReduceE, accumulator func(result ReduceE, elem E) ReduceE) ReduceE {
stream.evaluation()
if len(stream.source) == 0 {
return result
}
for _, v := range stream.source {
result = accumulator(result, v)
}
return result
}
// Parallel See: SliceStream.Parallel
func (stream SliceMappingStream[E, MapE, ReduceE]) Parallel(goroutines int) SliceMappingStream[E, MapE, ReduceE] {
stream.SliceStream = stream.SliceStream.Parallel(goroutines)
return stream
}
// ForEach See: SliceStream.ForEach
func (stream SliceMappingStream[E, MapE, ReduceE]) ForEach(action func(int, E)) SliceMappingStream[E, MapE, ReduceE] {
stream.SliceStream = stream.SliceStream.ForEach(action)
return stream
}
// Filter See: SliceStream.Filter
func (stream SliceMappingStream[E, MapE, ReduceE]) Filter(predicate func(E) bool) SliceMappingStream[E, MapE, ReduceE] {
stream.SliceStream = stream.SliceStream.Filter(predicate)
return stream
}
// Limit See: SliceStream.Limit
func (stream SliceMappingStream[E, MapE, ReduceE]) Limit(maxSize int) SliceMappingStream[E, MapE, ReduceE] {
stream.SliceStream = stream.SliceStream.Limit(maxSize)
return stream
}
// SortFunc See: SliceStream.SortFunc
func (stream SliceMappingStream[E, MapE, ReduceE]) SortFunc(less func(a, b E) bool) SliceMappingStream[E, MapE, ReduceE] {
stream.SliceStream = stream.SliceStream.SortFunc(less)
return stream
}