Module

Data.HashMap

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

#HashMap Source

data HashMap t0 t1

Immutable hash maps from keys k to values v.

Note that this is an unordered collection.

Instances

#empty Source

empty :: forall k v. HashMap k v

The empty map.

#singleton Source

singleton :: forall k v. 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 k v. Hashable k => k -> HashMap k v -> Maybe v

Get a value by key.

#insert Source

insert :: forall k v. 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 k v. 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 k v. HashMap k v -> Int

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

size (singleton k v) == 1

#isEmpty Source

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

Test whether a map is empty.

isEmpty m == (m == empty)

#member Source

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

Test whether a key is in a map.

#upsert Source

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

Insert a new value if it doesn't exist or update the existing value by applying a function to it.

If you need to combine the new value with the existing value consider using insertWith instead.

#insertWith Source

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

Insert the new value if the key doesn't exist, otherwise combine the existing and new values.

The combining function is called with the existing value as the first argument and the new value as the second argument.

insertWith (<>) 5 "b" (singleton 5 "a") == singleton 5 "ab"

If your update function does not use the existing value, consider using upsert instead.

#update Source

update :: forall k v. 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 k v. 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.

#filter Source

filter :: forall k v. (v -> Boolean) -> HashMap k v -> HashMap k v

Remove key-value-pairs from a map for which the predicate does not hold.

filter (const False) m == empty
filter (const True) m == m

#filterWithKey Source

filterWithKey :: forall k v. (k -> v -> Boolean) -> HashMap k v -> HashMap k v

Remove key-value-pairs from a map for which the predicate does not hold.

Like filter, but the predicate takes both key and value.

#filterKeys Source

filterKeys :: forall k v. (k -> Boolean) -> HashMap k v -> HashMap k v

Remove all keys from the map for which the predicate does not hold.

difference m1 m2 == filterKeys (\k -> member k m2) m1

#mapMaybe Source

mapMaybe :: forall k v w. (v -> Maybe w) -> HashMap k v -> HashMap k w

Apply a function to all values in a hash map, discard the Nothing results, and keep the value of the Just results.

#mapMaybeWithKey Source

mapMaybeWithKey :: forall k v w. (k -> v -> Maybe w) -> HashMap k v -> HashMap k w

Apply a function to all key value pairs in a hash map, discard the Nothing results, and keep the value of the Just results.

#fromArray Source

fromArray :: forall k v. Hashable k => Array (Tuple k v) -> HashMap k v

Turn an array of pairs into a hash map.

This uses a mutable hash map internally and is faster than fromFoldable.

If you have an array of something other than tuples, use fromArrayBy.

#fromFoldable Source

fromFoldable :: forall f k v. 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.

If your input is an array, consider using fromArray instead.

#fromArrayBy Source

fromArrayBy :: forall a k v. Hashable k => (a -> k) -> (a -> v) -> Array a -> HashMap k v

Turn an array into a hash map given extraction functions for keys and values.

This uses a mutable hash map internally and is faster than fromFoldable and fromFoldableBy.

#fromFoldableBy Source

fromFoldableBy :: forall f a k v. Foldable f => Hashable k => (a -> k) -> (a -> v) -> f a -> HashMap k v

Turn a foldable functor into a hash map given extraction functions for keys and values.

If your input is an array, consider using fromArrayBy instead.

fromFoldableBy fst snd == fromFoldable

#fromFoldableWithIndex Source

fromFoldableWithIndex :: forall f k v. FoldableWithIndex k f => Hashable k => f v -> HashMap k v

Turn a foldable functor with index into a hash map.

This can be used to convert, for example, an ordered map into a hash map with the same keys and values, or an array into a hash map with values indexed by their position in the array.

fromFoldableWithIndex ["a", "b"] == fromArray [Tuple 0 "a", Tuple 1 "b"]

#toArrayBy Source

toArrayBy :: forall a k v. (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 k v. 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 k v. 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.

#union Source

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

Union two maps.

For duplicate keys, we keep the value from the left map.

#unionWith Source

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

Union two maps, combining the values for keys that appear in both maps using the given function.

unionWith (-) (singleton 0 3) (singleton 0 2) == singleton 0 1

#intersection Source

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

Intersect two maps.

#intersectionWith Source

intersectionWith :: forall k a b c. Hashable k => (a -> b -> c) -> HashMap k a -> HashMap k b -> HashMap k c

Intersect two maps, combining the values for keys that appear in both maps using the given function.

intersectionWith (-) (singleton 0 3) (singleton 0 2) == singleton 0 1

#difference Source

difference :: forall k v w. Hashable k => HashMap k v -> HashMap k w -> HashMap k v

Compute the difference of two maps, that is a new map of all the mappings in the left map that do not have a corresponding key in the right map.

#SemigroupHashMap Source

newtype SemigroupHashMap k v

This newtype provides a Semigroup instance for HashMap k v which delegates to the Semigroup v instance of elements. This newtype is deprecated and will be removed in the next major version. Use HashMap instead.

We are currently in step 2 of the following migration process:

  1. Add SemigroupHashMap with the new Semigroup instance and remove old instance from HashMap.

    The new instance uses unionWith append instead of union. You can recover the previous, left-biased behaviour by using SemigroupHashMap k (First v) in place of HashMap k v.

  2. Add new Semigroup instance to HashMap and deprecate SemigroupHashMap.

  3. Remove SemigroupHashMap.

Constructors

Instances

#nubHash Source

nubHash :: forall a. Hashable a => Array a -> Array a

Remove duplicates from an array.

Like nub from Data.Array, but uses a Hashable constraint instead of an Ord constraint.

#debugShow Source

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

#any Source

any :: forall k v. (v -> Boolean) -> HashMap k v -> Boolean

Returns true if at least one HashMap element satisfies the given predicate, iterating the HashMap only as necessary and stopping as soon as the predicate yields true.

Use this function instead of Foldable.any for more performance.