Documentation
¶
Overview ¶
Package functools provides functional programming tools.
Index ¶
- func All[T any](condition func(T) bool, entry []T) bool
- func Any[T any](condition func(T) bool, entry []T) bool
- func Count[T any](entry []T, value T) (count int)
- func CountBy[T any](entry []T, condition func(T) bool) (count int)
- func Equal[T any](a, b T, cmp func(T, T) int) bool
- func Equals[T any](cmp func(T, T) int, v ...T) bool
- func Filter[T any](condition func(T) bool, entry []T) []T
- func Greater[T any](a, b T, cmp func(T, T) int) bool
- func IsDecreasing[T any](entry []T, cmp func(T, T) int, strict bool) bool
- func IsIncreasing[T any](entry []T, cmp func(T, T) int, strict bool) bool
- func IsMonotonic[T any](entry []T, cmp func(T, T) int, strict bool) bool
- func IsSorted[T any](entry []T, cmps ...func(a, b T) int) bool
- func Less[T any](a, b T, cmp func(T, T) int) bool
- func Map[E, R any](handler func(E) R, entry []E) []R
- func Maps[T, U, R any](handler func(T, U) R, entry1 []T, entry2 []U) (output []R, err error)
- func Max[T any](cmp func(T, T) int, v ...T) T
- func Min[T any](cmp func(T, T) int, v ...T) T
- func Reduce[T any](handler func(T, T) T, entry []T, initial ...T) (result T, err error)
- func Sort[T any](entry []T, cmps ...func(a, b T) int)
- func Sorted[T any](entry []T, cmps ...func(a, b T) int) []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶
All checks whether all elements in the given array satisfy the specified condition.
Example ¶
condition1 := func(x int) bool { return x > 0 } condition2 := func(x int) bool { return x > 5 } arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} fmt.Println(All(condition1, arr)) fmt.Println(All(condition2, arr))
Output: true false
func Any ¶
Any checks if any element in the given slice satisfies the condition.
Example ¶
condition1 := func(x int) bool { return x < 0 } condition2 := func(x int) bool { return x > 5 } arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} fmt.Println(Any(condition1, arr)) fmt.Println(Any(condition2, arr))
Output: false true
func Count ¶
Count function takes a slice of elements of type T and a value of type T, then returns the count of occurrences of the value in the slice.
Example ¶
fmt.Println(Count([]int{1, 2, 3, 1}, 1))
Output: 2
func CountBy ¶
CountBy function takes a slice of elements of type T and a condition function that takes an element of type T and returns a boolean, then returns the count of elements that satisfy the condition.
Example ¶
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} f := func(a int) bool { return a%2 == 0 } fmt.Println(CountBy(arr, f))
Output: 4
func Equal ¶
Equal checks if two elements are equal based on the custom comparison function cmp.
Example ¶
fmt.Println(Equal(3, 9, func(a, b int) int { return sortedlist.AscendOrder(a%3, b%3) }))
Output: true
func Equals ¶
Equals checks if all elements in the slice are equal based on the custom comparison function cmp.
Example ¶
eq := func(a, b int) int { return sortedlist.AscendOrder(a%3, b%3) } equal := Equals(eq, 3, 6, 9) fmt.Println(equal)
Output: true
func Filter ¶
Filter is a generic function that filters elements of type T based on the provided condition function. It takes a condition function and a slice of type T as input, and returns a new slice containing elements that satisfy the condition.
Example ¶
arr := []int{1, 2, 3, 4, 5} f := func(x int) bool { return x%2 != 0 } fmt.Println(Filter(f, arr))
Output: [1 3 5]
func Greater ¶
Greater checks if a is greater than b based on the custom comparison function cmp.
Example ¶
fmt.Println(Greater(5, 3, func(a, b int) int { return sortedlist.AscendOrder(a%2, b%2) }))
Output: false
func IsDecreasing ¶
IsDecreasing checks if the elements in the slice are in decreasing order based on the custom comparison function cmp.
Example ¶
arr := []int{5, 4, 3, 3, 2} fmt.Println(IsDecreasing(arr, sortedlist.AscendOrder[int], false)) fmt.Println(IsDecreasing(arr, sortedlist.AscendOrder[int], true))
Output: true false
func IsIncreasing ¶
IsIncreasing checks if the elements in the slice are in increasing order based on the custom comparison function cmp.
Example ¶
arr := []int{1, 2, 3, 3, 4} fmt.Println(IsIncreasing(arr, sortedlist.AscendOrder[int], false)) fmt.Println(IsIncreasing(arr, sortedlist.AscendOrder[int], true))
Output: true false
func IsMonotonic ¶
IsMonotonic checks if the elements in the slice are either increasing or decreasing based on the custom comparison function cmp.
Example ¶
arr := []int{1, 2, 2, 4, 5} fmt.Println(IsMonotonic(arr, sortedlist.AscendOrder[int], false)) fmt.Println(IsMonotonic(arr, sortedlist.AscendOrder[int], true)) fmt.Println(IsMonotonic(arr, sortedlist.DescendOrder[int], false))
Output: true false true
func IsSorted ¶
IsSorted checks if the input slice is sorted according to the provided comparison functions.
Example ¶
arr := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5} fmt.Println(IsSorted(arr, sortedlist.AscendOrder[int])) Sort(arr, sortedlist.AscendOrder[int]) fmt.Println(IsSorted(arr, sortedlist.AscendOrder[int]))
Output: false true
func Less ¶
Less checks if a is less than b based on the custom comparison function cmp.
Example ¶
fmt.Println(Less(2, 3, func(a, b int) int { return sortedlist.AscendOrder(a%3, b%3) }))
Output: false
func Map ¶
func Map[E, R any](handler func(E) R, entry []E) []R
Map applies the given handler function to each element in the input slice and returns a new slice of results.
Example ¶
str := "hello" arr := []int{1, 2, 3, 4, 5} f := func(x int) string { return string(str[x-1]) } fmt.Println(Map(f, arr))
Output: [h e l l o]
func Maps ¶
Maps applies the given handler function to corresponding elements in the two input slices and returns a new slice of results, as well as an error if the input slices have different lengths.
Example ¶
arr1 := []int{1, 2, 3} arr2 := [][]any{{"a", -1}, {"b", -2}, {"c", -3}} f := func(x int, y []any) []any { return []any{x, y[0], y[1]} } m, _ := Maps(f, arr1, arr2) fmt.Println(m)
Output: [[1 a -1] [2 b -2] [3 c -3]]
func Max ¶
Max finds the maximum element in the given slice using the custom comparison function cmp.
Example ¶
max := Max(sortedlist.DescendOrder[int], 5, 3, 9, 2, 7) fmt.Println(max)
Output: 2
func Min ¶
Min finds the minimum element in the given slice using the custom comparison function cmp.
Example ¶
min := Min(sortedlist.DescendOrder[int], 5, 3, 9, 2, 7) fmt.Println(min)
Output: 9
func Reduce ¶
Reduce applies a binary function to the elements of an array to reduce them to a single value.
Example ¶
arr := []int{1, 2, 3, 4, 5} // sum r, _ := Reduce(func(a, b int) int { return a + b }, arr) fmt.Println(r) // product r, _ = Reduce(func(a, b int) int { return a * b }, arr) fmt.Println(r)
Output: 15 120
func Sort ¶
Sort sorts the input slice using the provided comparison functions.
Example ¶
arr := [][]int{{3, 1, 4}, {1, 5, 9}, {2, 6, 5}, {3, 5, 5}} f1 := func(a, b []int) int { return sortedlist.AscendOrder(a[0], b[0]) } f2 := func(a, b []int) int { return sortedlist.AscendOrder(a[1], b[1]) } fmt.Println(arr) Sort(arr, f1, f2) fmt.Println(arr)
Output: [[3 1 4] [1 5 9] [2 6 5] [3 5 5]] [[1 5 9] [2 6 5] [3 1 4] [3 5 5]]
Types ¶
This section is empty.