Skip to content

Commit b1d79ed

Browse files
Add difference for Map
1 parent d8527d8 commit b1d79ed

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

src/Data/Map.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Data.Map
55

66
import Prelude
77

8-
import Data.Map.Internal (Map, alter, checkValid, delete, empty, filter, filterKeys, filterWithKey, findMax, findMin, foldSubmap, fromFoldable, fromFoldableWith, insert, isEmpty, isSubmap, lookup, lookupGE, lookupGT, lookupLE, lookupLT, member, pop, showTree, singleton, size, submap, toUnfoldable, toUnfoldableUnordered, union, unionWith, unions, update, values)
8+
import Data.Map.Internal (Map, alter, checkValid, delete, empty, filter, filterKeys, filterWithKey, findMax, findMin, foldSubmap, fromFoldable, fromFoldableWith, insert, isEmpty, isSubmap, lookup, lookupGE, lookupGT, lookupLE, lookupLT, member, pop, showTree, singleton, size, submap, toUnfoldable, toUnfoldableUnordered, union, unionWith, unions, difference, update, values)
99
import Data.Set (Set)
1010
import Unsafe.Coerce (unsafeCoerce)
1111

src/Data/Map/Internal.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Data.Map.Internal
3232
, union
3333
, unionWith
3434
, unions
35+
, difference
3536
, isSubmap
3637
, size
3738
, filterWithKey
@@ -612,6 +613,11 @@ union = unionWith const
612613
unions :: forall k v f. Ord k => Foldable f => f (Map k v) -> Map k v
613614
unions = foldl union empty
614615

616+
-- | Difference of two maps. Return elements of the first map where
617+
-- | the keys do not exist in the second map.
618+
difference :: forall k v. Ord k => Map k v -> Map k v -> Map k v
619+
difference m1 m2 = foldl (flip delete) m1 (keys m2)
620+
615621
-- | Test whether one map contains all of the keys and values contained in another map
616622
isSubmap :: forall k v. Ord k => Eq v => Map k v -> Map k v -> Boolean
617623
isSubmap m1 m2 = LL.all f $ (toUnfoldable m1 :: LL.List (Tuple k v))

test/Test/Data/Map.purs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Prelude
44

55
import Control.Alt ((<|>))
66
import Data.Array as A
7-
import Data.Foldable (foldl, for_, all)
7+
import Data.Foldable (foldl, for_, all, and)
88
import Data.FoldableWithIndex (foldrWithIndex)
99
import Data.Function (on)
1010
import Data.FunctorWithIndex (mapWithIndex)
@@ -215,6 +215,12 @@ mapTests = do
215215
Just v -> Just v == v2
216216
Nothing -> not (in1 || in2)
217217

218+
log "difference"
219+
quickCheck $ \(TestMap m1) (TestMap m2) ->
220+
let d = M.difference m1 m2 :: M.Map SmallKey Int
221+
in and (map (\k -> M.member k d) (A.fromFoldable $ M.keys d)) &&
222+
and (map (\k -> not $ M.member k d) (A.fromFoldable $ M.keys m2))
223+
218224
log "size"
219225
quickCheck $ \xs ->
220226
let xs' = nubBy ((==) `on` fst) xs

0 commit comments

Comments
 (0)