README.md

# lazy_const

[![Package Version](https://img.shields.io/hexpm/v/lazy_const)](https://hex.pm/packages/lazy_const)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/lazy_const/)

A simple library wrapping [persistent terms](https://www.erlang.org/doc/apps/erts/persistent_term.html)
and [Maps](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)
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](lazy_const.html) docs to understand the performance implications.
Always measure to make sure using this library makes a difference.

```sh
gleam add lazy_const@1
```

```gleam
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>.