-
Notifications
You must be signed in to change notification settings - Fork 92
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
Some utilities in std.array
: remove_duplicates
and filter_map
#1958
Comments
I think the signature for |
Quick passing thoughts on these (mostly from a performance pov):
|
By the way, does Nix do anything fancy for sorting? I have a vague idea of sorting having a really bad performance in early Nickel. You'd like to have it as a fast primitive (using, say, Rust's sorting algorithm), but preserving laziness plus the fact that you take a user-defined order makes it quite hard to do so.
While I agree, IMHO it's still a common and useful addition that could be reused across Nickel projects. In the end many |
It mostly falls-back to the STL sort algorithm. The main fancy thing is an optimisation to bypass the language layer if the ordering is the built-in one: https://github.com/tweag/nix/blob/84aa8e9f196e50549aa5cdef76f61f442ce40b82/src/libexpr/primops.cc#L3305-L3310 (note also that the built-in comparison operator of Nix is more permissive than the Nickel one as it accepts anything except records and functions) |
I see. Using the standard sort algorithm is easier in Nix because the interpreter just use mutually recursive functions to force values. In Nickel, as we have a stack machine (in practice a loop and a heap-allocated stack), its more annoying. But I suppose we could make it work by nesting call to |
Is your feature request related to a problem? Please describe.
I need to take values that fit in to nice high-level schema and re-organize them for ad-hoc "back ends" like Ansible inventories. Some more array processing functions would help in this transformation.
Describe the solution you'd like
std.array.remove_duplicates
. This could be written to take an equality function and likely have the typeforall a (a -> a -> Bool) -> Array a-> Array a
. It could also use built-in equality testing for a simpler definition likeforall a Array a -> Array a
.std.array.filter_map
. I'm not sure if the type of function is express-able -- i.e. it would need a type likeforall a b. (a -> b) -> Array a -> Array b
butb
could be aBool
(with the only acceptable value beingfalse
).Describe alternatives you've considered
I have workarounds that bloat the code a bit.
Additional context
Hopefully these are fairly understood functions -- they definitely exist in other functional languages. The type system of Nickel is a bit different than what I'm used to so perhaps the types would need adjustment.
The text was updated successfully, but these errors were encountered: