narrowtux / abacus

Parses and evaluates mathematical expressions in Elixir. Inspired by math.js
MIT License
87 stars 17 forks source link

Support generating random number in the math expression #20

Open ghost opened 4 years ago

ghost commented 4 years ago

I am suggesting that we add a rand function to the library.

narrowtux commented 4 years ago

You can add your own functions via the scope:

Abacus.eval("rand() * 10", %{rand: &:rand.normal/0})
{:ok, 4.778221007148446}

I'd rather keep the scope of included functions as low as possible. I could see a global configuration for functions that should be included in the whole project though, something like this:

config :abacus, Abacus, scope: %{
  rand: {:rand, :normal, 0},
  now: {DateTime, :utc_now, 0}
}