" mean in an elixir? I read the elixir codebase on github and I see it often |> . It does not appear in the list +14 ...">

What does "|>" mean in an elixir?

I read the elixir codebase on github and I see it often |>

. It does not appear in the list

+14


source to share


3 answers


This is the operator. From linked docs:



This statement enters the expression on the left as the first argument to the function call on the right.

Examples of

iex> [1, [2], 3] |> List.flatten()

[1, 2, 3]

The above example is the same as calling List.flatten([1, [2], 3])

.

+15


source


it gives you a way to avoid wrong code like this:

orders = Order.get_orders(current_user)
transactions = Transaction.make_transactions(orders)
payments = Payment.make_payments(transaction, true)

      

the same code using a pipeline operator:

current_user
|> Order.get_orders
|> Transaction.make_transactions
|> Payment.make_payments(true)

      

Look at the Payment.make_payments function, there is a second bool parameter if it is the first parameter:



def make_payments(bool_parameter, transactions) do
   //function 
end

      

it didn't work anymore.

When developing an elixir application, keep in mind that important parameters should come first, in the future this will give you the opportunity to use a pipeline operator.

I hate this question when writing non-elixir code: what should I name this variable? I spend a lot of time answering.

+12


source


In addition to Stefan's excellent comment, you can read the "Pipeline Operator" section of this blog post for a better understanding of the use case that the Pipeline Operator is designed to address in Elixir. The important idea is this:

The pipeline operator allows you to combine different operations without using intermediate variables.,. The code can be easily followed by reading from top to bottom. We pass the state through various transformations to get the desired result, each transform returning some modified version of the state.

+2


source







All Articles