narrowtux / abacus Goto Github PK
View Code? Open in Web Editor NEWParses and evaluates mathematical expressions in Elixir. Inspired by math.js
License: MIT License
Parses and evaluates mathematical expressions in Elixir. Inspired by math.js
License: MIT License
Hey!
Our use case only involves evaluating basic maths and floating point manipulation functions - we would love to be able to disable the evaluation of things we don't need, such as factorial, bitwise, comparison, boolean operators, Enum API, etc.
Do you know of any way we could achieve that right now, and if not, would it be possible to add such a configuration option to the library? For example, a "whitelist" of operations.
iex(1)> Abacus.eval "a>5", %{"a" => 4.5}
{:ok, false}
iex(2)> Abacus.eval "a>5", %{"a" => "4.5"}
{:ok, true}
Of course, I can do a coercion to respective Float
before eval
ing, but people expect this to be done by abacus :)
I believe, the coercion might be easily done here. Please confirm, and I will provide the pull request.
Thanks in advance.
>>
<<
&
|
|^
~
(and
or
xor
not
)0b...
0o...
0x...
I am suggesting that we add a rand function to the library.
meter
to m
)m / s
should be parsed as {:divide, {:unit, "m"}, {:unit, "s"}}
{:unit, binary}
to a unit definitioneval/2
functions to support multiplication operations (/
, *
, ^
) on amountsm
, k
, M
, ...)The parser should be improved so that we can extract the line and column where a syntax error happened in the input.
With this, error reports like the following should be possible:
Input: 1+)
^
Unexpected Token '('.
What needs to be done for this:
...
Offer registering own functions by name in runtime, so they can be called in a abacus expression.
Should return "1234"
:
iex(13)> Abacus.eval("\"1234\"", %{})
{:ok, 1234}
This is just a cosmetic thing, but maybe interesting for others...
The dialyzer is complaining about the eval function and a boolean expression. According to the docs, the return type is always a number(), which is not correct in the case of an boolean expression evaluation. There the return value is an atom(), 'true' or 'false'.
So is there a way to tell the eval function that we have an boolean return value? Or change the return value in general to any()?
I was hoping that this would automagically work?
Abacus.eval("concat(a,b)", %{"a" => "hello ", "b" => "world"})
error: undefined variable "var0"
Abacus.eval("a + \" \" + b", %{"a" => "hello", "b" => "world"})
{:error, %ArithmeticError{message: "bad argument in arithmetic expression"}}
I don't see any string functions in the source code.
.
for accessing key[expr]
for accessing an index of an arraySupporting Polish Notation would be a cool addition to this library in my opinion. Or a polish notation evaluation library may be a idea for another math library? But I dont know how something like this would be done. Just throwing the idea out here.
https://en.wikipedia.org/wiki/Polish_notation
Thanks for reading my issue!
Easy one.
Is it possible to introduce Abacus.syntax?/[1,2]
function, returning {:ok}
/ {:error, extended_info}
? It might be quite handy in case of deferred operations. In my use case, I accept an expression as, say, user input, and execute it later on some alert. I would appreciate having an ability to inform the user that the expression introduced has syntax glitches.
I could provide a pull request
for just this feature, but I think it’s up to you to decide whether you want to use it also in eval/[1,2]
, or not.
AbacusSql.Term.parse ~s["\\""] == {:ok, "\\"}
should be
{:ok, "\""}
==
!=
<
>
<=
>=
x ? a : b
and
, or
, not
Currently the formula’s parser fails with:
{:error, {1, :math_term_parser, ['syntax error before: ', '\'.\'']}}
on floats with omitted leading/trailing zero (.25
/ 2.
.) It is quite common to have floats being written that way, so I would suggest it makes sense to accept this notation.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.