@@ -20,6 +20,7 @@ module Data.HashMap.Monoidal
2020 -- * Often-needed functions
2121 , toList
2222 , fromList
23+ , fromListWith
2324 , singleton
2425 , size
2526 , member
@@ -31,7 +32,7 @@ module Data.HashMap.Monoidal
3132 , delete
3233 , mapKeys
3334 , insert
34− , insertOrReplace
35+ , insertWith
3536 , modify
3637 , modifyDef
3738 , map
@@ -125,7 +126,7 @@ instance Newtype (MonoidalHashMap k a) (M.HashMap k a) where
125126#if MIN_VERSION_base(4,7,0)
126127instance (Eq k, Hashable k, Semigroup a) => Exts.IsList (MonoidalHashMap k a) where
127128 type Item (MonoidalHashMap k a) = (k, a)
128− fromList = MonoidalHashMap . M.fromListWith (<>)
129+ fromList = MonoidalHashMap . M.fromList
129130 {-# INLINE fromList #-}
130131 toList = M.toList . unpack
131132 {-# INLINE toList #-}
@@ -182,38 +183,44 @@ keys = M.keys . unpack
182183{-# INLINE keys #-}
183184
184185-- | /O(n*log n)/. Construct a map with the supplied mappings. If the list
185−-- contains duplicate mappings, values will be merged using (mappend newVal oldVal).
186−fromList :: (Eq k, Hashable k, Semigroup a) => [(k,a)] -> MonoidalHashMap k a
187−fromList = pack . M.fromListWith (<>)
186+-- contains duplicate mappings, values will be replaced.
187+fromList :: (Eq k, Hashable k) => [(k,a)] -> MonoidalHashMap k a
188+fromList = pack . M.fromList
188189{-# INLINE fromList #-}
189190
191+-- | /O(n*log n)/. Construct a map with the supplied mappings. If the list
192+-- contains duplicate mappings, values will be merged using the provided combining function.
193+fromListWith :: (Eq k, Hashable k) => (a -> a -> a) -> [(k,a)] -> MonoidalHashMap k a
194+fromListWith f = pack . M.fromListWith f
195+{-# INLINE fromListWith #-}
196+
197+
190198-- | /O(n*log n)/. Return a list of this map's elements. The list is produced
191199-- lazily. The order of its elements is unspecified.
192200toList :: MonoidalHashMap k a -> [(k,a)]
193201toList = M.toList . unpack
194202{-# INLINE toList #-}
195203
196−-- | /O(log n)/. Insert a value on some key, if it exists -- mappend
197−-- to the existing one.
198−insert :: (Semigroup a, Hashable k, Eq k)
199− => a
200− -> k
204+-- | /O(log n)/. Insert a value on some key, if it exists replace the value.
205+insert :: (Hashable k, Eq k)
206+ => k
207+ -> a
201208 -> MonoidalHashMap k a
202209 -> MonoidalHashMap k a
203−insert x k = pack
204− . M.insertWith (<>) k x
210+insert k x = pack
211+ . M.insert k x
205212 . unpack
206213
207−-- | /O(log n)/. Insert a value on some key with a function, if value
208−-- under key exists -- replace it.
209−insertOrReplace :: (Semigroup a, Hashable k, Eq k)
210− => a
211− -> k
212− -> MonoidalHashMap k a
213− -> MonoidalHashMap k a
214−insertOrReplace x k = pack
215− . M.insert k x
216− . unpack
214+-- | /O(log n)/. Insert a value on some key, if it exists apply the combining function.
215+insertWith :: (Hashable k, Eq k)
216+ => (a -> a -> a)
217+ -> k
218+ -> a
219+ -> MonoidalHashMap k a
220+ -> MonoidalHashMap k a
221+insertWith f k x = pack
222+ . M.insertWith f k x
223+ . unpack
217224
218225-- | /O(log n)/. Modify a value on some key with a function, if value
219226-- under key doesn't exist -- use mempty.
@@ -228,7 +235,7 @@ modify f k = pack
228235
229236-- | /O(log n)/. Modify a value on some key with a function, providing
230237-- a default value if that key doesn't exist.
231−modifyDef :: (Semigroup a, Hashable k, Eq k)
238+modifyDef :: (Hashable k, Eq k)
232239 => a -> (a -> a)
233240 -> k -> MonoidalHashMap k a
234241 -> MonoidalHashMap k a
@@ -239,7 +246,7 @@ modifyDef d f k = pack
239246
240247-- | /O(n)/. Map a function to each key of a map, if it will result
241248-- in duplicated mappings, their values will be merged in unspecified order
242−mapKeys :: (Semigroup a, Hashable k, Eq k, Hashable k', Eq k')
249+mapKeys :: (Hashable k, Eq k, Hashable k', Eq k')
243250 => (k -> k') -> MonoidalHashMap k a -> MonoidalHashMap k' a
244251mapKeys f = fromList
245252 . fmap (\(k, v) -> (f k, v))