Packages
gleam_stdlib
0.35.0
1.0.3
1.0.2
1.0.1
1.0.0
0.71.0
0.70.0
0.69.0
0.68.1
0.68.0
0.67.1
0.67.0
0.65.0
0.64.0
0.63.2
0.63.1
0.63.0
0.62.1
0.62.0
0.61.0
0.60.0
0.59.0
0.58.0
0.57.0
0.56.0
0.55.0
0.54.0
0.53.0
0.52.0
0.51.0
0.50.0
0.49.0
0.48.0
0.47.0
0.46.0
0.45.0
0.44.0
0.43.0
0.42.0
0.41.0
0.40.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.1
0.35.0
0.34.0
0.33.1
0.33.0
0.32.1
0.32.0
0.31.0
0.30.2
0.30.1
0.30.0
0.29.2
0.29.1
0.29.0
0.28.2
0.28.1
0.28.0
0.27.0
0.26.1
0.26.0
0.25.0
0.24.0
0.23.0
0.22.3
0.22.2
0.22.1
0.22.0
0.21.0
0.20.0
0.19.3
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.18.0-rc1
0.17.1
0.17.0
0.16.0
0.15.0
0.14.0
0.13.0
0.12.0
0.11.0
0.10.1
0.10.0
0.9.0
0.8.0
0.7.0
0.6.0
0.5.0
0.4.0
0.4.0-rc1
0.3.1
0.3.0
0.2.0
retired
A standard library for the Gleam programming language
Current section
Files
Jump to
Current section
Files
src/gleam/dict.gleam
import gleam/option.{type Option}
/// 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](https://erlang.org/doc/man/maps.html) for more
/// information.
///
pub type Dict(key, value)
/// 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
///
/// ```gleam
/// new() |> size
/// // -> 0
/// ```
///
/// ```gleam
/// new() |> insert("key", "value") |> size
/// // -> 1
/// ```
///
@external(erlang, "maps", "size")
@external(javascript, "../gleam_stdlib.mjs", "map_size")
pub fn size(dict: Dict(k, v)) -> Int
/// 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
///
/// ```gleam
/// new()
/// // -> from_list([])
/// ```
///
/// ```gleam
/// new() |> insert("key", 0)
/// // -> from_list([#("key", 0)])
/// ```
///
@external(erlang, "maps", "to_list")
@external(javascript, "../gleam_stdlib.mjs", "map_to_list")
pub fn to_list(dict: Dict(key, value)) -> List(#(key, value))
/// 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.
///
@external(erlang, "maps", "from_list")
pub fn from_list(list: List(#(k, v))) -> Dict(k, v) {
fold_list_of_pair(list, new())
}
fn fold_list_of_pair(
over list: List(#(k, v)),
from initial: Dict(k, v),
) -> Dict(k, v) {
case list {
[] -> initial
[x, ..rest] -> fold_list_of_pair(rest, insert(initial, x.0, x.1))
}
}
/// Determines whether or not a value present in the dict for a given key.
///
/// ## Examples
///
/// ```gleam
/// new() |> insert("a", 0) |> has_key("a")
/// // -> True
/// ```
///
/// ```gleam
/// new() |> insert("a", 0) |> has_key("b")
/// // -> False
/// ```
///
pub fn has_key(dict: Dict(k, v), key: k) -> Bool {
do_has_key(key, dict)
}
@external(erlang, "maps", "is_key")
fn do_has_key(key: k, dict: Dict(k, v)) -> Bool {
get(dict, key) != Error(Nil)
}
/// Creates a fresh dict that contains no values.
///
pub fn new() -> Dict(key, value) {
do_new()
}
@external(erlang, "maps", "new")
@external(javascript, "../gleam_stdlib.mjs", "new_map")
fn do_new() -> Dict(key, value)
/// 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
///
/// ```gleam
/// new() |> insert("a", 0) |> get("a")
/// // -> Ok(0)
/// ```
///
/// ```gleam
/// new() |> insert("a", 0) |> get("b")
/// // -> Error(Nil)
/// ```
///
pub fn get(from: Dict(key, value), get: key) -> Result(value, Nil) {
do_get(from, get)
}
@external(erlang, "gleam_stdlib", "map_get")
@external(javascript, "../gleam_stdlib.mjs", "map_get")
fn do_get(a: Dict(key, value), b: key) -> Result(value, Nil)
/// 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
///
/// ```gleam
/// new() |> insert("a", 0)
/// // -> from_list([#("a", 0)])
/// ```
///
/// ```gleam
/// new() |> insert("a", 0) |> insert("a", 5)
/// // -> from_list([#("a", 5)])
/// ```
///
pub fn insert(
into dict: Dict(k, v),
for key: k,
insert value: v,
) -> Dict(k, v) {
do_insert(key, value, dict)
}
@external(erlang, "maps", "put")
@external(javascript, "../gleam_stdlib.mjs", "map_insert")
fn do_insert(a: key, b: value, c: Dict(key, value)) -> Dict(key, value)
/// Updates all values in a given dict by calling a given function on each key
/// and value.
///
/// ## Examples
///
/// ```gleam
/// from_list([#(3, 3), #(2, 4)])
/// |> map_values(fn(key, value) { key * value })
/// // -> from_list([#(3, 9), #(2, 8)])
/// ```
///
pub fn map_values(in dict: Dict(k, v), with fun: fn(k, v) -> w) -> Dict(k, w) {
do_map_values(fun, dict)
}
@external(erlang, "maps", "map")
fn do_map_values(f: fn(key, value) -> b, dict: Dict(key, value)) -> Dict(key, b) {
let f = fn(dict, k, v) { insert(dict, k, f(k, v)) }
dict
|> fold(from: new(), with: f)
}
/// 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
///
/// ```gleam
/// from_list([#("a", 0), #("b", 1)]) |> keys
/// // -> ["a", "b"]
/// ```
///
pub fn keys(dict: Dict(keys, v)) -> List(keys) {
do_keys(dict)
}
@external(erlang, "maps", "keys")
fn do_keys(dict: Dict(k, v)) -> List(k) {
let list_of_pairs = to_list(dict)
do_keys_acc(list_of_pairs, [])
}
fn reverse_and_concat(remaining, accumulator) {
case remaining {
[] -> accumulator
[item, ..rest] -> reverse_and_concat(rest, [item, ..accumulator])
}
}
fn do_keys_acc(list: List(#(k, v)), acc: List(k)) -> List(k) {
case list {
[] -> reverse_and_concat(acc, [])
[x, ..xs] -> do_keys_acc(xs, [x.0, ..acc])
}
}
/// 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
///
/// ```gleam
/// from_list([#("a", 0), #("b", 1)]) |> values
/// // -> [0, 1]
/// ```
///
pub fn values(dict: Dict(k, values)) -> List(values) {
do_values(dict)
}
@external(erlang, "maps", "values")
fn do_values(dict: Dict(k, v)) -> List(v) {
let list_of_pairs = to_list(dict)
do_values_acc(list_of_pairs, [])
}
fn do_values_acc(list: List(#(k, v)), acc: List(v)) -> List(v) {
case list {
[] -> reverse_and_concat(acc, [])
[x, ..xs] -> do_values_acc(xs, [x.1, ..acc])
}
}
/// Creates a new dict from a given dict, minus any entries that a given function
/// returns `False` for.
///
/// ## Examples
///
/// ```gleam
/// from_list([#("a", 0), #("b", 1)])
/// |> filter(fn(key, value) { value != 0 })
/// // -> from_list([#("b", 1)])
/// ```
///
/// ```gleam
/// from_list([#("a", 0), #("b", 1)])
/// |> filter(fn(key, value) { True })
/// // -> from_list([#("a", 0), #("b", 1)])
/// ```
///
pub fn filter(
in dict: Dict(k, v),
keeping predicate: fn(k, v) -> Bool,
) -> Dict(k, v) {
do_filter(predicate, dict)
}
@external(erlang, "maps", "filter")
fn do_filter(f: fn(key, value) -> Bool, dict: Dict(key, value)) -> Dict(
key,
value,
) {
let insert = fn(dict, k, v) {
case f(k, v) {
True -> insert(dict, k, v)
_ -> dict
}
}
dict
|> fold(from: new(), with: insert)
}
/// Creates a new dict from a given dict, only including any entries for which the
/// keys are in a given list.
///
/// ## Examples
///
/// ```gleam
/// from_list([#("a", 0), #("b", 1)])
/// |> take(["b"])
/// // -> from_list([#("b", 1)])
/// ```
///
/// ```gleam
/// from_list([#("a", 0), #("b", 1)])
/// |> take(["a", "b", "c"])
/// // -> from_list([#("a", 0), #("b", 1)])
/// ```
///
pub fn take(
from dict: Dict(k, v),
keeping desired_keys: List(k),
) -> Dict(k, v) {
do_take(desired_keys, dict)
}
@external(erlang, "maps", "with")
fn do_take(desired_keys: List(k), dict: Dict(k, v)) -> Dict(k, v) {
insert_taken(dict, desired_keys, new())
}
fn insert_taken(
dict: Dict(k, v),
desired_keys: List(k),
acc: Dict(k, v),
) -> Dict(k, v) {
let insert = fn(taken, key) {
case get(dict, key) {
Ok(value) -> insert(taken, key, value)
_ -> taken
}
}
case desired_keys {
[] -> acc
[x, ..xs] -> insert_taken(dict, xs, insert(acc, x))
}
}
/// 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
///
/// ```gleam
/// let a = from_list([#("a", 0), #("b", 1)])
/// let b = from_list([#("b", 2), #("c", 3)])
/// merge(a, b)
/// // -> from_list([#("a", 0), #("b", 2), #("c", 3)])
/// ```
///
pub fn merge(
into dict: Dict(k, v),
from new_entries: Dict(k, v),
) -> Dict(k, v) {
do_merge(dict, new_entries)
}
@external(erlang, "maps", "merge")
fn do_merge(dict: Dict(k, v), new_entries: Dict(k, v)) -> Dict(k, v) {
new_entries
|> to_list
|> fold_inserts(dict)
}
fn insert_pair(dict: Dict(k, v), pair: #(k, v)) -> Dict(k, v) {
insert(dict, pair.0, pair.1)
}
fn fold_inserts(new_entries: List(#(k, v)), dict: Dict(k, v)) -> Dict(k, v) {
case new_entries {
[] -> dict
[x, ..xs] -> fold_inserts(xs, insert_pair(dict, x))
}
}
/// 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
///
/// ```gleam
/// from_list([#("a", 0), #("b", 1)]) |> delete("a")
/// // -> from_list([#("b", 1)])
/// ```
///
/// ```gleam
/// from_list([#("a", 0), #("b", 1)]) |> delete("c")
/// // -> from_list([#("a", 0), #("b", 1)])
/// ```
///
pub fn delete(from dict: Dict(k, v), delete key: k) -> Dict(k, v) {
do_delete(key, dict)
}
@external(erlang, "maps", "remove")
@external(javascript, "../gleam_stdlib.mjs", "map_remove")
fn do_delete(a: k, b: Dict(k, v)) -> 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
///
/// ```gleam
/// from_list([#("a", 0), #("b", 1)]) |> drop(["a"])
/// // -> from_list([#("b", 2)])
/// ```
///
/// ```gleam
/// from_list([#("a", 0), #("b", 1)]) |> drop(["c"])
/// // -> from_list([#("a", 0), #("b", 1)])
/// ```
///
/// ```gleam
/// from_list([#("a", 0), #("b", 1)]) |> drop(["a", "b", "c"])
/// // -> from_list([])
/// ```
///
pub fn drop(
from dict: Dict(k, v),
drop disallowed_keys: List(k),
) -> Dict(k, v) {
case disallowed_keys {
[] -> dict
[x, ..xs] -> drop(delete(dict, x), xs)
}
}
/// 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 the function
/// gets `None` as its argument, otherwise it gets `Some(value)`.
///
/// ## Example
///
/// ```gleam
/// let dict = from_list([#("a", 0)])
/// let increment = fn(x) {
/// case x {
/// Some(i) -> i + 1
/// None -> 0
/// }
/// }
///
/// update(dict, "a", increment)
/// // -> from_list([#("a", 1)])
///
/// update(dict, "b", increment)
/// // -> from_list([#("a", 0), #("b", 0)])
/// ```
///
pub fn update(
in dict: Dict(k, v),
update key: k,
with fun: fn(Option(v)) -> v,
) -> Dict(k, v) {
dict
|> get(key)
|> option.from_result
|> fun
|> insert(dict, key, _)
}
fn do_fold(
list: List(#(k, v)),
initial: acc,
fun: fn(acc, k, v) -> acc,
) -> acc {
case list {
[] -> initial
[#(k, v), ..rest] -> do_fold(rest, fun(initial, k, v), fun)
}
}
/// 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
///
/// ```gleam
/// let dict = from_list([#("a", 1), #("b", 3), #("c", 9)])
/// fold(dict, 0, fn(accumulator, key, value) { accumulator + value })
/// // -> 13
/// ```
///
/// ```gleam
/// import gleam/string
///
/// let dict = from_list([#("a", 1), #("b", 3), #("c", 9)])
/// fold(dict, "", fn(accumulator, key, value) {
/// string.append(accumulator, key)
/// })
/// // -> "abc"
/// ```
///
pub fn fold(
over dict: Dict(k, v),
from initial: acc,
with fun: fn(acc, k, v) -> acc,
) -> acc {
dict
|> to_list
|> do_fold(initial, fun)
}