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:
📕 Functions returning a single item:
- Find(📚, 💬) (📕, 💥)
- Last(📚) (📕, 💥)
- Max(📚) (📕, 💥)
- Min(📚) (📕, 💥)
- Reduce(📚, 📕, 💬) 📕
- ReduceAsync(📚, 🧑🔧️, 💬) 📕
- ReduceWhile(📚, 📕, 💬) (📕, 💥)
- Sum(📚) 📕
🔢 Functions returning an int:
🖨 Functions that take a slice and return a slice:
📚 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 comparable, 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 elements in arr
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 slice.
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 element in arr
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 from slice
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 arr 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 the length of count each.
If items can't be split evenly, the final chunk will be 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 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 in arr.
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 count of el occurrences in arr.
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 slice
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 a given slice 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 comparable, G comparable](items S, f func(el T) G) S
DedupBy returns a given slice 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 the slice
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 the slice
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 the slice without elements on given positions
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 target elements that
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 of every nth element in the enumerable 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 arr 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 every element from arr
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 every element from slice
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 every element from arr 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 slice 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 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 any elements of the slices in the matching positions. If len of the slices is different, false is returned. It is similar to Any except it Zip's by two slices.
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 slice of T for which F returned 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 slice of element for which 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 is like Find, but return element index instead of element itself. Returns -1 if element not 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 element from array by 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 cap 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 the items slice with the item inserted at the given index.
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 arr
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 the slice 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 the slice
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 slice of T and returns slice of 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 slice of T and returns slice of 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 returns slice of `f` results for which `f` also returned true.
Example ¶
package main import ( "fmt" "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 := fmt.Sprintf("%d", 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 from arr
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 arr
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 the given function returned true, in the order as they appear in the input slice. The second returned slice contains the items for which the function returned false.
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 the slice.
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 cortesian product of elements
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
Product returns cortesian 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 every element in slice of `items` and returns `acc`.
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 slice 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 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 is like filter but it returns slice of T for which F returned false
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 slice 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 given arr 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 element in arr 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 is like Reduce, but returns slice of f results
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 sorted slice
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 the items using for exah element the value returned bu the given function.
The function might be called more than once for the same element. It 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 slice 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 the slice is sorted and all items 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 arr 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 slice starts with the given prefix slice. If prefix 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.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 from arr
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 slice of every nth elements
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 slice of count random elements from the slice
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 arr 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 channel with elements from the slice
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 converts the given slice into a map where items from the slice are the keys of the resulting map and all values are equal to the given `val` value.
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 keys are indices and values are items from the given slice.
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 arr with only first occurrences of every 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 checks if each item in the given slice 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 sliding window for the given slice
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 the slice with filtered out element
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 makes a single element slice out of the given value
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 chan of arrays of elements from given arrs on the same position.
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.