narrowtux / abacus

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

Add 'in' operator for list membership #28

Closed benknowles closed 3 weeks ago

benknowles commented 4 weeks ago

This PR adds the in keyword to enable checking list membership.

Examples:

Abacus.eval("\"apple\" in fruits", %{"fruits" => ["apple", "banana", "orange"]})

Abacus.eval("a in b", %{"a" => 1, "b" => [1,2,3]})

RHS always expects a list (can be nested access), LHS can be any expression.

narrowtux commented 3 weeks ago

I would say that this operator doesn't really fit in the standard library, since we can't do list literals anyway. I would like to keep the available API to a minimum, since developers can always pass functions in the scope. Try passing %{in: &Kernel.in/2} as the scope and then using a slightly revised abacus term in(3, list).

See also: #20

I would maybe accept a feature that allows binary operators to be configured, so every developer can have their own custom set of operators.