Two Unique Ways of Piping data in Elixir
Published in
2 min readNov 20, 2018
Dreaming in pipe |>
No junk, straight to code
Assume data = {:ok, [1,2,3,4,5]}
and we need to pass the data
to the next function.
Do we need to write another function to do that job for this small operation? I don’t think so.
Check this…
TYPE –1
data
|> (fn {:ok, list} -> list end).()
|> length()
5
TYPE — 2
Here, instead of sending the length
itself, I would like to send a message something looks like Total Images : 5
Do I again need to use inline function fn again here? I don’t think so.
Check this…
data
|> (& {:ok, list} -> list ).()
|> length()
|> (&"Total Images : #{&1}").()
From elixir 1.12 we are having then/2
we can simply use