Module MMultiMap

This module is undocumented. This is a list of its definitions.

add :: MMap.T a (MList.T b) -> a -> b -> <Proc> ()

add m k v adds the value v to the values associated with the key k.

The bucket for k does not have to exist: the first add for a key creates a new MList for it, later ones append to that list. There is no separate "create bucket" step.

Example:

> m = MMap.create () :: <Proc> MMultiMap.T String Integer
> MMultiMap.add m "a" 1
> MMultiMap.add m "a" 2
> MMultiMap.get m "a"
[1, 2]
get :: MMap.T a (MList.T b) -> a -> <Proc> [b]

The values associated with the given key, or an empty list if the map contains no such key.

The returned list is not a copy. It is MList.freeze of the very bucket the map still holds, so it aliases live mutable state: a later add for the same key mutates a list the caller already received as an immutable [b]. A list value silently changing length under the caller's feet breaks every assumption immutable lists normally carry.

Use the result immediately, or copy it (for example with MList.toList on the bucket, or by forcing a copy of the returned list) before storing it anywhere or handing it on. An empty result is safe: it is a fresh [], not a bucket.

Example:

> m = MMultiMap.indexBy (\s -> length s) ["a", "bb", "cc"]
> xs = MMultiMap.get m 2
> xs
["bb", "cc"]
> MMultiMap.add m 2 "dd"
> xs
["bb", "cc", "dd"]
indexBy :: (a -> <c> b) -> [a] -> <Proc,c> MMap.T b (MList.T a)

Creates a new multimap that associates each value of the given list with the key computed for it by the given function.

This is a group-by: the key is derived from the value, and values sharing a key end up in the same bucket in the order they appeared in the input list.

Example:

> m = MMultiMap.indexBy (\s -> length s) ["a", "bb", "cc"]
> sort (MMap.keys m)
[1, 2]
> MMultiMap.get m 2
["bb", "cc"]
> MMultiMap.get m 5
[]