Skip to content

Commit

Permalink
Merge pull request #5 from mschristiansen/map-difference
Browse files Browse the repository at this point in the history
Add difference for Map
  • Loading branch information
garyb authored Oct 4, 2018
2 parents 3dbd202 + a09b16e commit 5b21750
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Data.Map

import Prelude

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)
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)
import Data.Set (Set)
import Unsafe.Coerce (unsafeCoerce)

Expand Down
6 changes: 6 additions & 0 deletions src/Data/Map/Internal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Data.Map.Internal
, union
, unionWith
, unions
, difference
, isSubmap
, size
, filterWithKey
Expand Down Expand Up @@ -612,6 +613,11 @@ union = unionWith const
unions :: forall k v f. Ord k => Foldable f => f (Map k v) -> Map k v
unions = foldl union empty

-- | Difference of two maps. Return elements of the first map where
-- | the keys do not exist in the second map.
difference :: forall k v w. Ord k => Map k v -> Map k w -> Map k v
difference m1 m2 = foldl (flip delete) m1 (keys m2)

-- | Test whether one map contains all of the keys and values contained in another map
isSubmap :: forall k v. Ord k => Eq v => Map k v -> Map k v -> Boolean
isSubmap m1 m2 = LL.all f $ (toUnfoldable m1 :: LL.List (Tuple k v))
Expand Down
8 changes: 7 additions & 1 deletion test/Test/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Prelude

import Control.Alt ((<|>))
import Data.Array as A
import Data.Foldable (foldl, for_, all)
import Data.Foldable (foldl, for_, all, and)
import Data.FoldableWithIndex (foldrWithIndex)
import Data.Function (on)
import Data.FunctorWithIndex (mapWithIndex)
Expand Down Expand Up @@ -215,6 +215,12 @@ mapTests = do
Just v -> Just v == v2
Nothing -> not (in1 || in2)

log "difference"
quickCheck $ \(TestMap m1) (TestMap m2) ->
let d = M.difference (m1 :: M.Map SmallKey Int) (m2 :: M.Map SmallKey String)
in and (map (\k -> M.member k m1) (A.fromFoldable $ M.keys d)) &&
and (map (\k -> not $ M.member k d) (A.fromFoldable $ M.keys m2))

log "size"
quickCheck $ \xs ->
let xs' = nubBy ((==) `on` fst) xs
Expand Down

0 comments on commit 5b21750

Please sign in to comment.