-
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? |
-- pFilterKeys :: Fun (Int, Int) Bool -> IMap -> Property | ||
-- pFilterKeys fun m = | ||
-- valid m' .&&. toList m' === Prelude.filter (apply fun) (toList m) | ||
-- where | ||
-- m' = filterKeys (apply2 fun) m | ||
|
||
-- pFilter :: Fun (Int, Int) Bool -> IMap -> Property | ||
-- pFilter fun m = | ||
-- valid m' .&&. toList m' === Prelude.filter (apply fun) (toList m) | ||
-- where | ||
-- m' = filter (apply2 fun) m |
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.
Did you mean to add these? Currently they are commented out.
pFilterWithKey :: Fun (Int, Int) Bool -> IMap -> Property | ||
pFilterWithKey fun m = | ||
valid m' .&&. toList m' === Prelude.filter (apply fun) (toList m) | ||
where | ||
m' = filterWithKey (apply2 fun) m |
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.
Please move property tests to intmap-properties.hs
.
test_filterKeys :: Assertion | ||
test_filterKeys = filterKeys (> 4) (fromList [(5,"a"), (3,"b")]) @?= singleton 5 "a" |
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.
This is not used, it needs to be added to the list of tests above.
, testCase "filterKeys" test_filterKeys
Also, it would be good to have property tests (like for IntMap).
-- | \(O(n)\). Filter all keys that satisfy the predicate. | ||
-- | ||
-- > filterKeys (> 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" | ||
|
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.
Please add a @since FIXME
at the end of the docs. It will be updated by maintainers to the next release version.
Same for IntMap.
#866