-
Notifications
You must be signed in to change notification settings - Fork 178
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 'between' to 'Data.Map' and 'Data.Set' #803
base: master
Are you sure you want to change the base?
Conversation
Interesting. But this is only one of four kinds of "between". What about the others? |
Perhaps a more generalized version could be added, along with something like:
If this was added, I still think the simpler inclusive version ought to be added for ergonomics sake, but I might just be biased because that's the one I need 🙂 |
Yeah, I think you're biased. I imagine just as many people will need the others. |
For reference: GHC contains a related Note that it's implemented using |
why would implementation via
and the first I'm all for keeping interfaces small, but this could be a case where the current interface does not allow an efficient implementation. |
Thanks for pointing this out, @jwaldmann! I think I misunderstood the current implementation and also missed the bit where In light of this, I think the current implementation is actually pretty good. |
Strictness - I did not check, but yes, I thought that all balanced maps are strict in the structure. Note, my argument (too much work) also holds if only one of the components returned by Perhaps the suggested function (and implementation) can be generalized to take a "bitonic filter" predicate? (a function [EDIT] the |
I made a note of this at https://gitlab.haskell.org/ghc/ghc/-/issues/21217 |
See also #778 . It says that implementation via split is "at most a constant factor off", which I believe for time, but not for space. |
I wonder if we can generalize this idea some more, taking some inspiration from
Now
We might want to do something different with each part: preserve it, drop it, or transform it. |
Patch Description
This PR adds 2 new functions to containers:
Data.Map.between
, andData.Set.between
. These functions will consume a map/set (respectively), and will filter the map/set so that the keys/values lie between an upper and lower bound (inclusive). This operation comes up pretty often in certain applications, and feels generally useful.Notes
I'm by no means convinced that
between
is a good name, so if anyone feels that they have something more evocative please chime in!