Documentation ¶
Overview ¶
Package collections provides utilities for working with collections (slices and maps).
Things like Map, Fold, Filter, Reject, etc. are available as generic top-level functions. In almost every case, there is an `<func>Err` equivalent (MapErr, FoldErr, etc.), which allows for transform/predicate functions to return an error.
Finally, all of these methods return new objects rather than mutate that target.
Index ¶
- func All[T any](sl []T, pred func(T) bool) bool
- func AllErr[T any](sl []T, pred func(T) (bool, error)) (bool, error)
- func Filter[T any](sl []T, pred func(T) bool) []T
- func FilterErr[T any](sl []T, pred func(T) (bool, error)) ([]T, error)
- func Fold[T, U any](sl []T, initVal U, fn func(T, U) U) U
- func FoldErr[T, U any](sl []T, initVal U, fn func(T, U) (U, error)) (U, error)
- func Map[T, U any](sl []T, fn func(T) U) []U
- func MapEntries[M ~map[K]V, K, K2 comparable, V, V2 any](m M, fn func(K, V) (K2, V2)) map[K2]V2
- func MapErr[T, U any](sl []T, fn func(T) (U, error)) ([]U, error)
- func MapKeys[M ~map[K]V, K, K2 comparable, V any](m M, fn func(K) K2) map[K2]V
- func MapKeysErr[M ~map[K]V, K, K2 comparable, V any](m M, fn func(K) (K2, error)) (map[K2]V, error)
- func MapValues[M ~map[K]V, K comparable, V any, V2 any](m M, fn func(V) V2) map[K]V2
- func MapValuesErr[M ~map[K]V, K comparable, V any, V2 any](m M, fn func(V) (V2, error)) (map[K]V2, error)
- func None[T any](sl []T, pred func(T) bool) bool
- func NoneErr[T any](sl []T, pred func(T) (bool, error)) (bool, error)
- func Reject[T any](sl []T, pred func(T) bool) []T
- func RejectErr[T any](sl []T, pred func(T) (bool, error)) ([]T, error)
- func Sum[T constraints.Integer | constraints.Float](sl []T) T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AllErr ¶
AllErr determines whether or not the predicate is true for all members in sl. If pred returns an error, it is returned immediately and the condition is deemed to be false.
func Filter ¶
Filter returns a new slice of T containing only those elements for which pred returns true.
func FilterErr ¶
FilterErr returns a new slice of T containing only those elements for which pred returns true. If pred returns an error, it will return that error immediately and discontinue iteration.
func Fold ¶
func Fold[T, U any](sl []T, initVal U, fn func(T, U) U) U
Fold creates a single value of type U from the slice of T. It works by iterating over all elements in sl calling fn for each value along with the accumlator (starts with initVal).
Each call to fn returns the updated accumulator. At the end of the iteration, the final value is returned.
Example ¶
Count occurrences of words in a slice
package main import ( "fmt" "github.com/pseudomuto/pkg/collections" ) func main() { in := []string{"some", "words", "go", "here", "and", "here", "words"} counts := collections.Fold(in, make(map[string]int), func(word string, acc map[string]int) map[string]int { if _, ok := acc[word]; !ok { acc[word] = 0 } acc[word]++ return acc }) for _, w := range []string{"and", "go", "here", "some", "words"} { fmt.Printf("%s: %d\n", w, counts[w]) } }
Output: and: 1 go: 1 here: 2 some: 1 words: 2
func FoldErr ¶
FoldErr creates a single value of type U from the slice of T. It works by iterating over all elements in sl calling fn for each value along with the accumlator (starts with initVal). If fn returns an error, it will return the initVal value and the error immediately without continuing the fold.
Each call to fn returns the updated accumulator. At the end of the iteration, the final value is returned.
func Map ¶
func Map[T, U any](sl []T, fn func(T) U) []U
Map maps the given slice from type T to type U and returns a new slice.
Example ¶
Convert slice of int to slice of string
package main import ( "fmt" "github.com/pseudomuto/pkg/collections" ) func main() { strings := collections.Map([]int{1, 2, 3, 4, 5}, func(i int) string { return fmt.Sprintf("str-%d", i) }) for _, str := range strings { fmt.Println(str) } }
Output: str-1 str-2 str-3 str-4 str-5
func MapEntries ¶
func MapEntries[M ~map[K]V, K, K2 comparable, V, V2 any](m M, fn func(K, V) (K2, V2)) map[K2]V2
MapEntries creates a new map[K2]V2 from the supplied map[K]V by using the result of fn for each key/value in m.
func MapErr ¶
MapErr maps the given slice from type T to type U and returns a new slice. If fn returns an error, it will be returned immediately and the mapping operation will be stopped.
Example ¶
Convert slice of string to slice of int using strconv.
package main import ( "fmt" "strconv" "github.com/pseudomuto/pkg/collections" ) func main() { // all good, no errors here ints, _ := collections.MapErr([]string{"10", "20", "30"}, strconv.Atoi) for _, i := range ints { fmt.Printf("%d\n", i+1) } // can't parse "nope" into int _, err := collections.MapErr([]string{"nope"}, strconv.Atoi) fmt.Println(err) }
Output: 11 21 31 strconv.Atoi: parsing "nope": invalid syntax
func MapKeys ¶
func MapKeys[M ~map[K]V, K, K2 comparable, V any](m M, fn func(K) K2) map[K2]V
MapKeys creates a new map[K2]V from the supplied map[K]V by using the result of fn for each key in m.
func MapKeysErr ¶
func MapKeysErr[M ~map[K]V, K, K2 comparable, V any](m M, fn func(K) (K2, error)) (map[K2]V, error)
MapKeysErr creates a new map[K2]V from the supplied map[K]V by using the result of fn for each key in m. If fn returns an error, it will be returned immediately.
func MapValues ¶
func MapValues[M ~map[K]V, K comparable, V any, V2 any](m M, fn func(V) V2) map[K]V2
MapValues creates a new map[K]V2 from the supplied map[K]V by using the result of fn for each value in m.
func MapValuesErr ¶
func MapValuesErr[M ~map[K]V, K comparable, V any, V2 any](m M, fn func(V) (V2, error)) (map[K]V2, error)
MapValuesErr creates a new map[K]V2 from the supplied map[K]V by using the result of fn for each value in m. If fn returns an error, it is returned immediately.
func NoneErr ¶
NoneErr determines whether the predicate is false for all members of sl. If pred returns an error, the error is returned immediately and the result is deemed to be false.
func Reject ¶
Reject returns a new slice of T containing only those element for which pred returns false.
func RejectErr ¶
RejectErr returns a new slice of T containing only those element for which pred returns false. If pred returns an error, it will be returned immediately and iteration will be stopped.
func Sum ¶
func Sum[T constraints.Integer | constraints.Float](sl []T) T
Sum sums up all of the values in sl and returns the result.
Example ¶
Sum of up the the first 5 integers
package main import ( "fmt" "github.com/pseudomuto/pkg/collections" ) func main() { fmt.Printf("%d\n", collections.Sum([]int{1, 2, 3, 4, 5})) }
Output: 15
Types ¶
This section is empty.