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) =
  red_black_tree_map.Map(k, v)

Values

pub fn count(map: red_black_tree_map.Map(k, v)) -> Int

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

pub fn delete(
  from map: red_black_tree_map.Map(k, v),
  this key: k,
) -> red_black_tree_map.Map(k, v)

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

pub fn filter(
  in map: red_black_tree_map.Map(k, v),
  for property: fn(k, v) -> Bool,
) -> red_black_tree_map.Map(k, v)

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: red_black_tree_map.Map(k, v),
  from initial: a,
  with reducer: fn(a, k, v) -> a,
) -> a

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(#(k, v)),
  compare: fn(k, k) -> order.Order,
) -> red_black_tree_map.Map(k, v)

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

pub fn get(
  in map: red_black_tree_map.Map(k, v),
  key key: k,
) -> Result(v, Nil)

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

pub fn has_key(
  in map: red_black_tree_map.Map(k, v),
  key key: k,
) -> Bool

Checks if the map contains a given key.

pub fn insert(
  into map: red_black_tree_map.Map(k, v),
  key key: k,
  value value: v,
) -> red_black_tree_map.Map(k, v)

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

pub fn merge(
  intro dict: red_black_tree_map.Map(k, v),
  from new_entries: red_black_tree_map.Map(k, v),
) -> red_black_tree_map.Map(k, v)

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

pub fn new(
  compare: fn(k, k) -> order.Order,
) -> red_black_tree_map.Map(k, v)

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

pub fn take(
  from map: red_black_tree_map.Map(k, v),
  keeping desired: List(k),
) -> red_black_tree_map.Map(k, v)

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: red_black_tree_map.Map(k, v),
) -> List(#(k, v))

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

Search Document