Skip to content

Commit 1c8ebc2

Browse files
committed
Changes after review
1 parent 5e14d30 commit 1c8ebc2

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

containers-tests/tests/intmap-strictness.hs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ pInsertLookupWithKeyValueStrict f k v m
8787
not (isBottom $ M.insertLookupWithKey (const3 1) k bottom m)
8888
| otherwise = isBottom $ M.insertLookupWithKey (apply3 f) k bottom m
8989

90+
pFilterWithKey :: Fun (Int, Int) Bool -> IMap -> Property
91+
pFilterWithKey fun m =
92+
valid m' .&&. toList m' === Prelude.filter (apply fun) (toList m)
93+
where
94+
m' = filterWithKey (apply2 fun) m
95+
96+
-- pFilterKeys :: Fun (Int, Int) Bool -> IMap -> Property
97+
-- pFilterKeys fun m =
98+
-- valid m' .&&. toList m' === Prelude.filter (apply fun) (toList m)
99+
-- where
100+
-- m' = filterKeys (apply2 fun) m
101+
102+
-- pFilter :: Fun (Int, Int) Bool -> IMap -> Property
103+
-- pFilter fun m =
104+
-- valid m' .&&. toList m' === Prelude.filter (apply fun) (toList m)
105+
-- where
106+
-- m' = filter (apply2 fun) m
107+
90108
------------------------------------------------------------------------
91109
-- test a corner case of fromAscList
92110
--
@@ -199,6 +217,7 @@ tests =
199217
pInsertLookupWithKeyValueStrict
200218
, testProperty "fromAscList is somewhat value-lazy" pFromAscListLazy
201219
, testProperty "fromAscList is somewhat value-strict" pFromAscListStrict
220+
, testProperty "filterWithKey" pFilterWithKey
202221
#if __GLASGOW_HASKELL__ >= 806
203222
, testProperty "strict foldr'" pStrictFoldr'
204223
, testProperty "strict foldl'" pStrictFoldl'

containers/src/Data/IntMap/Internal.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2582,13 +2582,10 @@ filter p m
25822582
-- | \(O(n)\). Filter all keys that satisfy some predicate.
25832583
--
25842584
-- > filterKeys (> 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
2585+
-- > filterKeys (> 4) == filterWithKey (\k _ -> k > 4)
25852586

25862587
filterKeys :: (Key -> Bool) -> IntMap a -> IntMap a
2587-
filterKeys predicate = go
2588-
where
2589-
go Nil = Nil
2590-
go t@(Tip k _) = if predicate k then t else Nil
2591-
go (Bin p m l r) = bin p m (go l) (go r)
2588+
filterKeys predicate = filterWithKey (\k _ -> predicate k)
25922589

25932590
-- | \(O(n)\). Filter all keys\/values that satisfy some predicate.
25942591
--

0 commit comments

Comments
 (0)