-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathintegration.go
120 lines (109 loc) · 3.54 KB
/
integration.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package steams
// Distinct returns a new Steam containing only the unique elements from the input Steam.
// It uses a map to track seen elements and filters out duplicates.
func Distinct[T comparable](s Steam[T]) Steam[T] {
m := make(map[T]bool)
slice := s.Collect()
results := make(List[T], 0, s.Count())
for _, v := range slice {
if !m[v] {
m[v] = true
results = append(results, v)
}
}
return results
}
// Mapping applies the provided mapper function to each element in the List and returns a new List of type R.
func Mapping[T, R any](s Steam[T], mapper func(T) R) Steam[R] {
results := make(List[R], s.Count())
for i, v := range s.Collect() {
results[i] = mapper(v)
}
return results
}
// CollectSteamToSteam2 transforms a Steam of type T into a Steam2 of key-value pairs,
// where keys and values are derived from the provided keyFunc and valueFunc.
// It collects elements from the input Steam and maps them to a new Steam2 instance.
func CollectSteamToSteam2[K comparable, V, T any](s Steam[T], keyFunc func(T) K, valueFunc func(T) V) Steam2[K, V] {
m := make(map[K]V)
for _, v := range s.Collect() {
m[keyFunc(v)] = valueFunc(v)
}
return Map[K, V](m)
}
// CollectSteam2ToSteam transforms a Steam2 of key-value pairs into a Steam of type R,
// using the provided mapper function to convert each key-value pair into a single value of type R.
func CollectSteam2ToSteam[K comparable, V, R any](s Steam2[K, V], mapper func(K, V) R) Steam[R] {
m := s.Collect()
results := make([]R, len(m))
var index int
for k, v := range m {
results[index] = mapper(k, v)
index++
}
return List[R](results)
}
// GroupBy groups elements of a Steam by a classifier function that maps each element to a key.
// It returns a Steam2 where each key corresponds to a Steam of elements that share that key.
func GroupBy[K comparable, V any](s Steam[V], classifier func(V) K) Steam2[K, Steam[V]] {
m := make(Map[K, Steam[V]])
for _, v := range s.Collect() {
c := classifier(v)
if _, ok := m[c]; ok {
m[c] = append(m[c].(List[V]), v)
} else {
m[c] = append(List[V]{}, v)
}
}
return m
}
// GroupByCounting groups elements of a Steam by a classifier function and counts the occurrences of each key.
// It returns a Steam2 where each key corresponds to the count of elements that share that key.
func GroupByCounting[K comparable, V any](s Steam[V], classifier func(V) K) Steam2[K, int] {
m := make(Map[K, int])
for _, v := range s.Collect() {
c := classifier(v)
if _, ok := m[c]; ok {
m[c] = m[c] + 1
} else {
m[c] = 1
}
}
return m
}
// Zip combines two Steams into a single Steam of structs, where each struct contains one element from each input Steam.
// It panics if the two Steams do not have the same length.
func Zip[T, R any](s1 Steam[T], s2 Steam[R]) Steam[struct {
first T
second R
}] {
slice1 := s1.Collect()
slice2 := s2.Collect()
if len(slice1) != len(slice2) {
panic("Steams must have the same length")
}
result := make(List[struct {
first T
second R
}], len(slice1))
for i := range slice1 {
result[i] = struct {
first T
second R
}{slice1[i], slice2[i]}
}
return result
}
// Of creates a Steam from a variadic list of elements of type T.
func Of[T any](args ...T) Steam[T] {
return List[T](args)
}
// OfSlice creates a Steam from a slice of elements of type T.
func OfSlice[T any](slice []T) Steam[T] {
return Of(slice...)
}
// OfMap creates a Steam2 from a map of key-value pairs.
// The keys and values are derived from the provided map.
func OfMap[K comparable, V any](m map[K]V) Steam2[K, V] {
return Map[K, V](m)
}