<div dir="ltr">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:<div><br>
<div><pre style="margin-top:0px;margin-bottom:0px;padding:0px;border:0px;font-size:12px;font-family:Consolas,'Liberation Mono',Courier,monospace;color:rgb(51,51,51);line-height:18px"><div class="" id="LC33" style="margin:0px;padding:0px 0px 0px 10px;border:0px">
<span class="" style="margin:0px;padding:0px;border:0px"> memory</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">.</span><span class="" style="margin:0px;padding:0px;border:0px">take</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">(</span><span class="" style="margin:0px;padding:0px;border:0px">freePointer</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">)</span></div>
<div class="" id="LC34" style="margin:0px;padding:0px 0px 0px 10px;border:0px"> <span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">.</span><span class="" style="margin:0px;padding:0px;border:0px">grouped</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">(</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(0,153,153)">10</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">)</span></div>
<div class="" id="LC36" style="margin:0px;padding:0px 0px 0px 10px;border:0px"> <span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">.</span><span class="" style="margin:0px;padding:0px;border:0px">map</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">(</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">_</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">.</span><span class="" style="margin:0px;padding:0px;border:0px">fold</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">("")(</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">_</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">+</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(221,17,68)">"\t"</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">+</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">_</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">))</span></div>
<div class="" id="LC37" style="margin:0px;padding:0px 0px 0px 10px;border:0px"> <span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">.</span><span class="" style="margin:0px;padding:0px;border:0px">reduce</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">(</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">_</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">+</span><span class="" style="margin:0px;padding:0px;border:0px;color:rgb(221,17,68)">"\n"</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">+</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">_</span><span class="" style="margin:0px;padding:0px;border:0px;font-weight:bold">)</span></div>
</pre></div><div style><br></div><div style>This code converts a flat array of ints into pretty 10-ints-per-row heap dumps, and is probably nicer than most things you would write using for loops and variables.</div><div style>
<br></div></div><div style>In general, method chaining is the same as having an implicit "this" object that you are operating on without needing to specify it (since it gets returned by each method). Apart from saving syntax (less tokens to write) this also reduces the number of possible ways you can do things. I mean, if you write this:</div>
<div style><br></div><div style><pre class="" style="font-size:12px;line-height:16px;font-family:Consolas,'Liberation Mono',Courier,monospace;word-wrap:break-word;width:744px;margin-top:0px;margin-bottom:0px;padding:0px;color:rgb(0,0,0)">
<div class="" id="file-gistfile1-scala-LC1"><span class="" style="color:rgb(51,51,51)">memory</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">take</span><span class="" style="font-weight:bold">(</span><span class="" style="color:rgb(51,51,51)">freePointer</span><span class="" style="font-weight:bold">)</span></div>
<div class="" id="file-gistfile1-scala-LC3"><span class="" style="color:rgb(51,51,51)">memory</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">grouped</span><span class="" style="font-weight:bold">(</span><span class="" style="color:rgb(0,153,153)">10</span><span class="" style="font-weight:bold">)</span></div>
<div class="" id="file-gistfile1-scala-LC5"><span class="" style="color:rgb(51,51,51)">memory</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">map</span><span class="" style="font-weight:bold">(</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">map</span><span class="" style="font-weight:bold">(</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">+</span><span class="" style="color:rgb(221,17,68)">""</span><span class="" style="font-weight:bold">))</span></div>
<div class="" id="file-gistfile1-scala-LC11"><span class="" style="color:rgb(51,51,51)">memory</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">map</span><span class="" style="font-weight:bold">(</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">reduce</span><span class="" style="font-weight:bold">(</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">+</span><span class="" style="color:rgb(221,17,68)">"\t"</span><span class="" style="font-weight:bold">+</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">))</span></div>
<div class="" id="file-gistfile1-scala-LC12"><span class="" style="color:rgb(51,51,51)">memory</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">reduce</span><span class="" style="font-weight:bold">(</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">+</span><span class="" style="color:rgb(221,17,68)">"\n"</span><span class="" style="font-weight:bold">+</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">)</span></div>
</pre></div><div style><br></div><div style>It's about the same; but how many times have you seen code like this:</div><div style><br></div><div style><pre class="" style="font-size:12px;line-height:16px;font-family:Consolas,'Liberation Mono',Courier,monospace;word-wrap:break-word;width:744px;margin-top:0px;margin-bottom:0px;padding:0px">
<div class="" id="file-gistfile1-scala-LC1" style="color:rgb(0,0,0)"><span class="" style="color:rgb(51,51,51)">memory</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">take</span><span class="" style="font-weight:bold">(</span><span class="" style="color:rgb(51,51,51)">freePointer</span><span class="" style="font-weight:bold">)</span></div>
<div class="" id="file-gistfile1-scala-LC2" style="color:rgb(0,0,0)"><span class="" style="color:rgb(153,153,136);font-style:italic">// some comment</span></div><div class="" id="file-gistfile1-scala-LC3" style="color:rgb(0,0,0)">
<span class="" style="color:rgb(51,51,51)">memory</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">grouped</span><span class="" style="font-weight:bold">(</span><span class="" style="color:rgb(0,153,153)">10</span><span class="" style="font-weight:bold">)</span></div>
<div class="" id="file-gistfile1-scala-LC4" style="color:rgb(0,0,0)"><span class="" style="color:rgb(51,51,51)">unrelatedthing</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">dostuff</span><span class="" style="font-weight:bold">()</span></div>
<div class="" id="file-gistfile1-scala-LC5" style="color:rgb(0,0,0)"><span class="" style="color:rgb(51,51,51)">memory</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">map</span><span class="" style="font-weight:bold">(</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">map</span><span class="" style="font-weight:bold">(</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">+</span><span class="" style="color:rgb(221,17,68)">""</span><span class="" style="font-weight:bold">))</span></div>
<div class="" id="file-gistfile1-scala-LC6" style="color:rgb(0,0,0)"><span class="" style="color:rgb(51,51,51)">unrelatedthing</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">domorestuff</span><span class="" style="font-weight:bold">()</span></div>
<div class="" id="file-gistfile1-scala-LC7" style="color:rgb(0,0,0)"><span class="" style="color:rgb(153,153,136);font-style:italic">/**</span></div><div class="" id="file-gistfile1-scala-LC8" style="color:rgb(0,0,0)"><span class="" style="color:rgb(153,153,136);font-style:italic"> * SOME BIG COMMENT</span></div>
<div class="" id="file-gistfile1-scala-LC9" style="color:rgb(0,0,0)"><span class="" style="color:rgb(153,153,136);font-style:italic"> * i am cow </span></div><div class="" id="file-gistfile1-scala-LC9" style="color:rgb(0,0,0)">
<span class="" style="color:rgb(153,153,136);font-style:italic"> * hear me moo</span></div><div class="" id="file-gistfile1-scala-LC9" style="color:rgb(0,0,0)"><span class="" style="color:rgb(153,153,136);font-style:italic"> * i weight twice as much as you</span></div>
<div class="" id="file-gistfile1-scala-LC9" style="color:rgb(0,0,0)"><span class="" style="color:rgb(153,153,136);font-style:italic"> * and i look good on the barbecue</span></div><div class="" id="file-gistfile1-scala-LC10" style="color:rgb(0,0,0)">
<span class="" style="color:rgb(153,153,136);font-style:italic"> */</span></div><div class="" id="file-gistfile1-scala-LC10"><span class="" style="font-style:italic"><span class="" style="font-style:normal"><font color="#333333">do_stuff_with_cows</font></span><span class="" style="color:rgb(0,0,0);font-style:normal;font-weight:bold">()</span><font color="#999988"><br>
</font></span></div><div class="" id="file-gistfile1-scala-LC11" style="color:rgb(0,0,0)"><span class="" style="color:rgb(51,51,51)">memory</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">map</span><span class="" style="font-weight:bold">(</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">reduce</span><span class="" style="font-weight:bold">(</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">+</span><span class="" style="color:rgb(221,17,68)">"\t"</span><span class="" style="font-weight:bold">+</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">))</span></div>
<div class="" id="file-gistfile1-scala-LC12" style="color:rgb(0,0,0)"><span class="" style="color:rgb(51,51,51)">memory</span><span class="" style="font-weight:bold">.</span><span class="" style="color:rgb(51,51,51)">reduce</span><span class="" style="font-weight:bold">(</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">+</span><span class="" style="color:rgb(221,17,68)">"\n"</span><span class="" style="font-weight:bold">+</span><span class="" style="font-weight:bold">_</span><span class="" style="font-weight:bold">)</span></div>
</pre></div><div style><br></div><div style>Which makes it a huge pain to figure out what is going on with <span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Courier,monospace;font-size:12px;line-height:16px;white-space:pre">memory</span>? Method chaining <i>prevents</i> this sort of thing from happening in the first place, which is really nice. Even if I try to avoid this, I haven't seen any code base where this hasn't happened in various places, causing endless headaches.</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, May 13, 2013 at 9:36 PM, Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On 14/05/13 03:23, Juancarlo Añez wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Mon, May 13, 2013 at 11:58 AM, Terry Jan Reedy <<a href="mailto:tjreedy@udel.edu" target="_blank">tjreedy@udel.edu</a>> wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I disagree that the above is unpythonic. In Python, functions are objects<br>
like everything else. Define them, pass them to functions, like anything<br>
else. 'Unpythonic' is treating functions as special, other than that they<br>
are called (have a call method).<br>
</blockquote>
<br>
<br>
I beg to disagree. Functions are objects in python, but they get particular<br>
treatment.<br>
<br>
You can do:<br>
<br>
def f():<br>
pass<br>
x = f<br>
<br>
<br>
But you can't do:<br>
<br>
x = def(): pass<br>
</blockquote>
<br>
<br></div></div>
That has nothing to do with *function objects*, and everything to do with the *keyword* def being a statement. If you avoid "def", you can do this:<br>
<br>
x = lambda: None<br>
<br>
<br>
or even this:<br>
<br>
from types import FunctionType<br>
x = FunctionType(code, globals, name, argdefs, closure)<br>
<br>
<br>
Creating functions in this way is not exactly convenient, but it is possible.<br>
<br>
<br>
There is a sense in which functions (and classes, and modules) are "different", not because Python chooses to treat them differently, but because they are inherently different. They are complex, almost free-form compound objects, and there is no "nice" syntax for creating them inside an expression. The closest Python comes to is lambda for functions, and that is limited to a single expression.<br>
<br>
But regardless of the difficulty of creating a function object, once you have one, it is a first class object. Anything you can do with any other object, you can do with a function object. I'm with Terry on this: the code snippet you gave, where a function object is passed to another function, is a standard Python idiom and perfectly pythonic.<br>
<br>
It's not uncommon to have to create data before you use it, even when you could create it in-place where you use it. E.g. we might choose to write:<br>
<br>
data = [lots of items here]<br>
x = some_value()<br>
result = func(x, data)<br>
<br>
<br>
instead of:<br>
<br>
result = func(some_value(), [lots of items here])<br>
<br>
<br>
to make the function call more readable, or to keep to some maximum line length, or in order to re-use some of the values. So even when we *can* embed values in a call, often we choose not to. The fact that you don't have a choice when it comes to functions is not a major problem. Even if you could write:<br>
<br>
result = func(def (): lots of code goes here)<br>
<br>
<br>
you probably shouldn't.<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
<br>
-- <br>
Steven</font></span><div class="HOEnZb"><div class="h5"><br>
______________________________<u></u>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br></div>