Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Pipelining

elixier_notebooks/Pipelining.livemd

Pipelining

Section

You can pipe data in functions like you would with the unix pipe operator, this should be preferd instead of doing nested function calls.

foo(bar(zor(data)))

The pipeline operator promotes clarity.

Best Practice is doing only if function arity is 1, If the arity of a function is more than 1, then make sure to use parentheses.

hello_world = "Hello, World !"
"Hello, World !"
hello_world
# Convert to uppercase
|> String.upcase()
|> String.split()
# Trim leading/trailing whitespace from each word
|> Enum.map(&String.trim/1)
["HELLO,", "WORLD", "!"]

Here we “pipe” the data “Hello, World” in IO.puts() the value from |> will be the first argument value in the function

hello_world
# Convert to uppercase
|> String.upcase()
# Split the string into a list of words
|> String.split()
# Trim leading/trailing whitespace from each word
|> Enum.map(&String.trim/1)
# Join the list back into a string with spaces
|> Enum.join(" ")
"HELLO, WORLD !"