Documentation ¶
Overview ¶
Package funcy provides a collection of functional utilities using generics.
Index ¶
- Variables
- func Any[E any](s []E, f evaluator[E]) bool
- func Contains[E comparable](s []E, v E) bool
- func Dedupe[E comparable](s []E) []E
- func Filter[E any](s []E, f retainer[E]) []E
- func Find[E any](s []E, f finder[E]) (E, error)
- func Map[E any](s []E, f mapper[E]) []E
- func Quicksort[E constraints.Ordered](arr []E) []E
- func Reduce[E any](s []E, init E, f reducer[E]) E
- func ReduceRight[E any](s []E, init E, f reducer[E]) E
- func Reverse[E any](s []E) []E
- func Sort[E constraints.Ordered](s []E) []E
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("not found")
Functions ¶
func Any ¶
Any applies the function it's passed to each element in the slice and returns true for the first element for which the function returns true. Otherwise, it returns false.
Example ¶
list := []int{3, 1, 2} fmt.Println(Any(list, func(item int) bool { return item%3 == 0 }))
Output: true
func Contains ¶
func Contains[E comparable](s []E, v E) bool
Contains returns true if the slice it's passed contains the element it's passed.
Example ¶
list := []string{"foo", "bar", "baz"} fmt.Println(Contains(list, "foo"))
Output: true
func Dedupe ¶
func Dedupe[E comparable](s []E) []E
Dedupe returns the slice it's passed with duplicate elements removed.
Example ¶
list := []string{"foo", "foo", "bar"} fmt.Println(Dedupe(list))
Output: [foo bar]
func Filter ¶
func Filter[E any](s []E, f retainer[E]) []E
Filter applies the function it's passed to each element in the slice it's passed to filter out unwanted elements and return the resulting, filtered slice.
Example ¶
list := []int{3, 1, 2} fmt.Println(Filter(list, func(item int) bool { return item == 3 })) //
Output: [3]
func Find ¶
Find applies the function it's passed to each element in the slice and returns the first element for which the function returns true.
Example ¶
list := []int{3, 1, 2} fmt.Println(Find(list, func(item int) bool { return item%3 == 0 }))
Output: 3 <nil>
func Map ¶
func Map[E any](s []E, f mapper[E]) []E
Map applies the function it's passed to each element in the slice it's passed and returns the resulting slice.
Example ¶
list := []int{3, 1, 2} fmt.Println(Map(list, func(item int) int { return item + 2 }))
Output: [5 3 4]
func Quicksort ¶
func Quicksort[E constraints.Ordered](arr []E) []E
Quicksort implements the quicksort algorithm to sort the slice it's passed.
func Reduce ¶
func Reduce[E any](s []E, init E, f reducer[E]) E
Reduce executes the function it's passed on each element of the slice it's passed, in order and beginning with the specified initial value, passing in the return value from the calculation on the preceding element. It returns the final result, after operating on all elements in the slice.
Example ¶
list := []int{3, 1, 2} fmt.Println(Reduce(list, 0, func(cur, val int) int { return cur + val }))
Output: 6
func ReduceRight ¶
func ReduceRight[E any](s []E, init E, f reducer[E]) E
ReduceRight executes the function it's passed on each element of the slice it's passed, in reverse order and beginning with the specified initial value, passing in the return value from the calculation on the preceding element. It returns the final result, after operating on all elements in the slice.
Example ¶
list := []int{2, 5, 10} fmt.Println(ReduceRight(list, 20, func(cur, val int) int { return cur - val }))
Output: 3
func Reverse ¶
func Reverse[E any](s []E) []E
Reverse returns the slice it's passed in reverse order.
Example ¶
list := []string{"foo", "bar", "baz"} fmt.Println(Reverse(list))
Output: [baz bar foo]
func Sort ¶
func Sort[E constraints.Ordered](s []E) []E
Sort returns the slice it's passed with all elements in ascending sorted order. It uses Quicksort.
Example ¶
list := []int{3, 1, 2} fmt.Println(Sort(list))
Output: [1 2 3]
Types ¶
This section is empty.