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, R any, RS ~[]R](slice S, mapper func(item T) RS) RS
- func Compact[S ~[]T, T comparable](slice S) S
- func Contains[S ~[]T, T comparable](slice S, candidate T) bool
- func Difference[S ~[]T, T comparable](slice S, other S) S
- func Filter[S ~[]T, T any](slice S, predicate func(item T) bool) []T
- 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) []T
- 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 FirstOr[T any](slice []T, fallback T) T
- func FirstOrEmpty[T any](slice []T) T
- func GroupBy[S ~[]T, T any, R comparable](slice S, predicate func(item T) R) map[R][]T
- func Intersect[S ~[]T, T comparable](slice S, other S) S
- 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 T, b T) bool) T
- func Mean[S ~[]T, T constraint.Numeric](slice S) T
- func MeanBy[S ~[]T, T any, R constraint.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 T, b T) bool) T
- func Partition[S ~[]T, T any](slice S, predicate func(item T) bool) (S, S)
- func Product[S ~[]T, T constraint.Numeric](slice S) T
- func ProductBy[S ~[]T, T any, R constraint.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 Sum[T constraint.Numeric](slice []T) T
- func SumBy[S ~[]T, T any, R constraint.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 S, other S) S
- func Unique[S ~[]T, T comparable](slice S) []T
- func UniqueBy[S ~[]T, T any, R comparable](slice S, predicate func(item T) R) []T
- func UniqueMap[S ~[]T, T any, R comparable](slice S, mapper func(item T) R) []R
Examples ¶
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 applies a function to each item in the slice.
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 applies a function to each item in the slice and provides the index of the item.
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 transforms and flattens a slice from one type to another using a mapper function. Function should return a slice or `nil`, if `nil` is returned then no value is added to the final result.
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 Compact ¶ added in v0.9.0
func Compact[S ~[]T, T comparable](slice S) S
Compact returns a slice with all the non-zero items.
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 Difference ¶ added in v0.9.2
func Difference[S ~[]T, T comparable](slice S, other S) S
Difference returns a slice with the union of the two slices.
func Filter ¶
Filter filters a slice using a predicate function.
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 to a slice of another type using a mapper function.
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 filters and transforms a slice to a slice of another type using a mapper function.
func FilterWithIndex ¶
FilterWithIndex is like Filter, but it accepts a predicate function that takes an index as well.
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 FirstOr ¶ added in v0.9.2
func FirstOr[T any](slice []T, fallback T) T
FirstOr returns the first item in the slice or a fallback value if the slice is empty.
func FirstOrEmpty ¶ added in v0.9.2
func FirstOrEmpty[T any](slice []T) T
FirstOrEmpty returns the first item in the slice or the empty value if the slice is empty.
func GroupBy ¶
func GroupBy[S ~[]T, T any, R comparable](slice S, predicate func(item T) R) map[R][]T
GroupBy returns a map of slices grouped by a key produced by a predicate 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 Intersect ¶ added in v0.9.2
func Intersect[S ~[]T, T comparable](slice S, other S) S
Intersect returns a slice with the intersection of the two slices.
func Map ¶
Map transforms a slice to a slice of another type using a mapper function.
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 it accepts a mapper function that takes an index as well.
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 provides the maximum value of the 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 value of the slice as determined by the provided maximum function.
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 constraint.Numeric](slice S) T
Mean returns the mean of the values of 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}
mean := slicex.Mean(numbers)
fmt.Println(mean)
}
Output: 5
func MeanBy ¶
func MeanBy[S ~[]T, T any, R constraint.Numeric](slice S, valueFunc func(item T) R) R
MeanBy returns the mean of the values of the slice as determined by the provided value function.
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 value of the 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 value of the slice as determined by the provided minimum function.
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 Partition ¶
Partition splits a slice into two slices based on a predicate.
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 constraint.Numeric](slice S) T
Product returns the product of the values of 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}
product := slicex.Product(numbers)
fmt.Println(product)
}
Output: 518400
func ProductBy ¶
func ProductBy[S ~[]T, T any, R constraint.Numeric](slice S, productFunc func(item T) R) R
ProductBy returns the product of the values in the slice as determined by the provided product function.
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 transforms and flattens a slice to another type.
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 slice with the revers of the slice.
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 Sum ¶
func Sum[T constraint.Numeric](slice []T) T
Sum returns the sum of all the values of 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 constraint.Numeric](slice S, sumFunc func(item T) R) R
SumBy returns the sum of all the values of the slice as determined by the provided sum function.
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 predicate to determine the map key.
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 S, other S) S
Union returns a slice with the union of the two slices.
func Unique ¶
func Unique[S ~[]T, T comparable](slice S) []T
Unique returns a slice with all duplicate values removed.
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) []T
UniqueBy returns a slice with unique values determined by a predicate function.
func UniqueMap ¶
func UniqueMap[S ~[]T, T any, R comparable](slice S, mapper func(item T) R) []R
UniqueMap maps a slice to a slice of another type using a mapper function and removes duplicate values.
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']
Types ¶
This section is empty.