Skip to content

jonathanmfung/bidict

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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:

  • delete -> delete_a, delete_b
  • drop -> drop_a, drop_b
  • get -> get_a, get_b
  • has_key -> has_a, has_b
  • new map_keys
  • new map

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

About

Bijective Dictionary for Gleam

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages