<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Scala Code Review: foldLeft and foldRight</title>
	<atom:link href="http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/feed/" rel="self" type="application/rss+xml" />
	<link>http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/</link>
	<description></description>
	<lastBuildDate>Wed, 25 Jan 2012 15:21:25 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Matt</title>
		<link>http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/#comment-715</link>
		<dc:creator><![CDATA[Matt]]></dc:creator>
		<pubDate>Thu, 01 Sep 2011 22:34:23 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=239#comment-715</guid>
		<description><![CDATA[Thanks Christopher. Those look like good alternatives to a plain List.]]></description>
		<content:encoded><![CDATA[<p>Thanks Christopher. Those look like good alternatives to a plain List.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christopher Currie</title>
		<link>http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/#comment-714</link>
		<dc:creator><![CDATA[Christopher Currie]]></dc:creator>
		<pubDate>Wed, 31 Aug 2011 19:22:29 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=239#comment-714</guid>
		<description><![CDATA[For future readers, this post was written just before Scala 2.8 was released. in 2.8+, non-strict collections are your friends:

scala&gt; 1 to 325000
res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3...
scala&gt; res0.foldRight(&quot;&quot;)((x,y)=&gt;&quot;X&quot;)
res1: java.lang.String = X

scala&gt; res0.toList.view
res2: java.lang.Object with scala.collection.SeqView[Int,List[Int]] = SeqView(...)

scala&gt; res2.foldRight(&quot;&quot;)((x,y)=&gt;&quot;X&quot;)
res3: java.lang.String = X]]></description>
		<content:encoded><![CDATA[<p>For future readers, this post was written just before Scala 2.8 was released. in 2.8+, non-strict collections are your friends:</p>
<p>scala&gt; 1 to 325000<br />
res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3&#8230;<br />
scala&gt; res0.foldRight(&#8220;&#8221;)((x,y)=&gt;&#8221;X&#8221;)<br />
res1: java.lang.String = X</p>
<p>scala&gt; res0.toList.view<br />
res2: java.lang.Object with scala.collection.SeqView[Int,List[Int]] = SeqView(&#8230;)</p>
<p>scala&gt; res2.foldRight(&#8220;&#8221;)((x,y)=&gt;&#8221;X&#8221;)<br />
res3: java.lang.String = X</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Apache Camel med Scala &#171; Stacktrace.se</title>
		<link>http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/#comment-698</link>
		<dc:creator><![CDATA[Apache Camel med Scala &#171; Stacktrace.se]]></dc:creator>
		<pubDate>Tue, 22 Mar 2011 14:57:19 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=239#comment-698</guid>
		<description><![CDATA[[...] sista vi gör är att använda den högre ordningens funktion foldLeft på översättningslistan. Här ser vi hur funktionell programmering kan bli otroligt kompakt och [...]]]></description>
		<content:encoded><![CDATA[<p>[...] sista vi gör är att använda den högre ordningens funktion foldLeft på översättningslistan. Här ser vi hur funktionell programmering kan bli otroligt kompakt och [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/#comment-693</link>
		<dc:creator><![CDATA[Matt]]></dc:creator>
		<pubDate>Mon, 21 Feb 2011 01:28:51 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=239#comment-693</guid>
		<description><![CDATA[Never!]]></description>
		<content:encoded><![CDATA[<p>Never!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh</title>
		<link>http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/#comment-692</link>
		<dc:creator><![CDATA[Josh]]></dc:creator>
		<pubDate>Sun, 20 Feb 2011 15:43:59 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=239#comment-692</guid>
		<description><![CDATA[Please, stop punitively punishing us with your puns, pal.]]></description>
		<content:encoded><![CDATA[<p>Please, stop punitively punishing us with your puns, pal.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eddy</title>
		<link>http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/#comment-655</link>
		<dc:creator><![CDATA[eddy]]></dc:creator>
		<pubDate>Mon, 29 Nov 2010 19:20:21 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=239#comment-655</guid>
		<description><![CDATA[I think it&#039;s more a case of providing room for optimisation within the compiler, in the future. The compiler *could* short-circuit certain situations, meaning that deprecating foldRight just now would be unwise until such optimisation has been attempted.

As Knuth said: &quot;We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil&quot;

Thanks for your post; it was a good read.]]></description>
		<content:encoded><![CDATA[<p>I think it&#8217;s more a case of providing room for optimisation within the compiler, in the future. The compiler *could* short-circuit certain situations, meaning that deprecating foldRight just now would be unwise until such optimisation has been attempted.</p>
<p>As Knuth said: &#8220;We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil&#8221;</p>
<p>Thanks for your post; it was a good read.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/#comment-647</link>
		<dc:creator><![CDATA[Matt]]></dc:creator>
		<pubDate>Tue, 10 Aug 2010 22:46:10 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=239#comment-647</guid>
		<description><![CDATA[Zoheb, your comment is interesting, but I&#039;m having trouble understanding how it can be so.

Firstly, you say that reverse.foldLeft is at least O(n).  I don&#039;t see how it can be anything other than O(n).  Reverse is clear enough.  It builds a second singly linked list out of the first, one item at a time.  That&#039;s O(n).  And then foldLeft runs, which is implemented as a while loop.  That&#039;s O(n).  The combination is O(n+n) which is the same as O(n).

Secondly (and this is the one that I&#039;ve been puzzling over) I don&#039;t think foldRight runs in O(1).  Let&#039;s take the foldRight call in your example.

&lt;pre&gt;
scala&gt; var x: Boolean = true
x: Boolean = true

scala&gt; def time = (new java.util.Date).getTime
time: Long

scala&gt; var list = (1 to 10).toList.map(x =&gt; x==1)
list: List[Boolean] = List(true, false, false, false, false, false, false, false, false, false)

scala&gt; val t1 = time; for (_  val t1 = time; for (_ &lt;- 1 to 10000) { x=list.foldRight(false)(_&#124;&#124;_) }; println (time - t1)
16
t1: Long = 1281477156551
&lt;/pre&gt;

This foldRight applied to a list of 10 booleans 10,000 times takes 16 milliseconds.

&lt;pre&gt;
scala&gt; list = (1 to 100).toList.map(x =&gt; x==1)
list: List[Boolean] = List(true, false, false, false, ...

scala&gt; val t1 = time; for (_  val t1 = time; for (_ &lt;- 1 to 10000) { x=list.foldRight(false)(_&#124;&#124;_) }; println (time - t1)
94
t1: Long = 1281477234035
&lt;/pre&gt;

This foldRight applied to a list of 100 booleans 10,000 times takes 94 milliseconds. That&#039;s 10 times as many items and it took about 6 times as long.

&lt;pre&gt;
scala&gt; list = (1 to 1000).toList.map(x =&gt; x==1)
list: List[Boolean] = List(true, false, false, false, ...

scala&gt; val t1 = time; for (_  val t1 = time; for (_ &lt;- 1 to 10000) { x=list.foldRight(false)(_&#124;&#124;_) }; println (time - t1)
984
t1: Long = 1281477246817
&lt;/pre&gt;

This foldRight applied to a list of 1000 booleans 10,000 times takes about 984 milliseconds. That&#039;s 10 times as many items and it took about 10 times as long.

&lt;pre&gt;
scala&gt; list = (1 to 2000).toList.map(x =&gt; x==1)
list: List[Boolean] = List(true, false, false, false, ...

scala&gt; val t1 = time; for (_  val t1 = time; for (_ &lt;- 1 to 10000) { x=list.foldRight(false)(_&#124;&#124;_) }; println (time - t1)
1797
t1: Long = 1281477309082
&lt;/pre&gt;

This foldRight applied to a list of 2000 booleans 10,000 times takes about 1797 milliseconds. That&#039;s 2 times as many items and it took about 1.8 times as long.

This is all consistent with an O(n) foldRight, and it&#039;s what I would expect from the implementation.  When you say that foldRight &quot;can execute in O(1) time&quot;, do you mean that it should theoretically be possible for the compiler to short circuit the logic in certain applications of foldRight? Because I&#039;m not seeing any such optimization in these numbers.]]></description>
		<content:encoded><![CDATA[<p>Zoheb, your comment is interesting, but I&#8217;m having trouble understanding how it can be so.</p>
<p>Firstly, you say that reverse.foldLeft is at least O(n).  I don&#8217;t see how it can be anything other than O(n).  Reverse is clear enough.  It builds a second singly linked list out of the first, one item at a time.  That&#8217;s O(n).  And then foldLeft runs, which is implemented as a while loop.  That&#8217;s O(n).  The combination is O(n+n) which is the same as O(n).</p>
<p>Secondly (and this is the one that I&#8217;ve been puzzling over) I don&#8217;t think foldRight runs in O(1).  Let&#8217;s take the foldRight call in your example.</p>
<pre>
scala&gt; var x: Boolean = true
x: Boolean = true

scala&gt; def time = (new java.util.Date).getTime
time: Long

scala&gt; var list = (1 to 10).toList.map(x =&gt; x==1)
list: List[Boolean] = List(true, false, false, false, false, false, false, false, false, false)

scala&gt; val t1 = time; for (_  val t1 = time; for (_ &lt;- 1 to 10000) { x=list.foldRight(false)(_||_) }; println (time - t1)
16
t1: Long = 1281477156551
</pre>
<p>This foldRight applied to a list of 10 booleans 10,000 times takes 16 milliseconds.</p>
<pre>
scala&gt; list = (1 to 100).toList.map(x =&gt; x==1)
list: List[Boolean] = List(true, false, false, false, ...

scala&gt; val t1 = time; for (_  val t1 = time; for (_ &lt;- 1 to 10000) { x=list.foldRight(false)(_||_) }; println (time - t1)
94
t1: Long = 1281477234035
</pre>
<p>This foldRight applied to a list of 100 booleans 10,000 times takes 94 milliseconds. That&#8217;s 10 times as many items and it took about 6 times as long.</p>
<pre>
scala&gt; list = (1 to 1000).toList.map(x =&gt; x==1)
list: List[Boolean] = List(true, false, false, false, ...

scala&gt; val t1 = time; for (_  val t1 = time; for (_ &lt;- 1 to 10000) { x=list.foldRight(false)(_||_) }; println (time - t1)
984
t1: Long = 1281477246817
</pre>
<p>This foldRight applied to a list of 1000 booleans 10,000 times takes about 984 milliseconds. That&#8217;s 10 times as many items and it took about 10 times as long.</p>
<pre>
scala&gt; list = (1 to 2000).toList.map(x =&gt; x==1)
list: List[Boolean] = List(true, false, false, false, ...

scala&gt; val t1 = time; for (_  val t1 = time; for (_ &lt;- 1 to 10000) { x=list.foldRight(false)(_||_) }; println (time - t1)
1797
t1: Long = 1281477309082
</pre>
<p>This foldRight applied to a list of 2000 booleans 10,000 times takes about 1797 milliseconds. That&#8217;s 2 times as many items and it took about 1.8 times as long.</p>
<p>This is all consistent with an O(n) foldRight, and it&#8217;s what I would expect from the implementation.  When you say that foldRight &#8220;can execute in O(1) time&#8221;, do you mean that it should theoretically be possible for the compiler to short circuit the logic in certain applications of foldRight? Because I&#8217;m not seeing any such optimization in these numbers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zoheb Vacheri</title>
		<link>http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/#comment-646</link>
		<dc:creator><![CDATA[Zoheb Vacheri]]></dc:creator>
		<pubDate>Tue, 10 Aug 2010 03:21:31 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=239#comment-646</guid>
		<description><![CDATA[I meant to say that *foldRight* can execute in O(1) time]]></description>
		<content:encoded><![CDATA[<p>I meant to say that *foldRight* can execute in O(1) time</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zoheb Vacheri</title>
		<link>http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/#comment-645</link>
		<dc:creator><![CDATA[Zoheb Vacheri]]></dc:creator>
		<pubDate>Sat, 07 Aug 2010 20:15:22 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=239#comment-645</guid>
		<description><![CDATA[reverse.foldLeft has at least O(n) complexity whereas foldLeft can execute in O(1) time.

List(true,false,false).foldRight(false)(_&#124;&#124;_) = = true

In Haskell, foldr can operate on infinite lists, while foldl cannot]]></description>
		<content:encoded><![CDATA[<p>reverse.foldLeft has at least O(n) complexity whereas foldLeft can execute in O(1) time.</p>
<p>List(true,false,false).foldRight(false)(_||_) = = true</p>
<p>In Haskell, foldr can operate on infinite lists, while foldl cannot</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Little tricks on working with LARGE list in Scala &#171; Ekkmanz in geeky life!</title>
		<link>http://oldfashionedsoftware.com/2009/07/10/scala-code-review-foldleft-and-foldright/#comment-598</link>
		<dc:creator><![CDATA[Little tricks on working with LARGE list in Scala &#171; Ekkmanz in geeky life!]]></dc:creator>
		<pubDate>Wed, 18 Nov 2009 06:37:08 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=239#comment-598</guid>
		<description><![CDATA[[...] is dropRight since I use this method in order to reduce list size. Others also had shown that foldRight could cause this error as well. If you want to know which method may harm you then you may tries [...]]]></description>
		<content:encoded><![CDATA[<p>[...] is dropRight since I use this method in order to reduce list size. Others also had shown that foldRight could cause this error as well. If you want to know which method may harm you then you may tries [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

