mirror of
https://github.com/samber/lo.git
synced 2026-04-22 23:47:11 +08:00
1.1 KiB
1.1 KiB
name, slug, sourceRef, category, subCategory, playUrl, similarHelpers, position, signatures, variantHelpers
| name | slug | sourceRef | category | subCategory | playUrl | similarHelpers | position | signatures | variantHelpers | ||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| PartitionBy | partitionby | parallel/slice.go#L92 | parallel | slice |
|
40 |
|
|
Returns a slice of groups where contiguous elements sharing the same key are batched together. Groups are created from the results of running each element of the collection through the predicate. The predicate is called in parallel and the order of groups follows their first appearance in the collection.
import (
lop "github.com/samber/lo/parallel"
)
groups := lop.PartitionBy([]int{-2, -1, 0, 1, 2, 3, 4, 5}, func(x int) string {
if x < 0 { return "negative" }
if x%2 == 0 { return "even" }
return "odd"
})
// [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}}
Works with any comparable key type:
type Bucket int
parts := lop.PartitionBy([]int{1,2,2,3,3,3}, func(x int) Bucket {
return Bucket(x)
})
// [][]int{{1}, {2,2}, {3,3,3}}