worm 🪱

Package Version Hex Docs

gleam add worm
import gleam/regex
import worm

type RegexCache {
  RegexCache(greeting: regex.Regex, name: regex.Regex)
}

fn regexes() -> RegexCache {
  use <- worm.persist()
  let assert Ok(greeting) = regex.from_string("^\\w+")
  let assert Ok(name) = regex.from_string("\\w+$")
  RegexCache(greeting, name)
}

pub fn main() {
  let RegexCache(greeting: initial, ..) = regexes()

  "Hello, Gleam!"
  |> regex.scan(with: initial) // [regex.Match("Hello", [])]

  Nil
}

While this library was initial concieved of as a cache for regex compilation, it can be used for any deterministic calculation which you only want to run once and reuse many times.

Please read the documentation before using this library as it is not the best solution for many situations. API documentation can be found at https://hexdocs.pm/worm/worm.html.

Search Document