Documentation
¶
Overview ¶
Package parallel provides versions of slicex functions that execute in parallel.
Index ¶
- func Apply[S ~[]T, T any](slice S, apply func(item T))
- func ApplyWithLimit[S ~[]T, T any](slice S, apply func(item T), concurrency int)
- func GroupBy[S ~[]T, T any, R comparable](slice S, predicate func(item T) R) map[R]S
- func GroupByWithLimit[S ~[]T, T any, R comparable](slice S, predicate func(item T) R, concurrency int) map[R]S
- func Map[S ~[]T, T any, R any](slice S, mapper func(item T) R) []R
- func MapWithLimit[S ~[]T, T any, R any](slice S, mapper func(item T) R, concurrency int) []R
- func Partition[S ~[]T, T any](slice S, predicate func(item T) bool) (S, S)
- func PartitionWithLimit[S ~[]T, T any](slice S, predicate func(item T) bool, concurrency int) (S, S)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
func Apply[S ~[]T, T any](slice S, apply func(item T))
Apply applies a function to each item in the slice. The apply function is called in parallel.
Example ¶
package main
import (
"fmt"
"sync/atomic"
"github.com/SharkByteSoftware/go-snk/slicex/parallel"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
var total atomic.Int64
parallel.Apply(numbers, func(n int) {
total.Add(int64(n))
})
fmt.Println(total.Load())
}
Output: 21
func ApplyWithLimit ¶ added in v0.9.4
ApplyWithLimit applies a function to each item in the slice. The apply function is called in parallel with a max concurrency.
Example ¶
package main
import (
"fmt"
"sync/atomic"
"github.com/SharkByteSoftware/go-snk/slicex/parallel"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
var total atomic.Int64
parallel.ApplyWithLimit(numbers, func(n int) {
total.Add(int64(n))
}, 2)
fmt.Println(total.Load())
}
Output: 21
func GroupBy ¶
func GroupBy[S ~[]T, T any, R comparable](slice S, predicate func(item T) R) map[R]S
GroupBy returns a map of slices grouped by a key produced by a predicate function. The predicate is called in parallel, and the results are returned in the order they appear in the slice.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/SharkByteSoftware/go-snk/slicex/parallel"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
groups := parallel.GroupBy(numbers, func(n int) string {
if n%2 == 0 {
return "even"
}
return "odd"
})
evens := groups["even"]
odds := groups["odd"]
slices.Sort(evens)
slices.Sort(odds)
fmt.Println(evens)
fmt.Println(odds)
}
Output: [2 4 6] [1 3 5]
func GroupByWithLimit ¶ added in v0.9.4
func GroupByWithLimit[S ~[]T, T any, R comparable](slice S, predicate func(item T) R, concurrency int) map[R]S
GroupByWithLimit returns a map of slices grouped by a key produced by a predicate function. The predicate is called in parallel with a max concurrency, and the results are returned in the order they appear in the slice.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/SharkByteSoftware/go-snk/slicex/parallel"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
groups := parallel.GroupByWithLimit(numbers, func(n int) string {
if n%2 == 0 {
return "even"
}
return "odd"
}, 2)
evens := groups["even"]
odds := groups["odd"]
slices.Sort(evens)
slices.Sort(odds)
fmt.Println(evens)
fmt.Println(odds)
}
Output: [2 4 6] [1 3 5]
func Map ¶
Map transforms a slice to a slice of another type using a mapper function. The mapper function is called in parallel, and results are returned in order they appear in the slice.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex/parallel"
)
func main() {
numbers := []int{1, 2, 3, 4, 5}
squares := parallel.Map(numbers, func(n int) int { return n * n })
fmt.Println(squares)
}
Output: [1 4 9 16 25]
func MapWithLimit ¶ added in v0.9.4
MapWithLimit transforms a slice to a slice of another type using a mapper function with a max concurrency. The mapper function is called in parallel, and results are returned in order they appear in the slice.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex/parallel"
)
func main() {
numbers := []int{1, 2, 3, 4, 5}
squares := parallel.MapWithLimit(numbers, func(n int) int { return n * n }, 2)
fmt.Println(squares)
}
Output: [1 4 9 16 25]
func Partition ¶
Partition splits a slice into two slices based on a predicate. The predicate is called in parallel, and the results are returned in the order they appear in the slice.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/SharkByteSoftware/go-snk/slicex/parallel"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
even, odd := parallel.Partition(numbers, func(n int) bool { return n%2 == 0 })
slices.Sort(even)
slices.Sort(odd)
fmt.Println(even)
fmt.Println(odd)
}
Output: [2 4 6] [1 3 5]
func PartitionWithLimit ¶ added in v0.9.4
func PartitionWithLimit[S ~[]T, T any](slice S, predicate func(item T) bool, concurrency int) (S, S)
PartitionWithLimit splits a slice into two slices based on a predicate. The predicate is called in parallel with a max concurrency, and the results are returned in the order they appear in the slice.
Example ¶
package main
import (
"fmt"
"slices"
"github.com/SharkByteSoftware/go-snk/slicex/parallel"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
even, odd := parallel.PartitionWithLimit(numbers, func(n int) bool { return n%2 == 0 }, 2)
slices.Sort(even)
slices.Sort(odd)
fmt.Println(even)
fmt.Println(odd)
}
Output: [2 4 6] [1 3 5]
Types ¶
This section is empty.