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

Handles the , separator for rolls.

The separator allows for multiple, separate dice expressions to be evaluated and only one returned based on provided options:

  • :highest: returns the highest calculated value and is the default option
  • :lowest: returns the lowest calculated value

Examples:

iex> ExDiceRoller.roll("1,2")
2
iex> ExDiceRoller.roll("1,1")
1
iex> ExDiceRoller.roll("1,2", opts: [:highest])
2
iex> ExDiceRoller.roll("1,2", opts: [:lowest])
1
iex> ExDiceRoller.roll("1d6+2,10d8+3", opts: [:highest])
49
iex> ExDiceRoller.roll("1d6+8,10d8+5", opts: [:lowest])
14

Seperator expressions can be wrapped in parentheses to be utilized it as a subexpression in a larger expression.

Examples:

iex> ExDiceRoller.roll("(5d1,2d1)+5", opts: [:highest])
10
iex> ExDiceRoller.roll("(5d1,2d1)+5", opts: [:lowest])
7

Separator Use And Keeping Dice

The separator can be used alongside kept dice rolls, provided:

  • one side is a list and the other a number
  • both sides are lists of equal length

When both sides are lists of equal length, separator will begin comparing the values from both lists by index location.

iex> ExDiceRoller.roll("5d6,5d100", opts: [:keep, :lowest])
[2, 2, 6, 4, 5]
iex> ExDiceRoller.roll("5d6,5d100", opts: [:keep, :highest])
[47, 6, 49, 91, 54]

iex> ExDiceRoller.roll("(5d2,5d6)+5", opts: [:highest, :keep])
[7, 9, 9, 11, 6]
iex> ExDiceRoller.roll("(5d1,5d100)+5", opts: [:lowest, :keep])
[6, 6, 6, 6, 6]

iex> ExDiceRoller.roll("5d6, 3", opts: [:keep])
[3, 3, 6, 4, 5]
iex> ExDiceRoller.roll("3, 5d6", opts: [:keep])
[3, 4, 4, 6, 3]

iex> ExDiceRoller.roll("4, xd5", x: ["1d4", 2.5], opts: [:keep])
[5, 4, 4, 4]

iex> ExDiceRoller.roll("2d4, 1d8", opts: [:keep])
** (ArgumentError) cannot use separator on lists of differing lengths