Restrict and delete keys from a Map using intervals#49
Restrict and delete keys from a Map using intervals#49Bodigrim merged 2 commits intomsakai:masterfrom
Conversation
|
Could you possibly elaborate how new functions differ from |
I'm not sure I understand the question but I can try. This is about I don't use |
|
In fact, restrictInterval m i = Map.filterKeys (`Interval.member` i) mbut the implementation of this PR is much faster : the implementation in terms of To be able to get a logarithmic complexity, one needs pattern match on |
|
@Bodigrim I added better documentation and a comparison between the performance of I also added a benchmark. On my computer, |
Ah, thanks, now I see.
Not quite, here is an example of what > import Data.Interval as I
> import Data.IntervalMap.Lazy as IML
> IML.delete (8 I.<..< 18) $ IML.insert (0 I.<..< 10) 42 $ IML.insert (15 I.<..< 25) 24 mempty
fromList [(Finite 0 <..<= Finite 8,42),(Finite 18 <=..< Finite 25,24)]I think this is actually quite similar to what you are implementing here. Say, if there was a function |
This makes a lot of sense, and indeed you're right, However, in my use case, I need to work with If I understand well your suggestion, this would mean that, to get Moreover, the function |
|
Before I forget, there's also a question I haven't figured out yet : If you decide to add Pros for
|
|
Big picture is that I don't really feel like an entirely new module re-exporting entire |
That's fine by me. I did it this way because I intended to copy paste it to get the strict version for More concretely, we could move |
|
If I may add, the same functions can also be implemented for |
I don't quite see why you need a dedicated strict version: it seems the implementation only ever inserts values which were already present in the map, so if it was a lazy |
I don't think I understand.
Ideally, there would be a Except if we go and ask the |
They are the same data structure and |
2a8ac86 to
4ee3163
Compare
Oh now I understand, thank you for this explanation, I don't know why, I was convinced of the contrary, sorry. This is great ! I deleted the I still believe my argument makes sense for other |
15264e7 to
1a61254
Compare
Bodigrim
left a comment
There was a problem hiding this comment.
Thanks! I'll take another look over the weekend, just a few notes for now.
773b4f3 to
953b22d
Compare
|
I just implemented benchmarking and tests for the I'm now satisfied with the current state of this PR, except maybe for :
I'll try to think of better names over the week-end. |
1d3a4fa to
258dc2a
Compare
|
I just added another optimization : using |
src/Data/Interval.hs
Outdated
| -- * Operations on the keys of 'Data.Map' | ||
| , restrictKeysToInterval | ||
| , withoutKeysFromInterval | ||
| , splitInterval |
There was a problem hiding this comment.
As usual, naming is the hardest problem :)
I wonder if we really need splitInterval. The name is very unfortunate (one would expect it to split an interval, not split a map by keys in and out of an interval) and I cannot come up with a better one. Maybe in rare cases when this function is needed, one can satisfactory use restrict... and without...?..
How do you feel about restrictMapKeysToInterval instead of restrictKeysToInterval?
There was a problem hiding this comment.
As usual, naming is the hardest problem :)
Haha exactly !
I wonder if we really need splitInterval. The name is very unfortunate
What about splitMapAtBoundariesOf and splitSetAtBoundariesOf ?
Maybe in rare cases when this function is needed, one can satisfactory use restrict... and without...?...
Maybe but it would be slower than the current implementation if all three intervals are needed, and performance is the whole reason to be of these functions : otherwise why not just use filterKeys ?
How do you feel about
restrictMapKeysToIntervalinstead ofrestrictKeysToInterval?
It makes a lot of sense. withoutMapKeysFomInterval makes a bit less sense but is fine by me.
There was a problem hiding this comment.
Maybe but it would be slower than the current implementation if all three intervals are needed, and performance is the whole reason to be of these functions : otherwise why not just use
filterKeys?
Even restrictKeysToInterval + withoutKeysFromInterval will still be faster than filterKeys and with a better asymptotic behaviour.
Also containers themselves do not offer a combined restrictKeys and withoutKeys. Let's leave it out for now; we can keep it in the Git history so that it can be restored back if needed.
What about
splitMapAtBoundariesOfandsplitSetAtBoundariesOf?
Not quite, to be honest. Judging by the name I'd expect such function to split Map into three parts: before, inside and after the interval.
There was a problem hiding this comment.
Not quite, to be honest. Judging by the name I'd expect such function to split
Mapinto three parts: before, inside and after the interval.
Maybe I'm not understanding something this is precisely what the function does. The documentation I wrote says
-- | Split a 'Map' into three 'Map's, such that, respectively,
--
-- 1. the keys are less than the interval
-- 2. the keys are contained in the interval
-- 3. the keys are greater than the intervalThere was a problem hiding this comment.
If you say so, I will remove splitMapAtBoundariesOfand splitSetAtBoundariesOf.
[Edit] Seeing your comment below, I removed them.
8eaeb20 to
ee82b69
Compare
|
Thank you, awesome! |
Thanks a lot for your time ! |
Hi there !
This PR adds the possibility restrict or delete all the keys within an interval from a
Map. If you like the idea, I can add the same functions for strict maps.Cheers !
Alice