A simple Elixir library for creating mixfix operators - operators where parts are interspersed with arguments.
Mixfix operators allow you to create custom syntax where operator parts are mixed with arguments. For example, instead of writing if(condition, then_branch, else_branch), you can create a more readable syntax like xif(condition.then(consequence).else(otherwise)).
Use defmix to create operators where arguments must be provided in the exact order specified:
defmodule Example do
import Mixfix
# Define a mixfix operator with parts: xif, then, else
defmix xif_then_else(condition, consequence, otherwise) do
if condition do
consequence
else
otherwise
end
end
end
# Usage - arguments must be in order: condition, then, else
xif(true.then("yes").else("no")) # => "yes"
xif(false.then("yes").else("no")) # => "no"Use defmix_unordered to create operators where arguments can be provided in any order:
defmodule Example do
import Mixfix
# Define an unordered mixfix operator
defmix_unordered xif2_then_else(condition, consequence, otherwise) do
if condition do
consequence
else
otherwise
end
end
end
# Usage - arguments can be in any order
xif2(true.then("yes").else("no")) # => "yes"
xif2(true.else("no").then("yes")) # => "yes" (same result!)