Skip to content

Commit

Permalink
Changes after review 2
Browse files Browse the repository at this point in the history
  • Loading branch information
flip111 committed Nov 17, 2024
1 parent 1c8ebc2 commit 09ebe0f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
23 changes: 22 additions & 1 deletion containers-tests/tests/intmap-properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Data.Foldable (foldMap)
import Data.Function
import Data.Traversable (Traversable(traverse), foldMapDefault)
import Prelude hiding (lookup, null, map, filter, foldr, foldl, foldl')
import qualified Prelude (map)
import qualified Prelude (map, filter)

import Data.List (nub,sort)
import qualified Data.List as List
Expand Down Expand Up @@ -180,6 +180,9 @@ main = defaultMain $ testGroup "intmap-properties"
, testProperty "deleteMin" prop_deleteMinModel
, testProperty "deleteMax" prop_deleteMaxModel
, testProperty "filter" prop_filter
, testProperty "filterWithKey" prop_filterWithKey
, testProperty "filterKeys" prop_filterKeys
, testProperty "filterKeysFidelity" prop_filterKeysFidelity
, testProperty "partition" prop_partition
, testProperty "takeWhileAntitone" prop_takeWhileAntitone
, testProperty "dropWhileAntitone" prop_dropWhileAntitone
Expand Down Expand Up @@ -1470,6 +1473,24 @@ prop_filter p ys = length ys > 0 ==>
in valid m .&&.
m === fromList (List.filter (apply p . snd) xs)

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

prop_filterKeys :: Fun Int Bool -> IMap -> Property
prop_filterKeys fun m =
valid m' .&&. toList m' === Prelude.filter (apply fun . fst) (toList m)
where
m' = filterKeys (apply fun) m

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

prop_partition :: Fun Int Bool -> [(Int, Int)] -> Property
prop_partition p ys = length ys > 0 ==>
let xs = List.nubBy ((==) `on` fst) ys
Expand Down
19 changes: 0 additions & 19 deletions containers-tests/tests/intmap-strictness.hs
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,6 @@ 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

------------------------------------------------------------------------
-- test a corner case of fromAscList
--
Expand Down Expand Up @@ -217,7 +199,6 @@ 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'
Expand Down
1 change: 1 addition & 0 deletions containers-tests/tests/map-properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ main = defaultMain $ testGroup "map-properties"
, testCase "fromDistinctAscList" test_fromDistinctAscList
, testCase "fromDistinctDescList" test_fromDistinctDescList
, testCase "filter" test_filter
, testCase "filterKeys" test_filterKeys
, testCase "filterWithKey" test_filterWithKey
, testCase "partition" test_partition
, testCase "partitionWithKey" test_partitionWithKey
Expand Down
1 change: 1 addition & 0 deletions containers/src/Data/Map/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2861,6 +2861,7 @@ filter p m
-- | \(O(n)\). Filter all keys that satisfy the predicate.
--
-- > filterKeys (> 4) (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
-- @since FIXME

filterKeys :: (k -> Bool) -> Map k a -> Map k a
filterKeys p m = filterWithKey (\k _ -> p k) m
Expand Down

0 comments on commit 09ebe0f

Please sign in to comment.