Skip to content

Commit

Permalink
Merge branch 'main' into jfaer/has_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffFaer committed Jan 7, 2025
2 parents 2add19b + 50eef69 commit 09f747f
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions keepsorted/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (

type cmpFunc[T any] func(T, T) int

// andThen returns a cmpFunc based on the current one that first checks the
// current cmpFunc and only checks the next cmpFunc if the current cmpFunc
// thinks the two elements are equal.
func (f cmpFunc[T]) andThen(next cmpFunc[T]) cmpFunc[T] {
return func(a, b T) int {
if c := f(a, b); c != 0 {
Expand All @@ -15,16 +18,21 @@ func (f cmpFunc[T]) andThen(next cmpFunc[T]) cmpFunc[T] {
}
}

// reverse returns a cmpFunc based on the current one that yields the opposite
// order.
func (f cmpFunc[T]) reversed() cmpFunc[T] {
return func(a, b T) int {
return f(b, a)
}
}

// comparing creates a cmpFunc that orders T based on one of its properties, R.
func comparing[T any, R cmp.Ordered](f func(T) R) cmpFunc[T] {
return comparingFunc(f, cmp.Compare)
}

// comparingFunc creates a cmpFunc that orders T based on one of its properties,
// R and R has its own explicit ordering.
func comparingFunc[T, R any](f func(T) R, cmp cmpFunc[R]) cmpFunc[T] {
return func(a, b T) int {
r1, r2 := f(a), f(b)
Expand Down

0 comments on commit 09f747f

Please sign in to comment.