Hi, All,<div><br></div><div>As Masklinn pointed out, the issue is that there's no concise way to accumulate or compress an iterable without resorting to the use of the reduce() function.<div><br></div><div>To answer Greg's point, it's not like list comprehensions cover all the cases allowed by the concept of mapping. There are cases, such as when the construction rule is too complicated, where comprehensions cannot be applied. Given that, why not explore the idea of if and how we can make (simple) reductions more readable.</div>
<div> </div><div>To me, the tricky part was resolving the ambiguity between a map and reduce expression in the comprehension. Given that the first argument to the reduce() function is the accumulated value, which is something that the map() function doesn't need, I felt that we could use that to make the distinction between the two in the comprehension. In particular, if the expression in the comprehension refers to the accumulated value (through the "_" variable), then it could treat it as a reduction as opposed to a mapping.</div>
<div><br></div><div>Here's another example to illustrate the desired behavior, which basically adds a list of numbers (without resorting to the use of a reduce()):</div><div><br></div><div>>>> numbers=[1,2,3,4,5]</div>
<div>>>> [_+number for number in numbers]</div><div>15 </div><div><br></div><div>Just to clarify, we're just talking about some syntactic sugar here, nothing more, nothing less.</div><div><br></div><div>Best Regards,<br>
Karthick Sankarachary<br>
<br><br><div class="gmail_quote">On Mon, Oct 10, 2011 at 2:26 PM, Greg Ewing <span dir="ltr"><<a href="mailto:greg.ewing@canterbury.ac.nz">greg.ewing@canterbury.ac.nz</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">Karthick Sankarachary wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
For one thing, list comprehensions don't let you create immutable sequences (such as a tuple) by design. For another, it does not provide a concise alternative to reduce() function.<br>
</blockquote>
<br></div>
There was a big discussion a while back about a syntax for<br>
reduction operations. The conclusion reached was that the<br>
concept of reduction has so many degrees of freedom that,<br>
beyond the very simple cases covered by the reduce() function,<br>
nothing is clearer than simply writing out the loop explicitly.<br><font color="#888888">
<br>
-- <br>
Greg</font><div><div></div><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></div>