qol_gleam/qol_dict

Values

pub fn map_keys(
  in dict: dict.Dict(k, v),
  with fun: fn(k, v) -> a,
) -> dict.Dict(a, List(v))

Updates all keys in a given dict by calling a given function on each key and value.

Examples

assert from_list([#(1, "a"), #(2, "b"), #(3, "c"), #(4, "d"), #(5, "e"), #(6, "f")])
  |> map_keys(fn(key, _value) { key % 3 })
  == from_list([#(0, ["f", "c"]), #(1, ["d", "a"]), #(2, ["e", "b"])])

Origin

This function is commonly expected from other programming language.

pub fn update(
  in dict: dict.Dict(k, v),
  update key: k,
  with fun: fn(v) -> v,
) -> dict.Dict(k, v)

Creates a new dict with one entry updated using a given function.

If there was not an entry in the dict for the given key then dict will not be changed.

Examples

let dict = from_list([#("a", 0)])
let increment = fn(x) {
  case x {
    Some(i) -> i + 1
    None -> 0
  }
}

assert update(dict, "a", increment) == from_list([#("a", 1)])
assert update(dict, "b", increment) == from_list([#("a", 0)])
Search Document