Generic slice utilities for Go. Map, filter, reduce, and more with type safety
go get github.com/philiprehberger/go-sliceutilGeneric slice utilities for Go. Map, filter, reduce, and more with type safety
go get github.com/philiprehberger/go-sliceutil
import "github.com/philiprehberger/go-sliceutil"
doubled := sliceutil.Map([]int{1, 2, 3}, func(n int) int {
return n * 2
})
// [2, 4, 6]
evens := sliceutil.Filter([]int{1, 2, 3, 4, 5}, func(n int) bool {
return n%2 == 0
})
// [2, 4]
sum := sliceutil.Reduce([]int{1, 2, 3, 4}, func(acc, n int) int {
return acc + n
}, 0)
// 10
unique := sliceutil.Unique([]int{1, 2, 2, 3, 3})
// [1, 2, 3]
flat := sliceutil.Flatten([][]int{{1, 2}, {3, 4}})
// [1, 2, 3, 4]
common := sliceutil.Intersect([]int{1, 2, 3}, []int{2, 3, 4})
// [2, 3]
diff := sliceutil.Difference([]int{1, 2, 3}, []int{2, 3, 4})
// [1]
all := sliceutil.Union([]int{1, 2, 3}, []int{2, 3, 4})
// [1, 2, 3, 4]
val, ok := sliceutil.Find([]int{1, 2, 3, 4}, func(n int) bool {
return n > 2
})
// val: 3, ok: true
idx := sliceutil.FindIndex([]string{"a", "b", "c"}, func(s string) bool {
return s == "b"
})
// 1
hasEven := sliceutil.Any([]int{1, 3, 4}, func(n int) bool {
return n%2 == 0
})
// true
allPositive := sliceutil.All([]int{1, 2, 3}, func(n int) bool {
return n > 0
})
// true
type Item struct {
Name string
Price int
}
sorted := sliceutil.SortBy(items, func(i Item) int {
return i.Price
})
// sorted by price ascending
first3 := sliceutil.Take([]int{1, 2, 3, 4, 5}, 3)
// [1, 2, 3]
rest := sliceutil.Drop([]int{1, 2, 3, 4, 5}, 2)
// [3, 4, 5]
cleaned := sliceutil.Compact([]string{"", "hello", "", "world"})
// ["hello", "world"]
nonZero := sliceutil.Compact([]int{0, 1, 0, 2, 3})
// [1, 2, 3]
evens, odds := sliceutil.Partition([]int{1, 2, 3, 4, 5}, func(n int) bool {
return n%2 == 0
})
// evens: [2, 4], odds: [1, 3, 5]
| Function | Description |
|---|---|
Map[T, R] | Transform each element |
Filter[T] | Keep elements matching predicate |
Reduce[T, R] | Fold into single value |
Unique[T] | Remove duplicates, preserve order |
UniqueBy[T, K] | Deduplicate by key function |
Flatten[T] | Flatten one level of nesting |
FlatMap[T, R] | Map then flatten |
Zip[T, U] | Pair elements from two slices |
Partition[T] | Split by predicate |
Chunk[T] | Split into chunks of given size |
Reverse[T] | Return reversed copy |
Shuffle[T] | Return shuffled copy |
Contains[T] | Check if element exists |
IndexOf[T] | Return index or -1 |
First[T] | Return first element |
Last[T] | Return last element |
Find[T] | First element matching predicate |
FindIndex[T] | Index of first match, -1 if none |
Any[T] | True if any element matches |
All[T] | True if all elements match |
SortBy[T, K] | Sort by extracted key |
Take[T] | Return first n elements |
Drop[T] | Skip first n elements |
Compact[T] | Remove zero-value elements |
Intersect[T] | Elements in both slices |
Difference[T] | Elements in first but not second |
Union[T] | All unique elements from both |
SymmetricDifference[T] | Elements in either but not both |
go test ./...
go vet ./...
If you find this project useful: