underscore/unique_by.go
Ruidy 1031038d42
feat: add Chunk, ContainsBy, UniqueBy, ParallelMap, map helpers
- Add `Chunk` to split slices into groups of size n.
- Add `ContainsBy` for predicate-based containment checks.
- Add `UniqueBy` to deduplicate slices by key selector.
- Add `ParallelMap` for concurrent mapping with context and error
handling.
- Add `maps.Keys` and `maps.Values` helpers for extracting map
keys/values.
- Update README and docs for new features.
- Refactor `Contains` to use `slices.Contains`.
2025-09-01 18:03:38 -04:00

16 lines
445 B
Go

package underscore
// UniqueBy returns a slice of unique values from the given slice using a key selector.
// The first occurrence of each key is kept and order is preserved.
func UniqueBy[T any, K comparable](values []T, key func(T) K) (uniques []T) {
seen := make(map[K]struct{})
for _, v := range values {
k := key(v)
if _, ok := seen[k]; ok {
continue
}
seen[k] = struct{}{}
uniques = append(uniques, v)
}
return uniques
}