Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Strict Map
with hashed keys. Import as:
import qualified RIO.HashMap as HM
- data HashMap k v :: * -> * -> *
- empty :: HashMap k v
- singleton :: Hashable k => k -> v -> HashMap k v
- null :: HashMap k v -> Bool
- size :: HashMap k v -> Int
- member :: (Eq k, Hashable k) => k -> HashMap k a -> Bool
- lookup :: (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
- lookupDefault :: (Eq k, Hashable k) => v -> k -> HashMap k v -> v
- insert :: (Eq k, Hashable k) => k -> v -> HashMap k v -> HashMap k v
- insertWith :: (Eq k, Hashable k) => (v -> v -> v) -> k -> v -> HashMap k v -> HashMap k v
- delete :: (Eq k, Hashable k) => k -> HashMap k v -> HashMap k v
- adjust :: (Eq k, Hashable k) => (v -> v) -> k -> HashMap k v -> HashMap k v
- update :: (Eq k, Hashable k) => (a -> Maybe a) -> k -> HashMap k a -> HashMap k a
- alter :: (Eq k, Hashable k) => (Maybe v -> Maybe v) -> k -> HashMap k v -> HashMap k v
- union :: (Eq k, Hashable k) => HashMap k v -> HashMap k v -> HashMap k v
- unionWith :: (Eq k, Hashable k) => (v -> v -> v) -> HashMap k v -> HashMap k v -> HashMap k v
- unionWithKey :: (Eq k, Hashable k) => (k -> v -> v -> v) -> HashMap k v -> HashMap k v -> HashMap k v
- unions :: (Eq k, Hashable k) => [HashMap k v] -> HashMap k v
- map :: (v1 -> v2) -> HashMap k v1 -> HashMap k v2
- mapWithKey :: (k -> v1 -> v2) -> HashMap k v1 -> HashMap k v2
- traverseWithKey :: Applicative f => (k -> v1 -> f v2) -> HashMap k v1 -> f (HashMap k v2)
- difference :: (Eq k, Hashable k) => HashMap k v -> HashMap k w -> HashMap k v
- differenceWith :: (Eq k, Hashable k) => (v -> w -> Maybe v) -> HashMap k v -> HashMap k w -> HashMap k v
- intersection :: (Eq k, Hashable k) => HashMap k v -> HashMap k w -> HashMap k v
- intersectionWith :: (Eq k, Hashable k) => (v1 -> v2 -> v3) -> HashMap k v1 -> HashMap k v2 -> HashMap k v3
- intersectionWithKey :: (Eq k, Hashable k) => (k -> v1 -> v2 -> v3) -> HashMap k v1 -> HashMap k v2 -> HashMap k v3
- foldl' :: (a -> v -> a) -> a -> HashMap k v -> a
- foldlWithKey' :: (a -> k -> v -> a) -> a -> HashMap k v -> a
- foldr :: (v -> a -> a) -> a -> HashMap k v -> a
- foldrWithKey :: (k -> v -> a -> a) -> a -> HashMap k v -> a
- filter :: (v -> Bool) -> HashMap k v -> HashMap k v
- filterWithKey :: (k -> v -> Bool) -> HashMap k v -> HashMap k v
- mapMaybe :: (v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2
- mapMaybeWithKey :: (k -> v1 -> Maybe v2) -> HashMap k v1 -> HashMap k v2
- keys :: HashMap k v -> [k]
- elems :: HashMap k v -> [v]
- toList :: HashMap k v -> [(k, v)]
- fromList :: (Eq k, Hashable k) => [(k, v)] -> HashMap k v
- fromListWith :: (Eq k, Hashable k) => (v -> v -> v) -> [(k, v)] -> HashMap k v
Documentation
data HashMap k v :: * -> * -> * #
A map from keys to values. A map cannot contain duplicate keys; each key can map to at most one value.
Eq2 HashMap | |
Show2 HashMap | |
Hashable2 HashMap | |
Functor (HashMap k) | |
Foldable (HashMap k) | |
Traversable (HashMap k) | |
Eq k => Eq1 (HashMap k) | |
(Eq k, Hashable k, Read k) => Read1 (HashMap k) | |
Show k => Show1 (HashMap k) | |
Hashable k => Hashable1 (HashMap k) | |
(Eq k, Hashable k) => IsList (HashMap k v) | |
(Eq k, Eq v) => Eq (HashMap k v) | |
(Data k, Data v, Eq k, Hashable k) => Data (HashMap k v) | |
(Eq k, Hashable k, Read k, Read e) => Read (HashMap k e) | |
(Show k, Show v) => Show (HashMap k v) | |
(Eq k, Hashable k) => Semigroup (HashMap k v) | |
(Eq k, Hashable k) => Monoid (HashMap k v) | |
(NFData k, NFData v) => NFData (HashMap k v) | |
(Hashable k, Hashable v) => Hashable (HashMap k v) | |
type Item (HashMap k v) | |
Construction
Basic interface
lookup :: (Eq k, 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.
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.
insert :: (Eq k, 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.
insertWith :: (Eq k, Hashable k) => (v -> v -> v) -> k -> v -> HashMap k v -> 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
delete :: (Eq k, Hashable k) => k -> HashMap k v -> HashMap k v #
O(log n) Remove the mapping for the specified key from this map if present.
adjust :: (Eq k, 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.
Combine
Union
union :: (Eq k, Hashable 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.
unionWith :: (Eq k, Hashable k) => (v -> v -> v) -> 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 provided function (first argument) will be used to compute the result.
unionWithKey :: (Eq k, Hashable k) => (k -> v -> v -> v) -> 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 provided function (first argument) will be used to compute the result.
unions :: (Eq k, Hashable k) => [HashMap k v] -> HashMap k v #
Construct a set containing all elements from a list of sets.
Transformations
map :: (v1 -> v2) -> HashMap k v1 -> HashMap k v2 #
O(n) Transform this map by applying a function to every value.
mapWithKey :: (k -> v1 -> v2) -> HashMap k v1 -> HashMap k v2 #
O(n) Transform this map by applying a function to every value.
traverseWithKey :: Applicative f => (k -> v1 -> f v2) -> HashMap k v1 -> f (HashMap k v2) #
O(n) Transform this map by accumulating an Applicative result from every value.
Difference and intersection
difference :: (Eq k, Hashable k) => HashMap k v -> HashMap k w -> HashMap k v #
O(n*log m) Difference of two maps. Return elements of the first map not existing in the second.
differenceWith :: (Eq k, Hashable k) => (v -> w -> Maybe v) -> HashMap k v -> HashMap k w -> HashMap k v #
intersection :: (Eq k, Hashable k) => HashMap k v -> HashMap k w -> HashMap k v #
O(n*log m) Intersection of two maps. Return elements of the first map for keys existing in the second.
intersectionWith :: (Eq k, Hashable k) => (v1 -> v2 -> v3) -> HashMap k v1 -> HashMap k v2 -> 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.
intersectionWithKey :: (Eq k, Hashable k) => (k -> v1 -> v2 -> v3) -> HashMap k v1 -> HashMap k v2 -> 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
foldl' :: (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 before using the result in the next application. This function is strict in the starting value.
foldlWithKey' :: (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 before using the result in the next application. This function is strict in the starting value.
foldr :: (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).
foldrWithKey :: (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
filter :: (v -> Bool) -> HashMap k v -> HashMap k v #
O(n) Filter this map by retaining only elements which values satisfy a predicate.
filterWithKey :: (k -> v -> Bool) -> HashMap k v -> HashMap k v #
O(n) Filter this map by retaining only elements satisfying a predicate.
mapMaybe :: (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.
mapMaybeWithKey :: (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
Lists
toList :: 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.
fromList :: (Eq k, 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.
fromListWith :: (Eq k, 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 (f newVal oldVal).
For example:
fromListWith (+) [ (x, 1) | x <- xs ]
will create a map with number of occurrences of each element in xs.
fromListWith (++) [ (k, [v]) | (k, v) <- xs ]
will group all values by their keys in a list 'xs :: [(k, v)]' and return a 'HashMap k [v]'.