woylie
I just released Spek, a boolean expression engine based on the one written for LetMe 2.0.0.
Features
- Expression structs and builder functions for boolean logic:
AllOf,AnyOf,Not,Literal,Check. - Evaluation of boolean expressions with optional early stopping and optional evaluation tree output.
- Optimization of boolean expressions using boolean algebra transformation.
- Macros for concisely defining reusable check functions.
The readme is quite comprehensive, so let me just post some code examples here.
Defining a check module
defmodule DeviceChecks do
import Spek.Macros
defcheck device_online(device, reason: :device_offline) do
device.online?
end
defcheck battery_above_20(device, reason: :battery_too_low) do
device.battery_level > 20
end
defcheck charging(device) do
device.charging?
end
defcheck low_power_mode_enabled(device) do
device.low_power_mode?
end
end
(The use of the defcheck macro is optional.)
Composing, optimizing, and evaluating expressions
iex> import Spek
iex> battery_safe =
...> all_of([
...> DeviceChecks.device_online_check(),
...> DeviceChecks.battery_above_20_check()
...> ])
iex> charging_safe =
...> all_of([
...> DeviceChecks.device_online_check(),
...> DeviceChecks.charging_check()
...> ])
iex> rule =
...> any_of([
...> battery_safe,
...> charging_safe,
...> DeviceChecks.low_power_mode_enabled_check()
...> ])
%Spek.AnyOf{
children: [
%Spek.AllOf{
children: [
%Spek.Check{
module: DeviceChecks,
fun: :device_online,
args: [:ctx]
},
%Spek.Check{
module: DeviceChecks,
fun: :battery_above_20,
args: [:ctx]
}
]
},
%Spek.AllOf{
children: [
%Spek.Check{
module: DeviceChecks,
fun: :device_online,
args: [:ctx]
},
%Spek.Check{
module: DeviceChecks,
fun: :charging,
args: [:ctx]
}
]
},
%Spek.Check{
module: DeviceChecks,
fun: :low_power_mode_enabled,
args: [:ctx]
}
]
}
iex> rule = optimize(rule)
%Spek.AnyOf{
children: [
%Spek.AllOf{
children: [
%Spek.Check{
module: DeviceChecks,
fun: :device_online,
args: [:ctx]
},
%Spek.AnyOf{
children: [
%Spek.Check{
module: DeviceChecks,
fun: :battery_above_20,
args: [:ctx]
},
%Spek.Check{
module: DeviceChecks,
fun: :charging,
args: [:ctx]
}
]
}
]
},
%Spek.Check{
module: DeviceChecks,
fun: :low_power_mode_enabled,
args: [:ctx]
}
]
}
iex> device = %{
...> online?: true,
...> battery_level: 12,
...> charging?: false,
...> low_power_mode?: false
...> }
iex> Spek.eval?(rule, device)
false
iex> device = %{
...> online?: false,
...> battery_level: 25,
...> charging?: false,
...> low_power_mode?: false
...> }
iex> Spek.eval_tree(rule, device)
{
:error,
%Spek.EvaluationError{
expression: %Spek.AnyOf{
children: [
%Spek.AllOf{
satisfied?: false,
children: [
%Spek.Check{
module: DeviceChecks,
fun: :device_online,
args: [:ctx],
result: {:error, :device_offline},
satisfied?: false
}
]
},
%Spek.Check{
module: DeviceChecks,
fun: :low_power_mode_enabled,
args: [:ctx],
result: {:error, :failed},
satisfied?: false
}
],
satisfied?: false
},
message: "rule evaluation failed"
}
}
Feedback
Your feedback is appreciated, especially when it comes to API design or feature ideas.
What may come in a future release:
- Serialization/deserialization of expressions
- Extract errors of an evaluated expression into a list of error reasons
Trending in Announcing
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub.
Docs are at OpenaiEx User Gu...
New
WebAuthnLiveComponent WebAuthnComponents
See this post about renaming the package.
Passwordless authentication for Phoenix LiveView app...
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
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
Hello,
I’m sharing my plugin here in forum after some time so it has time to mature and proof yourself.
I use Claude Code daily on a pr...
New
Other Trending Topics
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
What IDE or editor are you using for Elixir development?
Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
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
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex











Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Asd
Hi, congratulations on the release
. I have a question: when and what to use this library for?
woylie
Use cases can be:
woylie
Version 0.2.0 adds various functions to collect evaluation results into flat lists.