Module

Data.HashMap

Package
purescript-unordered-collections
Repository
fehrenbach/purescript-unordered-collections

#HashMap Source

data HashMap :: Type -> Type -> Type

Immutable hash maps from keys k to values v.

Note that this is an unordered collection.

The implementation is based on "Optimizing Hash-Array Mapped Tries for Fast and Lean Immutable JVM Collections" (Steindorfer and Vinju, OOPSLA 2015, https://doi.org/10.1145/2814270.2814312).

Instances

#empty Source

empty :: forall v k. HashMap k v

The empty map.

#singleton Source

singleton :: forall v k. Hashable k => k -> v -> HashMap k v

A map of one key and its associated value.

singleton k v == insert k v empty

#lookup Source

lookup :: forall v k. Hashable k => k -> HashMap k v -> Maybe v

Get a value by key.

#insert Source

insert :: forall v k. Hashable k => k -> v -> HashMap k v -> HashMap k v

Insert or replace a value.

lookup k (insert k v m) == Just v

#delete Source

delete :: forall v k. Hashable k => k -> HashMap k v -> HashMap k v

Remove a key and its associated value from a map.

lookup k (delete k m) == Nothing

#size Source

size :: forall v k. HashMap k v -> Int

Returns the number of key-value pairs in a map.

size (singleton k v) == 1

#isEmpty Source

isEmpty :: forall v k. HashMap k v -> Boolean

Test whether a map is empty.

isEmpty m == (m == empty)

#member Source

member :: forall v k. Hashable k => k -> HashMap k v -> Boolean

Test whether a key is in a map.

#update Source

update :: forall v k. Hashable k => (v -> Maybe v) -> k -> HashMap k v -> HashMap k v

Update or delete the value for a key in a map

#alter Source

alter :: forall v k. Hashable k => (Maybe v -> Maybe v) -> k -> HashMap k v -> HashMap k v

Insert a value, delete a value, or update a value for a key in a map

#fromFoldable Source

fromFoldable :: forall v k f. Foldable f => Hashable k => f (Tuple k v) -> HashMap k v

Turn a foldable functor of pairs into a hash map.

In the presence of duplicate keys, later (by foldl) mappings overwrite earlier mappings.

#toArrayBy Source

toArrayBy :: forall v k a. (k -> v -> a) -> HashMap k v -> Array a

Convert a map to an array using the given function.

Note that no particular order is guaranteed.

toArrayBy Tuple (singleton 1 2) == [Tuple 1 2]
toArrayBy const        m == keys m
toArrayBy (flip const) m == values m

#keys Source

keys :: forall v k. HashMap k v -> Array k

Returns the keys of the map in no particular order.

If you need both keys and values, use toArrayBy rather than both keys and values.

#values Source

values :: forall v k. HashMap k v -> Array v

Returns the values of the map in no particular order.

If you need both keys and values, use toArrayBy rather than both keys and values.

#debugShow Source

debugShow :: forall v k. HashMap k v -> String