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 | |||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| GroupBy | groupby | parallel/slice.go#L73 | parallel | slice |
|
30 |
|
|
Returns a map composed of keys generated from the results of running each element of the collection through the predicate. The predicate is called in parallel. Values keep the input order within each group.
import (
lop "github.com/samber/lo/parallel"
)
groups := lop.GroupBy([]int{0, 1, 2, 3, 4, 5}, func(i int) int {
return i % 3
})
// map[int][]int{0: {0, 3}, 1: {1, 4}, 2: {2, 5}}
Custom key types work as long as they are comparable:
type Kind string
groups2 := lop.GroupBy([]string{"go", "rust", "java"}, func(s string) Kind {
if len(s) <= 2 { return "short" }
return "long"
})
// map[Kind][]string{"short": {"go"}, "long": {"rust", "java"}}