Elixir's pipe operator in Python
Implementing 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!

May 25, 2016
122 words


Categories
Tags
python elixir

Contact me

Hi! I'm Lorenzo Bolla, Senior Software Architect in London, UK.

When I'm not programming in Python, Elixir, Go or Haskell, I am playing either basketball, piano or chess.