Home | Articles | CV (pdf | short)
<2016-05-25> by Lorenzo

Elixir's pipe operator in Python

Elixir implements lots of nice ideas, one of which is its pipe operator. It allows you to write things like:

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

Or:

iex> inc = fn x -> x + 1 end
iex> dec = fn x -> x - 1 end
iex> square = fn x -> x * x end
iex> 5 |> inc.() |> dec.() |> square.()
25

Today, just for fun, I tried to implement something similar in Python. Here is the result:

So, you can write something like:

>>> 5 |P| inc |P| dec |P| square
25

Obviously, this is just a toy: it does not implement all the features available in the Elixir's one, but it was fun!