-
Notifications
You must be signed in to change notification settings - Fork 16
/
slice_ordered.go
106 lines (92 loc) · 3.16 KB
/
slice_ordered.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package stream
import (
"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
)
// SliceOrderedStream Generics constraints based on constraints.Ordered
type SliceOrderedStream[E constraints.Ordered] struct {
SliceComparableStream[E]
}
// NewSliceByOrdered new stream instance, generics constraints based on constraints.Ordered
func NewSliceByOrdered[E constraints.Ordered](source []E) SliceOrderedStream[E] {
return SliceOrderedStream[E]{SliceComparableStream: NewSliceByComparable(source)}
}
// IsSorted reports whether x is sorted in ascending order.
// Compare according to the constraints.Ordered.
// If the source is empty or nil then true is returned.
func (stream SliceOrderedStream[E]) IsSorted() bool {
stream.evaluation()
return slices.IsSorted(stream.source)
}
// Max Returns the maximum element of this stream.
// Compare according to the constraints.Ordered.
// If the source is empty or nil then E Type default value is returned. ok return false
func (stream SliceOrderedStream[E]) Max() (max E, ok bool) {
stream.evaluation()
if len(stream.source) == 0 {
return
}
for i, v := range stream.source {
if v > max || i == 0 {
max = v
}
}
return max, true
}
// Min Returns the minimum element of this stream.
// Compare according to the constraints.Ordered.
// If the source is empty or nil then E Type default value is returned. ok return false
func (stream SliceOrderedStream[E]) Min() (min E, ok bool) {
stream.evaluation()
if len(stream.source) == 0 {
return
}
for i, v := range stream.source {
if v < min || i == 0 {
min = v
}
}
return min, true
}
// Sort Returns a sorted stream consisting of the elements of this stream.
// Sorted according to slices.Sort.
func (stream SliceOrderedStream[E]) Sort() SliceOrderedStream[E] {
stream.evaluation()
slices.Sort(stream.source)
return stream
}
// Distinct See SliceComparableStream.Distinct
func (stream SliceOrderedStream[E]) Distinct() SliceOrderedStream[E] {
stream.SliceComparableStream = stream.SliceComparableStream.Distinct()
return stream
}
// Parallel See: SliceStream.Parallel
func (stream SliceOrderedStream[E]) Parallel(goroutines int) SliceOrderedStream[E] {
stream.SliceStream = stream.SliceStream.Parallel(goroutines)
return stream
}
// ForEach See: SliceStream.ForEach
func (stream SliceOrderedStream[E]) ForEach(action func(int, E)) SliceOrderedStream[E] {
stream.SliceStream = stream.SliceStream.ForEach(action)
return stream
}
// Filter See: SliceStream.Filter
func (stream SliceOrderedStream[E]) Filter(predicate func(E) bool) SliceOrderedStream[E] {
stream.SliceStream = stream.SliceStream.Filter(predicate)
return stream
}
// Limit See: SliceStream.Limit
func (stream SliceOrderedStream[E]) Limit(maxSize int) SliceOrderedStream[E] {
stream.SliceStream = stream.SliceStream.Limit(maxSize)
return stream
}
// Map See: SliceStream.Map
func (stream SliceOrderedStream[E]) Map(mapper func(E) E) SliceOrderedStream[E] {
stream.SliceStream = stream.SliceStream.Map(mapper)
return stream
}
// SortFunc See: SliceStream.SortFunc
func (stream SliceOrderedStream[E]) SortFunc(less func(a, b E) bool) SliceOrderedStream[E] {
stream.SliceStream = stream.SliceStream.SortFunc(less)
return stream
}