Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
This module provides a HashMap
variant which uses the value's
Monoid
instance to accumulate conflicting entries when merging
Map
s.
While some functions mirroring those of HashMap
are provided
here for convenience, more specialized needs will likely want to use
either the Newtype
or Wrapped
instances to manipulate the
underlying Map
.
Synopsis
- newtype MonoidalHashMap k a = MonoidalHashMap {
- getMonoidalHashMap :: HashMap k a
- toList :: MonoidalHashMap k a -> [(k, a)]
- fromList :: (Eq k, Hashable k) => [(k, a)] -> MonoidalHashMap k a
- fromListWith :: (Eq k, Hashable k) => (a -> a -> a) -> [(k, a)] -> MonoidalHashMap k a
- singleton :: (Eq k, Hashable k) => k -> a -> MonoidalHashMap k a
- size :: MonoidalHashMap k a -> Int
- member :: (Eq k, Hashable k) => k -> MonoidalHashMap k a -> Bool
- notMember :: (Eq k, Hashable k) => k -> MonoidalHashMap k a -> Bool
- lookup :: (Eq k, Hashable k) => k -> MonoidalHashMap k v -> Maybe v
- lookupM :: (Eq k, Hashable k, Monoid v) => k -> MonoidalHashMap k v -> v
- elems :: MonoidalHashMap k a -> [a]
- keys :: MonoidalHashMap k a -> [k]
- delete :: (Eq k, Hashable k) => k -> MonoidalHashMap k a -> MonoidalHashMap k a
- mapKeys :: (Hashable k, Eq k, Hashable k', Eq k') => (k -> k') -> MonoidalHashMap k a -> MonoidalHashMap k' a
- insert :: (Hashable k, Eq k) => k -> a -> MonoidalHashMap k a -> MonoidalHashMap k a
- insertWith :: (Hashable k, Eq k) => (a -> a -> a) -> k -> a -> MonoidalHashMap k a -> MonoidalHashMap k a
- modify :: (Monoid a, Hashable k, Eq k) => (a -> a) -> k -> MonoidalHashMap k a -> MonoidalHashMap k a
- modifyDef :: (Hashable k, Eq k) => a -> (a -> a) -> k -> MonoidalHashMap k a -> MonoidalHashMap k a
- map :: (v1 -> v2) -> MonoidalHashMap k v1 -> MonoidalHashMap k v2
- filterWithKey :: (k -> v -> Bool) -> MonoidalHashMap k v -> MonoidalHashMap k v
Documentation
newtype MonoidalHashMap k a Source #
A HashMap
with monoidal accumulation
Instances
Often-needed functions
toList :: MonoidalHashMap k a -> [(k, a)] Source #
O(n*log n). Return a list of this map's elements. The list is produced lazily. The order of its elements is unspecified.
fromList :: (Eq k, Hashable k) => [(k, a)] -> MonoidalHashMap k a Source #
O(n*log n). Construct a map with the supplied mappings. If the list contains duplicate mappings, values will be replaced.
fromListWith :: (Eq k, Hashable k) => (a -> a -> a) -> [(k, a)] -> MonoidalHashMap k a Source #
O(n*log n). Construct a map with the supplied mappings. If the list contains duplicate mappings, values will be merged using the provided combining function.
singleton :: (Eq k, Hashable k) => k -> a -> MonoidalHashMap k a Source #
O(1). A map with a single element.
size :: MonoidalHashMap k a -> Int Source #
O(1). The number of elements in the map.
member :: (Eq k, Hashable k) => k -> MonoidalHashMap k a -> Bool Source #
O(log n). Is the key a member of the map? See also notMember
.
notMember :: (Eq k, Hashable k) => k -> MonoidalHashMap k a -> Bool Source #
O(log n). Is the key not a member of the map? See also member
.
lookup :: (Eq k, Hashable k) => k -> MonoidalHashMap k v -> Maybe v Source #
O(log n) Return the value to which the specified key is mapped,
or Nothing
if this map contains no mapping for the key.
lookupM :: (Eq k, Hashable k, Monoid v) => k -> MonoidalHashMap k v -> v Source #
O(log n) Return the value to which the specified key is mapped, or mempty if this map contains no mapping for the key.
elems :: MonoidalHashMap k a -> [a] Source #
O(n). Return a list of this map's values. The list is produced lazily.
keys :: MonoidalHashMap k a -> [k] Source #
O(n). Return all keys of the map in ascending order. Subject to list fusion.
delete :: (Eq k, Hashable k) => k -> MonoidalHashMap k a -> MonoidalHashMap k a Source #
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.
mapKeys :: (Hashable k, Eq k, Hashable k', Eq k') => (k -> k') -> MonoidalHashMap k a -> MonoidalHashMap k' a Source #
O(n). Map a function to each key of a map, if it will result in duplicated mappings, their values will be merged in unspecified order
insert :: (Hashable k, Eq k) => k -> a -> MonoidalHashMap k a -> MonoidalHashMap k a Source #
O(log n). Insert a value on some key, if it exists replace the value.
insertWith :: (Hashable k, Eq k) => (a -> a -> a) -> k -> a -> MonoidalHashMap k a -> MonoidalHashMap k a Source #
O(log n). Insert a value on some key, if it exists apply the combining function.
modify :: (Monoid a, Hashable k, Eq k) => (a -> a) -> k -> MonoidalHashMap k a -> MonoidalHashMap k a Source #
O(log n). Modify a value on some key with a function, if value under key doesn't exist -- use mempty.
modifyDef :: (Hashable k, Eq k) => a -> (a -> a) -> k -> MonoidalHashMap k a -> MonoidalHashMap k a Source #
O(log n). Modify a value on some key with a function, providing a default value if that key doesn't exist.
map :: (v1 -> v2) -> MonoidalHashMap k v1 -> MonoidalHashMap k v2 Source #
O(n) Transform this map by applying a function to every value.
filterWithKey :: (k -> v -> Bool) -> MonoidalHashMap k v -> MonoidalHashMap k v Source #
O(n) Filter this map by retaining only elements satisfying a predicate.