ExDiceRoller v1.0.0-rc.2 ExDiceRoller.ListComprehension View Source

Contains functionality for list comphrensions in ExDiceRoller.

ExDiceRoller also has a certain amount of list comprehension support when calculating dice roll equations and ‘keeping’ rolls. The default behavior when working with kept rolls is as follows:

  1. If one side of an expression is a list, and the other a value, the action will apply the value to each value in the list.
  2. If both sides of an expression are lists of equal length, the values of each list are applied to their counterpart in the other list. An error is raised if the lengths of the two lists are different.
  3. Combination rolls, such as 3d5d6, will perform each roll expressions in succession. Kept values from each roll expression is then used as the number of sides in the succeeding expression.

Example of one side of an expression being a kept list and the other a value:

iex> {:ok, fun} = ExDiceRoller.compile("5d6+11")
iex> fun.(opts: [:keep])
[14, 13, 17, 15, 16]

Example of both sides being lists:

iex> {:ok, fun} = ExDiceRoller.compile("5d6+(5d10+20)")
iex> fun.(opts: [:keep])
[25, 32, 34, 30, 26]

Example with lists of differing lengths:

iex> ExDiceRoller.roll("5d6+6d6", opts: [:keep])
** (ArgumentError) cannot use math operators on lists of differing lengths

Example of dice rolls of dice rolls:

iex> ExDiceRoller.roll("1d1d4", opts: [:keep])
[4]
iex> ExDiceRoller.roll("2d1d4", opts: [:keep])
[3, 2]
iex> ExDiceRoller.roll("2d6d4", opts: [:keep])
[2, 3, 2, 4, 4, 4, 3, 3]

Link to this section Summary

Functions

Applies the given function and options to both the left and right sides of an expression

Applies the given function and options to both the left and right sides of an expression. If either or both sides are lists, the functions are applied against each element of the list. Any resulting lists or nested lists, will be flattened to a single list

Link to this section Types

Link to this section Functions

Link to this function apply(l, r, args, err_name, fun) View Source
apply(left(), right(), any(), String.t(), function()) :: return_val()

Applies the given function and options to both the left and right sides of an expression.

If both sides are lists, a check is made to verify they are the same size. If they are not the same size, an error is raised. Otherwise, the values of each list are applied to their counterpart in the other list.

Link to this function flattened_apply(l, r, args, fun) View Source
flattened_apply(left(), right(), any(), function()) :: return_val()

Applies the given function and options to both the left and right sides of an expression. If either or both sides are lists, the functions are applied against each element of the list. Any resulting lists or nested lists, will be flattened to a single list.