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 the value
fn(_key, _right, _keep, discard) { discard() }
Keep and insert the value.
fn(_key, right, keep, _discard) { keep(right) }
Keep the value and negate it, effectively treating the left-side as 0.
fn(_key, right, keep, _discard) { keep(-right) }
A strategy which always fail, indicating that values should always be present
on the left dictionary.
fn(_key, _right, _keep, _discard) { fail }
Union Strategies
Combine values by taking their difference.
fn(_key, left, right, keep, _discard) { keep(left - right) }
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()
}
}
A strategy which always fail, enforcing the dict contains no duplicate.
fn(_, _, _, _, _) {
fail @"unexpected duplicate key found in dict."
}
Combine values by keeping the values present in the left-most dict.
fn(_key, left, _right, keep, _discard) { keep(left) }
Combine values by keeping the values present in the right-most dict.
fn(_key, _left, right, keep, _discard) { keep(right) }
Combine values by taking their sum.
fn(_key, left, right, keep, _discard) { keep(left + right) }
Discard the value
fn(_key, _right, _keep, discard) { discard() }
Keep and insert the value.
fn(_key, right, keep, _discard) { keep(right) }
Keep the value and negate it, effectively treating the left-side as 0.
fn(_key, right, keep, _discard) { keep(-right) }
A strategy which always fail, indicating that values should always be present on the left dictionary.
fn(_key, _right, _keep, _discard) { fail }
Combine values by taking their difference.
fn(_key, left, right, keep, _discard) { keep(left - right) }
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()
}
}
A strategy which always fail, enforcing the dict contains no duplicate.
fn(_, _, _, _, _) {
fail @"unexpected duplicate key found in dict."
}
Combine values by keeping the values present in the left-most dict.
fn(_key, left, _right, keep, _discard) { keep(left) }
Combine values by keeping the values present in the right-most dict.
fn(_key, _left, right, keep, _discard) { keep(right) }
Combine values by taking their sum.
fn(_key, left, right, keep, _discard) { keep(left + right) }