From 6fb203af23c9910ca6776d0673a0256690e0fbcd Mon Sep 17 00:00:00 2001 From: Stefan Fehrenbach Date: Sat, 28 May 2022 15:02:16 +0200 Subject: [PATCH] Add Semigroup instance for HashMap This new instance delegates to the Semigroup of the elements. --- src/Data/HashMap.purs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Data/HashMap.purs b/src/Data/HashMap.purs index 53eb615..7850bbe 100644 --- a/src/Data/HashMap.purs +++ b/src/Data/HashMap.purs @@ -80,8 +80,11 @@ instance hashHashMap :: (Hashable k, Hashable v) => Hashable (HashMap k v) where foreign import hashPurs :: forall k v. (v -> Int) -> HashMap k v -> Int --- instance monoidHashMap :: Hashable k => Monoid (HashMap k v) where --- mempty = empty +instance monoidHashMap :: (Hashable k, Semigroup v) => Monoid (HashMap k v) where + mempty = empty + +instance semigroupHashMap :: (Hashable k, Semigroup v) => Semigroup (HashMap k v) where + append = unionWith append instance functorHashMap :: Functor (HashMap k) where map f = mapWithIndex (const f) @@ -127,9 +130,11 @@ foreign import traverseWithIndexPurs :: forall k v w m. (forall a. a -> m a) -> -- | This newtype provides a `Semigroup` instance for `HashMap k v` --- | which delegates to the `Semigroup v` instance of elements. +-- | which delegates to the `Semigroup v` instance of elements. This +-- | newtype is deprecated and will be removed in the next major +-- | version. Use `HashMap` instead. -- | --- | We are currently in step 1 of the following migration process: +-- | We are currently in step 2 of the following migration process: -- | 1. Add `SemigroupHashMap` with the new `Semigroup` instance and remove old instance from `HashMap`. -- | -- | The new instance uses `unionWith append` instead of `union`.