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 strategy for inserting a value from a dictionnary into another.

Alias

InsertStrategy<key, value> = fn(
    key,
    value,
    KeepValue<key, value>,
    DiscardValue<key, value>,
  ) ->
    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

Insert Strategies

discard() -> InsertStrategy<key, value>

Discard the value

fn(_key, _right, _keep, discard) { discard() }

keep() -> InsertStrategy<key, value>

Keep and insert the value.

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

negate() -> InsertStrategy<key, Int>

Keep the value and negate it, effectively treating the left-side as 0.

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

never() -> InsertStrategy<ByteArray, value>

A strategy which always fail, indicating that values should always be present on the left dictionary.

fn(_key, _right, _keep, _discard) { fail }

Union Strategies

difference() -> UnionStrategy<key, Int>

Combine values by taking their difference.

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

difference_if_non_zero() -> UnionStrategy<key, Int>

Keep the resulting difference only if it’s non zero; otherwise discard the key entirely.

fn(_key, left, right, keep, discard) {
  let value = left - right
  if value != 0 {
    keep(value)
  } else {
    discard()
  }
}

expect_no_duplicate() -> UnionStrategy<ByteArray, 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