ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - batate/elixir-pipes: Macros for more flexible composition with the Elixir Pipe operator
Macros for more flexible composition with the Elixir Pipe operator - batate/elixir-pipes
Visit Site

GitHub - batate/elixir-pipes: Macros for more flexible composition with the Elixir Pipe operator

GitHub - batate/elixir-pipes: Macros for more flexible composition with the Elixir Pipe operator

elixir-pipes

Elixir Pipes is an Elixir extension that extends the pipe (|>) operator through macros.

The Pipe: Elixir Flavor Packet

Some of the best programmers who have taken an early dive into Elixir have mentioned the pipe as one of the key features of the language. It allows a clear, concise expression of the programmer's intent:

  def inc(x), do: x + 1
  def double(x), do: x * 2
  
  1 |> inc |> double

The return value of each function is used as the first argument of the next function in the pipe. It's a beautiful expression that makes the intent of the programmer clear.

Trouble in the Kitchen

Sometimes, you need to compose functions with a different strategy. Say your functions use Erlang-style APIs. You might have functions that return {:ok, value} or {:error, value}. Then, the pipe operator might make things difficult. After you receive an error code, you probably want the pipe to stop.

Elixir-Pipes allows you to specify a strategy, in one concise space, that you can then apply to all segments in an Elixir pipe. This capability will help you compose many different types of functions. How many times have you wanted to:

  • compose a pipe that uses some variation of a function call like [1, 2, 3] |> add(1) |> times(2) ?
  • halt the execution of a pipe on error?
  • tease nils to empty strings, without changing your original functions?
  • transform exceptions to Erlang-style {:error, x} tuples?

The recipes are all there waiting for you.

Getting Started

All you need to do to get started is to add the project to your mix file as a dependency. Then, when you want to use the macros, you'll simply use it:

use Pipe

...

That's it. After that, you can continue to use unadorned pipes, or use one of the prepackaged compositions. Initially, we have three:

pipe_matching

This function will compose as long as the computed value matches the value so far. For example, consider this Russian Roulette application:

defmodule RussianRoulette do
  use Pipe

  def click(params) do
    IO.puts "params: #{inspect params} | click..."
    {:ok, "click"}
  end

  def bang(params) do
    IO.puts "params: #{inspect params} | BANG."
    {:error, "bang"}
  end

  def roll do
    pipe_matching {:ok, _},
      {:ok, ""} |> click |> click |> bang |> click
  end

  def rhs_roll do
    pipe_matching x, {:ok, x},
      {:ok, ""} |> click |> click |> bang |> click
  end

end

...would produce...

iex(1)> RussianRoulette.roll
params: {:ok, ""} | click...
params: {:ok, "click"} | click...
params: {:ok, "click"} | BANG.
{:error, "bang"}

It would evaluate functions as long as the accumulator matched the expression. In this case, we process statements as long as the composition yields an :ok on the left hand side.

If we want to pass just the right hand side down the pipeline, we can use pipe_matching/3:

iex(1)> RussianRoulette.rhs_roll
params: "" | click...
params: "click" | click...
params: "click" | BANG.
{:error, "bang"}

pipe_while

Sometimes, you may want to test on something other than a match. This composition strategy will continue as long as your composition satisfies the test function you provide. To implement the above, you could do this just as well:

    def while_test({:ok, _}), do: true
    def while_test(_), do: false
    def inc({code, x}), do: {code, x + 1}
    def double({code, x}), do: {code, x * 2}

    pipe_while(&while_test/1, {:ok, ""} |> click |> click |> bang |> click )

You could also write tests for testing a value, such as whether a value is even, whether a record is valid, or whether a user is authorized.

pipe_with

Sometimes, you want to write the composition rules yourself. You can do this with pipe_with function, pipe where function has a sig of f(x, pipe_segment) where pipe_segment is a function in the pipe. The macro will pass the accumulated value and a function that wraps each pipe segment to your function.

Say you have a list, and you want to do arithmetic on each element of the list. You can do so with pipe_with like this:

  def inc(x), do: x + 1
  def double(x), do: x * 2

  pipe_with fn(acc, f) -> Enum.map(acc, f) end,
        [ 1, 2, 3] |> inc |> double

This returns

[(1 + 1) * 2, (2 + 1) * 2, (3 + 1) * 2]

or

[4, 6, 8]

You could also wrap exceptions, and translate them to the form {:error, acc}, or change nils to blank strings or empty arrays.

Contributions are welcome. Just send a pull request (you must have tests).

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory