aiken/collection/dict/strategy

This module contains strategies used to transform Dict. You can safely ignore this module if you aren’t using union_with nor insert_with.

Types

A callback to discard a value at a given key

Alias

DiscardValue<key, value> = fn() -> Pairs<key, value>

A callback to keep a combined value at a given key

Alias

KeepValue<key, value> = fn(value) -> Pairs<key, value>

A strategy for combining two values in a dictionnary that belong to the same key.

Alias

UnionStrategy<key, value> = fn(
    key,
    value,
    value,
    KeepValue<key, value>,
    DiscardValue<key, value>,
  ) ->
    Pairs<key, value>

Functions

Union Strategies

expect_no_duplicate() -> UnionStrategy<key, value>

A strategy which always fail, enforcing the dict contains no duplicate.

fn(_, _, _, _, _) {
  fail @"unexpected duplicate key found in dict."
}

keep_left() -> UnionStrategy<key, value>

Combine values by keeping the values present in the left-most dict.

fn(_key, left, _right, keep, _discard) { keep(left) }

keep_right() -> UnionStrategy<key, value>

Combine values by keeping the values present in the right-most dict.

fn(_key, _left, right, keep, _discard) { keep(right) }

sum() -> UnionStrategy<key, Int>

Combine values by taking their sum.

fn(_key, left, right, keep, _discard) { keep(left + right) }

sum_if_non_zero() -> UnionStrategy<key, Int>

Combine values by taking their sum, only if it is non-null. If the resulting sum is 0, discard the key/value entirely.

fn(_key, left, right, keep, discard) {
  let value = left + right
  if value == 0 {
    discard()
  } else {
    keep(value)
  }
}
Search Document