Skip to content

Commit 75b9bf4

Browse files
Re-add Semigroup/Monoid Map instances with Warn (#54)
* Add Semigroup/Monoid Map instance with Warn * Ignore user defined warning * Drop udw censoring since only test uses it * Added changelog entry
1 parent ef2f9ef commit 75b9bf4

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Breaking changes:
99

1010
New features:
1111
- Exported `Data.Map.Internal` data constructors (#52 by @natefaubion)
12+
- Add unbiased `Semigroup`/`Monoid` instances to `Map` with `Warn` (#54 by @JordanMartinez)
1213

1314
Bugfixes:
1415

src/Data/Map/Internal.purs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
6464
import Data.Tuple (Tuple(Tuple), snd, uncurry)
6565
import Data.Unfoldable (class Unfoldable, unfoldr)
6666
import Partial.Unsafe (unsafePartial)
67+
import Prim.TypeError (class Warn, Text)
6768

6869
-- | `Map k v` represents maps from keys of type `k` to values of type `v`.
6970
data Map k v
@@ -92,6 +93,20 @@ instance ordMap :: (Ord k, Ord v) => Ord (Map k v) where
9293
instance showMap :: (Show k, Show v) => Show (Map k v) where
9394
show m = "(fromFoldable " <> show (toAscArray m) <> ")"
9495

96+
instance semigroupMap ::
97+
( Warn (Text "Data.Map's `Semigroup` instance is now unbiased and differs from the left-biased instance defined in PureScript releases <= 0.13.x.")
98+
, Ord k
99+
, Semigroup v
100+
) => Semigroup (Map k v) where
101+
append l r = unionWith append l r
102+
103+
instance monoidSemigroupMap ::
104+
( Warn (Text "Data.Map's `Semigroup` instance is now unbiased and differs from the left-biased instance defined in PureScript releases <= 0.13.x.")
105+
, Ord k
106+
, Semigroup v
107+
) => Monoid (Map k v) where
108+
mempty = empty
109+
95110
instance altMap :: Ord k => Alt (Map k) where
96111
alt = union
97112

test/Test/Data/Map.purs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,27 @@ mapTests = do
402402
let expected = M.delete 1 m
403403
result === expected
404404

405+
log "SemigroupMap's Semigroup instance is based on value's Semigroup instance"
406+
quickCheck \(Tuple leftStr rightStr :: Tuple String String) -> do
407+
let key = "foo"
408+
let left = M.singleton key leftStr
409+
let right = M.singleton key rightStr
410+
let result = left <> right
411+
let expected = M.singleton key $ leftStr <> rightStr
412+
result == expected
413+
quickCheck \(Tuple leftStr rightStr :: Tuple String String) -> do
414+
let key = "foo"
415+
let left = M.singleton key $ First leftStr
416+
let right = M.singleton key $ First rightStr
417+
let result = left <> right
418+
result == left
419+
quickCheck \(Tuple leftStr rightStr :: Tuple String String) -> do
420+
let key = "foo"
421+
let left = M.singleton key $ Last leftStr
422+
let right = M.singleton key $ Last rightStr
423+
let result = left <> right
424+
result == right
425+
405426
log "SemigroupMap's Semigroup instance is based on value's Semigroup instance"
406427
quickCheck \(Tuple leftStr rightStr :: Tuple String String) -> do
407428
let key = "foo"

0 commit comments

Comments
 (0)