vic/having
{ "defaultBranch": "master", "description": "Haskell like `where` sugar for Elixir. A pipe-able `with` special form.", "fullName": "vic/having", "homepage": "", "language": "Elixir", "name": "having", "pushedAt": "2017-02-20T23:02:24Z", "stargazersCount": 7, "updatedAt": "2020-05-04T22:29:44Z", "url": "https://github.com/vic/having"}Having
Section titled “Having”Haskell like where sugar for Elixir. A pipe-able with special form.
import HavingThe 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 + bwhere a = 3 b = a * aCould be written in Elixir like:
iex> a + b...> |> having(a: 3, b: a * a)12that will be compiled into:
with a = 3, b = a * a,do: a + bEvery 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 havings:
{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} endendNote 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
Installation
Section titled “Installation”Available in Hex, the package can be installed as:
- Add
havingto your list of dependencies inmix.exs:
```elixirdef deps do [{:having, "~> 0.1.0"}]end```2. Ensure having is started before your application:
```elixirdef application do [applications: [:having]]end```