# MultiMap

`enum MultiMap[k: Type, v: Type] with Sendable`Source
`case MultiMap(Map[k, Set[v]])`

The MultiMap type.

A MultiMap is a Map that allows multiple values for a key. Multiple values are stored in a Set so duplicates are not allowed.

The key and value types must implement the Eq and Order traits.

### Instances

`instance CommutativeMonoid[MultiMap[k, v]] with Order[k], Order[v]`Source
`instance CommutativeSemiGroup[MultiMap[k, v]] with Order[k], Order[v]`Source
`instance Eq[MultiMap[k, v]] with Eq[k], Eq[v]`Source
`instance Foldable[MultiMap[k]]`Source
`instance Hash[MultiMap[k, v]] with Hash[k], Hash[v]`Source
`instance Monoid[MultiMap[k, v]] with Order[k], Order[v]`Source
`instance Order[MultiMap[k, v]] with Order[k], Order[v]`Source
`instance SemiGroup[MultiMap[k, v]] with Order[k], Order[v]`Source
`instance Sendable[MultiMap[k, v]] with Sendable[k], Sendable[v]`Source
`instance ToString[MultiMap[k, v]] with ToString[k], ToString[v]`Source
`instance UnorderedFoldable[MultiMap[k]]`Source

## Definitions

`def adjust(f: v -> v \ ef, k: k, m: MultiMap[k, v]): MultiMap[k, v] \ ef with Order[k], Order[v]`

Updates `m` with `k => map(f, vs)` if `k => vs` is in `m`.

Otherwise, returns `m`.

`def adjustWithKey(f: k -> (v -> v \ ef), k: k, m: MultiMap[k, v]): MultiMap[k, v] \ ef with Order[k], Order[v]`

Updates `m` with `k => map(f(k), vs)` if `k => vs` is in `m`. Otherwise, returns `m`.

`@ParallelWhenPure `
`def count(f: k -> (v -> Bool \ ef), m: MultiMap[k, v]): Int32 \ ef`

Returns the number of mappings `k => v` in `m` that satisfy the predicate `f`.

Purity reflective: Runs in parallel when given a pure function `f`.

`def difference(m1: MultiMap[k, v], m2: MultiMap[k, v]): MultiMap[k, v] with Order[k], Order[v]`

Returns the difference of `m1` and `m2` i.e `m1 - m2` (left biased).

`def empty(): MultiMap[k, v]`

Returns the empty MultiMap.

`def exists(f: k -> (v -> Bool \ ef), m: MultiMap[k, v]): Bool \ ef`

Returns `true` if and only if at least one mapping in `m` satisfies the predicate `f`.

Returns `false` if `m` is the empty MultiMap.

`def filter(f: v -> Bool \ ef, m: MultiMap[k, v]): MultiMap[k, v] \ ef with Order[k], Order[v]`

Returns a MultiMap of all mappings `k => v` in `m` where `v` satisfies the predicate `f`.

`def filterWithKey(f: k -> (v -> Bool \ ef), m: MultiMap[k, v]): MultiMap[k, v] \ ef with Order[k], Order[v]`

Returns a MultiMap of all mappings `k => v` in `m` where `(k, v)` satisfies the predicate `f`.

`def find(f: k -> (v -> Bool \ ef), m: MultiMap[k, v]): Option[(k, v)] \ ef`

Alias for `findLeft`.

`def findLeft(f: k -> (v -> Bool \ ef), m: MultiMap[k, v]): Option[(k, v)] \ ef`

Optionally returns the first mapping of `m` that satisfies the predicate `f` when searching from left to right.

`def findRight(f: k -> (v -> Bool \ ef), m: MultiMap[k, v]): Option[(k, v)] \ ef`

Optionally returns the first mapping of `m` that satisfies the predicate `f` when searching from right to left.

`def foldLeft(f: b -> (v -> b \ ef), s: b, m: MultiMap[k, v]): b \ ef`

Applies `f` to a start value `s` and all values in `m` going from left to right.

That is, the result is of the form: `f(...f(f(s, v1), v2)..., vn)`.

`def foldLeftWithKey(f: b -> (k -> (v -> b \ ef)), s: b, m: MultiMap[k, v]): b \ ef`

Applies `f` to a start value `s` and all key-value pairs in `m` going from left to right.

That is, the result is of the form: `f(...f(f(s, k1, v1), k2, v2)..., vn)`.

`def foldMap(f: v -> b \ ef, m: MultiMap[k, v]): b \ ef with Monoid[b]`

Returns the result of mapping each value and combining the results.

`def foldMapWithKey(f: k -> (v -> b \ ef), m: MultiMap[k, v]): b \ ef with Monoid[b]`

Returns the result of mapping each key-value pair and combining the results.

`def foldRight(f: v -> (b -> b \ ef), s: b, m: MultiMap[k, v]): b \ ef`

Applies `f` to a start value `s` and all values in `m` going from right to left.

That is, the result is of the form: `f(v1, ...f(vn-1, f(vn, s)))`.

`def foldRightWithCont(f: v -> ((Unit -> b \ ef) -> b \ ef), z: b, m: MultiMap[k, v]): b \ ef`

Applies `f` to a start value `z` and all values in `m` going from right to left.

That is, the result is of the form: `f(v1, ...f(vn-1, f(vn, z)))`. A `foldRightWithCont` allows early termination by not calling the continuation.

`def foldRightWithKey(f: k -> (v -> (b -> b \ ef)), s: b, m: MultiMap[k, v]): b \ ef`

Applies `f` to a start value `s` and all key-value pairs in `m` going from right to left.

That is, the result is of the form: `f(k1, v1, ...f(kn-1, vn-1, f(kn, vn, s)))`.

`def foldRightWithKeyCont(f: k -> (v -> ((Unit -> b \ ef) -> b \ ef)), z: b, m: MultiMap[k, v]): b \ ef`

Applies `f` to a start value `z` and all key-value pairs in `m` going from right to left.

That is, the result is of the form: `f(k1, v1, ...f(kn-1, vn-1, f(kn, vn, z)))`. A `foldRightWithKeyCont` allows early termination by not calling the continuation.

`def foldWithKey(f: b -> (k -> (v -> b \ ef)), s: b, m: MultiMap[k, v]): b \ ef`

Alias for `foldLeftWithKey`.

`def forAll(f: k -> (v -> Bool \ ef), m: MultiMap[k, v]): Bool \ ef`

Returns `true` if and only if all mappings in `m` satisfy the predicate `f`.

Returns `true` if `m` is the empty MultiMap.

`def forEach(f: k -> (v -> Unit \ ef), m: MultiMap[k, v]): Unit \ ef`

Applies `f` to every `(key, value)` of MultiMap `m`.

`def forEachWithIndex(f: Int32 -> (k -> (v -> Unit \ ef)), m: MultiMap[k, v]): Unit \ ef`

Applies `f` to tuple `(index, key, value)` formed of the keys and values of MultiMap `m` and the index of the traversal.

`def get(k: k, m: MultiMap[k, v]): Set[v] with Order[k]`

Returns `Some(vs)` if `k => vs` is in `m`.

Otherwise returns `Nil`.

`def insert(k: k, v: v, m: MultiMap[k, v]): MultiMap[k, v] with Order[k], Order[v]`

Updates `m` with `k => v`.

`def insertAll(k: k, vs: t[v], m: MultiMap[k, v]): MultiMap[k, v] \ Aef[t] with Order[k], Order[v], Foldable[t]`

Updates `m` with `k => vs`.

Where `vs` is any foldable container. If `vs` is empty nothing is inserted.

`def intersection(m1: MultiMap[k, v], m2: MultiMap[k, v]): MultiMap[k, v] with Order[k], Order[v]`

Returns the intersection of `m1` and `m2`.

`def isEmpty(m: MultiMap[k, v]): Bool`

Returns `true` if and only if `m` is the empty MultiMap.

`def iterator(rc: Region[r], m: MultiMap[k, v]): Iterator[(k, Set[v]), r, r] \ r`

Returns an iterator over all key-value pairs in `m` i.e. `k => Set#{v_1, ..., v_n}`.

`def keysOf(m: MultiMap[k, v]): Set[k] with Order[k]`

Returns the keys of `m`.

`@ParallelWhenPure `
`def map(f: v1 -> v2 \ ef, m: MultiMap[k, v1]): MultiMap[k, v2] \ ef with Order[v2]`

Returns a MultiMap with mappings `k => f(v)` for every `k => v` in `m`.

Purity reflective: Runs in parallel when given a pure function `f`.

`@ParallelWhenPure `
`def mapWithKey(f: k -> (v1 -> v2 \ ef), m: MultiMap[k, v1]): MultiMap[k, v2] \ ef with Order[v2]`

Returns a MultiMap with mappings `k => f(k, v)` for every `k => v` in `m`.

Purity reflective: Runs in parallel when given a pure function `f`.

`def memberOf(k: k, m: MultiMap[k, v]): Bool with Order[k]`

Returns `true` if and only if `m` contains the key `k`.

`def nonEmpty(m: MultiMap[k, v]): Bool`

Returns `true` if and only if `m` is a non-empty MultiMap.

`def reduceLeft(f: v -> (v -> v \ ef), m: MultiMap[k, v]): Option[v] \ ef`

Applies `f` to all values in `m` going from left to right until a single value `v` is obtained. Returns `Some(v)`.

That is, the result is of the form: `Some(f(...f(f(v1, v2), v3)..., vn))`

Returns `None` if `m` is the empty MultiMap.

`def reduceLeftWithKey(f: k -> (v -> (k -> (v -> (k, v) \ ef))), m: MultiMap[k, v]): Option[(k, v)] \ ef`

Applies `f` to all mappings in `m` going from left to right until a single mapping `(k, v)` is obtained. Returns `Some((k, v))`.

That is, the result is of the form: `Some(f(...f(f(k1, v1, k2, v2), k3, v3)..., kn, vn))`

Returns `None` if `m` is the empty MultiMap.

`def reduceRight(f: v -> (v -> v \ ef), m: MultiMap[k, v]): Option[v] \ ef`

Applies `f` to all values in `m` going from right to left until a single value `v` is obtained. Returns `Some(v)`.

That is, the result is of the form: `Some(f(v1, ...f(vn-2, f(vn-1, vn))...))`

Returns `None` if `m` is the empty MultiMap.

`def reduceRightWithKey(f: k -> (v -> (k -> (v -> (k, v) \ ef))), m: MultiMap[k, v]): Option[(k, v)] \ ef`

Applies `f` to all mappings in `m` going from right to left until a single mapping `(k, v)` is obtained. Returns `Some((k, v))`.

That is, the result is of the form: `Some(f(k1, v1, ...f(kn-2, vn-2, f(kn-1, vn-1, kn, vn))...))`

Returns `None` if `m` is the empty MultiMap.

`def remove(k: k, m: MultiMap[k, v]): MultiMap[k, v] with Order[k]`

Removes the mapping `k` from the MultiMap `m`.

`def removeWithValue(k: k, v: v, m: MultiMap[k, v]): MultiMap[k, v] with Order[k], Order[v]`

Removes the mapping of `(k, v)` from the MultiMap `m` it it exists.

`def singleton(k: k, v: v): MultiMap[k, v] with Order[k], Order[v]`

Returns the singleton MultiMap where key `k` is mapped to value `v`.

`def sumKeys(m: MultiMap[Int32, v]): Int32`

Returns the sum of all keys in the MultiMap `m`.

`def sumValues(m: MultiMap[k, Int32]): Int32`

Returns the sum of all values in the MultiMap `m`.

`@ParallelWhenPure `
`def sumWith(f: k -> (v -> Int32 \ ef), m: MultiMap[k, v]): Int32 \ ef`

Returns the sum of all key-value pairs `k => v` in the MultiMap `m` according to the function `f`.

Purity reflective: Runs in parallel when given a pure function `f`.

`def toAscList(m: MultiMap[k, v]): List[(k, v)]`

Returns the MultiMap `m` as a list of singleton key-value pairs in ascending order.

`def toAssocList(m: MultiMap[k, v]): List[(k, Set[v])]`

Returns the MultiMap `m` as a list of `(key, Set[value])` pairs in ascending order.

`def toDescList(m: MultiMap[k, v]): List[(k, v)]`

Returns the MultiMap `m` as a list of singleton key-value pairs in descending order.

`def toList(m: MultiMap[k, v]): List[(k, v)]`

Returns the MultiMap `m` as a list of singleton key-value pairs.

`def toMap(m: MultiMap[k, v]): Map[k, Set[v]]`

Returns the MultiMap `m` as a list of singleton key-value pairs in ascending order.

`def toMutDeque(rc: Region[r], m: MultiMap[k, v]): MutDeque[(k, Set[v]), r] \ r`

Returns the MultiMap `m` as a list of singleton key-value pairs in ascending order.

`def toString(m: MultiMap[k, v]): String with ToString[k], ToString[v]`

Returns a string representation of `m`.

`def union(m1: MultiMap[k, v], m2: MultiMap[k, v]): MultiMap[k, v] with Order[k], Order[v]`

Returns the union of `m1` and `m2`.

`def update(f: v -> Option[v] \ ef, k: k, m: MultiMap[k, v]): MultiMap[k, v] \ ef with Order[k], Order[v]`

Updates `m` with `k => v1` if `k => v` is in `m` and `f(v) = Some(v1)`. Otherwise, returns `m`.

`def updateWithKey(f: k -> (v -> Option[v] \ ef), k: k, m: MultiMap[k, v]): MultiMap[k, v] \ ef with Order[k], Order[v]`

Updates `m` with `k => v1` if `k => v` is in `m` and `f(k, v) = Some(v1)`. Otherwise, returns `m`.

`def valuesOf(m: MultiMap[k, v]): List[v] with Order[k]`

Returns the values of `m`.

Answer may contain duplicates where values were ascribed to multiple keys.