In Elixir, the pipe operator '|>'
takes the output of the expression on the left of it, and feeds it in as the first argument to the function on the right of it.
You can even tag on additional functions together in an endless chain to form a pipeline of functions to be called, very similar to the Unix method of piping command utilities together, e.g. 'ps ax|grep vim|awk '{ print $1 }'
.
In other words, the following statement using the pipe operator |>
:
f( a ) |> g( b ) |> h( c )
is equivalent to:
h( g( f( a ), b ), c )
This can be extended to include functions with multiple parameters like this:
f( a, b ) |> g( c, d )
being equivalent to:
g( f( a, b ), c, d )
See if you can figure out what the following does:
-5 |> abs |> Integer.to_string |> IO.puts
Here's a small hint to help you along:
IO.puts(Integer.to_string(abs(-5)))
Here are some references in which you might be interested:
No I didn't forget. The answer is 5 of course.