Skip to content

Add difference for Map #5

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

Merged
merged 3 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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