diff --git a/functor.go b/functor.go new file mode 100644 index 0000000..b68026d --- /dev/null +++ b/functor.go @@ -0,0 +1,22 @@ +package stl4go + +// Less wraps the '<' operator for ordered types. +func Less[T Ordered](a, b T) bool { + return a < b +} + +// Greater wraps the '>' operator for ordered types. +func Greater[T Ordered](a, b T) bool { + return a > b +} + +// OrderedCompare provide default CompareFn for ordered types. +func OrderedCompare[T Ordered](a, b T) int { + if a < b { + return -1 + } + if a > b { + return 1 + } + return 0 +} diff --git a/generated_doc.md b/generated_doc.md index 1b4171c..e8d45e9 100755 --- a/generated_doc.md +++ b/generated_doc.md @@ -26,7 +26,6 @@ Package stl4go is a generic container and algorithm library for go. - [func DescSort[T Ordered](a []T)](<#func-descsort>) - [func DescStableSort[T Ordered](a []T)](<#func-descstablesort>) - [func Equal[T comparable](a, b []T) bool](<#func-equal>) -- [func Equals[T comparable](a, b T) bool](<#func-equals>) - [func Find[T comparable](a []T, x T) (index int, ok bool)](<#func-find>) - [func FindIf[T any](a []T, cond func(T) bool) (index int, ok bool)](<#func-findif>) - [func Generate[T any](a []T, gen func() T)](<#func-generate>) @@ -322,14 +321,6 @@ Equal returns whether two slices are equal. Return true if they are the same len Complexity: O\(min\(len\(a\), len\(b\)\)\). -## func [Equals]() - -```go -func Equals[T comparable](a, b T) bool -``` - -Equals wraps the '==' operator for comparable types. - ## func [Find]() ```go @@ -360,7 +351,7 @@ Generate fill each element of \`a\`\` with \`gen\(\)\`. Complexity: O\(len\(a\)\). -## func [Greater]() +## func [Greater]() ```go func Greater[T Ordered](a, b T) bool @@ -420,7 +411,7 @@ IsSorted returns whether the slice a is sorted in ascending order. Complexity: O\(len\(a\)\). -## func [Less]() +## func [Less]() ```go func Less[T Ordered](a, b T) bool @@ -540,7 +531,7 @@ NoneOf return true pred\(e\) returns true for none emements e in a. Complexity: O\(len\(a\)\). -## func [OrderedCompare]() +## func [OrderedCompare]() ```go func OrderedCompare[T Ordered](a, b T) int diff --git a/types.go b/types.go index 51cda6f..b9a4334 100644 --- a/types.go +++ b/types.go @@ -52,29 +52,3 @@ type CompareFn[T any] func(a, b T) int // HashFn is a function that returns the hash of 't'. type HashFn[T any] func(t T) uint64 - -// Equals wraps the '==' operator for comparable types. -func Equals[T comparable](a, b T) bool { - return a == b -} - -// Less wraps the '<' operator for ordered types. -func Less[T Ordered](a, b T) bool { - return a < b -} - -// Greater wraps the '>' operator for ordered types. -func Greater[T Ordered](a, b T) bool { - return a > b -} - -// OrderedCompare provide default CompareFn for ordered types. -func OrderedCompare[T Ordered](a, b T) int { - if a < b { - return -1 - } - if a > b { - return 1 - } - return 0 -}