What is "Elixir" used for?

1

Reading some code I see |> ser commonly used. What does it mean?

    
asked by anonymous 17.02.2018 / 04:56

1 answer

1

The |> symbol is known as the pipe operator . The |> pipe operator is extremely useful and will certainly improve the readability of your code with Elixir.

It basically takes the output of the expression from the left and passes as the first argument of a function on the right.

Let's say I do not know about the pipe operator and want to multiply all the values in a list by two, then filter those that are bigger than 4, our code will look like this:

lista = [1, 2 , 3]
Enum.filter(Enum.map(lista, &(&1 * 2)), &(&1 > 4))

If we use the pipe operator, our example will look different:

lista = [1, 2, 3]
# A lista gerada no primeiro map será o primeiro parametro do filter
Enum.map(lista, &(&1 * 2)) |> Enum.filter(&(&1 > 4))

You can read more about the pipe operator and other language features at official documentation and at elixirschool

    
17.02.2018 / 04:56