gleam/dict

Types

A dictionary of keys and values.

Any type can be used for the keys and values of a dict, but all the keys must be of the same type and all the values must be of the same type.

Each key can only be present in a dict once.

Dicts are not ordered in any way, and any unintentional ordering is not to be relied upon in your code as it may change in future versions of Erlang or Gleam.

See the Erlang map module for more information.

pub type Dict(key, value)

Values

pub fn combine(
  dict: Dict(k, v),
  other: Dict(k, v),
  with fun: fn(v, v) -> v,
) -> Dict(k, v)

Creates a new dict from a pair of given dicts by combining their entries.

If there are entries with the same keys in both dicts the given function is used to determine the new value to use in the resulting dict.

Examples

let a = from_list([#("a", 0), #("b", 1)])
let b = from_list([#("a", 2), #("c", 3)])
assert combine(a, b, fn(one, other) { one + other })
  == from_list([#("a", 2), #("b", 1), #("c", 3)])
pub fn delete(from dict: Dict(k, v), delete key: k) -> Dict(k, v)

Creates a new dict from a given dict with all the same entries except for the one with a given key, if it exists.

Examples

assert from_list([#("a", 0), #("b", 1)]) |> delete("a")
  == from_list([#("b", 1)])
assert from_list([#("a", 0), #("b", 1)]) |> delete("c")
  == from_list([#("a", 0), #("b", 1)])
pub fn drop(
  from dict: Dict(k, v),
  drop disallowed_keys: List(k),
) -> Dict(k, v)

Creates a new dict from a given dict with all the same entries except any with keys found in a given list.

Examples

assert from_list([#("a", 0), #("b", 1)]) |> drop(["a"])
  == from_list([#("b", 1)])
assert from_list([#("a", 0), #("b", 1)]) |> drop(["c"])
  == from_list([#("a", 0), #("b", 1)])
assert from_list([#("a", 0), #("b", 1)]) |> drop(["a", "b", "c"])
  == from_list([])
pub fn each(dict: Dict(k, v), fun: fn(k, v) -> a) -> Nil

Calls a function for each key and value in a dict, discarding the return value.

Useful for producing a side effect for every item of a dict.

import gleam/io

let dict = from_list([#("a", "apple"), #("b", "banana"), #("c", "cherry")])

assert
  each(dict, fn(k, v) {
    io.println(k <> " => " <> v)
  })
  == Nil
// a => apple
// b => banana
// c => cherry

The order of elements in the iteration is an implementation detail that should not be relied upon.

pub fn filter(
  in dict: Dict(k, v),
  keeping predicate: fn(k, v) -> Bool,
) -> Dict(k, v)

Creates a new dict from a given dict, minus any entries that a given function returns False for.

Examples

assert from_list([#("a", 0), #("b", 1)])
  |> filter(fn(key, value) { value != 0 })
  == from_list([#("b", 1)])
assert from_list([#("a", 0), #("b", 1)])
  |> filter(fn(key, value) { True })
  == from_list([#("a", 0), #("b", 1)])
pub fn fold(
  over dict: Dict(k, v),
  from initial: acc,
  with fun: fn(acc, k, v) -> acc,
) -> acc

Combines all entries into a single value by calling a given function on each one.

Dicts are not ordered so the values are not returned in any specific order. Do not write code that relies on the order entries are used by this function as it may change in later versions of Gleam or Erlang.

Examples

let dict = from_list([#("a", 1), #("b", 3), #("c", 9)])
assert fold(dict, 0, fn(accumulator, key, value) { accumulator + value })
  == 13
import gleam/string

let dict = from_list([#("a", 1), #("b", 3), #("c", 9)])
assert
  fold(dict, "", fn(accumulator, key, value) {
    string.append(accumulator, key)
  })
  == "abc"
pub fn from_list(list: List(#(k, v))) -> Dict(k, v)

Converts a list of 2-element tuples #(key, value) to a dict.

If two tuples have the same key the last one in the list will be the one that is present in the dict.

pub fn get(from: Dict(k, v), get: k) -> Result(v, Nil)

Fetches a value from a dict for a given key.

The dict may not have a value for the key, so the value is wrapped in a Result.

Examples

assert new() |> insert("a", 0) |> get("a") == Ok(0)
assert new() |> insert("a", 0) |> get("b") == Error(Nil)
pub fn has_key(dict: Dict(k, v), key: k) -> Bool

Determines whether or not a value is present in the dict for a given key.

Examples

assert new() |> insert("a", 0) |> has_key("a")
assert !{ new() |> insert("a", 0) |> has_key("b") }
pub fn insert(
  into dict: Dict(k, v),
  for key: k,
  insert value: v,
) -> Dict(k, v)

Inserts a value into the dict with the given key.

If the dict already has a value for the given key then the value is replaced with the new value.

Examples

assert new() |> insert("a", 0) == from_list([#("a", 0)])
assert new() |> insert("a", 0) |> insert("a", 5) == from_list([#("a", 5)])
pub fn is_empty(dict: Dict(k, v)) -> Bool

Determines whether or not the dict is empty.

Examples

assert new() |> is_empty
assert !{ new() |> insert("b", 1) |> is_empty }
pub fn keys(dict: Dict(k, v)) -> List(k)

Gets a list of all keys in a given dict.

Dicts are not ordered so the keys are not returned in any specific order. Do not write code that relies on the order keys are returned by this function as it may change in later versions of Gleam or Erlang.

Examples

assert from_list([#("a", 0), #("b", 1)]) |> keys == ["a", "b"]
pub fn map_values(
  in dict: Dict(k, v),
  with fun: fn(k, v) -> a,
) -> Dict(k, a)

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

Examples

assert from_list([#(3, 3), #(2, 4)])
  |> map_values(fn(key, value) { key * value })
  == from_list([#(3, 9), #(2, 8)])
pub fn merge(
  into dict: Dict(k, v),
  from new_entries: Dict(k, v),
) -> Dict(k, v)

Creates a new dict from a pair of given dicts by combining their entries.

If there are entries with the same keys in both dicts the entry from the second dict takes precedence.

Examples

let a = from_list([#("a", 0), #("b", 1)])
let b = from_list([#("b", 2), #("c", 3)])
assert merge(a, b) == from_list([#("a", 0), #("b", 2), #("c", 3)])
pub fn new() -> Dict(k, v)

Creates a fresh dict that contains no values.

pub fn size(dict: Dict(k, v)) -> Int

Determines the number of key-value pairs in the dict. This function runs in constant time and does not need to iterate the dict.

Examples

assert new() |> size == 0
assert new() |> insert("key", "value") |> size == 1
pub fn take(
  from dict: Dict(k, v),
  keeping desired_keys: List(k),
) -> Dict(k, v)

Creates a new dict from a given dict, only including any entries for which the keys are in a given list.

Examples

assert from_list([#("a", 0), #("b", 1)])
  |> take(["b"])
  == from_list([#("b", 1)])
assert from_list([#("a", 0), #("b", 1)])
  |> take(["a", "b", "c"])
  == from_list([#("a", 0), #("b", 1)])
pub fn to_list(dict: Dict(k, v)) -> List(#(k, v))

Converts the dict to a list of 2-element tuples #(key, value), one for each key-value pair in the dict.

The tuples in the list have no specific order.

Examples

Calling to_list on an empty dict returns an empty list.

assert new() |> to_list == []

The ordering of elements in the resulting list is an implementation detail that should not be relied upon.

assert new()
  |> insert("b", 1)
  |> insert("a", 0)
  |> insert("c", 2)
  |> to_list
  == [#("a", 0), #("b", 1), #("c", 2)]
pub fn upsert(
  in dict: Dict(k, v),
  update key: k,
  with fun: fn(option.Option(v)) -> v,
) -> Dict(k, v)

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

If there was not an entry in the dict for the given key then the function gets None as its argument, otherwise it gets Some(value).

Examples

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

assert upsert(dict, "a", increment) == from_list([#("a", 1)])
assert upsert(dict, "b", increment) == from_list([#("a", 0), #("b", 0)])
pub fn values(dict: Dict(k, v)) -> List(v)

Gets a list of all values in a given dict.

Dicts are not ordered so the values are not returned in any specific order. Do not write code that relies on the order values are returned by this function as it may change in later versions of Gleam or Erlang.

Examples

assert from_list([#("a", 0), #("b", 1)]) |> values == [0, 1]
Search Document