Documentation ¶
Overview ¶
Package slice returns generic functions to work with slices.
Index ¶
- func Contains[T comparable](s []T, elem T) bool
- func ContainsEq[T any](s []T, elem T, eq equality.Equality[T]) bool
- func Equal[T any](a []T, b []T, eq equality.Equality[T]) bool
- func Exists[T any](s []T, cond func(T) bool) bool
- func Filter[A any](s []A, p func(A) bool) []A
- func Forall[T any](s []T, cond func(T) bool) bool
- func IndexOf[T any](elem T, elems []T, eq equality.Equality[T]) int
- func Map[A, B any](s []A, f func(A) B) []B
- func PrefixOf[T any](a, b []T, eq equality.Equality[T]) bool
- func RemoveAll[T any](s []T, elem T, eq equality.Equality[T]) []T
- func RemoveAt[T any](s []T, index int) []T
- func RemoveFirst[T any](s []T, elem T, eq equality.Equality[T]) []T
- func Shuffle[T any](s []T)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](s []T, elem T) bool
Contains checks whether a slice contains an element
func ContainsEq ¶
ContainsEq checks whether a slice contains an element
func Filter ¶ added in v1.3.0
Filter only keeps the elements where the predicate returns true
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/slice" ) func main() { l := []int{4, 13, 7, 13, 13, 8} r := slice.Filter(l, func(x int) bool { return x < 10 }) fmt.Printf("r = %#v\n", r) }
Output: r = []int{4, 7, 8}
func IndexOf ¶
IndexOf returns the index of the first occurrence of elem in the slice elems or -1 if elem is not in the slice.
func Map ¶ added in v1.3.0
func Map[A, B any](s []A, f func(A) B) []B
Map maps a function over the slice
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/slice" ) func main() { l1 := []int{1, 2, 3} l2 := slice.Map(l1, func(x int) string { return fmt.Sprintf("%dx", x) }) fmt.Printf("l2 = %#v\n", l2) }
Output: l2 = []string{"1x", "2x", "3x"}
func RemoveAll ¶
RemoveAll removes all occurrences of the element from the slice and returns the modified slice.
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/equality" "github.com/peterzeller/go-fun/slice" ) func main() { l := []int{4, 13, 7, 13, 13, 8} r := slice.RemoveAll(l, 13, equality.Default[int]()) fmt.Printf("r = %#v\n", r) }
Output: r = []int{4, 7, 8}
func RemoveAt ¶
RemoveAt removes the element at the given index from the slice and returns the modified slice.
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/slice" ) func main() { l := []int{4, 13, 7, 8} r := slice.RemoveAt(l, 2) fmt.Printf("r = %#v\n", r) }
Output: r = []int{4, 13, 8}
func RemoveFirst ¶
RemoveFirst removes the first occurrence of an element from the slice and returns the modified slice.
Example ¶
package main import ( "fmt" "github.com/peterzeller/go-fun/equality" "github.com/peterzeller/go-fun/slice" ) func main() { l := []int{4, 13, 7, 13, 13, 8} r := slice.RemoveFirst(l, 13, equality.Default[int]()) fmt.Printf("r = %#v\n", r) }
Output: r = []int{4, 7, 13, 13, 8}
Types ¶
This section is empty.