Custom Sigils In Elixir
You can create custom sigils by following the sigil_{character}
definition pattern. Let's make an addition sigil sigil_a
that sums up space separated numbers.
defmodule CustomSigil do
def sigil_a(string, []) do
string
|> String.split(" ")
|> Enum.map(& String.to_integer(&1))
|> Enum.sum()
end
end
# ~a(2 4 6)
#=> 12
# ~a(12 12)
#=> 24
Tweet