# AssocList module

Association Lists, a la functional programming, in Motoko.

import List "list";

public type AssocList<K,V> = List.List<(K,V)>;

## find

find the value associated with a given key, or null if absent.

public func find<K,V>(al : AssocList<K,V>,
k:K,
k_eq:(K,K)->Bool)
: ?V

## replace

replace the value associated with a given key, or add it, if missing. returns old value, or null, if no prior value existed.

public func replace<K,V>(al : AssocList<K,V>,
k:K,
k_eq:(K,K)->Bool,
ov: ?V)
: (AssocList<K,V>, ?V)

## diff

The key-value pairs of the final list consist of those pairs of the left list whose keys are not present in the right list; the values of the right list are irrelevant.

public func diff<K,V,W>(al1: AssocList<K,V>,
al2: AssocList<K,W>,
keq: (K,K)->Bool)
: AssocList<K,V>

## mapAppend

public func mapAppend<K,V,W,X>(al1:AssocList<K,V>,
al2:AssocList<K,W>,
vbin:(?V,?W)->X)
: AssocList<K,X>

## disj

This operation generalizes the notion of “set union” to finite maps. Produces a “disjunctive image” of the two lists, where the values of matching keys are combined with the given binary operator.

For unmatched key-value pairs, the operator is still applied to create the value in the image. To accommodate these various situations, the operator accepts optional values, but is never applied to (null, null).

public func disj<K,V,W,X>(al1:AssocList<K,V>,
al2:AssocList<K,W>,
keq:(K,K)->Bool,
vbin:(?V,?W)->X)
: AssocList<K,X>

## join

This operation generalizes the notion of “set intersection” to finite maps. Produces a “conjuctive image” of the two lists, where the values of matching keys are combined with the given binary operator, and unmatched key-value pairs are not present in the output.

public func join<K,V,W,X>(al1 : AssocList<K,V>,
al2:AssocList<K,W>,
keq:(K,K)->Bool,
vbin:(V,W)->X)
: AssocList<K,X>

## fold

This fold produces the list content, but where the list’s nil and cons constructors are replaced with the given value and function, respectively (nil and cons arguments).

public func fold<K,V,X>(al:AssocList<K,V>,
nil:X,
cons:(K,V,X)->X)
: X