<?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: Lots And Lots Of foldLeft Examples</title>
	<atom:link href="http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/feed/" rel="self" type="application/rss+xml" />
	<link>http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/</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: Jacek</title>
		<link>http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/#comment-728</link>
		<dc:creator><![CDATA[Jacek]]></dc:creator>
		<pubDate>Wed, 25 Jan 2012 15:21:25 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=316#comment-728</guid>
		<description><![CDATA[Thanks for this great post! It provided a great way to get my hands dirty with the &quot;foldLeft&quot; way of thinking - which was a nice brain teaser and helped me understand the ideas behind (e.g. the recursive definition of lists in Scala).

Now I&#039;m continuing to the rest of your Scala posts :)]]></description>
		<content:encoded><![CDATA[<p>Thanks for this great post! It provided a great way to get my hands dirty with the &#8220;foldLeft&#8221; way of thinking &#8211; which was a nice brain teaser and helped me understand the ideas behind (e.g. the recursive definition of lists in Scala).</p>
<p>Now I&#8217;m continuing to the rest of your Scala posts :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/#comment-713</link>
		<dc:creator><![CDATA[Matt]]></dc:creator>
		<pubDate>Wed, 31 Aug 2011 14:38:16 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=316#comment-713</guid>
		<description><![CDATA[That&#039;s very kind of you, Kai.  I&#039;m glad you&#039;ve enjoyed my posts.  I&#039;ve been meaning to write some new ones, and it&#039;s good motivation to know that people are reading them.]]></description>
		<content:encoded><![CDATA[<p>That&#8217;s very kind of you, Kai.  I&#8217;m glad you&#8217;ve enjoyed my posts.  I&#8217;ve been meaning to write some new ones, and it&#8217;s good motivation to know that people are reading them.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kai Wähner</title>
		<link>http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/#comment-712</link>
		<dc:creator><![CDATA[Kai Wähner]]></dc:creator>
		<pubDate>Wed, 31 Aug 2011 12:25:40 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=316#comment-712</guid>
		<description><![CDATA[Hey Matt,

congratulations for your Scala blogs. I found this one using Google. It forwarded me to your foldLeft explanation. That one forwarded me to your Tail-Recursion basics. While reading them, Google forwarded me to your &quot;Null, null, Nil, ...&quot; blog.

These blogs are very good, because they are natural and easy to understand! The main problem of Scala is the lack of good, easy to ready blogs and books, still. Odersky and some others wrote books with good contents, but these books are only good as reference book, because they are to difficult to understand IMO.

Best regards,
Kai Wähner (Twitter: @KaiWaehner)]]></description>
		<content:encoded><![CDATA[<p>Hey Matt,</p>
<p>congratulations for your Scala blogs. I found this one using Google. It forwarded me to your foldLeft explanation. That one forwarded me to your Tail-Recursion basics. While reading them, Google forwarded me to your &#8220;Null, null, Nil, &#8230;&#8221; blog.</p>
<p>These blogs are very good, because they are natural and easy to understand! The main problem of Scala is the lack of good, easy to ready blogs and books, still. Odersky and some others wrote books with good contents, but these books are only good as reference book, because they are to difficult to understand IMO.</p>
<p>Best regards,<br />
Kai Wähner (Twitter: @KaiWaehner)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: links for 2010-08-18 &#171; Dan Creswell&#8217;s Linkblog</title>
		<link>http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/#comment-648</link>
		<dc:creator><![CDATA[links for 2010-08-18 &#171; Dan Creswell&#8217;s Linkblog]]></dc:creator>
		<pubDate>Wed, 18 Aug 2010 12:06:22 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=316#comment-648</guid>
		<description><![CDATA[[...] Lots And Lots Of foldLeft Examples (tags: scala foldleft functionalprogramming) [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Lots And Lots Of foldLeft Examples (tags: scala foldleft functionalprogramming) [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matthias</title>
		<link>http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/#comment-640</link>
		<dc:creator><![CDATA[Matthias]]></dc:creator>
		<pubDate>Mon, 05 Jul 2010 19:44:12 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=316#comment-640</guid>
		<description><![CDATA[I love your last example -&gt; Just great to see if you just know Java. 

Go for Scala &amp; functional programming!]]></description>
		<content:encoded><![CDATA[<p>I love your last example -&gt; Just great to see if you just know Java. </p>
<p>Go for Scala &amp; functional programming!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/#comment-605</link>
		<dc:creator><![CDATA[Matt]]></dc:creator>
		<pubDate>Tue, 22 Dec 2009 22:30:58 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=316#comment-605</guid>
		<description><![CDATA[The same principles apply for domain objects.  You build up your accumulator by applying some code for each item in the list.  For example, say you had a class called Vector2D.  It stores a 2-dimensional vector using Cartesian coordinates.  Like so:

class Vector2D(val x: Double, val y: Double) {
  def +(that: Vector2D) = new Vector2D(this.x + that.x, this.y + that.y)
}

Now, if you have several vectors that you want to sum, you do it just as for integers.

val finalVector = vectorList.foldLeft(new Vector(0,0))(_+_)

Or say you had a list of employees and you want to calculate the yearly payroll:

val payroll = employeeList.foldLeft(0)(_+_.salary)

Or perhaps a user has selected a number of Products and he is ready to purchase:

val purchaseOrder = productList.foldLeft(new PurchaseOrder)(_.addProduct(_))

Is this the kind of thing you were looking for?]]></description>
		<content:encoded><![CDATA[<p>The same principles apply for domain objects.  You build up your accumulator by applying some code for each item in the list.  For example, say you had a class called Vector2D.  It stores a 2-dimensional vector using Cartesian coordinates.  Like so:</p>
<p>class Vector2D(val x: Double, val y: Double) {<br />
  def +(that: Vector2D) = new Vector2D(this.x + that.x, this.y + that.y)<br />
}</p>
<p>Now, if you have several vectors that you want to sum, you do it just as for integers.</p>
<p>val finalVector = vectorList.foldLeft(new Vector(0,0))(_+_)</p>
<p>Or say you had a list of employees and you want to calculate the yearly payroll:</p>
<p>val payroll = employeeList.foldLeft(0)(_+_.salary)</p>
<p>Or perhaps a user has selected a number of Products and he is ready to purchase:</p>
<p>val purchaseOrder = productList.foldLeft(new PurchaseOrder)(_.addProduct(_))</p>
<p>Is this the kind of thing you were looking for?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Walter</title>
		<link>http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/#comment-604</link>
		<dc:creator><![CDATA[Walter]]></dc:creator>
		<pubDate>Mon, 21 Dec 2009 13:09:38 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=316#comment-604</guid>
		<description><![CDATA[You showed some very nice examples, but only for basic operations of built-in types. I&#039;m missing some real-word examples with Domain objects.]]></description>
		<content:encoded><![CDATA[<p>You showed some very nice examples, but only for basic operations of built-in types. I&#8217;m missing some real-word examples with Domain objects.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scala Code Review: foldLeft and foldRight &#171; Matt Malone&#8217;s Old-Fashioned Software Development Blog</title>
		<link>http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/#comment-489</link>
		<dc:creator><![CDATA[Scala Code Review: foldLeft and foldRight &#171; Matt Malone&#8217;s Old-Fashioned Software Development Blog]]></dc:creator>
		<pubDate>Thu, 20 Aug 2009 23:13:01 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=316#comment-489</guid>
		<description><![CDATA[[...] I&#8217;ve created a new post in which I list lots and lots of foldLeft examples in case you&#8217;d like to learn more about what folding can [...]]]></description>
		<content:encoded><![CDATA[<p>[...] I&#8217;ve created a new post in which I list lots and lots of foldLeft examples in case you&#8217;d like to learn more about what folding can [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt</title>
		<link>http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/#comment-451</link>
		<dc:creator><![CDATA[Matt]]></dc:creator>
		<pubDate>Thu, 06 Aug 2009 18:42:29 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=316#comment-451</guid>
		<description><![CDATA[Glad to hear that it has been useful for you.  Implementing the list operations yourself is a great way to learn how they work and to get used to the functional style of programming.  Thanks a lot for the contribution, Sekib.]]></description>
		<content:encoded><![CDATA[<p>Glad to hear that it has been useful for you.  Implementing the list operations yourself is a great way to learn how they work and to get used to the functional style of programming.  Thanks a lot for the contribution, Sekib.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sekib</title>
		<link>http://oldfashionedsoftware.com/2009/07/30/lots-and-lots-of-foldleft-examples/#comment-448</link>
		<dc:creator><![CDATA[Sekib]]></dc:creator>
		<pubDate>Thu, 06 Aug 2009 16:02:09 +0000</pubDate>
		<guid isPermaLink="false">http://oldfashionedsoftware.com/?p=316#comment-448</guid>
		<description><![CDATA[Hi Matthew,

thanks for the articles. I really enjoyed reading &#039;em all. 
Here are some examples on learning scala with your posts (MyList extension):

def foldLeft[B](z: B)(f: (B, A) =&gt; B): B = {
  def loop(l:MyList[A], acc: B, ff: (B, A) =&gt; B):B = {
    if (!l.isEmpty) loop(l.tail, ff(acc, l.head), ff)
    else acc
  }

  loop(this, z, f)
}

def foldRight[B](z: B)(f: (A, B) =&gt; B): B = 
  this.reverse.foldLeft(z)((res,cur) =&gt; f(cur, res))

def foreach(f: A =&gt; Unit) {
  this.foldLeft()((res, cur) =&gt; f(cur))
}

def forall(f: A =&gt; Boolean): Boolean =
  this.foldLeft[Boolean](true)((res, cur) =&gt; res &amp;&amp; f(cur))
  
def length() : Int =
  this.foldLeft(0)((sum,_) =&gt; sum + 1)

def map[B](f: A =&gt; B): MyList[B] =
  this.foldRight[MyList[B]](MyList[B]())((cur, res) =&gt; f(cur) :: res)
  //this.foldLeft[MyList[B]](MyList[B]())((res, cur) =&gt; f(cur) :: res).reverse

def filter(f: A =&gt; Boolean): MyList[A] = 
  this.foldLeft[MyList[A]](MyList())((res, cur) =&gt; if (f(cur)) {cur :: res} else res).reverse

def reduceLeft[B &gt;: A](f: (B, A) =&gt; B): B =
  if (isEmpty) throw new UnsupportedOperationException(&quot;empty.reduceLeft&quot;)
  else tail.foldLeft[B](head)(f)

def reduceRight[B &gt;: A](f: (A, B) =&gt; B): B =
  if (isEmpty) throw new UnsupportedOperationException(&quot;empty.reduceLeft&quot;)
  else tail.foldRight[B](head)(f)

def exists(p: A =&gt; Boolean): Boolean =
  this.foldLeft[Boolean](false)((res, cur) =&gt; res &#124;&#124; p(cur))

// no foldLeft but functional and tail-recursive
def indices: MyList[Int] = {
  def loop(l: MyList[A], res: MyList[Int], idx:Int):MyList[Int] = 
    if(! l.isEmpty) loop(l.tail, idx :: res, idx+1)
    else res

  loop(this, MyList[Int](), 0).reverse
}

def dropWhile(p: A =&gt; Boolean): MyList[A] = {
  def loop (l:MyList[A], f: A =&gt; Boolean): MyList[A] = 
    if (!l.isEmpty &amp;&amp; f(l.head)) loop (l.tail, f)
    else l

  loop(this, p)
}

def takeWhile(p: A =&gt; Boolean): MyList[A] = {
  def loop(l: MyList[A], res: MyList[A], f: A =&gt; Boolean): MyList[A] = 
    if (!l.isEmpty &amp;&amp; f(l.head)) loop(l.tail, l.head :: res, f)
    else res

  loop(this, MyList[A](), p) reverse
}

def drop(n: Int): MyList[A] = {
  def loop(l: MyList[A], idx:Int, cur: Int):MyList[A] = 
    if (!l.isEmpty &amp;&amp; cur 0) loop(l.tail, idx, cur+1)
    else l

  loop(this, n, 0)
}

def reverseMap[B](f: A =&gt; B): MyList[B] = {
  def loop(l: MyList[A], res: MyList[B]): MyList[B] = {
    if(l.isEmpty) res
    else loop(l.tail, f(l.head) :: res)
  }
  loop(this, MyList())
}

Sorry if the formatting is broken :)]]></description>
		<content:encoded><![CDATA[<p>Hi Matthew,</p>
<p>thanks for the articles. I really enjoyed reading &#8216;em all.<br />
Here are some examples on learning scala with your posts (MyList extension):</p>
<p>def foldLeft[B](z: B)(f: (B, A) =&gt; B): B = {<br />
  def loop(l:MyList[A], acc: B, ff: (B, A) =&gt; B):B = {<br />
    if (!l.isEmpty) loop(l.tail, ff(acc, l.head), ff)<br />
    else acc<br />
  }</p>
<p>  loop(this, z, f)<br />
}</p>
<p>def foldRight[B](z: B)(f: (A, B) =&gt; B): B =<br />
  this.reverse.foldLeft(z)((res,cur) =&gt; f(cur, res))</p>
<p>def foreach(f: A =&gt; Unit) {<br />
  this.foldLeft()((res, cur) =&gt; f(cur))<br />
}</p>
<p>def forall(f: A =&gt; Boolean): Boolean =<br />
  this.foldLeft[Boolean](true)((res, cur) =&gt; res &amp;&amp; f(cur))</p>
<p>def length() : Int =<br />
  this.foldLeft(0)((sum,_) =&gt; sum + 1)</p>
<p>def map[B](f: A =&gt; B): MyList[B] =<br />
  this.foldRight[MyList[B]](MyList[B]())((cur, res) =&gt; f(cur) :: res)<br />
  //this.foldLeft[MyList[B]](MyList[B]())((res, cur) =&gt; f(cur) :: res).reverse</p>
<p>def filter(f: A =&gt; Boolean): MyList[A] =<br />
  this.foldLeft[MyList[A]](MyList())((res, cur) =&gt; if (f(cur)) {cur :: res} else res).reverse</p>
<p>def reduceLeft[B &gt;: A](f: (B, A) =&gt; B): B =<br />
  if (isEmpty) throw new UnsupportedOperationException(&#8220;empty.reduceLeft&#8221;)<br />
  else tail.foldLeft[B](head)(f)</p>
<p>def reduceRight[B &gt;: A](f: (A, B) =&gt; B): B =<br />
  if (isEmpty) throw new UnsupportedOperationException(&#8220;empty.reduceLeft&#8221;)<br />
  else tail.foldRight[B](head)(f)</p>
<p>def exists(p: A =&gt; Boolean): Boolean =<br />
  this.foldLeft[Boolean](false)((res, cur) =&gt; res || p(cur))</p>
<p>// no foldLeft but functional and tail-recursive<br />
def indices: MyList[Int] = {<br />
  def loop(l: MyList[A], res: MyList[Int], idx:Int):MyList[Int] =<br />
    if(! l.isEmpty) loop(l.tail, idx :: res, idx+1)<br />
    else res</p>
<p>  loop(this, MyList[Int](), 0).reverse<br />
}</p>
<p>def dropWhile(p: A =&gt; Boolean): MyList[A] = {<br />
  def loop (l:MyList[A], f: A =&gt; Boolean): MyList[A] =<br />
    if (!l.isEmpty &amp;&amp; f(l.head)) loop (l.tail, f)<br />
    else l</p>
<p>  loop(this, p)<br />
}</p>
<p>def takeWhile(p: A =&gt; Boolean): MyList[A] = {<br />
  def loop(l: MyList[A], res: MyList[A], f: A =&gt; Boolean): MyList[A] =<br />
    if (!l.isEmpty &amp;&amp; f(l.head)) loop(l.tail, l.head :: res, f)<br />
    else res</p>
<p>  loop(this, MyList[A](), p) reverse<br />
}</p>
<p>def drop(n: Int): MyList[A] = {<br />
  def loop(l: MyList[A], idx:Int, cur: Int):MyList[A] =<br />
    if (!l.isEmpty &amp;&amp; cur 0) loop(l.tail, idx, cur+1)<br />
    else l</p>
<p>  loop(this, n, 0)<br />
}</p>
<p>def reverseMap[B](f: A =&gt; B): MyList[B] = {<br />
  def loop(l: MyList[A], res: MyList[B]): MyList[B] = {<br />
    if(l.isEmpty) res<br />
    else loop(l.tail, f(l.head) :: res)<br />
  }<br />
  loop(this, MyList())<br />
}</p>
<p>Sorry if the formatting is broken :)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

