[Python-ideas] Elixir inspired pipe to apply a series of functions

Antoine Pitrou solipsis at pitrou.net
Fri Jun 14 14:57:18 CEST 2013


On Thu, 13 Jun 2013 20:06:10 +0200
Jan Wrobel <wrr at mixedbit.org> wrote:
> 
> Which is dense, and needs to be read backwards (last operation is
> written first), but as Joe notes, the alternative is also not very
> compelling:
> 
>   decrypted_blocks = map(decrypt_block, pairwise([iv] + secret_blocks))
>   combined_blocks = reduce(lambda result, block: result.append(block))
>   return pkcs7_unpad(combined_blocks)

Perhaps if you stopped wanting to use map / reduce your code would be
more readable? How about list comprehensions or simple loops?

> The pipe operator nicely separates subsequent operations and allows to
> read them in a natural order without the need for temporary variables.
> Something like:
> 
>       [iv] + secret_blocks |> pairwise |> map, decrypt_block |> \
>          reduce, lambda result, block: result.append(block) |> \
>          pkcs7_unpad

Perhaps it's natural to you, but it's unreadable to me :-o

Regards

Antoine.




More information about the Python-ideas mailing list