-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add comparison combinators for sorting to the standard library #1030
Comments
This is not necessarily directly relevant for Nickel, but I would like to point out the remarkable monoid instance for The gist is Eq <> o = o
o <> _ = o This correspond to lexicographic ordering. But it shines when combined with the monoid instance for For instance, here is a definition of the lexical ordering on pairs: (compare `on` fst) <> (compare `on` snd) (Now, the Anyway, there are some hidden gems of the sort in Haskell's library. Unfortunately, they're not really all in a conveniently accessible place, but you'll find some of them in Data.Ord. It may be worth looking there for inspiration. |
I opened a PR with implementations of the functions suggested here. The PR uses Also, I happily noticed that |
* Comparisons for number, string, array, and record See #1030 * simplify * address code review comments * update snapshots * remove unneeded contract annotation * fix doc string formatting * simplify std.array.compare * std.record.on -> std.record.apply_on
In a recent Nickel hour we would have found use for a corpus of comparison functions and combinators. For context, the
array.sort
function in the standard library takes as the first parameter a function of typea -> a -> [| `Lesser, `Equal, `Greater |]
, which is supposed to perform the comparison operation during the sort.Currently, users of
array.sort
are required to write this function by hand. We would instead like to add comparison functions to the standard library to compare types for which there is a reasonable default:num.compare
should use the builtin<
operatorstring.compare
should use lexicographic string comparisonAdditionally, we would like combinators to build more complex comparison functions, e.g.
array.lexicographically f
to lexicographically compare arrays with the preexisting comparison functionf
record.on field f
to compare records containing a certain field by applying a preexisting comparison functionf
to that field onlyThe text was updated successfully, but these errors were encountered: