Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure style function "threading" in Python

Tags:

Clojure has a "->" macro which inserts each expression recursively as the first argument of the next expression.

This means that I could write:

(-> arg f1 f2 f3) 

and it behaves like (shell piping):

f3(f2(f1(arg))) 

I would like to do this in Python; however, searching seems to be a nightmare! I couldn't search for "->", and neither could I search for Python function threading!

Is there a way to overload, say, the | operator so that I could write this in Python?

arg | f1 | f2 | f3 

Thanks!

like image 977
Vimal Avatar asked Feb 10 '11 18:02

Vimal


1 Answers

Or possibly use the reduce function in the following way:

reduce(lambda x,f : f(x), [f1,f2,f3], arg) 
like image 73
Howard Avatar answered Sep 24 '22 00:09

Howard