Documentation ΒΆ
Overview ΒΆ
π Package slices provides generic functions for slices.
The package is inspired by Enum and List Elixir modules.
Conventions ΒΆ
- All functions accepting a slice accept it as the very first argument.
- If a function provides 2 implementations one of which accepts a function (for example, Equal and EqualBy), the one accepting the function has suffix "By".
- If a function is concurrent, it has suffix "Async".
- Almost all functions are pure and don't modify the given slice. The only exception so far is Shuffle.
Functions ΒΆ
This package has a lot of functions and it might be hard to find what you need if you don't know how it is called. Because of that, this section provides an easier to navigate list of all functions defined here. Each of them is grouped based on the return type.
Also, for easier visual navigation, a signature is provided for each function where instead of argument type and name an emoji is used:
- π is a slice
- π is a slice element
- π¬ is a function
- β is a bool
- πΊ is a channel
- πΊ is a map
- π₯ is an error
- π’ is an int
- π² is a randomization seed
- π§βπ§οΈ is a number of workers
π² Randomization functions:
- Choice(π, π²) (π, π₯)
- Shuffle(π, π²)
- TakeRandom(π, π’, π²) (π, π₯)
β Functions returning a bool:
- All(π, π¬) β
- AllAsync(π, π§βπ§οΈ, π¬) β
- Any(π, π¬) β
- AnyAsync(π, π§βπ§οΈ, π¬) β
- Contains(π, π) β
- EndsWith(π, π) β
- Equal(π, π) β
- EqualBy(π, π, π¬) β
- Sorted(π) β
- SortedUnique(π) β
- Same(π) β
- StartsWith(π, π) β
- Unique(π) β
πΊ Functions returning a map:
πΊ Functions returning a channel:
- Cycle(π) πΊ
- Permutations(π, π’) πΊ
- Product(π, π’) πΊ
- Product2(...π) πΊ
- ToChannel(π) πΊ
- Zip(...π) πΊ
π Functions returning a single item:
- Find(π, π¬) (π, π₯)
- Last(π) (π, π₯)
- Max(π) (π, π₯)
- Min(π) (π, π₯)
- Reduce(π, π, π¬) π
- ReduceAsync(π, π§βπ§οΈ, π¬) π
- ReduceWhile(π, π, π¬) (π, π₯)
- Sum(π) π
π’ Functions returning an int:
- Count(π, π) π’
- CountBy(π, π¬) π’
- FindIndex(π, π¬) π’
- Index(π, π) (π’, π₯)
- IndexBy(π, π¬) (π’, π₯)
π¨ Functions that take a slice and return a slice:
- Copy(π) π
- Dedup(π) π
- DropZero(π) π
- Reverse(π) π
- Shrink(π) π
- Sort(π) π
- Uniq(π) π
π Functions returning a new slice:
- ChunkBy(π, π¬) π
- ChunkEvery(π, π’) (π, π₯)
- Concat(...π) π
- DedupBy(π, π¬) π
- Delete(π, π) π
- DeleteAll(π, π) π
- DeleteAt(π, π’) (π, π₯)
- Difference(π, π) π
- DropEvery(π, π’, π’) (π, π₯)
- DropWhile(π, π¬) π
- Filter(π, π¬) π
- FilterAsync(π, π§βπ§οΈ, π¬) π
- Grow(π, π’) π
- InsertAt(π, π’, π) (π, π₯)
- Intersect(π, π) π
- Intersperse(π, π) π
- Map(π, π¬) π
- MapAsync(π, π§βπ§οΈ, π¬) π
- MapFilter(π, π¬) π
- Prepend(π, ...π) π
- Reject(π, π¬) π
- Repeat(π, π’) π
- Replace(π, π’, π’, π) (π, π₯)
- Scan(π, π, π¬) π
- SortBy(π, π¬) π
- Split(π, π) π
- TakeEvery(π, π’, π’) (π, π₯)
- TakeWhile(π, π¬) π
- Union(π, π) π
- Window(π, π’) (π, π₯)
- Without(π, π) π
- Wrap(π) π
πΆ Functions returning a something else or nothing:
Index ΒΆ
- Variables
- func All[S ~[]T, T any](items S, f func(el T) bool) bool
- func AllAsync[S ~[]T, T any](items S, workers int, f func(el T) bool) bool
- func Any[S ~[]T, T any](items S, f func(el T) bool) bool
- func AnyAsync[S ~[]T, T any](items S, workers int, f func(el T) bool) bool
- func Choice[S ~[]T, T any](items S, seed int64) (T, error)
- func ChunkBy[S ~[]T, T comparable, G comparable](items S, f func(el T) G) []S
- func ChunkEvery[S ~[]T, T any](items S, count int) ([]S, error)
- func Concat[S ~[]T, T any](slices ...S) S
- func Contains[S ~[]T, T comparable](items S, el T) bool
- func Copy[S ~[]T, T any](items S) S
- func Count[S ~[]T, T comparable](items S, el T) int
- func CountBy[S ~[]T, T any](items S, f func(el T) bool) int
- func Cycle[S ~[]T, T any](items S) chan T
- func Dedup[S ~[]T, T comparable](items S) S
- func DedupBy[S ~[]T, T any, G comparable](items S, f func(el T) G) S
- func Delete[S ~[]T, T comparable](items S, element T) S
- func DeleteAll[S ~[]T, T comparable](items S, element T) S
- func DeleteAt[S ~[]T, T any](items S, indices ...int) (S, error)
- func Difference[S1 ~[]T, S2 ~[]T, T comparable](target S1, exclude S2) []T
- func DropEvery[S ~[]T, T any](items S, nth int, from int) (S, error)
- func DropWhile[S ~[]T, T any](items S, f func(el T) bool) S
- func DropZero[S ~[]T, T comparable](items S) S
- func Each[S ~[]T, T any](items S, f func(el T))
- func EachAsync[S ~[]T, T any](items S, workers int, f func(el T))
- func EachErr[S ~[]E, E any](items S, f func(el E) error) error
- func EndsWith[S ~[]T, T comparable](items S, suffix S) bool
- func Equal[S1 ~[]T, S2 ~[]T, T comparable](items S1, other S2) bool
- func EqualBy[S1 ~[]E1, S2 ~[]E2, E1, E2 any](s1 S1, s2 S2, eq func(E1, E2) bool) bool
- func Filter[S ~[]T, T any](items S, f func(el T) bool) S
- func FilterAsync[S ~[]T, T any](items S, workers int, f func(el T) bool) S
- func Find[S ~[]T, T any](items S, f func(el T) bool) (T, error)
- func FindIndex[S ~[]T, T any](items S, f func(el T) bool) int
- func GroupBy[S ~[]T, T any, K comparable](items S, f func(el T) K) map[K]S
- func Grow[S ~[]T, T any](items S, n int) S
- func Index[S ~[]T, T comparable](items S, item T) (int, error)
- func IndexBy[S []T, T comparable](items S, f func(T) bool) (int, error)
- func InsertAt[S ~[]T, T any](items S, index int, item T) (S, error)
- func Intersect[S1 ~[]T, S2 ~[]T, T comparable](items1 S1, items2 S2) []T
- func Intersect2[S1 ~[]T, S2 ~[]T, T comparable](items1 S1, items2 S2) []T
- func Intersperse[S ~[]T, T any](items S, el T) S
- func Join[S ~[]T, T any](items S, sep string) string
- func Last[S ~[]T, T any](items S) (T, error)
- func Map[S ~[]T, T any, G any](items S, f func(el T) G) []G
- func MapAsync[S ~[]T, T any, G any](items S, workers int, f func(el T) G) []G
- func MapFilter[S ~[]T, T any, G any](items S, f func(el T) (G, bool)) []G
- func Max[S ~[]T, T constraints.Ordered](items S) (T, error)
- func Min[S ~[]T, T constraints.Ordered](items S) (T, error)
- func Partition[S ~[]T, T any](items S, f func(el T) bool) (S, S)
- func Permutations[S ~[]T, T any](items S, size int) chan S
- func Prepend[S ~[]T, T any](target S, items ...T) S
- func Product[S ~[]T, T any](items S, repeat int) chan []T
- func Product2[T any](items ...[]T) chan []T
- func Reduce[S ~[]T, T any, G any](items S, acc G, f func(el T, acc G) G) G
- func ReduceAsync[S ~[]T, T any](items S, workers int, f func(left T, right T) T) T
- func ReduceWhile[S ~[]T, T any, G any](items S, acc G, f func(el T, acc G) (G, error)) (G, error)
- func Reject[S ~[]T, T any](items S, f func(el T) bool) S
- func Repeat[S ~[]T, T any](items S, n int) S
- func Replace[S ~[]T, T comparable, I constraints.Integer](items S, start, end I, item T) (S, error)
- func Reverse[S ~[]T, T any](items S) S
- func Same[S ~[]T, T comparable](items S) bool
- func Scan[S ~[]T, T any, G any](items S, acc G, f func(el T, acc G) G) []G
- func Shrink[S ~[]T, T any](items S) S
- func Shuffle[S ~[]T, T any](items S, seed int64)
- func Sort[S ~[]T, T constraints.Ordered](items S) S
- func SortBy[S ~[]T, T any, K constraints.Ordered](items S, f func(el T) K) S
- func Sorted[S ~[]T, T constraints.Ordered](items S) bool
- func SortedUnique[S ~[]T, T constraints.Ordered](items S) bool
- func Split[S ~[]T, T comparable](items S, sep T) []S
- func StartsWith[S ~[]T, T comparable](items S, prefix S) bool
- func Sum[S ~[]T, T constraints.Ordered](items S) T
- func TakeEvery[S ~[]T, T any](items S, nth int, from int) (S, error)
- func TakeRandom[S ~[]T, T any](items S, count int, seed int64) (S, error)
- func TakeWhile[S ~[]T, T any](items S, f func(el T) bool) S
- func ToChannel[S ~[]T, T any](items S) chan T
- func ToKeys[S ~[]K, K comparable, V any](items S, val V) map[K]V
- func ToMap[S ~[]V, V any](items S) map[int]V
- func ToMapGroupedBy[V any, T comparable](items []V, keyExtractor func(V) T) map[T][]V
- func Union[S ~[]T, T comparable](left S, right S) S
- func Uniq[S ~[]T, T comparable](items S) S
- func Unique[S ~[]T, T comparable](items S) bool
- func Window[S ~[]T, T any](items S, size int) ([]S, error)
- func Without[S ~[]T, T comparable](items S, elements ...T) S
- func Wrap[T any](item T) []T
- func Zip[S ~[]T, T any](items ...S) chan S
Examples ΒΆ
- All
- AllAsync
- Any
- AnyAsync
- Choice
- ChunkBy
- ChunkEvery
- Concat
- Contains
- Copy
- Count
- CountBy
- Cycle
- Dedup
- DedupBy
- Delete
- DeleteAll
- DeleteAt
- Difference
- DropEvery
- DropWhile
- DropZero
- Each
- EachAsync
- EachErr
- EndsWith
- Equal
- EqualBy
- Filter
- FilterAsync
- Find
- FindIndex
- GroupBy
- Grow
- Index
- IndexBy
- InsertAt
- Intersect
- Intersperse
- Join
- Last
- Map
- MapAsync
- MapFilter
- Max
- Min
- Partition
- Permutations
- Prepend
- Product
- Product2
- Reduce
- ReduceAsync
- ReduceWhile
- Reject
- Repeat
- Replace
- Reverse
- Same
- Scan
- Shrink
- Shuffle
- Sort
- SortBy
- Sorted
- SortedUnique
- Split
- StartsWith
- Sum
- TakeEvery
- TakeRandom
- TakeWhile
- ToChannel
- ToKeys
- ToMap
- Union
- Uniq
- Unique
- Window
- Without
- Wrap
- Zip
Constants ΒΆ
This section is empty.
Variables ΒΆ
var ErrEmpty = errors.New("container is empty")
ErrEmpty is an error for empty slice when it's expected to have elements
var ErrNegativeValue = errors.New("negative value passed")
ErrNegativeValue is an error for passed index <0
var ErrNonPositiveValue = errors.New("value must be positive")
ErrNonPositiveValue is an error for passed step <=0
var ErrNotFound = errors.New("given element is not found")
ErrNotFound is an error for case when given element is not found
var ErrOutOfRange = errors.New("index is bigger than container size")
ErrOutOfRange is an error that for index bigger than slice size
Functions ΒΆ
func All ΒΆ
All returns true if f returns true for all items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { even := func(item int) bool { return item%2 == 0 } result := slices.All([]int{2, 4, 6}, even) fmt.Println(result) result = slices.All([]int{2, 4, 5}, even) fmt.Println(result) }
Output: true false
func AllAsync ΒΆ
AllAsync returns true if f returns true for all elements in items.
This is an asynchronous function. It will spawn as many goroutines as you specify in the `workers` argument. Set it to zero to spawn a new goroutine for each item.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { even := func(item int) bool { return item%2 == 0 } result := slices.AllAsync([]int{2, 4, 6}, 0, even) fmt.Println(result) }
Output: true
func Any ΒΆ
Any returns true if f returns true for any item in items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { even := func(item int) bool { return item%2 == 0 } result := slices.Any([]int{1, 2, 3}, even) fmt.Println(result) result = slices.Any([]int{1, 3, 5}, even) fmt.Println(result) }
Output: true false
func AnyAsync ΒΆ
AnyAsync returns true if f returns true for any element in items.
This is an asynchronous function. It will spawn as many goroutines as you specify in the `workers` argument. Set it to zero to spawn a new goroutine for each item.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { even := func(item int) bool { return item%2 == 0 } result := slices.AnyAsync([]int{1, 2, 3}, 0, even) fmt.Println(result) }
Output: true
func Choice ΒΆ
Choice chooses a random element from the slice. If seed is zero, UNIX timestamp will be used.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { result, _ := slices.Choice([]int{3, 4, 5, 6}, 13) fmt.Println(result) }
Output: 3
func ChunkBy ΒΆ
func ChunkBy[S ~[]T, T comparable, G comparable](items S, f func(el T) G) []S
ChunkBy splits items on every element for which f returns a new value.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{1, 3, 4, 6, 8, 9} remainder := func(item int) int { return item % 2 } result := slices.ChunkBy(s, remainder) fmt.Println(result) }
Output: [[1 3] [4 6 8] [9]]
func ChunkEvery ΒΆ
ChunkEvery splits items into groups of length count.
If items can't be split evenly, the final chunk will contain the remaining elements.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{1, 1, 2, 3, 5, 8, 13} result, _ := slices.ChunkEvery(s, 3) fmt.Println(result) }
Output: [[1 1 2] [3 5 8] [13]]
func Concat ΒΆ
func Concat[S ~[]T, T any](slices ...S) S
Concat concatenates the given slices into a single slice.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s1 := []int{3, 4, 5} s2 := []int{6, 7, 8} result := slices.Concat(s1, s2) fmt.Println(result) }
Output: [3 4 5 6 7 8]
func Contains ΒΆ
func Contains[S ~[]T, T comparable](items S, el T) bool
Contains returns true if el is in items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{2, 4, 6, 8} result := slices.Contains(s, 4) fmt.Println(result) result = slices.Contains(s, 3) fmt.Println(result) }
Output: true false
func Copy ΒΆ
func Copy[S ~[]T, T any](items S) S
Copy creates a copy of the given slice.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { orig := []int{3, 4} copy := slices.Copy(orig) orig = append(orig, 5) copy = append(copy, 6) fmt.Println(orig) fmt.Println(copy) }
Output: [3 4 5] [3 4 6]
func Count ΒΆ
func Count[S ~[]T, T comparable](items S, el T) int
Count return the count of el occurrences in items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{1, 0, 1, 0, 0, 1, 1, 0, 1, 0} result := slices.Count(s, 1) fmt.Println(result) }
Output: 5
func CountBy ΒΆ
CountBy returns how many times f returns true.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{1, 2, 3, 4, 5, 6} even := func(item int) bool { return item%2 == 0 } result := slices.CountBy(s, even) fmt.Println(result) }
Output: 3
func Cycle ΒΆ
func Cycle[S ~[]T, T any](items S) chan T
Cycle is an infinite loop over items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/channels" "github.com/life4/genesis/slices" ) func main() { s := []int{1, 2, 3} c := slices.Cycle(s) c = channels.Take(c, 5) result := channels.ToSlice(c) fmt.Println(result) }
Output: [1 2 3 1 2]
func Dedup ΒΆ
func Dedup[S ~[]T, T comparable](items S) S
Dedup returns items without consecutive duplicated elements.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{1, 2, 2, 3, 3, 3, 2, 3, 1, 1} result := slices.Dedup(s) fmt.Println(result) }
Output: [1 2 3 2 3 1]
func DedupBy ΒΆ
func DedupBy[S ~[]T, T any, G comparable](items S, f func(el T) G) S
DedupBy returns a copy of items, but without consecutive elements for which f returns the same result.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{1, 2, -2, -3, 3, -3, 2, 3, 1, 1} abs := func(x int) int { if x < 0 { return -x } return x } result := slices.DedupBy(s, abs) fmt.Println(result) }
Output: [1 2 -3 2 3 1]
func Delete ΒΆ
func Delete[S ~[]T, T comparable](items S, element T) S
Delete deletes the first occurrence of the element from items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 3, 4, 5} result := slices.Delete(s, 4) fmt.Println(result) }
Output: [3 5 3 4 5]
func DeleteAll ΒΆ
func DeleteAll[S ~[]T, T comparable](items S, element T) S
DeleteAll deletes all occurrences of the element from items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 3, 4, 5} result := slices.DeleteAll(s, 3) fmt.Println(result) }
Output: [4 5 4 5]
func DeleteAt ΒΆ
DeleteAt returns items without the elements in indices.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 3, 4, 5} result, _ := slices.DeleteAt(s, 1, 3) fmt.Println(result) }
Output: [3 5 4 5]
func Difference ΒΆ added in v1.9.0
func Difference[S1 ~[]T, S2 ~[]T, T comparable](target S1, exclude S2) []T
Difference returns a slice containing elements from target that are not in exclude.
The items in the result slice appear in the same order as in the first given slice. Each item appears only once.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s1 := []int{3, 4, 4, 5, 6, 6, 7} s2 := []int{5, 5, 4, 8} result := slices.Difference(s1, s2) fmt.Println(result) }
Output: [3 6 7]
func DropEvery ΒΆ
DropEvery returns a slice with every nth element in items dropped, starting with the first element.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 6, 7, 8} result, _ := slices.DropEvery(s, 2, 0) fmt.Println(result) }
Output: [4 6 8]
func DropWhile ΒΆ
DropWhile drops elements from the start of items while f returns true.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{2, 4, 6, 7, 8, 9, 10} even := func(x int) bool { return x%2 == 0 } result := slices.DropWhile(s, even) fmt.Println(result) }
Output: [7 8 9 10]
func DropZero ΒΆ added in v1.9.0
func DropZero[S ~[]T, T comparable](items S) S
DropZero returns a slice with every default value removed.
For example, for a slice of pointers it will drop nils, for a slice of ints it will drop zero, and for a slice of strings it will drop empty strings.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 5, 0, 6, 0, 0} result := slices.DropZero(s) fmt.Println(result) }
Output: [4 5 6]
func Each ΒΆ
func Each[S ~[]T, T any](items S, f func(el T))
Each calls f for each item in items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 5, 6} slices.Each(s, func(x int) { fmt.Println(x * 2) }) }
Output: 8 10 12
func EachAsync ΒΆ
EachAsync calls f for each element in items.
This is an asynchronous function. It will spawn as many goroutines as you specify in the `workers` argument. Set it to zero to spawn a new goroutine for each item.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 5, 6} sum := 0 slices.EachAsync(s, 0, func(x int) { sum += x }) fmt.Println(sum) }
Output: 15
func EachErr ΒΆ
EachErr calls f for each element in items until f returns an error.
Example ΒΆ
package main import ( "errors" "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 5, 6, 7, 8} err := slices.EachErr(s, func(x int) error { if x == 6 { return errors.New("six found") } fmt.Println(x * 2) return nil }) fmt.Println(err) }
Output: 8 10 six found
func EndsWith ΒΆ
func EndsWith[S ~[]T, T comparable](items S, suffix S) bool
EndsWith returns true if items ends with the given suffix slice.
If suffix is empty, it returns true.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 6, 7, 8} result := slices.EndsWith(s, []int{7, 8}) fmt.Println(result) }
Output: true
func Equal ΒΆ
func Equal[S1 ~[]T, S2 ~[]T, T comparable](items S1, other S2) bool
Equal returns true if the slices are equal.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s1 := []int{3, 4, 5} s2 := []int{3, 4, 5, 6} result := slices.Equal(s1, s2) fmt.Println(result) }
Output: false
func EqualBy ΒΆ
EqualBy returns true if the cmp function returns true for all element pairs in the two slices.
If the slices are different lengths, false is returned.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s1 := []int{3, 4, -5} s2 := []int{3, -4, 5} absEq := func(a, b int) bool { if a < 0 { a = -a } if b < 0 { b = -b } return a == b } result := slices.EqualBy(s1, s2, absEq) fmt.Println(result) }
Output: true
func Filter ΒΆ
Filter returns a slice containing only items where f returns true.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 5, 6, 7, 8, 10, 12, 13} even := func(x int) bool { return x%2 == 0 } result := slices.Filter(s, even) fmt.Println(result) }
Output: [4 6 8 10 12]
func FilterAsync ΒΆ
FilterAsync returns a slice containing only items where f returns true
This is an asynchronous function. It will spawn as many goroutines as you specify in the `workers` argument. Set it to zero to spawn a new goroutine for each item.
The resulting items have the same order as in the input slice.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 5, 6, 7, 8, 10, 12, 13} even := func(x int) bool { return x%2 == 0 } result := slices.FilterAsync(s, 0, even) fmt.Println(result) }
Output: [4 6 8 10 12]
func Find ΒΆ
Find returns the first element for which f returns true.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{5, 7, 9, 4, 3, 6} even := func(x int) bool { return x%2 == 0 } result, _ := slices.Find(s, even) fmt.Println(result) }
Output: 4
func FindIndex ΒΆ
FindIndex returns the index of the first element for which f returns true. Returns -1 if no matching element is found.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { type UserId int index := slices.FindIndex( []UserId{1, 2, 3, 4, 5}, func(el UserId) bool { return el == 3 }, ) fmt.Println(index) }
Output: 2
func GroupBy ΒΆ
func GroupBy[S ~[]T, T any, K comparable](items S, f func(el T) K) map[K]S
GroupBy groups items by the value returned by f.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 13, 14, 15, 23, 33} even := func(x int) int { return x % 10 } result := slices.GroupBy(s, even) fmt.Println(result) }
Output: map[3:[3 13 23 33] 4:[4 14] 5:[5 15]]
func Grow ΒΆ
Grow increases the slice capacity to fit at least n more elements.
So, for len(slice)=8 and n=2, the result will have a capacity of at least 10.
The function can be used to reduce allocations when inserting more elements into an existing slice.
If the slice already has sufficient capacity, this slice is returned unmodified.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := make([]int, 1, 4) fmt.Printf("Before: len=%d, cap=%d\n", len(s), cap(s)) r := slices.Grow(s, 5) fmt.Printf("After: len=%d, cap=%d\n", len(r), cap(r)) }
Output: Before: len=1, cap=4 After: len=1, cap=8
func Index ΒΆ
func Index[S ~[]T, T comparable](items S, item T) (int, error)
Index returns the index of the first occurrence of item in items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} index, _ := slices.Index(s, 4) fmt.Println(index) }
Output: 1
func IndexBy ΒΆ
func IndexBy[S []T, T comparable](items S, f func(T) bool) (int, error)
IndexBy returns the first index in items for which f returns true.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{5, 7, 9, 8, 3, 6} even := func(x int) bool { return x%2 == 0 } result, _ := slices.IndexBy(s, even) fmt.Println(result) }
Output: 3
func InsertAt ΒΆ
InsertAt returns items with item inserted at the given index.
If index is beyond the length of items, ErrOutOfRange is returned. If index is negative, ErrNegativeValue is returned.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} result, _ := slices.InsertAt(s, 1, 6) fmt.Println(result) }
Output: [3 6 4 5]
func Intersect ΒΆ added in v1.9.0
func Intersect[S1 ~[]T, S2 ~[]T, T comparable](items1 S1, items2 S2) []T
Intersect returns items that appear in both slices.
The items in the result slice appear in the same order as in the first given slice. Each item appears only once.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s1 := []int{3, 4, 5, 5, 6, 6, 7} s2 := []int{6, 5, 5, 4, 8} result := slices.Intersect(s1, s2) fmt.Println(result) }
Output: [4 5 6]
func Intersect2 ΒΆ added in v1.8.0
func Intersect2[S1 ~[]T, S2 ~[]T, T comparable](items1 S1, items2 S2) []T
Intersect2 is an alias for Intersect
DEPRECATED. Use Intersect instead.
func Intersperse ΒΆ
func Intersperse[S ~[]T, T any](items S, el T) S
Intersperse inserts el between each element of items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} result := slices.Intersperse(s, 1) fmt.Println(result) }
Output: [3 1 4 1 5]
func Join ΒΆ
Join concatenates elements of items to create a single string.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} result := slices.Join(s, "; ") fmt.Println(result) }
Output: 3; 4; 5
func Last ΒΆ
Last returns the last element from items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} result, _ := slices.Last(s) fmt.Println(result) }
Output: 5
func Map ΒΆ
Map applies f to all elements in items and returns a slice of the results.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 8, 15, 16, 23, 42} double := func(el int) int { return el * 2 } doubled := slices.Map(s, double) fmt.Println(doubled) }
Output: [8 16 30 32 46 84]
func MapAsync ΒΆ
MapAsync applies f to all elements in items and returns a slice of the results.
This is an asynchronous function. It will spawn as many goroutines as you specify in the `workers` argument. Set it to zero to spawn a new goroutine for each item.
The result items have the same order as in the input slice.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { pages := slices.MapAsync( []string{"google.com", "go.dev", "golang.org"}, 0, func(url string) string { return fmt.Sprintf("<web page for %s>", url) }, ) fmt.Println(pages) }
Output: [<web page for google.com> <web page for go.dev> <web page for golang.org>]
func MapFilter ΒΆ added in v1.3.0
MapFilter applies f to all elements in items, and returns a filtered slice of the results.
f returns two values: the mapped value, and a boolean indicating whether the result should be included in the filtered slice.
Example ΒΆ
package main import ( "fmt" "strconv" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 8, 15, 16, 23, 42} isEven := func(t int) (string, bool) { if t%2 == 0 { s := strconv.Itoa(t) return s, true } else { return "", false } } doubled := slices.MapFilter(s, isEven) fmt.Println(doubled) }
Output: [4 8 16 42]
func Max ΒΆ
func Max[S ~[]T, T constraints.Ordered](items S) (T, error)
Max returns the maximal element in items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{7, 42, 13} max, _ := slices.Max(s) fmt.Println(max) }
Output: 42
func Min ΒΆ
func Min[S ~[]T, T constraints.Ordered](items S) (T, error)
Min returns the minimal element from items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{42, 7, 13} min, _ := slices.Min(s) fmt.Println(min) }
Output: 7
func Partition ΒΆ added in v1.9.0
Partition splits items into two slices based on if f returns true or false.
The first returned slice contains the items for which f returned true. The second returned slice contains the remainder. Order is preserved in both slices.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 5, 6, 7, 8, 8, 7} isEven := func(x int) bool { return x%2 == 0 } even, odd := slices.Partition(s, isEven) fmt.Println(even) fmt.Println(odd) }
Output: [4 6 8 8] [5 7 7]
func Permutations ΒΆ
Permutations returns successive size-length permutations of elements from items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{1, 2, 3} ch := slices.Permutations(s, 2) result := make([][]int, 0) for x := range ch { result = append(result, x) } fmt.Println(result) }
Output: [[1 2] [1 3] [2 1] [2 3] [3 1] [3 2]]
func Prepend ΒΆ added in v1.9.0
func Prepend[S ~[]T, T any](target S, items ...T) S
Prepend returns the target slice with the given items added at the beginning.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 5, 6} result := slices.Prepend(s, 2, 3) fmt.Println(result) }
Output: [2 3 4 5 6]
func Product ΒΆ
Product returns the cartesian product of items to itself, repeat times.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{1, 2, 3} ch := slices.Product(s, 2) result := make([][]int, 0) for x := range ch { result = append(result, x) } fmt.Println(result) }
Output: [[1 1] [1 2] [1 3] [2 1] [2 2] [2 3] [3 1] [3 2] [3 3]]
func Product2 ΒΆ
func Product2[T any](items ...[]T) chan []T
Product2 returns the cartesian product of elements in the given slices.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s1 := []int{1, 2} s2 := []int{3, 4} ch := slices.Product2(s1, s2) result := make([][]int, 0) for x := range ch { result = append(result, x) } fmt.Println(result) }
Output: [[1 3] [1 4] [2 3] [2 4]]
func Reduce ΒΆ
Reduce applies f to acc and each element in items, reducing the slice to a single value.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} sum := func(a, b int) int { fmt.Printf("Received %d and %d\n", a, b) return a + b } result := slices.Reduce(s, 0, sum) fmt.Printf("Result is %d\n", result) }
Output: Received 3 and 0 Received 4 and 3 Received 5 and 7 Result is 12
func ReduceAsync ΒΆ
ReduceAsync reduces items to a single value with f.
This is an asynchronous function. It will spawn as many goroutines as you specify in the `workers` argument. Set it to zero to spawn a new goroutine for each item.
The function is guaranteed to be called with neighbored items. However, it may be called out of order. The results are collected into a new slice which is reduced again, until only one item remains. You can think about it as a piramid. On each iteration, 2 elements ar taken and merged together until only one remains.
An example for sum:
1 2 3 4 5 3 7 5 10 5 15
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} sum := func(a, b int) int { return a + b } result := slices.ReduceAsync(s, 0, sum) fmt.Println(result) }
Output: 12
func ReduceWhile ΒΆ
ReduceWhile is like Reduce, but stops when f returns an error.
Example ΒΆ
package main import ( "errors" "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 6} sum := func(a, b int) (int, error) { fmt.Printf("Received %d and %d\n", a, b) if a == 6 { return b, errors.New("got six") } return a + b, nil } result, err := slices.ReduceWhile(s, 0, sum) fmt.Printf("Result is %d\n", result) fmt.Printf("Error is '%v'\n", err) }
Output: Received 3 and 0 Received 4 and 3 Received 5 and 7 Received 6 and 12 Result is 12 Error is 'got six'
func Reject ΒΆ
Reject returns a slice containing only items where f returns false. It is the opposite of Filter.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 5, 6, 7, 8, 10, 12, 13} odd := func(x int) bool { return x%2 == 1 } result := slices.Reject(s, odd) fmt.Println(result) }
Output: [4 6 8 10 12]
func Repeat ΒΆ
Repeat repeats items n times.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 5, 6} result := slices.Repeat(s, 3) fmt.Println(result) }
Output: [4 5 6 4 5 6 4 5 6]
func Replace ΒΆ added in v1.9.0
func Replace[S ~[]T, T comparable, I constraints.Integer](items S, start, end I, item T) (S, error)
Replace replaces elements in items from start to end with the given item.
The item with the end index is not replaced.
Result:
- If start or end are negative, ErrNegativeValue is returned.
- If start is greater or equal to end, ErrOutOfRange is returned.
- If start or end is bigger than the slice len, ErrOutOfRange is returned.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{4, 5, 6, 7} result, _ := slices.Replace(s, 1, 3, 9) fmt.Println(result) }
Output: [4 9 9 7]
func Reverse ΒΆ
func Reverse[S ~[]T, T any](items S) S
Reverse returns items in reversed order.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} result := slices.Reverse(s) fmt.Println(result) }
Output: [5 4 3]
func Same ΒΆ
func Same[S ~[]T, T comparable](items S) bool
Same returns true if all elements in items are the same.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 3, 3, 3} result := slices.Same(s) fmt.Println(result) }
Output: true
func Scan ΒΆ
Scan applies f to acc and each element in items, feeding the output of the last call into the next call, and returning a slice of the results.
Scan is like Reduce, but it returns a slice of the results instead of a single value.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} sum := func(a, b int) int { return a + b } result := slices.Scan(s, 0, sum) fmt.Println(result) }
Output: [3 7 12]
func Shrink ΒΆ
func Shrink[S ~[]T, T any](items S) S
Shrink removes unused capacity from the slice.
In other words, the returned slice has capacity equal to length.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := make([]int, 1, 4) fmt.Printf("Before: len=%d, cap=%d\n", len(s), cap(s)) r := slices.Shrink(s) fmt.Printf("After: len=%d, cap=%d\n", len(r), cap(r)) }
Output: Before: len=1, cap=4 After: len=1, cap=1
func Shuffle ΒΆ
Shuffle in random order the given elements.
This is an in-place operation, it modifies the passed slice.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 6, 7, 8} slices.Shuffle(s, 13) fmt.Println(s) }
Output: [7 8 5 3 6 4]
func Sort ΒΆ
func Sort[S ~[]T, T constraints.Ordered](items S) S
Sort returns a sorted copy of items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{7, 6, 8, 5, 3, 6, 4} result := slices.Sort(s) fmt.Println(result) }
Output: [3 4 5 6 6 7 8]
func SortBy ΒΆ added in v1.9.0
func SortBy[S ~[]T, T any, K constraints.Ordered](items S, f func(el T) K) S
SortBy sorts items using the values returned by f.
The function might be called more than once for the same element. It is expected to be fast and always produce the same result.
The sort is stable. If two elements have the same ordering key, they are not swapped.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{7, -6, 6, 8, 5, 3, 6, 4} abs := func(x int) int { if x < 0 { return -x } return x } result := slices.SortBy(s, abs) fmt.Println(result) }
Output: [3 4 5 -6 6 6 7 8]
func Sorted ΒΆ
func Sorted[S ~[]T, T constraints.Ordered](items S) bool
Sorted returns true if items is sorted.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 3, 4, 5, 5, 5, 6, 7, 7, 12, 15} result := slices.Sorted(s) fmt.Println(result) }
Output: true
func SortedUnique ΒΆ added in v1.9.0
func SortedUnique[S ~[]T, T constraints.Ordered](items S) bool
SortedUnique returns true if items is sorted and all elements are unique.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 6, 7, 8, 12, 16, 19} result := slices.SortedUnique(s) fmt.Println(result) }
Output: true
func Split ΒΆ
func Split[S ~[]T, T comparable](items S, sep T) []S
Split splits items by sep.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 1, 5, 1, 1, 6, 7, 1} result := slices.Split(s, 1) fmt.Println(result) }
Output: [[3 4] [5] [] [6 7] []]
func StartsWith ΒΆ
func StartsWith[S ~[]T, T comparable](items S, prefix S) bool
StartsWith returns true if items starts with prefix. If prefix is empty, StartsWith returns true.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 6, 7, 8} result := slices.StartsWith(s, []int{3, 4}) fmt.Println(result) }
Output: true
func Sum ΒΆ
func Sum[S ~[]T, T constraints.Ordered](items S) T
Sum return sum of all elements in items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} result := slices.Sum(s) fmt.Println(result) }
Output: 12
func TakeEvery ΒΆ
TakeEvery returns a slice containing every nth element in items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 6, 7, 8} result, _ := slices.TakeEvery(s, 2, 0) fmt.Println(result) }
Output: [3 5 7]
func TakeRandom ΒΆ
TakeRandom returns a slice of count random elements from items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 6, 7, 8} result, _ := slices.TakeRandom(s, 3, 13) fmt.Println(result) }
Output: [7 8 5]
func TakeWhile ΒΆ
TakeWhile takes elements from items while f returns true.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 5, 7, 8, 9, 10, 11} odd := func(x int) bool { return x%2 == 1 } result := slices.TakeWhile(s, odd) fmt.Println(result) }
Output: [3 5 7]
func ToChannel ΒΆ
func ToChannel[S ~[]T, T any](items S) chan T
ToChannel returns a channel with elements from items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} ch := slices.ToChannel(s) result := make([]int, 0) for x := range ch { result = append(result, x) } fmt.Println(result) }
Output: [3 4 5]
func ToKeys ΒΆ
func ToKeys[S ~[]K, K comparable, V any](items S, val V) map[K]V
ToKeys returns a map where the keys are the elements in items and all values are set to val.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} result := slices.ToKeys(s, 2) fmt.Println(result) }
Output: map[3:2 4:2 5:2]
func ToMap ΒΆ
ToMap converts the given slice into a map where the keys are indices and the values are elements from items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5} result := slices.ToMap(s) fmt.Println(result) }
Output: map[0:3 1:4 2:5]
func ToMapGroupedBy ΒΆ added in v1.2.0
func ToMapGroupedBy[V any, T comparable](items []V, keyExtractor func(V) T) map[T][]V
ToMapGroupedBy is an alias for GroupBy.
DEPRECATED. Use GroupBy instead.
func Union ΒΆ added in v1.9.0
func Union[S ~[]T, T comparable](left S, right S) S
Union returns a slice of unique values from both slices preserving their order.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s1 := []int{3, 4, 5, 5, 6, 6, 7} s2 := []int{6, 5, 5, 4, 8} result := slices.Union(s1, s2) fmt.Println(result) }
Output: [3 4 5 6 7 8]
func Uniq ΒΆ
func Uniq[S ~[]T, T comparable](items S) S
Uniq returns items with only the first occurrence of each unique element.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 3, 4, 5, 4, 3, 3} result := slices.Uniq(s) fmt.Println(result) }
Output: [3 4 5]
func Unique ΒΆ added in v1.8.0
func Unique[S ~[]T, T comparable](items S) bool
Unique returns true if each element in items appears only once.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 3} result := slices.Unique(s) fmt.Println(result) }
Output: false
func Window ΒΆ
Window makes a sliding window for items.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 6} result, _ := slices.Window(s, 2) fmt.Println(result) }
Output: [[3 4] [4 5] [5 6]]
func Without ΒΆ
func Without[S ~[]T, T comparable](items S, elements ...T) S
Without returns items without the given elements.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s := []int{3, 4, 5, 6, 3, 4, 5, 6, 7, 8} result := slices.Without(s, 4, 5) fmt.Println(result) }
Output: [3 6 3 6 7 8]
func Wrap ΒΆ
func Wrap[T any](item T) []T
Wrap wraps item in a slice of the same type.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { result := slices.Wrap(4) fmt.Println(result) }
Output: [4]
func Zip ΒΆ
func Zip[S ~[]T, T any](items ...S) chan S
Zip returns a chan of slices each containing elements from consecutive positions in each input slice. The first slice in the channel will contain items[0][0], items[1][0], ..., items[n-1][0]. The second slice in the channel will contain items[0][1], items[1][1], ..., items[n-1][1]. If the slices are of unequal length, the shortest slice length will be used.
Example ΒΆ
package main import ( "fmt" "github.com/life4/genesis/slices" ) func main() { s1 := []int{3, 4, 5} s2 := []int{6, 7, 8, 9} ch := slices.Zip(s1, s2) result := make([][]int, 0) for x := range ch { result = append(result, x) } fmt.Println(result) }
Output: [[3 6] [4 7] [5 8]]
Types ΒΆ
This section is empty.