git.iko.soy/monoidal-containers
on master help sign in
Make functions correspond more closely to Data.Map behavior

This is a breaking change. The more "monoidal" behaviors that this reverts could eventuallly be re-added as a separate module that more thoroughly and systematically applies the monoid/semigroup operations. The goal of Data.Map.Monoidal is to be as similar to Data.Map as possible except for the Monoid/Semigroup instance that it replaces.

commit021685cf252a328ab34441b721e3d85c3a2402d5 authorAli Abrar <aliabrar@gmail.com> dateThu 22 Aug 2019, 08:38 (7 years ago) parentfd7b4b4
7 files changed+65−51
Changelog.md+7 monoidal-containers.cabal+1−1 src/Data/HashMap/Monoidal.hs+31−24 src/Data/IntMap/Monoidal.hs+8−8 src/Data/IntMap/Monoidal/Strict.hs+8−8 src/Data/Map/Monoidal.hs+8−8 src/Data/Map/Monoidal/Strict.hs+2−2
Changelog.md+7
@@ -1,5 +1,12 @@
11# Monoidal containers
22
3+# 0.6
4+
5+ * Deprecates 0.5.* and reverts behavior of fromList, insert, mapKeys, etc. to match behavior in Data.Map. The only difference in behavior between Data.Map.Monoidal.MonoidalMap and Data.Map.Map is now the semigroup and monoid instances (as was the case in 0.4 and earlier).
6+ * Fix the argument order of Data.HashMap.Monoidal.insert
7+ * Remove Data.HashMap.Monoidal.insertOrReplace as it is now identical to Data.HashMap.Monoidal.insert
8+ * Added Data.HashMap.Monoidal.insertWith and Data.HashMap.Monoidal.fromListWith
9+
310# 0.5.0.1
411
512 * Add a flag, `split-these`, to select whether to use the new "these"/"semialign" packages or the older combined "these" package.
monoidal-containers.cabal+1−1
@@ -1,5 +1,5 @@
11name: monoidal-containers
2−version: 0.5.0.1
2+version: 0.6
33synopsis: Containers with monoidal accumulation
44description:
55 Containers with merging via monoidal accumulation. The 'Monoid' instances
src/Data/HashMap/Monoidal.hs+31−24
@@ -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))
src/Data/IntMap/Monoidal.hs+8−8
@@ -321,8 +321,8 @@ empty :: forall a. MonoidalIntMap a
321321empty = coerce (M.empty :: M.IntMap a)
322322{-# INLINE empty #-}
323323
324−insert :: forall a. Semigroup a => Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
325−insert = coerce (M.insertWith (<>) :: Int -> a -> M.IntMap a -> M.IntMap a)
324+insert :: forall a. Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
325+insert = coerce (M.insert :: Int -> a -> M.IntMap a -> M.IntMap a)
326326{-# INLINE insert #-}
327327
328328insertWith :: forall a. (a -> a -> a) -> Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
@@ -421,8 +421,8 @@ mapAccumRWithKey :: forall a b c. (a -> Int -> b -> (a, c)) -> a -> MonoidalIntM
421421mapAccumRWithKey = coerce (M.mapAccumRWithKey :: (a -> Int -> b -> (a, c)) -> a -> M.IntMap b -> (a, M.IntMap c))
422422{-# INLINE mapAccumRWithKey #-}
423423
424−mapKeys :: forall a. Semigroup a => (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
425−mapKeys = coerce (M.mapKeysWith (<>) :: (Int -> Int) -> M.IntMap a -> M.IntMap a)
424+mapKeys :: forall a. (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
425+mapKeys = coerce (M.mapKeys :: (Int -> Int) -> M.IntMap a -> M.IntMap a)
426426{-# INLINE mapKeys #-}
427427
428428mapKeysWith :: forall a. (a -> a -> a) -> (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
@@ -481,8 +481,8 @@ toList :: forall a. MonoidalIntMap a -> [(Int, a)]
481481toList = coerce (M.toList :: M.IntMap a -> [(Int, a)])
482482{-# INLINE toList #-}
483483
484−fromList :: forall a. Semigroup a => [(Int, a)] -> MonoidalIntMap a
485−fromList = fromListWith (<>)
484+fromList :: forall a. [(Int, a)] -> MonoidalIntMap a
485+fromList = coerce (M.fromList :: [(Int, a)] -> M.IntMap a)
486486{-# INLINE fromList #-}
487487
488488fromListWith :: forall a. (a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
@@ -501,8 +501,8 @@ toDescList :: forall a. MonoidalIntMap a -> [(Int, a)]
501501toDescList = coerce (M.toDescList :: M.IntMap a -> [(Int, a)])
502502{-# INLINE toDescList #-}
503503
504−fromAscList :: forall a. Semigroup a => [(Int, a)] -> MonoidalIntMap a
505−fromAscList = fromAscListWith (<>)
504+fromAscList :: forall a. [(Int, a)] -> MonoidalIntMap a
505+fromAscList = coerce (M.fromAscList :: [(Int, a)] -> M.IntMap a)
506506{-# INLINE fromAscList #-}
507507
508508fromAscListWith :: forall a. (a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
src/Data/IntMap/Monoidal/Strict.hs+8−8
@@ -321,8 +321,8 @@ empty :: forall a. MonoidalIntMap a
321321empty = coerce (M.empty :: M.IntMap a)
322322{-# INLINE empty #-}
323323
324−insert :: forall a. Semigroup a => Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
325−insert = coerce (M.insertWith (<>) :: Int -> a -> M.IntMap a -> M.IntMap a)
324+insert :: forall a. Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
325+insert = coerce (M.insert :: Int -> a -> M.IntMap a -> M.IntMap a)
326326{-# INLINE insert #-}
327327
328328insertWith :: forall a. (a -> a -> a) -> Int -> a -> MonoidalIntMap a -> MonoidalIntMap a
@@ -421,8 +421,8 @@ mapAccumRWithKey :: forall a b c. (a -> Int -> b -> (a, c)) -> a -> MonoidalIntM
421421mapAccumRWithKey = coerce (M.mapAccumRWithKey :: (a -> Int -> b -> (a, c)) -> a -> M.IntMap b -> (a, M.IntMap c))
422422{-# INLINE mapAccumRWithKey #-}
423423
424−mapKeys :: forall a. Semigroup a => (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
425−mapKeys = coerce (M.mapKeysWith (<>) :: (Int -> Int) -> M.IntMap a -> M.IntMap a)
424+mapKeys :: forall a. (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
425+mapKeys = coerce (M.mapKeys :: (Int -> Int) -> M.IntMap a -> M.IntMap a)
426426{-# INLINE mapKeys #-}
427427
428428mapKeysWith :: forall a. (a -> a -> a) -> (Int -> Int) -> MonoidalIntMap a -> MonoidalIntMap a
@@ -481,8 +481,8 @@ toList :: forall a. MonoidalIntMap a -> [(Int, a)]
481481toList = coerce (M.toList :: M.IntMap a -> [(Int, a)])
482482{-# INLINE toList #-}
483483
484−fromList :: forall a. Semigroup a => [(Int, a)] -> MonoidalIntMap a
485−fromList = fromListWith (<>)
484+fromList :: forall a. [(Int, a)] -> MonoidalIntMap a
485+fromList = coerce (M.fromList :: [(Int, a)] -> M.IntMap a)
486486{-# INLINE fromList #-}
487487
488488fromListWith :: forall a. (a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
@@ -501,8 +501,8 @@ toDescList :: forall a. MonoidalIntMap a -> [(Int, a)]
501501toDescList = coerce (M.toDescList :: M.IntMap a -> [(Int, a)])
502502{-# INLINE toDescList #-}
503503
504−fromAscList :: forall a. Semigroup a => [(Int, a)] -> MonoidalIntMap a
505−fromAscList = fromAscListWith (<>)
504+fromAscList :: forall a. [(Int, a)] -> MonoidalIntMap a
505+fromAscList = coerce (M.fromAscList :: [(Int, a)] -> M.IntMap a)
506506{-# INLINE fromAscList #-}
507507
508508fromAscListWith :: forall a. (a -> a -> a) -> [(Int, a)] -> MonoidalIntMap a
src/Data/Map/Monoidal.hs+8−8
@@ -323,8 +323,8 @@ empty :: forall k a. MonoidalMap k a
323323empty = coerce (M.empty :: M.Map k a)
324324{-# INLINE empty #-}
325325
326−insert :: forall k a. (Ord k, Semigroup a) => k -> a -> MonoidalMap k a -> MonoidalMap k a
327−insert = insertWith (<>)
326+insert :: forall k a. Ord k => k -> a -> MonoidalMap k a -> MonoidalMap k a
327+insert = coerce (M.insert :: k -> a -> M.Map k a -> M.Map k a)
328328{-# INLINE insert #-}
329329
330330insertWith :: forall k a. Ord k => (a -> a -> a) -> k -> a -> MonoidalMap k a -> MonoidalMap k a
@@ -423,8 +423,8 @@ mapAccumRWithKey :: forall k a b c. (a -> k -> b -> (a, c)) -> a -> MonoidalMap
423423mapAccumRWithKey = coerce (M.mapAccumRWithKey :: (a -> k -> b -> (a, c)) -> a -> M.Map k b -> (a, M.Map k c))
424424{-# INLINE mapAccumRWithKey #-}
425425
426−mapKeys :: forall k1 k2 a. (Ord k2, Semigroup a) => (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a
427−mapKeys = mapKeysWith (<>)
426+mapKeys :: forall k1 k2 a. Ord k2 => (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a
427+mapKeys = coerce (M.mapKeys :: (k1 -> k2) -> M.Map k1 a -> M.Map k2 a)
428428{-# INLINE mapKeys #-}
429429
430430mapKeysWith :: forall k1 k2 a. Ord k2 => (a -> a -> a) -> (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a
@@ -501,8 +501,8 @@ toList :: forall k a. MonoidalMap k a -> [(k, a)]
501501toList = coerce (M.toList :: M.Map k a -> [(k, a)])
502502{-# INLINE toList #-}
503503
504−fromList :: forall k a. (Ord k, Semigroup a) => [(k, a)] -> MonoidalMap k a
505−fromList = fromListWith (<>)
504+fromList :: forall k a. Ord k => [(k, a)] -> MonoidalMap k a
505+fromList = coerce (M.fromList :: [(k, a)] -> M.Map k a)
506506{-# INLINE fromList #-}
507507
508508fromListWith :: forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> MonoidalMap k a
@@ -521,8 +521,8 @@ toDescList :: forall k a. MonoidalMap k a -> [(k, a)]
521521toDescList = coerce (M.toDescList :: M.Map k a -> [(k, a)])
522522{-# INLINE toDescList #-}
523523
524−fromAscList :: forall k a. (Eq k, Semigroup a) => [(k, a)] -> MonoidalMap k a
525−fromAscList = fromAscListWith (<>)
524+fromAscList :: forall k a. Eq k => [(k, a)] -> MonoidalMap k a
525+fromAscList = coerce (M.fromAscList :: [(k, a)] -> M.Map k a)
526526{-# INLINE fromAscList #-}
527527
528528fromAscListWith :: forall k a. Eq k => (a -> a -> a) -> [(k, a)] -> MonoidalMap k a
src/Data/Map/Monoidal/Strict.hs+2−2
@@ -322,8 +322,8 @@ empty :: forall k a. MonoidalMap k a
322322empty = coerce (M.empty :: M.Map k a)
323323{-# INLINE empty #-}
324324
325−insert :: forall k a. (Ord k, Semigroup a) => k -> a -> MonoidalMap k a -> MonoidalMap k a
326−insert = insertWith (<>)
325+insert :: forall k a. Ord k => k -> a -> MonoidalMap k a -> MonoidalMap k a
326+insert = coerce (M.insert :: k -> a -> M.Map k a -> M.Map k a)
327327{-# INLINE insert #-}
328328
329329insertWith :: forall k a. Ord k => (a -> a -> a) -> k -> a -> MonoidalMap k a -> MonoidalMap k a