[Python-ideas] Anonymous blocks (again):

Andrew Barnert abarnert at yahoo.com
Tue May 14 18:42:37 CEST 2013


On May 13, 2013, at 18:55, Haoyi Li <haoyi.sg at gmail.com> wrote:

> I do not think expression soup is particularly bad, it's just Javascript's implementation (as usual) that is pretty borked. In Scala, expression chaining lets you do some pretty nice things:
> 
> 
> 
>       memory.take(freePointer)
> 
> 
>             .grouped(10)
> 
> 
>             .map(_.fold("")(_+"\t"+_))
> 
> 
>             .reduce(_+"\n"+_)

In Python, all of these are non-mutating functions that return a new value. And you _can_ chain them together. Exactly the same way you would in Lisp, ML, or Haskell, or even JavaScript.

What JavaScript and other "fluent" languages add is a way to chain together _mutating_ methods. This hides a fundamental distinction between map and append, or sorted and sort. And that part is the problem. 

The mutability distinction is closely tied to the expression/assignment distinction. Notice that modern fluent languages also try to make _everything_ an expression, even for loops. And traditional (lispy) functional languages that don't have statements build the equivalent of the expression/statement distinction on top of mutability (e.g., set special forms).

You could point out that Scala is more readable than the Python equivalent, something like this:

    reduce(lambda ...,
                map(reduce(...
                     grouped(10, ...

And yes, that's a mess. But that's a separate issue. It's not because map doesn't return anything, it's because map isn't a method of list.

If your point about extra code creeping into the middle is that chaining methods, as opposed to chaining functions calls, discourages unnecessarily turning expressions into statements... That's an interesting point. But a separate argument from fluency, None-returning mutators, and statements that aren't expressions.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130514/e4747bd0/attachment-0001.html>


More information about the Python-ideas mailing list