R

rollex

daee3f3f Modified some timing tests · by Mike Chmielewski

Rollex

A dice roller expression evaluator implementing the Pratt Parser (AKA Top-Down Operator Precedence) written in Elixir!

You may have seen Dicer... That was my old library, which used a basic recursive descent parser/evaluator. This implementation uses a version of a the Pratt Parser algorithm, also known as Top-Down Operator Precedence. With protocols compiled/consolidated, it runs a touch faster than the RC one.

Any suggestions/merge requests for performance tweaks is greatly appreciated!

Installation

Pre-requisites

  • Erlang 17 or greater
  • Elixir 1.0.0 or greater
  • git (to clone the repository)

Running in Elixir's interactive shell

iex -S mix

For better performance, compile/consolidate protocols:

mix compile.protocols

iex -pa _build/dev/consolidated -S mix

From here, execute rolls like this:

iex(1)> Rollex.roll "1+2+3"

Adding as a mix dependency

In your mix.exs file:

  def application do
    [mod: {MyApp, []},
     applications: [:rollex]]
  end

and this:

  defp deps do
    [{:rollex, "0.1.0"}]
  end

Details

Rollex is an elixir application that lets you evaluate dice rolls with simple arithmetic operators.

  • The operators supported are +, -, /, *.
  • Grouping is via parentheses
  • Polyhedral dice are designated using the <quantity>d<sides> format (Ex. 20d8 or D100).
  • You can ask Rollex to take the top or bottom X rolls via the ^<quantity> (take top) and v<quantity> (take bottom) symbols (Ex. 10d100^5 [take top 5 results from 10 rolls of a 100-sided die])

Why?

Because it was a yet another fun, somewhat non-trivial way to work in Elixir.

Thanks

Thanks to Lukasz Wrobel for his short series on RD parsing, as well as Eli Bendersy's post on TDOP!