diff --git a/src/Data/HashMap.purs b/src/Data/HashMap.purs index 85ad665..8bf1fc0 100644 --- a/src/Data/HashMap.purs +++ b/src/Data/HashMap.purs @@ -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 @@ -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 diff --git a/test/Main.purs b/test/Main.purs index ac4e93d..a8bdd39 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -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 @@ -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) <> @@ -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 -> @@ -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)) -> @@ -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) ->