minigen

This module contains some useful functions for generating data in the Erlang ecosystem.

For more information see this website.

Types

A type for representing generators. In order for it to generate data, it should be passed as argument to the run or run_with_seed function.

pub type Generator(a) {
  Generator(fn(Seed) -> #(Seed, a))
}

Constructors

  • Generator(fn(Seed) -> #(Seed, a))

Functions

pub fn always(x: a) -> Generator(a)
pub fn boolean() -> Generator(Bool)

Creates a generator for boolean values. By running it, we get a random boolean value with 50% probability.

Erlang example

GEN=minigen:boolean(),
minigen:run(GEN).
true

Elixir example

:minigen.boolean
|> :minigen.run
false

Gleam example

minigen.boolean()
|> minigen.run
True
pub fn element_of_list(ls: List(a)) -> Generator(Result(a, Nil))

Creates a generator that randomly selects an element from a list. If the list is empty, then we will get an error.

Erlang examples

GEN=minigen:element_of_list([1, 2, 3]),
minigen:run(GEN).
{ok,2}
GEN=minigen:element_of_list([]),
minigen:run(GEN).
{error,nil}

Elixir examples

:minigen.element_of_list(["a", "b", "c", "d"])
|> :minigen.run
{:ok, "c"}
:minigen.element_of_list([])
|> :minigen.run
{:error, nil}

Gleam examples

minigen.element_of_list([0.5348931595479329, 0.47372875562526207, 0.7109364198110805])
|> minigen.run
Ok(0.7109364198110805)
minigen.element_of_list([])
|> minigen.run
Error(Nil)
pub fn float() -> Generator(Float)

Creates a generator for float values. By running it, we get a random float uniformly distributed between 0.0 (included) and 1.0 (excluded).

Erlang example

GEN=minigen:float(),
minigen:run(GEN).
0.7938520785840248

Elixir example

:minigen.float
|> :minigen.run
0.36087782004967894

Gleam example

minigen.float()
|> minigen.run
0.16296012690374562
pub fn integer(n: Int) -> Generator(Int)

Creates a generator for integer values. By running it, we get a random integer uniformly distributed between 0 (included) and n (excluded).

Erlang example

GEN=minigen:integer(10),
minigen:run(GEN).
4

Elixir example

:minigen.integer(10)
|> :minigen.run
8

Gleam example

minigen.integer(10)
|> minigen.run
6
pub fn list(gen: Generator(a), n: Int) -> Generator(List(a))
pub fn map(gen: Generator(a), fun: fn(a) -> b) -> Generator(b)
pub fn map2(
  gen_a: Generator(a),
  gen_b: Generator(b),
  fun: fn(a, b) -> c,
) -> Generator(c)
pub fn map3(
  gen_a: Generator(a),
  gen_b: Generator(b),
  gen_c: Generator(c),
  fun: fn(a, b, c) -> d,
) -> Generator(d)
pub fn map4(
  gen_a: Generator(a),
  gen_b: Generator(b),
  gen_c: Generator(c),
  gen_d: Generator(d),
  fun: fn(a, b, c, d) -> e,
) -> Generator(e)
pub fn map5(
  gen_a: Generator(a),
  gen_b: Generator(b),
  gen_c: Generator(c),
  gen_d: Generator(d),
  gen_e: Generator(e),
  fun: fn(a, b, c, d, e) -> f,
) -> Generator(f)
pub fn run(gen: Generator(a)) -> a

Creates pseudorandom data for a generator. If we call this function several times, we will probably get different results.

Erlang example

GEN=minigen:float(),
minigen:run(GEN).
0.7938520785840248

Elixir example

:minigen.float
|> :minigen.run
0.36087782004967894

Gleam example

minigen.float()
|> minigen.run
0.16296012690374562
pub fn run_with_seed(gen: Generator(a), i: Int) -> a

Creates pseudorandom data for a generator but the result depends on the integer seed value. If we call this function several times with the same seed, we will always get the same results.

Erlang example

GEN=minigen:float(),
minigen:run_with_seed(GEN, 1000).
0.7109364198110805

Elixir example

:minigen.float
|> :minigen.run_with_seed(999)
0.4944539429884903

Gleam example

minigen.float()
|> minigen.run_with_seed(998)
0.29739016530475904
pub fn sequence(gens: List(Generator(a))) -> Generator(List(a))
pub fn shuffled_list(ls: List(a)) -> Generator(List(a))

Creates a generator that changes the order of the elements in a list.

Erlang example

GEN=minigen:shuffled_list([1, 2, 3]),
minigen:run(GEN).
[2,1,3]

Elixir example

:minigen.shuffled_list(["a", "b", "c", "d"])
|> :minigen.run
["c", "d", "b", "a"]

Gleam example

minigen.shuffled_list([0.5348931595479329, 0.47372875562526207, 0.7109364198110805])
|> minigen.run
[0.47372875562526207, 0.5348931595479329, 0.7109364198110805]
pub fn string(n: Int) -> Generator(String)

Creates a generator for string values. The generated string will contain n alphanumerics.

Erlang example

GEN=minigen:string(6),
minigen:run(GEN).
"3Rzpqd"

Elixir example

:minigen.string(7)
|> :minigen.run
"eJKp8sc"

Gleam example

minigen.string(8)
|> minigen.run
"U3j641WL"
pub fn then(
  gen: Generator(a),
  fun: fn(a) -> Generator(b),
) -> Generator(b)
Search Document