Skip to content

Commit

Permalink
[chore] Add annotations to typed functools
Browse files Browse the repository at this point in the history
  • Loading branch information
BioCrossCoder committed May 20, 2024
1 parent b8c1da0 commit f86f7e4
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions typed/functools/all.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
1 change: 1 addition & 0 deletions typed/functools/any.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions typed/functools/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@ 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)
}
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)
}
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 {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
}
2 changes: 2 additions & 0 deletions typed/functools/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions typed/functools/filter.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions typed/functools/map.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions typed/functools/reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions typed/functools/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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...))
}

0 comments on commit f86f7e4

Please sign in to comment.