Documentation ¶
Index ¶
- func Contains[T comparable](arr []T, lookup T) bool
- func Dedupe[T comparable](arr []T) []T
- func Equal[T comparable](a []T, b []T) bool
- func Filter[T any](arr []T, predicate func(T) bool) []T
- func FlatMap[T any, N any](arr []T, f func(T) []N) []N
- func GetOr[K comparable, V any](dict map[K]V, key K, fallback V) V
- func Group[T any, N comparable](arr []T, f func(T) N) map[N][]T
- func Intersect[T comparable](arr1 []T, arr2 []T) []T
- func Last[T any](arr []T) (T, bool)
- func Map[T any, N any](arr []T, f func(T) N) []N
- func Reduce[T any, N any](arr []T, initial N, f func(N, T) N) N
- func Sum[T Num](arr []T) T
- type EmptyArrayError
- type Num
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](arr []T, lookup T) bool
Checks if lookup exists in arr
Example ¶
package main import ( "fmt" "github.com/birwin93/fn" ) func main() { fmt.Println(fn.Contains([]int{1, 2, 3}, 1)) }
Output: true
func Dedupe ¶
func Dedupe[T comparable](arr []T) []T
Removes duplicates from array
arr := []int{1, 2, 3, 3} arr = fn.Dedupe(arr) // outputs [1, 2, 3]
Example ¶
package main import ( "fmt" "github.com/birwin93/fn" ) func main() { arr := []int{1, 2, 3, 4, 1, 3} fmt.Println(fn.Dedupe(arr)) }
Output: [1 2 3 4]
func Equal ¶
func Equal[T comparable](a []T, b []T) bool
Checks if both slices have the same elements in the same order
Example ¶
package main import ( "fmt" "github.com/birwin93/fn" ) func main() { a := []int{1, 2, 3} b := []int{1, 2, 3} fmt.Println(fn.Equal(a, b)) }
Output: true
func Filter ¶
Removes items from array that does not pass predicate
Example ¶
package main import ( "fmt" "github.com/birwin93/fn" ) func main() { arr := []int{1, 2, 3} filteredArr := fn.Filter(arr, func(i int) bool { return i > 1 }) fmt.Println(filteredArr) }
Output: [2 3]
func FlatMap ¶
Maps items to an array of another type and then flattens into one array
Example ¶
package main import ( "fmt" "strconv" "github.com/birwin93/fn" ) func main() { arr := []int{1, 2, 3} mappedArr := fn.FlatMap(arr, func(i int) []string { return []string{strconv.Itoa(i), strconv.Itoa(i * 2)} }) fmt.Println(mappedArr) }
Output: [1 2 2 4 3 6]
func GetOr ¶
func GetOr[K comparable, V any](dict map[K]V, key K, fallback V) V
Gets the value for the key in the map, or returns provided fallback
Example ¶
package main import ( "fmt" "github.com/birwin93/fn" ) func main() { dict := map[int]string{ 1: "1", 2: "2", } fmt.Println(fn.GetOr(dict, 3, "3")) }
Output: 3
func Group ¶
func Group[T any, N comparable](arr []T, f func(T) N) map[N][]T
Example ¶
package main import ( "fmt" "github.com/birwin93/fn" ) func main() { type member struct { name string age int } billy := member{name: "Billy", age: 1} bob := member{name: "Bob", age: 1} chris := member{name: "Chris", age: 2} members := []member{billy, bob, chris} groupedMembers := fn.Group(members, func(m member) string { return string(m.name[0]) }) fmt.Println(groupedMembers) }
Output: map[B:[{Billy 1} {Bob 1}] C:[{Chris 2}]]
func Intersect ¶
func Intersect[T comparable](arr1 []T, arr2 []T) []T
Returns shared items between two arrays
Types ¶
type EmptyArrayError ¶
type EmptyArrayError struct{}
Error returned when an operator requires a non empty array
func (*EmptyArrayError) Error ¶
func (e *EmptyArrayError) Error() string
EmptyArrayError conformance to error
type Num ¶
type Num interface { constraints.Integer | constraints.Float | constraints.Complex }
Click to show internal directories.
Click to hide internal directories.