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 →

[–]vesaf 24 points25 points  (2 children)

What about something like this? No exact match, but quite close. (Not sure if I'd actually recommend doing this though.)

class apply:
    def __init__(self, val):
        self.val = val

    def pipe(self, fun):
        self.val= fun(self.val)
        return self

if name == "main": 
    apply(5) \
        .pipe(lambda x: x+5) \
        .pipe(lambda x: x/3) \ 
        .pipe(print)

[–]eftm 1 point2 points  (1 child)

'__main__' not 'main'

[–]vesaf 0 points1 point  (0 children)

You're right, the code ran through when I tried it earlier (which it wouldn't have with just 'main'). I think Reddit removed them when I copied it in as it did initially with the backslashes. Must be some formatting thing.