HORIZON HASKELLDocslts/ghc-9.14.x45dd5372026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 45dd537 · 2026-10-11

Modulemonoidal-containers-0.6.8.0Haskell2010

Data.Map.Monoidal

This module provides a Data.Map variant which uses the value's Monoid instance to accumulate conflicting entries when merging Maps.

While some functions mirroring those of Data.Map are provided here for convenience, more specialized needs will likely want to use either the Newtype or Wrapped instances to manipulate the underlying Map.

  • 1 type
  • 119 values
newtypenewtype MonoidalMap k a
#

A Map with monoidal accumulation

Constructors

Instances43FoldableWithIndex, FunctorWithIndex, TraversableWithIndex, TraverseMax, TraverseMin, Functor, …

Often-needed functions

119 declarations
valueassocs :: MonoidalMap k a -> [(k, a)]
#

O(n). Return all elements of the map and their keys

valueelems :: MonoidalMap k a -> [a]
#

O(n). Return all elements of the map in the ascending order of their keys. Subject to list fusion.

valuekeys :: MonoidalMap k a -> [k]
#

O(n). Return all keys of the map in ascending order. Subject to list fusion.

valuedelete :: Ord k => k -> MonoidalMap k a -> MonoidalMap k a
#

O(log n). Delete a key and its value from the map. When the key is not a member of the map, the original map is returned.

valuemapKeysMonotonic :: (k1 -> k2) -> MonoidalMap k1 a -> MonoidalMap k2 a
#

O(n). mapKeysMonotonic f s == mapKeys f s, but works only when f is strictly increasing (both monotonic and injective). That is, for any values x and y, if x < y then f x < f y and f is injective (i.e. it never maps two input keys to the same output key). The precondition is not checked. Semi-formally, we have:

and [x < y ==> f x < f y | x <- ls, y <- ls]
                    ==> mapKeysMonotonic f s == mapKeys f s
    where ls = keys s

This means that f maps distinct original keys to distinct resulting keys. This function has better performance than mapKeys.

mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")]) == fromList [(6, "b"), (10, "a")]
valid (mapKeysMonotonic (\ k -> k * 2) (fromList [(5,"a"), (3,"b")])) == True
valid (mapKeysMonotonic (\ _ -> 1)     (fromList [(5,"a"), (3,"b")])) == False