-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_slice_read.go
More file actions
30 lines (28 loc) · 901 Bytes
/
Copy pathparallel_slice_read.go
File metadata and controls
30 lines (28 loc) · 901 Bytes
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
package gofu
// IndexBy indexes elements by key concurrently. [ ReadOnly ] [ time: O(n/p); space: O(n)]
//
// Example:
//
// bigSlice.Parallel(4).IndexBy(func(v int) int { return v % 10 })
func (ps SliceParallel[T]) IndexBy[K comparable](fn func(T) K) Map[K, T] {
scale := ps.Scale(len(ps.Slice))
return collectMapResults(ps.MapChunks(scale,
func(_ int, s Slice[T]) Map[K, T] { return s.IndexBy(fn) },
))
}
// CountBy counts matching elements concurrently and sums the results. [ ReadOnly ] [ time: O(n/p); space: O(p)]
//
// Example:
//
// gofu.Slice([]int{1, 2, 3, 4, 5, 6}).Parallel(3).
// CountBy(func(v int) bool { return v%2 == 0 }) // 3
func (ps SliceParallel[T]) CountBy(fn func(T) bool) int {
results := ps.MapChunks(ps.Scale(len(ps.Slice)), func(_ int, chunk Slice[T]) int {
return chunk.CountBy(fn)
})
total := 0
for _, r := range results {
total += r
}
return total
}