ExDiceRoller v1.0.0-rc.2 ExDiceRoller.Compilers.Variable View Source

Handles compiling expressions that use variables.

Variables can be used to replace single letter characters in an expression with a value, such as a number or an anonymous function that accepts list arguments (args and opts, respectively).

Acceptable variable values include:

  • integers
  • floats
  • compiled functions matching t:Compiler.compiled_fun/2
  • strings that can be parsed by ExDiceRoller
  • lists composed of any of the above
  • lists of lists

Note that an error will be raised if values are not supplied for all varaibles in an expression.

Examples

iex> import ExDiceRoller.Sigil
ExDiceRoller.Sigil
iex> ExDiceRoller.roll(~a/1+x/, [x: 5])
6
iex> ExDiceRoller.roll("xdy+z", x: 5, y: 10, z: 50)
82
iex> ExDiceRoller.roll("xdy+z", [x: 5, y: 10, z: ~a/15d100/])
739
iex> ExDiceRoller.roll("xdy+z", x: [1, 2, 3], y: 1, z: 5, opts: [:keep])
[6, 6, 6, 6, 6, 6]
iex> ExDiceRoller.roll("xdy+z", [x: 1, y: [1, 10, 100], z: -6, opts: [:keep]])
[-5, -4, 66]
iex> ExDiceRoller.roll("xdy+z", x: [~a/1d2/, "1d4+1"], y: ["3,4d20/2", ~a/1d6/], z: 2, opts: [:keep])
[8, 8, 3, 3, 3, 10, 4, 7, 6, 3, 5, 4, 4, 4, 3]

iex> ExDiceRoller.roll("1+x")
** (ArgumentError) no variable 'x' was found in the arguments