<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Why Hate the for Loop?</title>
	<atom:link href="http://cafe.elharo.com/blogroll/why-hate-the-for-loop/feed/" rel="self" type="application/rss+xml" />
	<link>http://cafe.elharo.com/blogroll/why-hate-the-for-loop/</link>
	<description>Longer than a blog; shorter than a book</description>
	<lastBuildDate>Tue, 09 Mar 2010 18:31:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: postmodern</title>
		<link>http://cafe.elharo.com/blogroll/why-hate-the-for-loop/comment-page-1/#comment-101031</link>
		<dc:creator>postmodern</dc:creator>
		<pubDate>Wed, 13 Jun 2007 20:30:39 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/why-hate-the-for-loop/#comment-101031</guid>
		<description>Closures are also a tool for building concise methods by yielding control to a block instead of forcing the programmer to directly access the data from the object. For instance (haha pun) the join and map methods of class Array in Ruby.

[&quot;args&quot;, &quot;one&quot;, &quot;two&quot;, &quot;three].join(&quot;, &quot;)

or

args.reverse.join(&quot;, &quot;)

[5, 4, 2, 1].map { &#124;n&#124; n**2 }</description>
		<content:encoded><![CDATA[<p>Closures are also a tool for building concise methods by yielding control to a block instead of forcing the programmer to directly access the data from the object. For instance (haha pun) the join and map methods of class Array in Ruby.</p>
<p>["args", "one", "two", "three].join(&#8220;, &#8220;)</p>
<p>or</p>
<p>args.reverse.join(&#8220;, &#8220;)</p>
<p>[5, 4, 2, 1].map { |n| n**2 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nothing happens</title>
		<link>http://cafe.elharo.com/blogroll/why-hate-the-for-loop/comment-page-1/#comment-73259</link>
		<dc:creator>nothing happens</dc:creator>
		<pubDate>Thu, 05 Apr 2007 16:38:10 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/why-hate-the-for-loop/#comment-73259</guid>
		<description>[...] But the explanation that hit home most for me was by Adam Turoff at his site Notes On Haskell, in which he responds to a closures-skeptic who asks why peeps be hatin&#8217; on the good ol&#8217; &#8220;for&#8221; loop. [...]</description>
		<content:encoded><![CDATA[<p>[...] But the explanation that hit home most for me was by Adam Turoff at his site Notes On Haskell, in which he responds to a closures-skeptic who asks why peeps be hatin&#8217; on the good ol&#8217; &#8220;for&#8221; loop. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pure Danger Tech &#187; Blog Archive &#187; Java 7 Roundup (Feb 14th)</title>
		<link>http://cafe.elharo.com/blogroll/why-hate-the-for-loop/comment-page-1/#comment-56351</link>
		<dc:creator>Pure Danger Tech &#187; Blog Archive &#187; Java 7 Roundup (Feb 14th)</dc:creator>
		<pubDate>Wed, 14 Feb 2007 16:03:51 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/why-hate-the-for-loop/#comment-56351</guid>
		<description>[...] Elliotte Rusty Harold posted some closure related musings as &#8220;Why hate the for loop?&#8221; [...]</description>
		<content:encoded><![CDATA[<p>[...] Elliotte Rusty Harold posted some closure related musings as &#8220;Why hate the for loop?&#8221; [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: refactor.it &#187; Blog Archive &#187; I nemici di â€œforâ€ il loop</title>
		<link>http://cafe.elharo.com/blogroll/why-hate-the-for-loop/comment-page-1/#comment-55429</link>
		<dc:creator>refactor.it &#187; Blog Archive &#187; I nemici di â€œforâ€ il loop</dc:creator>
		<pubDate>Sat, 10 Feb 2007 22:24:29 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/why-hate-the-for-loop/#comment-55429</guid>
		<description>[...] Elliotte Rusty Harold parte da una proposta di sintassi per le closures in Java e finisce per domandarsi come mai cosÃ¬ spesso il loop di for diventa il bersaglio di cosÃ¬ tante critiche e, di conseguenza, di cosÃ¬ tante alternative. In effetti, nemmeno io ci sono molto affezionatoâ€¦ :-/ [...]</description>
		<content:encoded><![CDATA[<p>[...] Elliotte Rusty Harold parte da una proposta di sintassi per le closures in Java e finisce per domandarsi come mai cosÃ¬ spesso il loop di for diventa il bersaglio di cosÃ¬ tante critiche e, di conseguenza, di cosÃ¬ tante alternative. In effetti, nemmeno io ci sono molto affezionatoâ€¦ :-/ [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joseph. E. Davis</title>
		<link>http://cafe.elharo.com/blogroll/why-hate-the-for-loop/comment-page-1/#comment-55263</link>
		<dc:creator>Joseph. E. Davis</dc:creator>
		<pubDate>Sat, 10 Feb 2007 03:14:08 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/why-hate-the-for-loop/#comment-55263</guid>
		<description>Why, yes.</description>
		<content:encoded><![CDATA[<p>Why, yes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam</title>
		<link>http://cafe.elharo.com/blogroll/why-hate-the-for-loop/comment-page-1/#comment-55240</link>
		<dc:creator>Sam</dc:creator>
		<pubDate>Fri, 09 Feb 2007 23:07:34 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/why-hate-the-for-loop/#comment-55240</guid>
		<description>Joseph: Are you implying that there is something wrong with that argument?</description>
		<content:encoded><![CDATA[<p>Joseph: Are you implying that there is something wrong with that argument?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias</title>
		<link>http://cafe.elharo.com/blogroll/why-hate-the-for-loop/comment-page-1/#comment-55203</link>
		<dc:creator>Matthias</dc:creator>
		<pubDate>Fri, 09 Feb 2007 18:44:11 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/why-hate-the-for-loop/#comment-55203</guid>
		<description>Dude! It&#039;s about preventing bugs at compile time!

How do you guarantee at compile time that, inside your &quot;indexed for&quot; loop, you did not accidentally walk out of your data structure (array, dom tree, whatever...)? Such an error will never occur with the new for syntax!

But this is complete besides the point of adding closures to a programming language ...</description>
		<content:encoded><![CDATA[<p>Dude! It&#8217;s about preventing bugs at compile time!</p>
<p>How do you guarantee at compile time that, inside your &#8220;indexed for&#8221; loop, you did not accidentally walk out of your data structure (array, dom tree, whatever&#8230;)? Such an error will never occur with the new for syntax!</p>
<p>But this is complete besides the point of adding closures to a programming language &#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joseph. E. Davis</title>
		<link>http://cafe.elharo.com/blogroll/why-hate-the-for-loop/comment-page-1/#comment-55199</link>
		<dc:creator>Joseph. E. Davis</dc:creator>
		<pubDate>Fri, 09 Feb 2007 18:20:43 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/why-hate-the-for-loop/#comment-55199</guid>
		<description>This argument is like saying Java is pointless because it&#039;s easier to write &quot;Hello World&quot; on paper than go to the trouble of entering the canonical first Java program, compiling, and running it.

Loop constructs are simply a basic example to demonstrate closures.  Closures are much deeper and more powerful than that.</description>
		<content:encoded><![CDATA[<p>This argument is like saying Java is pointless because it&#8217;s easier to write &#8220;Hello World&#8221; on paper than go to the trouble of entering the canonical first Java program, compiling, and running it.</p>
<p>Loop constructs are simply a basic example to demonstrate closures.  Closures are much deeper and more powerful than that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Masklinn</title>
		<link>http://cafe.elharo.com/blogroll/why-hate-the-for-loop/comment-page-1/#comment-55155</link>
		<dc:creator>Masklinn</dc:creator>
		<pubDate>Fri, 09 Feb 2007 13:55:23 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/why-hate-the-for-loop/#comment-55155</guid>
		<description>&gt; However, sometimes you actually do need a particular order of execution

And in these cases you&#039;ll use a construct that guarantees order of execution

&gt; String concatenation is not commutative, and itâ€™s really important that we add the strings in the proper order.

Wrong, that&#039;s not what you want, what you want is to have the final string in the correct order, the order of concatenation does not matter, the computer could join the last two strings of the list first for all you care, as long as the final string is the one you wanted _it does not matter_ and saying otherwise is a fallacy. Not to mention, of course, the fact that this example is a strawman since most languages handle string-concatenation via native constructs, which means that you don&#039;t need to use `for` loops anyway.

Now since you went through so many efforts to build that strawman we&#039;re going to refute it shall we? And we&#039;re going to refute it using the tools that fits your fallacy the most: a functional, lazy-evaluating (which means that it _really_ doesn&#039;t guarantee order of evaluation) language: Haskell

&#160; &gt; let s = foldl1 (++) array

Well, it wasn&#039;t that complex was it?

It shall of course be noted that most people would use the function built in the standard prelude and write `let s = concat array`.

And in most case you don&#039;t care about the order of execution, the only thing you care about is the order of the result (note: for the cases when you do care about OOE, the languages that may break it all offer constructs that allow you to fix it however you want). Case in point: the `map` construct.

`map` is really simple: it applies a function each value of a collection, and returns a collection of the results _where the order of the results is the same as the order of the initial value_.

Does this mean that it can&#039;t be parallelized? Of course not, it just mean that you have to resync the final collection of all the values, you can see how to do that via Joe Armstrong&#039;s implementation of a simple `pmap` (Parallel Map): http://www.erlang.org/ml-archive/erlang-questions/200606/msg00187.html

The order of execution is unspecified, the mapping is completely parallelized, yet the result collection is ordered which is exactly what we wanted.

&gt; Thus the closure syntax and the for syntax really arenâ€™t equivalent and closures canâ€™t replace for loops.
Actually they can, they&#039;re just an example of inner iteration versus Java&#039;s or Python&#039;s &quot;outer iteration&quot; (via iteration objects) or -- worse -- Java&#039;s or C&#039;s &quot;manual iteration&quot; (via regular for loops). Smalltalk showed more than 20 years ago that inner iteration worked, Ruby merely took from Smalltalk&#039;s experience.

And you also forgot another information, or you didn&#039;t realize it: closures, or anonymous functions in general, can do much more than just iteration:

Want transactions? In Java, you have to setup and acquire your transaction manually, then remember to commit or rollback it at the end. How could you do that in Ruby? Well something along the lines of:

db_object.transaction do &#124;db&#124;
&#160;&#160;&#160;&#160; # do stuff
end

You&#039;re done, the transaction method takes a block and will handle all the low-level gritty stuff such as starting the transaction and committing or rollbacking it when then block has been executed.

Reading a file?

File.open &quot;myfile.txt&quot; do &#124;f&#124;
&#160;&#160;&#160;&#160; # do stuff with your file
end

Once you reach the end of the block the file will be neatly closed, _always_, if an exception is thrown from the block the file will still be cleanly closed. No need to muck around with nested levels of trys and excepts and finallys just to be sure that you can&#039;t leave a file open.

People usually show the iterative advantage of blocks via inner iteration, but that&#039;s only because it&#039;s the lowest level of simplicity, the one that shocks the most, and the one that _everyone_ will use first.

But blocks/anonymous functions/first class functions are not limited to iteration, far from that.

&gt; In functional systems, this works because there are no side-effects. Thread safety is almost free. However, this isnâ€™t true in Java. In Java you have to think very carefully about thread safety, and typical closure code doesnâ€™t do that.

Guess what? Smalltalk and Ruby are not functional either. And most functional languages aren&#039;t &quot;pure&quot;, meaning they&#039;re not side-effects free (they nearly all use the single-assignment principle though) hence thread safety wouldn&#039;t be &quot;almost free&quot; if they did indeed use threads (advanced concurrent functional languages don&#039;t).</description>
		<content:encoded><![CDATA[<p>&gt; However, sometimes you actually do need a particular order of execution</p>
<p>And in these cases you&#8217;ll use a construct that guarantees order of execution</p>
<p>&gt; String concatenation is not commutative, and itâ€™s really important that we add the strings in the proper order.</p>
<p>Wrong, that&#8217;s not what you want, what you want is to have the final string in the correct order, the order of concatenation does not matter, the computer could join the last two strings of the list first for all you care, as long as the final string is the one you wanted _it does not matter_ and saying otherwise is a fallacy. Not to mention, of course, the fact that this example is a strawman since most languages handle string-concatenation via native constructs, which means that you don&#8217;t need to use `for` loops anyway.</p>
<p>Now since you went through so many efforts to build that strawman we&#8217;re going to refute it shall we? And we&#8217;re going to refute it using the tools that fits your fallacy the most: a functional, lazy-evaluating (which means that it _really_ doesn&#8217;t guarantee order of evaluation) language: Haskell</p>
<p>&nbsp; &gt; let s = foldl1 (++) array</p>
<p>Well, it wasn&#8217;t that complex was it?</p>
<p>It shall of course be noted that most people would use the function built in the standard prelude and write `let s = concat array`.</p>
<p>And in most case you don&#8217;t care about the order of execution, the only thing you care about is the order of the result (note: for the cases when you do care about OOE, the languages that may break it all offer constructs that allow you to fix it however you want). Case in point: the `map` construct.</p>
<p>`map` is really simple: it applies a function each value of a collection, and returns a collection of the results _where the order of the results is the same as the order of the initial value_.</p>
<p>Does this mean that it can&#8217;t be parallelized? Of course not, it just mean that you have to resync the final collection of all the values, you can see how to do that via Joe Armstrong&#8217;s implementation of a simple `pmap` (Parallel Map): <a href="http://www.erlang.org/ml-archive/erlang-questions/200606/msg00187.html" rel="nofollow">http://www.erlang.org/ml-archive/erlang-questions/200606/msg00187.html</a></p>
<p>The order of execution is unspecified, the mapping is completely parallelized, yet the result collection is ordered which is exactly what we wanted.</p>
<p>&gt; Thus the closure syntax and the for syntax really arenâ€™t equivalent and closures canâ€™t replace for loops.<br />
Actually they can, they&#8217;re just an example of inner iteration versus Java&#8217;s or Python&#8217;s &#8220;outer iteration&#8221; (via iteration objects) or &#8212; worse &#8212; Java&#8217;s or C&#8217;s &#8220;manual iteration&#8221; (via regular for loops). Smalltalk showed more than 20 years ago that inner iteration worked, Ruby merely took from Smalltalk&#8217;s experience.</p>
<p>And you also forgot another information, or you didn&#8217;t realize it: closures, or anonymous functions in general, can do much more than just iteration:</p>
<p>Want transactions? In Java, you have to setup and acquire your transaction manually, then remember to commit or rollback it at the end. How could you do that in Ruby? Well something along the lines of:</p>
<p>db_object.transaction do |db|<br />
&nbsp;&nbsp;&nbsp;&nbsp; # do stuff<br />
end</p>
<p>You&#8217;re done, the transaction method takes a block and will handle all the low-level gritty stuff such as starting the transaction and committing or rollbacking it when then block has been executed.</p>
<p>Reading a file?</p>
<p>File.open &#8220;myfile.txt&#8221; do |f|<br />
&nbsp;&nbsp;&nbsp;&nbsp; # do stuff with your file<br />
end</p>
<p>Once you reach the end of the block the file will be neatly closed, _always_, if an exception is thrown from the block the file will still be cleanly closed. No need to muck around with nested levels of trys and excepts and finallys just to be sure that you can&#8217;t leave a file open.</p>
<p>People usually show the iterative advantage of blocks via inner iteration, but that&#8217;s only because it&#8217;s the lowest level of simplicity, the one that shocks the most, and the one that _everyone_ will use first.</p>
<p>But blocks/anonymous functions/first class functions are not limited to iteration, far from that.</p>
<p>&gt; In functional systems, this works because there are no side-effects. Thread safety is almost free. However, this isnâ€™t true in Java. In Java you have to think very carefully about thread safety, and typical closure code doesnâ€™t do that.</p>
<p>Guess what? Smalltalk and Ruby are not functional either. And most functional languages aren&#8217;t &#8220;pure&#8221;, meaning they&#8217;re not side-effects free (they nearly all use the single-assignment principle though) hence thread safety wouldn&#8217;t be &#8220;almost free&#8221; if they did indeed use threads (advanced concurrent functional languages don&#8217;t).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony Morris</title>
		<link>http://cafe.elharo.com/blogroll/why-hate-the-for-loop/comment-page-1/#comment-55138</link>
		<dc:creator>Tony Morris</dc:creator>
		<pubDate>Fri, 09 Feb 2007 11:34:37 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/why-hate-the-for-loop/#comment-55138</guid>
		<description>You mention remove: This is an example where iterators are useful. I usually use the following code to remove all elements of a certain kind:
for (int i = 0; i ) {
list.remove(i);
} else {
i++;
}
}

Correction.
The filter function is useful - and yes without the blubby (is that a word Paul?) looping constructs.
filter (&lt;10) [1..20]
[1,2,3,4,5,6,7,8,9]

Prefer a higher-order function over loops - think about it some more.</description>
		<content:encoded><![CDATA[<p>You mention remove: This is an example where iterators are useful. I usually use the following code to remove all elements of a certain kind:<br />
for (int i = 0; i ) {<br />
list.remove(i);<br />
} else {<br />
i++;<br />
}<br />
}</p>
<p>Correction.<br />
The filter function is useful &#8211; and yes without the blubby (is that a word Paul?) looping constructs.<br />
filter (&lt;10) [1..20]<br />
[1,2,3,4,5,6,7,8,9]</p>
<p>Prefer a higher-order function over loops &#8211; think about it some more.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
