-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move comparator functionality to a new file and introduce a…
… CmpFunc type to make things a little more fluent. (#58)
- Loading branch information
Showing
3 changed files
with
61 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package keepsorted | ||
|
||
import ( | ||
"cmp" | ||
) | ||
|
||
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 { | ||
return c | ||
} | ||
return next(a, b) | ||
} | ||
} | ||
|
||
// 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) | ||
return cmp(r1, r2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters