Skip to content

Caleb-o/slabs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slabs

Package Version Hex Docs Erlang-compatible JavaScript-compatible

This slab library in Gleam offers a robust data structure designed for managing collections of entries with immutability in mind. Each entry can be either vacant or occupied, allowing for efficient insertion and removal operations while ensuring safe access through well-defined functions. This library is ideal for scenarios where you need a reliable and performant way to handle dynamic collections without compromising on immutability.

gleam add slabs@1
import slabs

import gleam/int
import gleam/io

pub fn main() -> Nil {
  // [Occupied(45), Occupied(30), Occupied(20)], [0, 1, 2]
  let #(slab, _) =
    slabs.new()
    |> slabs.insert_many([20, 30, 45])

  // [Occupied(45), Occupied(30), Vacant(None)], 20
  let assert Ok(#(slab, _)) = slabs.remove(slab, 0)

  // [Occupied(45), Occupied(30), Occupied(15)], 0
  let #(slab, index) =
    slab
    |> slabs.insert(15)

  let assert option.Some(value) = slabs.get(slab, index)

  // 15
  value
  |> int.to_string
  |> io.println
}

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

About

Immutable slabs in Gleam!

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages