Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (45.3k points)

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!

1 Answer

0 votes
by (16.8k points)

You can easily implement something like this yourself.

def compose(current_value, *args):

    for func in args:

        current_value = func(current_value)

    return current_value

def double(n):

    return 2*n

print compose(5, double, double) # prints 20

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.5k questions

29.9k answers

500 comments

99.1k users

Browse Categories

...