mtvch
I was looking for a library for safely evaluating expressions written by users but didn’t find one that would match my needs. I wanted it to be extensible, support all JSON data types, and have its syntax and semantics intuitive to people who are used to Python.
That’s why I created ex_pression:
- Safe evaluation without access to other Elixir modules.
iex> ExPression.eval("exit(self())")
{:error, %ExPression.Error{name: "UndefinedFunctionError", message: "Function 'self/0' was referenced, but was not defined", data: %{function: :self}}}
- Support for JSON syntax and data types.
iex> ExPression.eval("""
{
"name": "ex_pression",
"deps": ["xpeg"]
}
""")
{:ok, %{"name" => "ex_pression", "deps" => ["xpeg"]}}
- Familiar python-like operators and standard functions.
iex> ExPression.eval(~s/{"1": "en", "2": "fr"}[str(int_code)]/, bindings: %{"int_code" => 1})
{:ok, "en"}
- Extend expressions by providing Elixir module with functions that you want to use.
defmodule MyFunctions do
# use $ special symbol in expressions
def handle_special("$", date_str), do: Date.from_iso8601!(date_str)
# Use diff function in expressions
def diff(date_1, date_2), do: Date.diff(date_1, date_2)
end
iex> ExPression.eval(~s/diff($"2023-02-02", $"2022-02-02")/, functions_module: MyFunctions)
{:ok, 365}
Full language description can be found here.
Trending in Announcing
Earmark is a pure-Elixir Markdown converter.
It is intended to be used as a library (just call Earmark.as_html), but can also be used as...
New
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library.
The progre...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hi everyone :waving_hand:
I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
Other Trending Topics
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New
Popcorn, the library that runs Elixir code in the browser, is now stable :popcorn:
This is possible because it now builds on a very stab...
New
If you haven’t tried Hologram yet, what’s stopped you?
Here’s why I’m asking. Building a framework turns out to be two jobs, the framewo...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #ai
- #authentication
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-eu
- #distillery
- #security
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
mayel
Looks interesting! I’m gonna be needing such a library in the future, so would be curious to hear opinions on the pros/cons of approaches taken by similar libs:
which seem to take a safer approach than these that use elixir more directly:
mtvch
Hello!
Thank you for your advice, I will work on adding a comparison table to README.md. You can track progress in the issue.
Here is a quick comparison with the libraries you mentioned:
Abacus and Formulae do not support composite types: objects and arrays. Also, they are not extensible: you can only use functions and syntax that library provides.
In this category I would mention evalex which works on top of rust’s evalexpr. But it’s lacking the same features.
Regarding Formula and formula2, I can’t even say much since there is no proper documentation.
Overall, first libraries you mentioned are designed mostly for math calculations, while ExPression is more general purpose. You can do various data transformations or other operations depending on your domain.
I really liked the dune project: it has sessions, support for composite Elixir data types and seems to be extensible. But it expects users to know Elixir, which is not the case for many systems. Most people know Python, that’s why I target this language.
Now I’d like to come to some of ExPression cons:
If you need a library for math calculations - try Formulae. If you need sessions and your users are not afraid of Elixir - try Dune. If your users are likely to know Python or if you want to let users define transformations on JSON data or if you want to extend expressions language for your specific domain - try ExPression.
mayel
Thank you, I appreciate you having taken the time
Eiji
I believe it would be worth to describe it a bit more. I believe that answering for example below questions may help a common readers to decide if and when to use it.
Why did you choose
python-like operators? Because it’s popular or sees as easy for beginners? How does it work withJSONsupport?For example here is a first example of search for python and json:
Please pay attention to single and double quotes. In
Elixirit’s a common gotcha and that’s why a new sigil for creatingcharlistsas well as it’s inspect behaviour have been introduced inElixir’s core, so now … Do you support single quotes (there is no mention about it)?Oh, that’s interesting example!
The link looks empty i.e.
[](here). Here is a working one Full ExPressions language description.Not that branch names are so important. Simply saying that it’s more common to use
mainname.Just for sure I guess that you are using such naming because of
python-like operators support, right? Just to let common readers know such naming is used in OOP languages.Elixir’s usingListandMapinstead.Do you support or have any plans for supporting custom operators? In your example there is a custom date type using
$symbol. Also is special supposed to be 1-letter long or it’s just an example?