diff --git a/typed/functools/all.go b/typed/functools/all.go index 26bd2dd..a22441e 100644 --- a/typed/functools/all.go +++ b/typed/functools/all.go @@ -1,5 +1,6 @@ package functools +// All checks whether all elements in the given array satisfy the specified condition. func All[T any](condition func(T) bool, entry []T) bool { for _, item := range entry { if !condition(item) { diff --git a/typed/functools/any.go b/typed/functools/any.go index f3038f4..af98667 100644 --- a/typed/functools/any.go +++ b/typed/functools/any.go @@ -1,5 +1,6 @@ package functools +// Any checks if any element in the given slice satisfies the condition. func Any[T any](condition func(T) bool, entry []T) bool { for _, item := range entry { if condition(item) { diff --git a/typed/functools/compare.go b/typed/functools/compare.go index 052dde3..17a5be3 100644 --- a/typed/functools/compare.go +++ b/typed/functools/compare.go @@ -2,6 +2,7 @@ package functools import "slices" +// Max finds the maximum element in the given slice using the custom comparison function cmp. func Max[T any](cmp func(T, T) int, v ...T) T { if len(v) == 0 { return *new(T) @@ -9,6 +10,7 @@ func Max[T any](cmp func(T, T) int, v ...T) T { return slices.MaxFunc(v, cmp) } +// Min finds the minimum element in the given slice using the custom comparison function cmp. func Min[T any](cmp func(T, T) int, v ...T) T { if len(v) == 0 { return *new(T) @@ -16,6 +18,7 @@ func Min[T any](cmp func(T, T) int, v ...T) T { return slices.MinFunc(v, cmp) } +// Equals checks if all elements in the slice are equal based on the custom comparison function cmp. func Equals[T any](cmp func(T, T) int, v ...T) bool { l := len(v) if l <= 1 { @@ -29,18 +32,22 @@ func Equals[T any](cmp func(T, T) int, v ...T) bool { return true } +// Equal checks if two elements are equal based on the custom comparison function cmp. func Equal[T any](a, b T, cmp func(T, T) int) bool { return cmp(a, b) == 0 } +// Less checks if a is less than b based on the custom comparison function cmp. func Less[T any](a, b T, cmp func(T, T) int) bool { return cmp(a, b) < 0 } +// Greater checks if a is greater than b based on the custom comparison function cmp. func Greater[T any](a, b T, cmp func(T, T) int) bool { return cmp(a, b) > 0 } +// IsIncreasing checks if the elements in the slice are in increasing order based on the custom comparison function cmp. func IsIncreasing[T any](entry []T, cmp func(T, T) int, strict bool) bool { for i := 1; i < len(entry); i++ { if (strict && !Greater(entry[i], entry[i-1], cmp)) || Less(entry[i], entry[i-1], cmp) { @@ -50,6 +57,7 @@ func IsIncreasing[T any](entry []T, cmp func(T, T) int, strict bool) bool { return true } +// IsDecreasing checks if the elements in the slice are in decreasing order based on the custom comparison function cmp. func IsDecreasing[T any](entry []T, cmp func(T, T) int, strict bool) bool { for i := 1; i < len(entry); i++ { if (strict && !Less(entry[i], entry[i-1], cmp)) || Greater(entry[i], entry[i-1], cmp) { @@ -59,6 +67,7 @@ func IsDecreasing[T any](entry []T, cmp func(T, T) int, strict bool) bool { return true } +// IsMonotonic checks if the elements in the slice are either increasing or decreasing based on the custom comparison function cmp. func IsMonotonic[T any](entry []T, cmp func(T, T) int, strict bool) bool { return IsIncreasing(entry, cmp, strict) || IsDecreasing(entry, cmp, strict) } diff --git a/typed/functools/count.go b/typed/functools/count.go index 16cab55..ffcf102 100644 --- a/typed/functools/count.go +++ b/typed/functools/count.go @@ -4,6 +4,7 @@ import ( "github.com/biocrosscoder/flex/common" ) +// 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. func Count[T any](entry []T, value T) (count int) { for _, v := range entry { if common.Equal(v, value) { @@ -13,6 +14,7 @@ func Count[T any](entry []T, value T) (count int) { return } +// 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. func CountBy[T any](entry []T, condition func(T) bool) (count int) { for _, v := range entry { if condition(v) { diff --git a/typed/functools/filter.go b/typed/functools/filter.go index e58cb74..34c268b 100644 --- a/typed/functools/filter.go +++ b/typed/functools/filter.go @@ -1,5 +1,7 @@ package functools +// 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. func Filter[T any](condition func(T) bool, entry []T) []T { output := make([]T, 0) for _, v := range entry { diff --git a/typed/functools/map.go b/typed/functools/map.go index 2758476..974b05c 100644 --- a/typed/functools/map.go +++ b/typed/functools/map.go @@ -1,7 +1,9 @@ +// Package functools provides functional programming tools. package functools import "github.com/biocrosscoder/flex/common" +// Map applies the given handler function to each element in the input slice and returns a new slice of results. func Map[E, R any](handler func(E) R, entry []E) []R { output := make([]R, len(entry)) for i, item := range entry { @@ -10,6 +12,8 @@ func Map[E, R any](handler func(E) R, entry []E) []R { return output } +// 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. func Maps[T, U, R any](handler func(T, U) R, entry1 []T, entry2 []U) (output []R, err error) { length := len(entry1) if length != len(entry2) { diff --git a/typed/functools/reduce.go b/typed/functools/reduce.go index aec11ce..da88058 100644 --- a/typed/functools/reduce.go +++ b/typed/functools/reduce.go @@ -2,6 +2,7 @@ package functools import "github.com/biocrosscoder/flex/common" +// Reduce applies a binary function to the elements of an array to reduce them to a single value. func Reduce[T any](handler func(T, T) T, entry []T, initial ...T) (result T, err error) { entryLength := len(entry) if entryLength == 0 { diff --git a/typed/functools/sort.go b/typed/functools/sort.go index c4979a2..83427b6 100644 --- a/typed/functools/sort.go +++ b/typed/functools/sort.go @@ -2,6 +2,7 @@ package functools import "slices" +// cmpFunc creates a composite comparison function by combining multiple comparison functions. func cmpFunc[T any](cmps ...func(a, b T) int) func(a, b T) int { return func(a, b T) (v int) { for _, cmp := range cmps { @@ -14,6 +15,7 @@ func cmpFunc[T any](cmps ...func(a, b T) int) func(a, b T) int { } } +// Sort sorts the input slice using the provided comparison functions. func Sort[T any](entry []T, cmps ...func(a, b T) int) { if len(cmps) == 0 { return @@ -24,12 +26,14 @@ func Sort[T any](entry []T, cmps ...func(a, b T) int) { } } +// Sorted returns a new sorted slice without modifying the original input slice. func Sorted[T any](entry []T, cmps ...func(a, b T) int) []T { result := slices.Clone(entry) Sort(result, cmps...) return result } +// IsSorted checks if the input slice is sorted according to the provided comparison functions. func IsSorted[T any](entry []T, cmps ...func(a, b T) int) bool { return slices.IsSortedFunc(entry, cmpFunc(cmps...)) }