lazy_const

Package Version Hex Docs

A simple library wrapping persistent terms and Maps to provide lazily initialised constants to Gleam. Please make sure you have read the documentation for your platform implementation and the notes on top of the module docs docs to understand the performance implications. Always measure to make sure using this library makes a difference.

gleam add lazy_const@1
import lazy_const
import gleam/regex

pub fn main() {
  // regex is constructed and returned here
  let re: Regex = alphanum_re()

  // ... do some work ...

  // calling the function again returns the same Regex without recompiling
  let re: Regex = alphanum_re()

  // ... more very important alphanum-matching ensues ...
}

fn alphanum_re() -> regex.Regex {
  use <- lazy_const.new(lazy_const.defined_in(alphanum_re))
  let assert Ok(re) = regex.from_string("[a-zA-Z0-9]+")
  re
}

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

Search Document