https://github.com/qqwy/elixir-capture_pipe
A pipe-macro for Elixir that allows bare function captures
https://github.com/qqwy/elixir-capture_pipe
Last synced: 7 months ago
JSON representation
A pipe-macro for Elixir that allows bare function captures
- Host: GitHub
- URL: https://github.com/qqwy/elixir-capture_pipe
- Owner: Qqwy
- License: mit
- Created: 2020-07-22T18:11:35.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-07-22T18:49:44.000Z (about 5 years ago)
- Last Synced: 2024-10-12T04:32:51.161Z (12 months ago)
- Language: Elixir
- Size: 10.7 KB
- Stars: 45
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CapturePipe
[](https://hex.pm/packages/capture_pipe)
[](https://travis-ci.com/Qqwy/elixir-capture_pipe)
[](https://hexdocs.pm/capture_pipe/index.html)CapturePipe exposes an extended pipe-operator that allows the usage of bare function captures.
This is useful to insert the pipe's results into a datastructure
such as a tuple.What this macro does, is if it encounters a `&` capture,
it wraps the whole operand in `(...).()` which is the
anonymous-function-call syntax that Elixir's normal pipe accepts,
that (argubably) is less easy on the eyes.For instance, `10 |> &{:ok, &1}` is turned into `10 |> (&{:ok, &1}).()`
## Examples
Still works as normal:
```elixir
iex> [1,2,3] |> Enum.map(fn x -> x + 1 end)
[2,3,4]
```Insert the result of an operation into a tuple
```elixir
iex> 42 |> &{:ok, &1}
{:ok, 42}
```It also works multiple times in a row.
```elixir
iex> 20 |> &{:ok, &1} |> &[&1, 2, 3]
[{:ok, 20}, 2, 3]
```Besides the function-capture syntax
CapturePipe also enables you to use anonymnous functions
directly inside a pipe, performing similar wrapping:```elixir
iex> 42 |> fn val -> to_string(val) end
"42"
```Even if the pipes are nested deeply
and interspersed with 'normal' pipe calls:```elixir
iex> (10
iex> |> &Kernel.div(20, &1)
iex> |> Kernel.-()
iex> |> to_string()
iex> |> &"The answer is: \#{&1}!"
iex> |> String.upcase()
iex> |> &{:ok, &1}
iex> )
{:ok, "THE ANSWER IS: -2!"}
```## Installation
Capturepipe can be installed
by adding `capture_pipe` to your list of dependencies in `mix.exs`:```elixir
def deps do
[
{:capture_pipe, "~> 0.1.0"}
]
end
```Documentation can be found at [https://hexdocs.pm/capture_pipe](https://hexdocs.pm/capture_pipe).