bidict

Package Version Hex Docs

A bijective dictionary. Each key corresponds to one value, and each value corresponds to one key. To achieve this, a Bidict stores two one-way mappings: Bidict(a2b: Dict(a, b), b2a: Dict(b, a)). All functions properly update both forward and reverse mappings.

API is mostly the same as gleam/dict, though with a few differences:

Install

gleam add bidict

Usage

import bidict

pub fn main() {
 let elements: Bidict(String, String) =
   [
     #("Hydrogen", "H"),
     #("Carbon", "C"),
     #("Bromine", "Br"),
     #("Neodymium", "Nd"),
   ]
   |> bidict.from_list

 let assert Ok("Br") = bidict.get_a(elements, "Bromine")
 let assert Error(Nil) = bidict.get_a(elements, "Oxygen")

 let assert Ok("Carbon") = bidict.get_b(elements, "C")
 let assert Error(Nil) = bidict.get_b(elements, "Al")

 let assert True = bidict.has_a(elements, "Hydrogen")
 let assert False = bidict.has_b(elements, "He")

 let assert False =
   elements
   |> bidict.delete_a("Neodymium")
   |> bidict.has_b("Ne")

 let assert 3 =
   elements
   |> bidict.insert("Bromine", "Nd")
   |> bidict.size
}

Further documentation can be found at https://hexdocs.pm/bidict.

Implementations in Other Languages

Search Document