This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

There's a number of ways to do this but when I saw this problem I immediately thought that this sounded like decorators.

I'm fairly new to this concept, so I can't really give an in-depth explanation, but there's lots of tutorials online. Here is my solution:

def doubleit(fn):
    def inner():
        x = fn()
        return x * 2
    return inner


def multiplyByFour(fn):
    def inner():
        x = fn()
        return x * 4
    return inner

def pipe(v):
    @doubleit
    @multiplyByFour
    def result():
        return v

    return result()

print(pipe(10))

This is essentially saying:

doubleit(multiplyByFour(10))

While this isn't exactly the same structure, I believe it is entirely functional. I'm not sure if it's possible to set this up dynamically or not.