Skip to content

Commit

Permalink
Revert "Change union to be right-biased"
Browse files Browse the repository at this point in the history
...for now, to keep compatibility with ordered-collections.

This reverts commit 21eee37.
  • Loading branch information
fehrenbach committed Jun 15, 2018
1 parent d26588b commit 6c34144
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/Data/HashMap.purs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ instance monoidHashMap :: Hashable k => Monoid (HashMap k v) where

-- | This is "the shallow" semigroup instance, where maps themselves
-- | are combined using `union` rather than elements being combined.
-- | For duplicate keys, values from the right map are preserved.
-- | For duplicate keys, values from the left map are preserved.
instance semigroupHashMap :: Hashable k => Semigroup (HashMap k v) where
append = union

Expand Down Expand Up @@ -205,11 +205,11 @@ foreign import size :: forall k v. HashMap k v -> Int

-- | Union two maps.
-- |
-- | For duplicate keys, we keep the value from the right map.
-- | For duplicate keys, we keep the value from the left map.
-- |
-- | This is the same as `Semigroup.append` aka `(<>)`.
union :: forall k v. Hashable k => HashMap k v -> HashMap k v -> HashMap k v
union = unionWith (\_ x -> x)
union = unionWith const

foreign import nubHashPurs :: forall a. (a -> a -> Boolean) -> (a -> Int) -> Array a -> Array a

Expand Down
18 changes: 7 additions & 11 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Prelude
import Data.Array as A
import Data.Array as Array
import Data.Foldable (foldMap, foldr)
import Data.FoldableWithIndex (foldMapWithIndex, foldlWithIndex)
import Data.FoldableWithIndex (foldMapWithIndex, foldlWithIndex, foldrWithIndex)
import Data.HashMap (HashMap)
import Data.HashMap as HM
import Data.HashSet as HS
Expand Down Expand Up @@ -142,9 +142,9 @@ main = do

log "fromFoldable (b <> a) = fromFoldable a <> fromFoldable b"
quickCheck' 1000 \ a (b :: Array (Tuple CollidingInt String)) ->
HM.fromFoldable (a <> b) === HM.fromFoldable a <> HM.fromFoldable b
HM.fromFoldable (b <> a) === HM.fromFoldable a <> HM.fromFoldable b
quickCheckWithSeed (mkSeed 376236318) 1 \ a (b :: Array (Tuple CollidingInt String)) ->
HM.fromFoldable (a <> b) == HM.fromFoldable a <> HM.fromFoldable b
HM.fromFoldable (b <> a) == HM.fromFoldable a <> HM.fromFoldable b
<?> ( " a: " <> show a <>
"\n b: " <> show b <>
"\n hma: " <> show (HM.fromFoldable a) <>
Expand All @@ -155,11 +155,11 @@ main = do
quickCheck' 100000 $ \(a :: Array (Tuple CollidingInt String)) b ->
let m = arbitraryHM a
n = arbitraryHM b
in HM.union m n === foldlWithIndex (\k m' v -> HM.insert k v m') m n
in HM.union m n === foldrWithIndex HM.insert n m
quickCheckWithSeed (mkSeed 376236318) 1 $ \(a :: Array (Tuple CollidingInt String)) b ->
let m = arbitraryHM a
n = arbitraryHM b
in HM.union m n === foldlWithIndex (\k m' v -> HM.insert k v m') m n
in HM.union n m === foldlWithIndex (\k m' v -> HM.insert k v m') m n

log "unionWith agrees with OrdMap"
quickCheck' 10000 $ \(a :: Array (Tuple CollidingInt Int)) (b :: Array (Tuple CollidingInt Int)) c f ->
Expand All @@ -172,11 +172,11 @@ main = do
Array.sort (OM.toUnfoldable (OM.unionWith f (OM.fromFoldable a) (OM.fromFoldable b)))


log "unionWith (flip const) = union"
log "unionWith const = union"
quickCheck $ \(a :: Array (Tuple CollidingInt String)) b ->
let m = arbitraryHM a
n = arbitraryHM b
in HM.union m n === HM.unionWith (flip const) m n
in HM.union m n === HM.unionWith const m n

log "map id = id"
quickCheck \ (a :: Array (Tuple CollidingInt Boolean)) ->
Expand Down Expand Up @@ -229,10 +229,6 @@ main = do
log "intersection = fromFoldable OS.intersection"
quickCheck' 100000 $ \(a :: Array (Tuple CollidingInt String)) (b :: Array (Tuple CollidingInt String)) ->
HS.fromFoldable (OS.intersection (OS.fromFoldable a) (OS.fromFoldable b)) === HS.fromFoldable a `HS.intersection` HS.fromFoldable b
quickCheckWithSeed (mkSeed 376236318) 1 $ \(a :: Array (Tuple CollidingInt String)) b ->
let m = arbitraryHM a
n = arbitraryHM b
in HM.union m n === foldlWithIndex (\k m' v -> HM.insert k v m') m n

log "l `HS.union` r === r `HS.union` l"
quickCheck' 10000 \(l' :: Array CollidingInt) (r':: Array CollidingInt) ->
Expand Down

0 comments on commit 6c34144

Please sign in to comment.