gleamy/map

This module provides an implementation of an ordered map data structure based on red-black trees. It associates keys of type k with values of type v. Keys are ordered by the comparison function.

Types

The Map(k, v) type represents a map that associates keys of type k with values of type v.

pub type Map(k, v) =
  tree.Map(k, v)

Functions

pub fn count(map: Map(a, b)) -> Int

Returns the number of key-value pairs in the map. Time complexity: O(n)

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

Removes a key-value pair from the map, if the key exists.

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

Creates a new map containing only the key-value pairs from the original map that satisfy a given predicate function.

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

Applies a function to every key-value pair in the map, accumulating the results with the provided initial accumulator value.

pub fn from_list(
  members: List(#(a, b)),
  compare: fn(a, a) -> Order,
) -> Map(a, b)

Creates a new map from a list of key-value pairs and a comparison function for keys.

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

Get the value associated with a given key in the map, if present.

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

Checks if the map contains a given key.

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

Inserts a new key-value pair into the map, overwriting the value if the key already exists.

pub fn merge(
  intro dict: Map(a, b),
  from new_entries: Map(a, b),
) -> Map(a, b)

Merges two maps into a new map, keeping values from the second map if keys collide.

pub fn new(compare: fn(a, a) -> Order) -> Map(a, b)

Creates a new empty map with the provided comparison function for keys.

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

Creates a new map containing only the key-value pairs from the original map where the keys are present in the given list of desired keys.

pub fn to_list(map: Map(a, b)) -> List(#(a, b))

Converts the map to a list of key-value pairs.

Search Document