-
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 filterKeys for Map and IntMap #972
base: master
Are you sure you want to change the base?
Conversation
Please don't include your Stack files. While I won't require it, I would prefer to see validity tests and also property tests (with validity tests) added both for |
Hello. The stack files i committed accidentally in my last commit. I will try to fix the test again |
We have filter p m = filterWithKey (\_ x -> p x) m So why not filterKeys p m = filterWithKey (\k _ -> p k) m instead of duplicating the definition of |
I don't see the advantage of this over just using |
It's the equivalent of |
Sure, I know what it does, but why should we add it, when we can just use |
I can't speak for the original poster @emilypi but for my point of view is that haskell libraries have many convenience functions that potentially can be expressed in terms of extra functions. With |
@flip111 that's exactly it. |
Really? I don't think I've ever seen it anywhere, could you give some examples? |
Some examples:
There are many more examples, but I don't want to spend too much time on it. I don't see a valid argument against this other than "why not use |
This seems like something that:
|
I wanted this function a couple weeks ago. Of course I just used |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seem to be enough people who want this to overcome my general reluctance to add semi-unnecessary functions to this package. I want some documentation and testing tweaks for both versions, even though I only added line comments to one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We seem to be missing property tests for filterWithKey
and filter
as well as the new function. The one for filterWithKey
should look something like this:
prop_filterWithKey :: Fun (Int, Int) Bool -> IMap -> Property
prop_filterWithKey fun m =
valid m' .&&. toList m' === Prelude.filter (apply fun) (toList m)
where
m' = filterWithKey (apply2 fun) m
The others should be similar, with appropriate tweaks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we implement filterKeys
in terms of filterWithKey
, that'd be great, but if we end up with the duplicate definition we currently have, I'd also want some tests for fidelity between the two a la
prop_filterKeysFidelity :: Fun Int Bool -> IMap -> Property
prop_filterKeysFidelity p m = fwk === fk
where
fwk = filterWithKey (\k _ -> apply p k) m
fk = filterKeys (apply p) m
-- > filterKeys (> 4) == filterWithKey (\k _ -> k > 4) | ||
|
||
filterKeys :: (Key -> Bool) -> IntMap a -> IntMap a | ||
filterKeys predicate = filterWithKey (\k _ -> predicate k) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice :)
Looks fine to me, but it's up to @treeowl |
@treeowl could you take a look please? Is there anything i can do or something that needs to be done? |
@flip111 would you like to finish the work here? |
Sorry i didn't do this earlier, sometimes things seem like a mountain when it's actually not that hard. It went a bit out of focus, thanks for the reminder @meooow25 |
I guess now, sync with upstream master, rebase and adjust changelog? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, just a few more comments and note the test build failure.
I guess now, sync with upstream master, rebase and adjust changelog?
Sure, you can do that if you like.
-- | \(O(n)\). Filter all keys that satisfy some predicate. | ||
-- | ||
-- > filterKeys (> 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" | ||
-- > filterKeys (> 4) == filterWithKey (\k _ -> k > 4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe treeowl is asking for something like this instead
containers/containers/src/Data/Map/Internal.hs
Lines 1516 to 1518 in b24068b
-- @ | |
-- take n = 'fromDistinctAscList' . 'Prelude.take' n . 'toAscList' | |
-- @ |
Which in this case is simply the definition.
-- @
-- filterKeys p = 'filterWithKey' (\k _ -> p k)
-- @
Also please do the same for Map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had put that there as example for the documentation for the users. Shall i not just keep that? I'm not familiar with @
in a docblock, is it a test? I can add those tests and at the same time keep prop_filterKeys
from intmap-properties
and map-properties
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it's not a test, just plain documentation. It will look like this: https://hackage.haskell.org/package/containers-0.7/docs/Data-Map-Lazy.html#v:take
Anyway, you've added it below so please remove this line "> filterKeys (> 4) == filterWithKey (\k _ -> k > 4)
".
-- | \(O(n)\). Filter all keys that satisfy some predicate. | ||
-- | ||
-- > filterKeys (> 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" | ||
-- > filterKeys (> 4) == filterWithKey (\k _ -> k > 4) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it's not a test, just plain documentation. It will look like this: https://hackage.haskell.org/package/containers-0.7/docs/Data-Map-Lazy.html#v:take
Anyway, you've added it below so please remove this line "> filterKeys (> 4) == filterWithKey (\k _ -> k > 4)
".
#866