gleam/map

Types

Map

A dictionary of keys and values.

Any type can be used for the keys and values of a map, 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 map once.

Maps 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 external type Map(key, value)

Functions

delete

pub fn delete(from map: Map(a, b), delete key: a) -> Map(a, b)

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

Examples

> delete([tuple("a", 0), tuple("b", 1)], "a")
from_list([tuple("b", 1)])

> delete([tuple("a", 0), tuple("b", 1)], "c")
from_list([tuple("a", 0), tuple("b", 1)])

drop

pub fn drop(
  from map: Map(a, b),
  drop disallowed_keys: List(a),
) -> Map(a, b)

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

Examples

> drop([tuple("a", 0), tuple("b", 1)], ["a"])
from_list([tuple("b", 2)])

> delete([tuple("a", 0), tuple("b", 1)], ["c"])
from_list([tuple("a", 0), tuple("b", 1)])

> drop([tuple("a", 0), tuple("b", 1)], ["a", "b", "c"])
from_list([])

filter

pub fn filter(
  in map: Map(a, b),
  for property: fn(a, b) -> Bool,
) -> Map(a, b)

Create a new map from a given map, minus any entries that a given function returns False for.

Examples

> from_list([tuple("a", 0), tuple("b", 1)])
> |> filter(fn(key, value) { value != 0 })
from_list([tuple("b", 1)])

> from_list([tuple("a", 0), tuple("b", 1)])
> |> filter(fn(key, value) { True })
from_list([tuple("a", 0), tuple("b", 1)])

fold

pub fn fold(
  over map: Map(a, b),
  from initial: c,
  with fun: fn(a, b, c) -> c,
) -> c

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

Maps 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 map = from_list([tuple("a", 1), tuple("b", 3), tuple("c", 9)])
> fold(map, 0, fn(key, value, accumulator) { accumulator + value })
13

> import gleam/string.{append}
> fold(map, "", fn(key, value, accumulator) { append(accumulator, value) })
"abc"

from_list

pub external fn from_list(
  List(tuple(key, value)),
) -> Map(key, value)

Convert a list of 2-element tuples tuple(key, value) to a map.

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

get

pub external fn get(
  from: Map(key, value),
  get: key,
) -> Result(value, Nil)

Fetch a value from a map for a given key.

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

Examples

> new() |> insert("a", 0) |> get("a")
Ok(0)

> new() |> insert("a", 0) |> get("b")
Error(Nil)

has_key

pub fn has_key(map: Map(a, b), key: a) -> Bool

Determind whether or not a value present in the map for a given key.

Examples

> new() |> insert("a", 0) |> has_key("a")
True

> new() |> insert("a", 0) |> has_key("b")
False

insert

pub fn insert(
  into map: Map(a, b),
  for key: a,
  insert value: b,
) -> Map(a, b)

Insert a value into the map with the given key.

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

Examples

> new() |> insert("a", 0) |> to_list
[tuple("a", 0)]

> new() |> insert("a", 0) |> insert("a", 5) |> to_list
[tuple("a", 5)]

keys

pub external fn keys(Map(keys, v)) -> List(keys)

Get a list of all keys in a given map.

Maps 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

> keys([tuple("a", 0), tuple("b", 1)])
["a", "b"]

map_values

pub fn map_values(
  in map: Map(a, b),
  with fun: fn(a, b) -> c,
) -> Map(a, c)

Update all values in a given map by calling a given function on each key and value.

Examples

> [tuple(3, 3), tuple(2, 4)]
> |> from_list
> |> map_values(fn(key, value) { key * value })
[tuple(3, 9), tuple(2, 8)]

merge

pub external fn merge(
  into: Map(k, v),
  merge: Map(k, v),
) -> Map(k, v)

Create a new map from a pair of given maps by combining their entries.

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

Examples

> let a = from_list([tuple("a", 0), tuple("b", 1)])
> let b = from_list([tuple("b", 2), tuple("c", 3)])
> merge(a, b)
from_list([tuple("a", 0), tuple("b", 2), tuple("c", 3)])

new

pub external fn new() -> Map(key, value)

Create a fresh map that contains no values.

size

pub external fn size(Map(k, v)) -> Int

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

Examples

> new() |> size()
0

> new() |> insert("key", "value") |> size()
1

take

pub fn take(
  from map: Map(a, b),
  keeping desired_keys: List(a),
) -> Map(a, b)

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

Examples

> from_list([tuple("a", 0), tuple("b", 1)])
> |> take(["b"])
from_list([tuple("b", 1)])

> from_list([tuple("a", 0), tuple("b", 1)])
> |> take(["a", "b", "c"])
from_list([tuple("a", 0), tuple("b", 1)])

to_list

pub external fn to_list(
  Map(key, value),
) -> List(tuple(key, value))

Convert the map to a list of 2-element tuples tuple(key, value), one for each key-value pair in the map.

The tuples in the list have no specific order.

Examples

> new() |> to_list()
[]

> new() |> insert("key", 0) |> to_list()
[tuple("key", 0)]

update

pub fn update(
  in map: Map(a, b),
  update key: a,
  with fun: fn(Result(b, Nil)) -> b,
) -> Map(a, b)

Create a new map with one entry updated using a given function.

If there was not an entry in the map for the given key then the function gets Error(Nil) as its argument, otherwise it gets Ok(value).

Example

> let increment = fn(x) {
>   case x {
>     Ok(i) -> i + 1
>     Error(Nil) -> 0
>   }
> }
> let map = from_list([tuple("a", 0)])
>
> update(map, "a" increment)
from_list([tuple("a", 1)])

> update(map, "b" increment)
from_list([tuple("a", 0), tuple("b", 0)])

values

pub external fn values(Map(k, values)) -> List(values)

Get a list of all values in a given map.

Maps 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

> keys(from_list([tuple("a", 0), tuple("b", 1)]))
[0, 1]