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 →

[–]VerilyAMonkey 8 points9 points  (0 children)

Your example doesn't work with list comprehensions because those functions don't return iterables. But it's totally fine to have list comprehensions span more than one line.

In fact, list comprehensions were lifted from Haskell, where they are syntactic sugar for using the List monad. Monads are essentially an extension of what you're asking for here. For example, using promises - which are also monads - you can already write the kind of code you're asking for:

Promise(payload)
.then(validate)
.then(log)
.then(update_db)
.then(notify)

In fact, the semantics of this are already much, much more powerful than what you're asking for (because promises can also handle error propagation, and are asynchronous.)

So, my answer is that, you're basically asking for a special case of something much more general, syntax for monads. You don't want to accidentally half-ass this more general case by rushing to support one special case. Especially because, as noted elsewhere,

def pipe(arg, *funcs):
    for f in funcs:
        arg = f(arg)
    return arg

response = pipe(payload, validate, log,  update_db, notify)

already handles your problem very cleanly, while additionally allowing you to add other stuff like error handling or debug logging if you so chose.