Haskell like where
sugar for Elixir. A pipe-able with
special form.
import Having
The having
macro is a tiny syntax sugar around with
. To make it work a bit
like the where
keyword for Haskell, where you can have an expression and
then some bindings for evaling it on.
For example, the following Haskell code
a + b
where
a = 3
b = a * a
Could be written in Elixir like:
iex> a + b
...> |> having(a: 3, b: a * a)
12
that will be compiled into:
with a = 3,
b = a * a,
do: a + b
Every having
gets rewritten into a corresponding with
special form.
And thus can have its own else:
clauses, guards on <-
just like with
.
For example if you pipe an expression into two having
s:
{a, b}
|> having(a = b * b)
|> having(b when b < 10 <- 2)
will get rewritten to:
with(b when b < 10 <- 2) do
with(a = b * b) do
{a, b}
end
end
Note that in the having
example, the expression you want to compute
is the first thing you see, instead of it being nested inside with
forms.
Also note that the most external with
corresponds to the latest piped having
this is important if you need to have some values that depend on others.
For more examples look at the documentation of having.ex
Available in Hex, the package can be installed as:
- Add
having
to your list of dependencies inmix.exs
:
```elixir
def deps do
[{:having, "~> 0.1.0"}]
end
```
- Ensure
having
is started before your application:
```elixir
def application do
[applications: [:having]]
end
```