HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulerio-0.1.22.0Haskell2010

RIO.HashMap

Strict Map with hashed keys. Import as:

import qualified RIO.HashMap as HM

This module does not export any partial functions. For those, see RIO.HashMap.Partial

  • 1 type
  • 38 values
  • Packagerio-0.1.22.0
  • Exports39
  • LanguageHaskell2010
  • LicenceMIT
  • SourceHashMap.hs
datadata HashMap k v
#

A map from keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Instances27Bifoldable, Eq2, Ord2, Show2, NFData2, Hashable2, …
  • Bifoldable HashMapDefined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Eq2 HashMapDefined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Ord2 HashMapDefined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Show2 HashMapDefined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • NFData2 HashMapDefined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Hashable2 HashMapDefined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • (Lift k, Lift v) => Lift (HashMap k v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Functor (HashMap k)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Foldable (HashMap k)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Traversable (HashMap k)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Eq k => Eq1 (HashMap k)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Ord k => Ord1 (HashMap k)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • (Hashable k, Read k) => Read1 (HashMap k)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Show k => Show1 (HashMap k)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • NFData k => NFData1 (HashMap k)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Hashable k => Hashable1 (HashMap k)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Hashable k => IsList (HashMap k v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • (Eq k, Eq v) => Eq (HashMap k v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal

    Note that, in the presence of hash collisions, equal HashMaps may behave differently, i.e. extensionality may be violated:

    Example2 expressions
    data D = A | B deriving (Eq, Show)instance Hashable D where hashWithSalt salt _d = salt
    Example2 expressions
    x = fromList [(A,1), (B,2)]y = fromList [(B,2), (A,1)]
    Example3 expressions
    x == yTruetoList x[(A,1),(B,2)]toList y[(B,2),(A,1)]

    In general, the lack of extensionality can be observed with any function that depends on the key ordering, such as folds and traversals.

  • (Data k, Data v, Hashable k) => Data (HashMap k v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • (Ord k, Ord v) => Ord (HashMap k v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal

    The ordering is total and consistent with the Eq instance. However, nothing else about the ordering is specified, and it may change from version to version of either this package or of hashable.

  • (Hashable k, Read k, Read e) => Read (HashMap k e)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • (Show k, Show v) => Show (HashMap k v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • Hashable k => Semigroup (HashMap k v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal

    <> = union

    If a key occurs in both maps, the mapping from the first will be the mapping in the result.

    Examples
    Example1 expression
    fromList [(1,'a'),(2,'b')] <> fromList [(2,'c'),(3,'d')]fromList [(1,'a'),(2,'b'),(3,'d')]
  • Hashable k => Monoid (HashMap k v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal

    mempty = empty

    mappend = union

    If a key occurs in both maps, the mapping from the first will be the mapping in the result.

    Examples
    Example1 expression
    mappend (fromList [(1,'a'),(2,'b')]) (fromList [(2,'c'),(3,'d')])fromList [(1,'a'),(2,'b'),(3,'d')]
  • (NFData k, NFData v) => NFData (HashMap k v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • (Hashable k, Hashable v) => Hashable (HashMap k v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal
  • type Item (HashMap k v) = (k, v)Defined in unordered-containers-0.2.21 · Data.HashMap.Internal

Construction

2 declarations

Basic interface

11 declarations
valuesize :: HashMap k v -> Int
#

O(n) Return the number of key-value mappings in this map.

valuelookup :: Hashable k => k -> HashMap k v -> Maybe v
#

O(\log n) Return the value to which the specified key is mapped, or Nothing if this map contains no mapping for the key.

valuelookupDefault
  1. :: Hashable k
  2. => v

    Default value to return.

  3. -> k
  4. -> HashMap k v
  5. -> v
#

O(\log n) Return the value to which the specified key is mapped, or the default value if this map contains no mapping for the key.

DEPRECATED: lookupDefault is deprecated as of version 0.2.11, replaced by findWithDefault.

valueinsert :: Hashable k => k -> v -> HashMap k v -> HashMap k v
#

O(\log n) Associate the specified value with the specified key in this map. If this map previously contained a mapping for the key, the old value is replaced.

valueinsertWith
  1. :: Hashable k
  2. => v -> v -> v
  3. -> k
  4. -> v
  5. -> HashMap k v
  6. -> HashMap k v
#

O(\log n) Associate the value with the key in this map. If this map previously contained a mapping for the key, the old value is replaced by the result of applying the given function to the new and old value. Example:

insertWith f k v map
  where f new old = new + old
valueadjust :: Hashable k => (v -> v) -> k -> HashMap k v -> HashMap k v
#

O(\log n) Adjust the value tied to a given key in this map only if it is present. Otherwise, leave the map alone.

valueupdate :: Hashable k => (a -> Maybe a) -> k -> HashMap k a -> HashMap k a
#

O(\log n) The expression (update f k map) updates the value x at k (if it is in the map). If (f x) is Nothing, the element is deleted. If it is (Just y), the key k is bound to the new value y.

valuealter
  1. :: Hashable k
  2. => Maybe v -> Maybe v
  3. -> k
  4. -> HashMap k v
  5. -> HashMap k v
#

O(\log n) The expression (alter f k map) alters the value x at k, or absence thereof.

alter can be used to insert, delete, or update a value in a map. In short:

lookup k (alter f k m) = f (lookup k m)

Combine

0 declarations

Union

valueunion :: Eq k => HashMap k v -> HashMap k v -> HashMap k v
#

O(n+m) The union of two maps. If a key occurs in both maps, the mapping from the first will be the mapping in the result.

Examples
Example1 expression
union (fromList [(1,'a'),(2,'b')]) (fromList [(2,'c'),(3,'d')])fromList [(1,'a'),(2,'b'),(3,'d')]
valueunionWith
  1. :: Eq k
  2. => v -> v -> v
  3. -> HashMap k v
  4. -> HashMap k v
  5. -> HashMap k v
#

O(n+m) The union of two maps. If a key occurs in both maps, the provided function (first argument) will be used to compute the result.

valueunionWithKey
  1. :: Eq k
  2. => k -> v -> v -> v
  3. -> HashMap k v
  4. -> HashMap k v
  5. -> HashMap k v
#

O(n+m) The union of two maps. If a key occurs in both maps, the provided function (first argument) will be used to compute the result.

valueunions :: Eq k => [HashMap k v] -> HashMap k v
#

Construct a set containing all elements from a list of sets.

Transformations

3 declarations
valuemap :: (v1 -> v2) -> HashMap k v1 -> HashMap k v2
#

O(n) Transform this map by applying a function to every value.

valuemapWithKey :: (k -> v1 -> v2) -> HashMap k v1 -> HashMap k v2
#

O(n) Transform this map by applying a function to every value.

valuetraverseWithKey
  1. :: Applicative f
  2. => k -> v1 -> f v2
  3. -> HashMap k v1
  4. -> f (HashMap k v2)
#

O(n) Perform an Applicative action for each key-value pair in a HashMap and produce a HashMap of all the results. Each HashMap will be strict in all its values.

traverseWithKey f = fmap (map id) . Data.HashMap.Lazy.traverseWithKey f

Note: the order in which the actions occur is unspecified. In particular, when the map contains hash collisions, the order in which the actions associated with the keys involved will depend in an unspecified way on their insertion order.

Difference and intersection

5 declarations
valuedifferenceWith
  1. :: Hashable k
  2. => v -> w -> Maybe v
  3. -> HashMap k v
  4. -> HashMap k w
  5. -> HashMap k v
#

O(n \log m) Difference with a combining function. When two equal keys are encountered, the combining function is applied to the values of these keys. If it returns Nothing, the element is discarded (proper set difference). If it returns (Just y), the element is updated with a new value y.

valueintersectionWith
  1. :: Eq k
  2. => v1 -> v2 -> v3
  3. -> HashMap k v1
  4. -> HashMap k v2
  5. -> HashMap k v3
#

O(n+m) Intersection of two maps. If a key occurs in both maps the provided function is used to combine the values from the two maps.

valueintersectionWithKey
  1. :: Eq k
  2. => k -> v1 -> v2 -> v3
  3. -> HashMap k v1
  4. -> HashMap k v2
  5. -> HashMap k v3
#

O(n+m) Intersection of two maps. If a key occurs in both maps the provided function is used to combine the values from the two maps.

Folds

4 declarations
valuefoldl' :: (a -> v -> a) -> a -> HashMap k v -> a
#

O(n) Reduce this map by applying a binary operator to all elements, using the given starting value (typically the left-identity of the operator). Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

valuefoldlWithKey' :: (a -> k -> v -> a) -> a -> HashMap k v -> a
#

O(n) Reduce this map by applying a binary operator to all elements, using the given starting value (typically the left-identity of the operator). Each application of the operator is evaluated before using the result in the next application. This function is strict in the starting value.

valuefoldr :: (v -> a -> a) -> a -> HashMap k v -> a
#

O(n) Reduce this map by applying a binary operator to all elements, using the given starting value (typically the right-identity of the operator).

valuefoldrWithKey :: (k -> v -> a -> a) -> a -> HashMap k v -> a
#

O(n) Reduce this map by applying a binary operator to all elements, using the given starting value (typically the right-identity of the operator).

Filter

4 declarations
valuefilter :: (v -> Bool) -> HashMap k v -> HashMap k v
#

O(n) Filter this map by retaining only elements which values satisfy a predicate.

valuemapMaybe :: (v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2
#

O(n) Transform this map by applying a function to every value and retaining only some of them.

valuemapMaybeWithKey :: (k -> v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2
#

O(n) Transform this map by applying a function to every value and retaining only some of them.

Conversions

2 declarations
valuekeys :: HashMap k v -> [k]
#

O(n) Return a list of this map's keys. The list is produced lazily.

valueelems :: HashMap k v -> [v]
#

O(n) Return a list of this map's values. The list is produced lazily.

Lists

valuetoList :: HashMap k v -> [(k, v)]
#

O(n) Return a list of this map's elements. The list is produced lazily. The order of its elements is unspecified, and it may change from version to version of either this package or of hashable.

valuefromList :: Hashable k => [(k, v)] -> HashMap k v
#

O(n \log n) Construct a map with the supplied mappings. If the list contains duplicate mappings, the later mappings take precedence.

valuefromListWith :: Hashable k => (v -> v -> v) -> [(k, v)] -> HashMap k v
#

O(n \log n) Construct a map from a list of elements. Uses the provided function f to merge duplicate entries with (f newVal oldVal).

Examples

Given a list xs, create a map with the number of occurrences of each element in xs:

let xs = ['a', 'b', 'a']
in fromListWith (+) [ (x, 1) | x <- xs ]

= fromList [('a', 2), ('b', 1)]

Given a list of key-value pairs xs :: [(k, v)], group all values by their keys and return a HashMap k [v].

let xs = ('a', 1), ('b', 2), ('a', 3)]
in fromListWith (++) [ (k, [v]) | (k, v) <- xs ]

= fromList [('a', [3, 1]), ('b', [2])]

Note that the lists in the resulting map contain elements in reverse order from their occurrences in the original list.

More generally, duplicate entries are accumulated as follows; this matters when f is not commutative or not associative.

fromListWith f [(k, a), (k, b), (k, c), (k, d)]
= fromList [(k, f d (f c (f b a)))]