Similar to the pipe operator in Unix, the pipe operator takes the return value from the expression on its left and provides it as the first parameter to the function call on its right.
The pipe operator is denoted by |>
in Elixir.
Let’s understand the pipeline with an example.
Consider that the following operations have to be performed on a given string:
The above three operations can be performed in different steps as different expressions, with the result of each step stored in a temporary variable.
We can also perform the sequence of the above operations via a pipe operator.
Let’s see the following code:
platform = " The educative platform "IO.puts(["Original string - ", platform])temp = String.trim(platform)temp = String.capitalize(temp)temp = String.split(temp, " ")IO.puts "Final string - "IO.inspect tempIO.puts "-----------"IO.puts "Using pipe operator"new_temp = platform |> String.trim() |> String.capitalize() |> String.split(" ")IO.inspect new_temp
string
is defined.trim()
function is applied and the result is stored in the temp
variable.capitalize()
function is applied and the result is stored in the temp
variable.split()
function is applied and the result is stored in the temp
variable.strings
in lines 5,7, and 9 as a single expression using the pipe operator.