-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,24 @@ pInsertLookupWithKeyValueStrict f k v m | |
not (isBottom $ M.insertLookupWithKey (const3 1) k bottom m) | ||
| otherwise = isBottom $ M.insertLookupWithKey (apply3 f) k bottom m | ||
|
||
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 | ||
|
||
-- 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 | ||
Comment on lines
+96
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you mean to add these? Currently they are commented out. |
||
|
||
------------------------------------------------------------------------ | ||
-- test a corner case of fromAscList | ||
-- | ||
|
@@ -199,6 +217,7 @@ tests = | |
pInsertLookupWithKeyValueStrict | ||
, testProperty "fromAscList is somewhat value-lazy" pFromAscListLazy | ||
, testProperty "fromAscList is somewhat value-strict" pFromAscListStrict | ||
, testProperty "filterWithKey" pFilterWithKey | ||
#if __GLASGOW_HASKELL__ >= 806 | ||
, testProperty "strict foldr'" pStrictFoldr' | ||
, testProperty "strict foldl'" pStrictFoldl' | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -115,7 +115,7 @@ main = defaultMain $ testGroup "map-properties" | |
, testCase "fromDistinctAscList" test_fromDistinctAscList | ||
, testCase "fromDistinctDescList" test_fromDistinctDescList | ||
, testCase "filter" test_filter | ||
, testCase "filterWithKey" test_filteWithKey | ||
, testCase "filterWithKey" test_filterWithKey | ||
, testCase "partition" test_partition | ||
, testCase "partitionWithKey" test_partitionWithKey | ||
, testCase "mapMaybe" test_mapMaybe | ||
|
@@ -820,8 +820,11 @@ test_filter = do | |
filter (> "x") (fromList [(5,"a"), (3,"b")]) @?= empty | ||
filter (< "a") (fromList [(5,"a"), (3,"b")]) @?= empty | ||
|
||
test_filteWithKey :: Assertion | ||
test_filteWithKey = filterWithKey (\k _ -> k > 4) (fromList [(5,"a"), (3,"b")]) @?= singleton 5 "a" | ||
test_filterKeys :: Assertion | ||
test_filterKeys = filterKeys (> 4) (fromList [(5,"a"), (3,"b")]) @?= singleton 5 "a" | ||
Comment on lines
+823
to
+824
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
||
|
||
test_filterWithKey :: Assertion | ||
test_filterWithKey = filterWithKey (\k _ -> k > 4) (fromList [(5,"a"), (3,"b")]) @?= singleton 5 "a" | ||
|
||
test_partition :: Assertion | ||
test_partition = do | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,6 +223,7 @@ module Data.IntMap.Internal ( | |
|
||
-- * Filter | ||
, filter | ||
, filterKeys | ||
, filterWithKey | ||
, restrictKeys | ||
, withoutKeys | ||
|
@@ -2578,6 +2579,14 @@ filter :: (a -> Bool) -> IntMap a -> IntMap a | |
filter p m | ||
= filterWithKey (\_ x -> p x) m | ||
|
||
-- | \(O(n)\). Filter all keys that satisfy some predicate. | ||
-- | ||
-- > filterKeys (> 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" | ||
flip111 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
-- > 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 commentThe reason will be displayed to describe this comment to others. Learn more. nice :) |
||
|
||
-- | \(O(n)\). Filter all keys\/values that satisfy some predicate. | ||
-- | ||
-- > filterWithKey (\k _ -> k > 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,6 +288,7 @@ module Data.Map.Internal ( | |
|
||
-- * Filter | ||
, filter | ||
, filterKeys | ||
, filterWithKey | ||
|
||
, takeWhileAntitone | ||
|
@@ -2857,6 +2858,13 @@ filter :: (a -> Bool) -> Map k a -> Map k a | |
filter p m | ||
= filterWithKey (\_ x -> p x) m | ||
|
||
-- | \(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 commentThe reason will be displayed to describe this comment to others. Learn more. Please add a |
||
filterKeys :: (k -> Bool) -> Map k a -> Map k a | ||
filterKeys p m = filterWithKey (\k _ -> p k) m | ||
|
||
-- | \(O(n)\). Filter all keys\/values that satisfy the predicate. | ||
-- | ||
-- > filterWithKey (\k _ -> k > 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 move property tests to
intmap-properties.hs
.