Documentation ¶
Overview ¶
Package godash implements slice/map related methods and is a wrapper of the github.com/samber/lo package.
This package focuses on only presenting a subset of functions from github.com/samber/lo, in order to make sure that the user takes advantage of Go's standard methods from the slice package etc. If any of the functions presented here gets included in the standard library, that function will be marked deprecated (the function will not get removed, but will point the user to the standard library).
Index ¶
- func Associate[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V
- func Chunk[T any](collection []T, size int) [][]T
- func EveryBy[T any](collection []T, predicate func(item T) bool) bool
- func Filter[V any](collection []V, predicate func(item V, index int) bool) []V
- func Find[T any](collection []T, predicate func(item T) bool) (T, bool)
- func FlatMap[T any, R any](collection []T, iteratee func(item T, index int) []R) []R
- func Flatten[T any](collection [][]T) []T
- func GroupBy[T any, U comparable](collection []T, iteratee func(item T) U) map[U][]T
- func KeyBy[K comparable, V any](collection []V, iteratee func(item V) K) map[K]V
- func Keys[K comparable, V any](in map[K]V) []K
- func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R
- func NoneBy[T any](collection []T, predicate func(item T) bool) bool
- func PartitionBy[T any, K comparable](collection []T, iteratee func(item T) K) [][]T
- func PickByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map
- func Reduce[T any, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R
- func SomeBy[T any](collection []T, predicate func(item T) bool) bool
- func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T
- func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R
- func Uniq[T comparable](collection []T) []T
- func UniqBy[T any, U comparable](collection []T, iteratee func(item T) U) []T
- func Values[K comparable, V any](in map[K]V) []V
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Associate ¶ added in v1.0.3
func Associate[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { type User struct { name string age int } users := []User{ {"foo", 20}, {"bar", 25}, {"baz", 40}, } fmt.Println(godash.Associate(users, func(u User) (string, int) { return u.name, u.age })) }
Output: map[bar:25 baz:40 foo:20]
func Chunk ¶
Chunk receives the collection and chunks it into a slice of slices each of the given size.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { integers := []int{0, 1, 1, 3, 2, 1, 1, 0} fmt.Println(godash.Chunk(integers, 3)) }
Output: [[0 1 1] [3 2 1] [1 0]]
func EveryBy ¶
EveryBy returns whether or not all the values within the collection meet the predicate.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { type User struct { name string age int } users := []User{ {"foo", 20}, {"bar", 25}, {"baz", 40}, } fmt.Println(godash.EveryBy(users, func(u User) bool { return u.age >= 20 })) }
Output: true
func Filter ¶
Filter iterates through the collection and returns a slice with only the values that match the predicate.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { integers := []int{-2, 0, 19, 5, 42} fmt.Println(godash.Filter(integers, func(x int, _ int) bool { return x%2 == 0 })) }
Output: [-2 0 42]
func Find ¶
Find returns the first value that meets the predicate, as well as whether or not a match was found.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { type User struct { name string age int } users := []User{ {"foo", 20}, {"bar", 25}, {"baz", 40}, } fmt.Println(godash.Find(users, func(u User) bool { return u.age > 24 })) }
Output: {bar 25} true
func FlatMap ¶ added in v1.0.4
FlatMap iterates through the collection and returns a slice with the values converted through the iteratee and flattened.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { integers := []int{0, 1, 2, 3} fmt.Println(godash.FlatMap(integers, func(x int, _ int) []int { return []int{2 * x, 2*x + 1} })) }
Output: [0 1 2 3 4 5 6 7]
func Flatten ¶
func Flatten[T any](collection [][]T) []T
Flatten flattens a slice of slices to a single slice.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { integers := [][]int{ {1, 2, 5, 10}, {9, 8, 1}, {0}, {42, 3}, } fmt.Println(godash.Flatten(integers)) }
Output: [1 2 5 10 9 8 1 0 42 3]
func GroupBy ¶
func GroupBy[T any, U comparable](collection []T, iteratee func(item T) U) map[U][]T
GroupBy groups the values of the collection using the iteratee, and returns it as a map.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { type User struct { id int name string } users := []User{ {1, "foo"}, {2, "foobar"}, {3, "foobar"}, {4, "foo"}, {5, "bar"}, } fmt.Println(godash.GroupBy(users, func(u User) string { return u.name })) }
Output: map[bar:[{5 bar}] foo:[{1 foo} {4 foo}] foobar:[{2 foobar} {3 foobar}]]
func KeyBy ¶
func KeyBy[K comparable, V any](collection []V, iteratee func(item V) K) map[K]V
KeyBy iterates through the collection and returns a map with the key generated via the iteratee.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { type User struct { id int name string } users := []User{ {1, "foo"}, {2, "foobar"}, {3, "foobar"}, {4, "foo"}, {5, "bar"}, } fmt.Println(godash.KeyBy(users, func(u User) int { return u.id })) }
Output: map[1:{1 foo} 2:{2 foobar} 3:{3 foobar} 4:{4 foo} 5:{5 bar}]
func Keys ¶
func Keys[K comparable, V any](in map[K]V) []K
Keys returns the keys of the map as a slice.
Example ¶
package main import ( "fmt" "slices" "github.com/hatena/godash" ) func main() { m := map[int]string{ 1: "foo", 2: "bar", 3: "baz", } keys := godash.Keys(m) slices.Sort(keys) fmt.Println(keys) }
Output: [1 2 3]
func Map ¶
Map iterates through the collection and returns a slice with the values converted though the iteratee.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { integers := []int{-2, 0, 19, 5, 42} fmt.Println(godash.Map(integers, func(x int, _ int) int { return x * 2 })) }
Output: [-4 0 38 10 84]
func NoneBy ¶
NoneBy returns whether or not all the values within the collection do not meet the predicate.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { type User struct { name string age int } users := []User{ {"foo", 20}, {"bar", 25}, {"baz", 40}, } fmt.Println(godash.NoneBy(users, func(u User) bool { return u.age > 40 })) }
Output: true
func PartitionBy ¶
func PartitionBy[T any, K comparable](collection []T, iteratee func(item T) K) [][]T
PartitionBy partitions the collection by the iteratee. This is similar to Chunk, but instead of a given size, a function can be applied. Use GroupBy if you want a map of slices.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { type User struct { id int name string } users := []User{ {1, "foo"}, {2, "foobar"}, {3, "foobar"}, {4, "foo"}, {5, "bar"}, } fmt.Println(godash.PartitionBy(users, func(u User) string { return u.name })) }
Output: [[{1 foo} {4 foo}] [{2 foobar} {3 foobar}] [{5 bar}]]
func PickByKeys ¶ added in v1.0.5
func PickByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map
PickByKeys returns same map type filtered by given keys.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { kv := map[string]int{"foo": 1, "bar": 2, "baz": 3} result := godash.PickByKeys(kv, []string{"foo", "baz"}) fmt.Printf("%v", result) }
Output: map[baz:3 foo:1]
func Reduce ¶
func Reduce[T any, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R
Reduce iterates through the collection and reduces it to one value, using the accumulator.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { integers := []int{-2, 0, 19, 5, 42} fmt.Println(godash.Reduce(integers, func(agg int, x int, _ int) int { return agg + x }, 0)) }
Output: 64
func SomeBy ¶ added in v1.0.4
SomeBy returns whether or not any of the values within the collection meet the predicate.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { type User struct { name string age int } users := []User{ {"foo", 20}, {"bar", 25}, {"baz", 40}, } fmt.Println(godash.SomeBy(users, func(u User) bool { return u.age >= 30 })) }
Output: true
func Sum ¶
func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T
Sum returns a sum of the values within the slice. The values of the slice need to be numbers.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { numbers := []float32{1, 5, 2, 3.2, 42} fmt.Println(godash.Sum(numbers)) }
Output: 53.2
func SumBy ¶
func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R
SumBy returns a sum of the values within the slice, added using the specified iteratee function. The values of the slice need to be numbers.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { type User struct { name string age int } users := []User{ {"foo", 20}, {"bar", 25}, {"baz", 40}, } fmt.Println(godash.SumBy(users, func(u User) int { return u.age })) }
Output: 85
func Uniq ¶
func Uniq[T comparable](collection []T) []T
Uniq returns a slice with only unique values.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { integers := []int{0, 1, 1, 3, 2, 1, 1, 0} fmt.Println(godash.Uniq(integers)) }
Output: [0 1 3 2]
func UniqBy ¶
func UniqBy[T any, U comparable](collection []T, iteratee func(item T) U) []T
UniqBy returns a slice with only unique values. The uniqueness is determined by the iteratee.
Example ¶
package main import ( "fmt" "github.com/hatena/godash" ) func main() { type User struct { id int name string } users := []User{ {1, "foo"}, {2, "foobar"}, {2, "foobar"}, {3, "foo"}, } fmt.Println(godash.UniqBy(users, func(u User) int { return u.id })) }
Output: [{1 foo} {2 foobar} {3 foo}]
func Values ¶
func Values[K comparable, V any](in map[K]V) []V
Values returns the values of the map as a slice.
Example ¶
package main import ( "fmt" "slices" "github.com/hatena/godash" ) func main() { m := map[int]string{ 1: "foo", 2: "bar", 3: "baz", } values := godash.Values(m) slices.Sort(values) fmt.Println(values) }
Output: [bar baz foo]
Types ¶
This section is empty.