Transformation
This section contains functions that transform a slice into another slice. Results are returned as a new slice.
Map
Map applies a function to each element in a slice and returns a new slice.
Usage:
slice.Map([]int{1, 2, 3}, func(v int) int { return v * 2 }) // []int{2, 4, 6}
Filter
Filter applies a condition function to each element in a slice and returns a new slice with only the elements that meet the condition.
Usage:
slice.Filter([]int{1, 2, 3}, func(v int) bool { return v > 1 }) // []int{2, 3}
Chunks
Chunks splits a slice into smaller slices of a given size.
Usage:
slice.Chunks([]int{1, 2, 3, 4, 5}, 2) // [[1 2] [3 4] [5]]
Cartesian
Cartesian returns a new slice with all possible combinations of elements from provided slices.
Usage:
slice.Cartesian([]int{1, 2}, []int{3, 4}) // [[1 3] [1 4] [2 3] [2 4]]
Limit
Limit returns a new slice with the first n elements from the provided slice.
Usage:
slice.Limit([]int{1, 2, 3}, 2) // []int{1, 2}
Unique
Unique returns a new slice with only unique elements from the provided slice.
Usage:
slice.Unique([]int{1, 2, 2, 3}) // []int{1, 2, 3}