functools

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package functools provides functional programming tools.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](condition func(T) bool, entry []T) bool

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

func Any[T any](condition func(T) bool, entry []T) bool

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

func Count[T any](entry []T, value T) (count int)

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

func CountBy[T any](entry []T, condition func(T) bool) (count int)

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

func Equal[T any](a, b T, cmp func(T, T) int) bool

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

func Equals[T any](cmp func(T, T) int, v ...T) bool

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

func Filter[T any](condition func(T) bool, entry []T) []T

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

func Greater[T any](a, b T, cmp func(T, T) int) bool

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

func IsDecreasing[T any](entry []T, cmp func(T, T) int, strict bool) bool

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, false))
fmt.Println(IsDecreasing(arr, sortedlist.AscendOrder, true))
Output:

true
false

func IsIncreasing

func IsIncreasing[T any](entry []T, cmp func(T, T) int, strict bool) bool

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, false))
fmt.Println(IsIncreasing(arr, sortedlist.AscendOrder, true))
Output:

true
false

func IsMonotonic

func IsMonotonic[T any](entry []T, cmp func(T, T) int, strict bool) bool

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, false))
fmt.Println(IsMonotonic(arr, sortedlist.AscendOrder, true))
fmt.Println(IsMonotonic(arr, sortedlist.DescendOrder, false))
Output:

true
false
true

func IsSorted

func IsSorted[T any](entry []T, cmps ...func(a, b T) int) bool

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))
Sort(arr, sortedlist.AscendOrder)
fmt.Println(IsSorted(arr, sortedlist.AscendOrder))
Output:

false
true

func Less

func Less[T any](a, b T, cmp func(T, T) int) bool

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

func Maps[T, U, R any](handler func(T, U) R, entry1 []T, entry2 []U) (output []R, err error)

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

func Max[T any](cmp func(T, T) int, v ...T) T

Max finds the maximum element in the given slice using the custom comparison function cmp.

Example
max := Max(sortedlist.DescendOrder, 5, 3, 9, 2, 7)
fmt.Println(max)
Output:

2

func Min

func Min[T any](cmp func(T, T) int, v ...T) T

Min finds the minimum element in the given slice using the custom comparison function cmp.

Example
min := Min(sortedlist.DescendOrder, 5, 3, 9, 2, 7)
fmt.Println(min)
Output:

9

func Reduce

func Reduce[T any](handler func(T, T) T, entry []T, initial ...T) (result T, err error)

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

func Sort[T any](entry []T, cmps ...func(a, b T) int)

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]]

func Sorted

func Sorted[T any](entry []T, cmps ...func(a, b T) int) []T

Sorted returns a new sorted slice without modifying the original input slice.

Example
arr := []int{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}
arr2 := Sorted(arr, sortedlist.AscendOrder)
fmt.Println(arr)
fmt.Println(arr2)
Output:

[3 1 4 1 5 9 2 6 5 3 5]
[1 1 2 3 3 4 5 5 5 6 9]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL