Documentation
¶
Overview ¶
Package slicex provides various slice utilities.
Index ¶
- func All[S ~[]T, T comparable](slice S, candidate T) bool
- func AllBy[S ~[]T, T any](slice S, predicate func(item T) bool) bool
- func Any[S ~[]T, T comparable](slice S, candidate T) bool
- func AnyBy[S ~[]T, T any](slice S, predicate func(item T) bool) bool
- func Apply[S ~[]T, T any](slice S, apply func(item T))
- func ApplyWithIndex[S ~[]T, T any](slice S, apply func(item T, index int))
- func Bind[S ~[]T, T any, RS ~[]R, R any](slice S, mapper func(item T) RS) RS
- func Chunk[S ~[]T, T any](slice S, size int) []S
- func Compact[S ~[]T, T comparable](slice S) S
- func Contains[S ~[]T, T comparable](slice S, candidate T) bool
- func Count[S ~[]T, T comparable](s S, value T) int
- func CountBy[S ~[]T, T any](s S, predicate func(T) bool) int
- func Difference[S ~[]T, T comparable](slice, other S) S
- func Filter[S ~[]T, T any](slice S, predicate func(item T) bool) S
- func FilterMap[S ~[]T, T any, R any](slice S, mapper func(item T) (R, bool)) []R
- func FilterMapWithIndex[S ~[]T, T any, R any](slice S, mapper func(item T, index int) (R, bool)) []R
- func FilterWithIndex[S ~[]T, T any](slice S, predicate func(item T, index int) bool) S
- func Find[S ~[]T, T comparable](slice S, candidate T) (T, bool)
- func FindBy[S ~[]T, T any](slice S, predicate func(item T) bool) (T, bool)
- func FindOr[S ~[]T, T comparable](slice S, candidate T, fallback T) T
- func FindOrBy[S ~[]T, T any](slice S, predicate func(item T) bool, fallback T) T
- func FirstBy[S ~[]T, T any](slice S, predicate func(item T) bool) (T, bool)
- func FirstOr[S ~[]T, T any](slice S, fallback T) T
- func FirstOrBy[S ~[]T, T any](slice S, predicate func(item T) bool, fallback T) T
- func FirstOrEmpty[S ~[]T, T any](slice S) T
- func Flatten[S ~[]E, E any](slice []S) []E
- func GroupBy[S ~[]T, T any, R comparable](slice S, predicate func(item T) R) map[R]S
- func IndexBy[S ~[]T, T any](slice S, predicate func(item T) bool) int
- func IndexOf[S ~[]T, T comparable](slice S, candidate T) int
- func Intersect[S ~[]T, T comparable](slice, other S) S
- func LastBy[S ~[]T, T any](slice S, predicate func(item T) bool) (T, bool)
- func LastOr[S ~[]T, T any](slice S, fallback T) T
- func LastOrBy[S ~[]T, T any](slice S, predicate func(item T) bool, fallback T) T
- func LastOrEmpty[S ~[]T, T any](slice S) T
- func Map[S ~[]T, T any, R any](slice S, mapper func(item T) R) []R
- func MapWithIndex[S ~[]T, T any, R any](slice S, mapper func(item T, idx int) R) []R
- func Max[S ~[]T, T cmp.Ordered](slice S) T
- func MaxBy[S ~[]T, T any](slice S, maxFunc func(a, b T) bool) T
- func Mean[S ~[]T, T constraints.Numeric](slice S) T
- func MeanBy[S ~[]T, T any, R constraints.Numeric](slice S, valueFunc func(item T) R) R
- func Min[S ~[]T, T cmp.Ordered](slice S) T
- func MinBy[S ~[]T, T any](slice S, minFunc func(a, b T) bool) T
- func None[S ~[]T, T comparable](s S, value T) bool
- func NoneBy[S ~[]T, T any](s S, predicate func(T) bool) bool
- func Partition[S ~[]T, T any](slice S, predicate func(item T) bool) (S, S)
- func Product[S ~[]T, T constraints.Numeric](slice S) T
- func ProductBy[S ~[]T, T any, R constraints.Numeric](slice S, productFunc func(item T) R) R
- func Reduce[S ~[]T, T any, R any](slice S, accumulator func(agg R, item T) R, initial R) R
- func Reverse[S ~[]T, T any](slice S) S
- func Rotate[S ~[]T, T any](slice S, n int) S
- func Sort[S ~[]T, T cmp.Ordered](s S) S
- func SortBy[S ~[]T, T any](s S, cmp func(a, b T) int) S
- func Sum[S ~[]T, T constraints.Numeric](slice S) T
- func SumBy[S ~[]T, T any, R constraints.Numeric](slice S, sumFunc func(item T) R) R
- func ToMap[S ~[]T, T any, K comparable](slice S, predicate func(item T) K) map[K]T
- func Union[S ~[]T, T comparable](slice, other S) S
- func Unique[S ~[]T, T comparable](slice S) S
- func UniqueBy[S ~[]T, T any, R comparable](slice S, predicate func(item T) R) S
- func UniqueMap[S ~[]T, T any, R comparable](slice S, mapper func(item T) R) []R
- func Window[S ~[]T, T any](slice S, size int) []S
- type Pair
Examples ¶
- All
- Any
- AnyBy
- Apply
- ApplyWithIndex
- Bind
- Chunk
- Compact
- Count
- CountBy
- Filter
- FilterWithIndex
- Find
- FindBy
- FindOr
- FindOrBy
- FirstBy
- FirstOr
- FirstOrBy
- FirstOrEmpty
- Flatten
- GroupBy
- IndexBy
- IndexOf
- LastBy
- LastOr
- LastOrBy
- LastOrEmpty
- Map
- MapWithIndex
- Max
- MaxBy
- Mean
- MeanBy
- Min
- MinBy
- None
- NoneBy
- Partition
- Product
- ProductBy
- Reduce
- Reverse
- Rotate
- Sort
- SortBy
- Sum
- SumBy
- ToMap
- Unique
- UniqueMap
- Window
- Zip
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
func All[S ~[]T, T comparable](slice S, candidate T) bool
All returns true if all items in the slice are equal to the given candidate.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers1 := []int{2, 2, 2}
numbers2 := []int{2, 2, 5}
result1 := slicex.All(numbers1, 2)
result2 := slicex.All(numbers2, 2)
fmt.Println(result1, result2)
}
Output: true false
func Any ¶
func Any[S ~[]T, T comparable](slice S, candidate T) bool
Any returns true if any item in the slice satisfies the predicate.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{2, 2, 2, 4, 5, 6}
result1 := slicex.Any(numbers, 2)
result2 := slicex.Any(numbers, 22)
fmt.Println(result1, result2)
}
Output: true false
func AnyBy ¶
AnyBy returns true if any item in the slice satisfies the predicate.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{2, 2, 2, 4, 12, 6}
result1 := slicex.AnyBy(numbers, func(item int) bool { return item%2 == 0 })
result2 := slicex.AnyBy(numbers, func(item int) bool { return item%2 != 0 })
fmt.Println(result1, result2)
}
Output: true false
func Apply ¶
func Apply[S ~[]T, T any](slice S, apply func(item T))
Apply calls the provided function on each element of the slice for side effects. No new slice is returned.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
var sum int
slicex.Apply(numbers, func(item int) {
sum += item
})
fmt.Println(sum)
}
Output: 21
func ApplyWithIndex ¶
ApplyWithIndex is like Apply, but the function also receives the index of each element.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
var sum int
slicex.ApplyWithIndex(numbers, func(_ int, index int) {
sum += numbers[index]
numbers[index] = sum
})
fmt.Println(sum, numbers)
}
Output: 21 [1 3 6 10 15 21]
func Bind ¶
Bind maps each element to a slice using the mapper function and concatenates the results into a single flat slice. This is equivalent to a flatMap operation. If the mapper returns nil or an empty slice, no elements are added.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
nestedNumbers := [][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
values := slicex.Bind(nestedNumbers, func(item []int) []int { return item })
fmt.Println(values)
}
Output: [1 2 3 4 5 6 7 8 9]
func Chunk ¶ added in v1.2.0
Chunk splits a slice into sub-slices of at most size n. The final chunk may be smaller than n if the slice length is not evenly divisible. Chunk panics if n < 1.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5}
chunks := slicex.Chunk(numbers, 2)
fmt.Println(chunks)
}
Output: [[1 2] [3 4] [5]]
func Compact ¶ added in v0.9.0
func Compact[S ~[]T, T comparable](slice S) S
Compact returns a new slice with all zero-value elements removed.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{0, 2, 3, 4, 5, 0}
values := slicex.Compact(numbers)
fmt.Println(values)
}
Output: [2 3 4 5]
func Contains ¶ added in v0.9.6
func Contains[S ~[]T, T comparable](slice S, candidate T) bool
Contains returns true if the slice contains the given candidate.
func Count ¶ added in v1.2.4
func Count[S ~[]T, T comparable](s S, value T) int
Count returns the number of elements in the slice equal to the given value.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 2, 3, 2, 4}
fmt.Println(slicex.Count(numbers, 2))
fmt.Println(slicex.Count(numbers, 5))
}
Output: 3 0
func CountBy ¶ added in v1.2.4
CountBy returns the number of elements in the slice satisfying the predicate.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
fmt.Println(slicex.CountBy(numbers, func(n int) bool { return n%2 == 0 }))
}
Output: 3
func Difference ¶ added in v0.9.2
func Difference[S ~[]T, T comparable](slice, other S) S
Difference returns a new slice containing elements present in slice but not in other. The order of the result is not guaranteed.
func Filter ¶
Filter returns a new slice containing only the items for which the predicate returns true. The original slice is not modified.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
values := slicex.Filter(numbers, func(item int) bool { return item%2 == 0 })
fmt.Println(values)
}
Output: [2 4 6]
func FilterMap ¶ added in v0.9.3
FilterMap filters and transforms a slice in a single pass. The mapper returns a value and a boolean; only values where the boolean is true are included in the result.
func FilterMapWithIndex ¶ added in v0.9.3
func FilterMapWithIndex[S ~[]T, T any, R any](slice S, mapper func(item T, index int) (R, bool)) []R
FilterMapWithIndex is like FilterMap, but the mapper also receives the index of each element.
func FilterWithIndex ¶
FilterWithIndex is like Filter, but the predicate also receives the index of each element.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
values := slicex.FilterWithIndex(numbers, func(_ int, idx int) bool {
return numbers[idx]%3 == 0
})
fmt.Println(values)
}
Output: [3 6]
func Find ¶
func Find[S ~[]T, T comparable](slice S, candidate T) (T, bool)
Find returns the first item in the slice that is equal to the given candidate.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []string{"one", "two", "three", "four", "five", "six", "seven", "not"}
value, found := slicex.Find(numbers, "four")
fmt.Println(value, found)
}
Output: four true
func FindBy ¶
FindBy returns the first item in the slice that satisfies the predicate.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []string{"one", "two", "not"}
value, found := slicex.FindBy(numbers, func(item string) bool {
return item != "one" && item != "two"
})
fmt.Println(value, found)
}
Output: not true
func FindOr ¶
func FindOr[S ~[]T, T comparable](slice S, candidate T, fallback T) T
FindOr returns the first item in the slice that is equal to the given candidate, or the fallback value if not found.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
value1 := slicex.FindOr(numbers, 22, 256)
value2 := slicex.FindOr(numbers, 6, 256)
fmt.Println(value1, value2)
}
Output: 256 6
func FindOrBy ¶
FindOrBy returns the first item in the slice that satisfies the predicate, or the fallback value if not found.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []string{"one", "two", "not"}
value1 := slicex.FindOrBy(numbers, func(item string) bool { return item == "not" },
"nothing")
value2 := slicex.FindOrBy(numbers, func(item string) bool { return item == "other" },
"nothing")
fmt.Println(value1, value2)
}
Output: not nothing
func FirstBy ¶ added in v1.2.0
FirstBy returns the first item in the slice that satisfies the predicate, along with a boolean indicating whether a match was found. Delegates to FindBy — exists for naming symmetry with LastBy.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 3, 4, 6}
value, found := slicex.FirstBy(numbers, func(n int) bool { return n%2 == 0 })
fmt.Println(value, found)
value, found = slicex.FirstBy(numbers, func(n int) bool { return n > 100 })
fmt.Println(value, found)
}
Output: 4 true 0 false
func FirstOr ¶ added in v0.9.2
func FirstOr[S ~[]T, T any](slice S, fallback T) T
FirstOr returns the first item in the slice or a fallback value if the slice is empty.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{10, 20, 30}
fmt.Println(slicex.FirstOr(numbers, -1))
fmt.Println(slicex.FirstOr([]int{}, -1))
}
Output: 10 -1
func FirstOrBy ¶ added in v1.2.0
FirstOrBy returns the first item in the slice that satisfies the predicate, or the fallback value if no match is found. Delegates to FindOrBy — exists for naming symmetry with LastOrBy.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 3, 4, 6}
fmt.Println(slicex.FirstOrBy(numbers, func(n int) bool { return n%2 == 0 }, -1))
fmt.Println(slicex.FirstOrBy(numbers, func(n int) bool { return n > 100 }, -1))
}
Output: 4 -1
func FirstOrEmpty ¶ added in v0.9.2
func FirstOrEmpty[S ~[]T, T any](slice S) T
FirstOrEmpty returns the first item in the slice or the empty value if the slice is empty.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{10, 20, 30}
fmt.Println(slicex.FirstOrEmpty(numbers))
fmt.Println(slicex.FirstOrEmpty([]int{}))
}
Output: 10 0
func Flatten ¶ added in v1.2.0
func Flatten[S ~[]E, E any](slice []S) []E
Flatten collapses a slice of slices into a single flat slice. Nil inner slices are skipped. This is equivalent to Bind with an identity mapper but expresses intent more clearly when no transformation is needed.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
nested := [][]int{{1, 2}, {3, 4}, {5}}
result := slicex.Flatten(nested)
fmt.Println(result)
}
Output: [1 2 3 4 5]
func GroupBy ¶
func GroupBy[S ~[]T, T any, R comparable](slice S, predicate func(item T) R) map[R]S
GroupBy groups slice elements into a map of slices, keyed by the result of the key selector function.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6, 8, 9, 10}
result := slicex.GroupBy(numbers, func(item int) int {
return item % 4
})
fmt.Println(result)
}
Output: map[0:[4 8] 1:[1 5 9] 2:[2 6 10] 3:[3]]
func IndexBy ¶ added in v1.2.0
IndexBy returns the index of the first element in the slice that satisfies the predicate, or -1 if not found.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 3, 5, 6, 7}
idx := slicex.IndexBy(numbers, func(n int) bool { return n%2 == 0 })
fmt.Println(idx)
}
Output: 3
func IndexOf ¶ added in v1.2.0
func IndexOf[S ~[]T, T comparable](slice S, candidate T) int
IndexOf returns the index of the first element in the slice equal to the candidate, or -1 if not found.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{10, 20, 30, 40}
fmt.Println(slicex.IndexOf(numbers, 30))
fmt.Println(slicex.IndexOf(numbers, 99))
}
Output: 2 -1
func Intersect ¶ added in v0.9.2
func Intersect[S ~[]T, T comparable](slice, other S) S
Intersect returns a new slice containing elements present in both slices. The order of the result is not guaranteed.
func LastBy ¶ added in v1.2.0
LastBy returns the last item in the slice that satisfies the predicate, along with a boolean indicating whether a match was found.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5}
value, found := slicex.LastBy(numbers, func(n int) bool { return n%2 == 0 })
fmt.Println(value, found)
value, found = slicex.LastBy(numbers, func(n int) bool { return n > 100 })
fmt.Println(value, found)
}
Output: 4 true 0 false
func LastOr ¶ added in v1.2.0
func LastOr[S ~[]T, T any](slice S, fallback T) T
LastOr returns the last element of the slice or a fallback value if the slice is empty.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3}
fmt.Println(slicex.LastOr(numbers, -1))
fmt.Println(slicex.LastOr([]int{}, -1))
}
Output: 3 -1
func LastOrBy ¶ added in v1.2.0
LastOrBy returns the last item in the slice that satisfies the predicate, or the fallback value if no match is found.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5}
fmt.Println(slicex.LastOrBy(numbers, func(n int) bool { return n%2 == 0 }, -1))
fmt.Println(slicex.LastOrBy(numbers, func(n int) bool { return n > 100 }, -1))
}
Output: 4 -1
func LastOrEmpty ¶ added in v1.2.0
func LastOrEmpty[S ~[]T, T any](slice S) T
LastOrEmpty returns the last element of the slice or the zero value if the slice is empty.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3}
fmt.Println(slicex.LastOrEmpty(numbers))
fmt.Println(slicex.LastOrEmpty([]int{}))
}
Output: 3 0
func Map ¶
Map transforms each element of a slice using the mapper function, returning a new slice of the same length. The original slice is not modified.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
values := slicex.Map(numbers, func(item int) string { return fmt.Sprintf("'%d'", item) })
fmt.Println(values)
}
Output: ['1' '2' '3' '4' '5' '6']
func MapWithIndex ¶
MapWithIndex is like Map, but the mapper also receives the index of each element.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
values := slicex.MapWithIndex(numbers, func(_ int, idx int) string {
return fmt.Sprintf("'%d'", numbers[idx])
})
fmt.Println(values)
}
Output: ['1' '2' '3' '4' '5' '6']
func Max ¶
Max returns the maximum element in the slice using natural ordering. Returns the zero value of T for an empty slice.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 30, 3, 4, 5, 6, -1, 9, 10}
maximum := slicex.Max(numbers)
fmt.Println(maximum)
}
Output: 30
func MaxBy ¶
MaxBy returns the maximum element in the slice as determined by the less function. less(a, b) should return true when a should be considered less than b. Returns the zero value of T for an empty slice.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
strings := []string{"a", "aa", "aaa"}
longest := slicex.MaxBy(strings, func(a string, b string) bool { return len(a) < len(b) })
fmt.Println(longest)
}
Output: aaa
func Mean ¶
func Mean[S ~[]T, T constraints.Numeric](slice S) T
Mean returns the arithmetic mean of all elements in the slice. Returns the zero value of T for an empty slice.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6, 8, 9, 10}
mean := slicex.Mean(numbers)
fmt.Println(mean)
}
Output: 5
func MeanBy ¶
func MeanBy[S ~[]T, T any, R constraints.Numeric](slice S, valueFunc func(item T) R) R
MeanBy returns the arithmetic mean of values produced by applying the value selector to each element. Returns the zero value of R for an empty slice.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []float32{-1, 0, 1, 2}
meanSquare := slicex.MeanBy(numbers, func(n float32) float32 { return n * n })
fmt.Println(meanSquare)
}
Output: 1.5
func Min ¶
Min returns the minimum element in the slice using natural ordering. Returns the zero value of T for an empty slice.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 30, 3, 4, 5, 6, -1, 9, 10}
minimum := slicex.Min(numbers)
fmt.Println(minimum)
}
Output: -1
func MinBy ¶
MinBy returns the minimum element in the slice as determined by the less function. less(a, b) should return true when a should be considered less than b. Returns the zero value of T for an empty slice.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
strings := []string{"a", "aa", "aaa"}
shortest := slicex.MinBy(strings, func(a string, b string) bool { return len(a) > len(b) })
fmt.Println(shortest)
}
Output: a
func None ¶ added in v1.2.4
func None[S ~[]T, T comparable](s S, value T) bool
None returns true if no item in the slice is equal to the given value.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 3, 5, 7}
fmt.Println(slicex.None(numbers, 2))
fmt.Println(slicex.None(numbers, 3))
}
Output: true false
func NoneBy ¶ added in v1.2.4
NoneBy returns true if no item in the slice satisfies the predicate.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 3, 5, 7}
fmt.Println(slicex.NoneBy(numbers, func(n int) bool { return n%2 == 0 }))
fmt.Println(slicex.NoneBy(numbers, func(n int) bool { return n > 4 }))
}
Output: true false
func Partition ¶
Partition splits a slice into two slices based on a predicate. Elements for which the predicate returns true are placed in the first slice; all others in the second.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6, 8, 9, 10}
even, odd := slicex.Partition(numbers, func(item int) bool { return item%2 == 0 })
fmt.Println(even, odd)
}
Output: [2 4 6 8 10] [1 3 5 9]
func Product ¶
func Product[S ~[]T, T constraints.Numeric](slice S) T
Product returns the product of all elements in the slice. Returns 1 for an empty slice.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6, 8, 9, 10}
product := slicex.Product(numbers)
fmt.Println(product)
}
Output: 518400
func ProductBy ¶
func ProductBy[S ~[]T, T any, R constraints.Numeric](slice S, productFunc func(item T) R) R
ProductBy returns the product of values produced by applying the value selector to each element. Returns 1 for an empty slice.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
strings := []string{"a", "aa", "aaa"}
product := slicex.ProductBy(strings, func(s string) int { return len(s) })
fmt.Println(product)
}
Output: 6
func Reduce ¶ added in v0.1.0
Reduce folds a slice into a single value by applying the accumulator function to each element in order, threading the result through each call. The initial value is used as the starting accumulator.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
nestedNumbers := [][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
acc := func(agg int, items []int) int {
return agg * slicex.Sum(items)
}
result := slicex.Reduce(nestedNumbers, acc, 1)
fmt.Println(result)
}
Output: 2160
func Reverse ¶
func Reverse[S ~[]T, T any](slice S) S
Reverse returns a new slice with the elements in reverse order. The original slice is not modified.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
values := slicex.Reverse(numbers)
fmt.Println(values)
}
Output: [6 5 4 3 2 1]
func Rotate ¶ added in v1.1.0
Rotate returns a copy of the slice with elements shifted left by n positions. Elements shifted off the front are wrapped to the back. A negative n shifts right instead. If the slice is empty, it is returned as-is.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5}
fmt.Println(slicex.Rotate(numbers, 2))
fmt.Println(slicex.Rotate(numbers, -2))
}
Output: [3 4 5 1 2] [4 5 1 2 3]
func Sort ¶ added in v1.2.4
Sort returns a sorted copy of the slice using the natural ordering of the elements. The original slice is not modified.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{5, 2, 8, 1, 9, 3}
fmt.Println(slicex.Sort(numbers))
fmt.Println(numbers) // original unchanged
}
Output: [1 2 3 5 8 9] [5 2 8 1 9 3]
func SortBy ¶ added in v1.2.4
SortBy returns a sorted copy of the slice using the provided comparison function. The original slice is not modified. cmp should return a negative number when a < b, zero when a == b, and a positive number when a > b.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
words := []string{"banana", "apple", "cherry", "date"}
byLength := slicex.SortBy(words, func(a, b string) int { return len(a) - len(b) })
fmt.Println(byLength)
fmt.Println(words) // original unchanged
}
Output: [date apple banana cherry] [banana apple cherry date]
func Sum ¶
func Sum[S ~[]T, T constraints.Numeric](slice S) T
Sum returns the sum of all elements in the slice.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6, 8, 9, 10}
sum := slicex.Sum(numbers)
fmt.Println(sum)
}
Output: 48
func SumBy ¶
func SumBy[S ~[]T, T any, R constraints.Numeric](slice S, sumFunc func(item T) R) R
SumBy returns the sum of values produced by applying the value selector to each element.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3}
sum := slicex.SumBy(numbers, func(n int) int { return n * n })
fmt.Println(sum)
}
Output: 14
func ToMap ¶
func ToMap[S ~[]T, T any, K comparable](slice S, predicate func(item T) K) map[K]T
ToMap converts a slice to a map using the key selector to determine each element's map key. If multiple elements produce the same key, the last one wins.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5, 6}
result := slicex.ToMap(numbers, func(item int) string { return fmt.Sprintf("%d~key", item) })
fmt.Println(result)
}
Output: map[1~key:1 2~key:2 3~key:3 4~key:4 5~key:5 6~key:6]
func Union ¶ added in v0.9.2
func Union[S ~[]T, T comparable](slice, other S) S
Union returns a new slice containing all unique elements from both slices. The order of the result is not guaranteed.
func Unique ¶
func Unique[S ~[]T, T comparable](slice S) S
Unique returns a new slice with duplicate values removed, preserving the order of first occurrence.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 1, 2, 2, 2, 5, 5, 5, 5}
values := slicex.Unique(numbers)
fmt.Println(values)
}
Output: [1 2 5]
func UniqueBy ¶ added in v0.9.2
func UniqueBy[S ~[]T, T any, R comparable](slice S, predicate func(item T) R) S
UniqueBy returns a new slice with duplicates removed, using the key selector to determine uniqueness. The original elements are returned; only the first element for each unique key is kept.
func UniqueMap ¶
func UniqueMap[S ~[]T, T any, R comparable](slice S, mapper func(item T) R) []R
UniqueMap transforms each element using the mapper function and returns the results with duplicates removed. Uniqueness is determined on the mapped values, not the original elements.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 1, 2, 2, 2, 5, 5, 5, 5}
values := slicex.UniqueMap(numbers, func(item int) string { return fmt.Sprintf("'%d'", item) })
fmt.Println(values)
}
Output: ['1' '2' '5']
func Window ¶ added in v1.1.0
Window returns a slice of overlapping sub-slices of the given size, advancing one position at a time. If the size is less than 1 or greater than the length of the slice, an empty slice is returned.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
numbers := []int{1, 2, 3, 4, 5}
windows := slicex.Window(numbers, 3)
for _, w := range windows {
fmt.Println(w)
}
}
Output: [1 2 3] [2 3 4] [3 4 5]
Types ¶
type Pair ¶ added in v1.1.0
type Pair[A, B any] struct { Left A Right B }
Pair holds two values of independent types.
func Zip ¶ added in v1.1.0
Zip combines two slices into a slice of Pairs, pairing elements by position. The result length is equal to the shorter of the two input slices.
Example ¶
package main
import (
"fmt"
"github.com/SharkByteSoftware/go-snk/slicex"
)
func main() {
names := []string{"alice", "bob", "carol"}
scores := []int{92, 85, 78}
pairs := slicex.Zip(names, scores)
for _, p := range pairs {
fmt.Printf("%s: %d\n", p.Left, p.Right)
}
}
Output: alice: 92 bob: 85 carol: 78