<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blumenfeld &#38; Maso, Inc. &#187; Scala Word of the Day</title>
	<atom:link href="http://www.blumenfeld-maso.com/category/scala-word-of-the-day/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blumenfeld-maso.com</link>
	<description>Cloud, SaaS, and Enterprise Software Design and Development</description>
	<lastBuildDate>Tue, 23 Aug 2011 15:04:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Scala Refactoring: Quiet-my-scope with an Implicit Parameter vs. Pimp-my-lib with an Implicit Conversion Method</title>
		<link>http://www.blumenfeld-maso.com/2011/05/scala-refactoring-quiet-my-scope-with-an-implicit-parameter-vs-pimp-my-lib-with-an-implicit-conversion-method/</link>
		<comments>http://www.blumenfeld-maso.com/2011/05/scala-refactoring-quiet-my-scope-with-an-implicit-parameter-vs-pimp-my-lib-with-an-implicit-conversion-method/#comments</comments>
		<pubDate>Tue, 31 May 2011 04:27:36 +0000</pubDate>
		<dc:creator>Brian Maso</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala Word of the Day]]></category>

		<guid isPermaLink="false">http://www.blumenfeld-maso.com/?p=205</guid>
		<description><![CDATA[The following two code snippets are equivalent, differing only on what the client code ends up looking like, but yielding in all other ways similar results: The code above can be refactored to the pimp-my-lib style which follows, and vice versa: In both cases, the imported LibraryCode.scala provides a callable method which requires state in [...]]]></description>
			<content:encoded><![CDATA[<p>The following two code snippets are equivalent, differing only on what the client code ends up looking like, but yielding in all other ways similar results:</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="scala" style="font-family:monospace;"><span class="co1">// --------------------</span>
<span class="co1">// In LibraryCode.scala...</span>
<a href="http://scala-lang.org"><span class="kw1">package</span></a> library.<span class="me1">code</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// client code expected to import the following method and provide</span>
<span class="co1">// an implicit Thing value for t -- a &quot;quiet-my-scope&quot; patterned method.</span>
<a href="http://scala-lang.org"><span class="kw1">def</span></a> addedFeatureToSomeThing<span class="br0">&#40;</span><a href="http://scala-lang.org"><span class="kw1">implicit</span></a> Thing t<span class="br0">&#41;</span> <span class="sy0">=</span> ...
&nbsp;
<span class="co1">// --------------------</span>
<span class="co1">// In ClientCode.scala...</span>
<a href="http://scala-lang.org"><span class="kw1">import</span></a> library.<span class="me1">code</span>.<span class="sy0">_</span>
&nbsp;
<span class="co1">// An implicit Thing value</span>
<a href="http://scala-lang.org"><span class="kw1">implicit</span></a> <a href="http://scala-lang.org"><span class="kw1">val</span></a> someThing<span class="sy0">:</span> Thing <span class="sy0">=</span> ...
&nbsp;
<span class="co1">// Use of the someThing value as an implicit parameter to the library method</span>
<a href="http://scala-lang.org"><span class="kw1">val</span></a> result <span class="sy0">=</span> addedFeatureToSomeThing</pre></div></div>
<p>The code above can be refactored to the pimp-my-lib style which follows, and <em>vice versa</em>:</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="scala" style="font-family:monospace;"><span class="co1">// --------------------</span>
<span class="co1">// In LibraryCode.scala</span>
<a href="http://scala-lang.org"><span class="kw1">package</span></a> library.<span class="me1">code</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// client code expected to import the following implicit conversion method,</span>
<span class="co1">// which effectively adds a new method &quot;addedFeature&quot; to the Thing class in the importing scope.</span>
<a href="http://scala-lang.org"><span class="kw1">implicit</span></a> <a href="http://scala-lang.org"><span class="kw1">def</span></a> convertThingToAddFeature<span class="br0">&#40;</span>t<span class="sy0">:</span> Thing<span class="br0">&#41;</span> <span class="sy0">=</span> <a href="http://scala-lang.org"><span class="kw1">new</span></a> <span class="br0">&#123;</span>
    <a href="http://scala-lang.org"><span class="kw1">def</span></a> addedFeature <span class="sy0">=</span> ...
    <span class="br0">&#125;</span>
&nbsp;
<span class="co1">// --------------------</span>
<span class="co1">// In ClientCode.scala...</span>
<a href="http://scala-lang.org"><span class="kw1">import</span></a> library.<span class="me1">code</span>.<span class="sy0">_</span>
&nbsp;
<a href="http://scala-lang.org"><span class="kw1">val</span></a> someThing<span class="sy0">:</span> Thing <span class="sy0">=</span> ...
&nbsp;
<span class="co1">// Use of the pimp-my-lib-patterned convertThingToAddFeature implicit conversion method</span>
<a href="http://scala-lang.org"><span class="kw1">val</span></a> result <span class="sy0">=</span> someThing.<span class="me1">addedFeature</span></pre></div></div>
<p>In both cases, the imported <em>LibraryCode.scala</em> provides a callable method which requires state in the form of a Thing object. I&#8217;m assuming the Thing type is also defined external to the client code &#8212; probably in a third-party library.</p>
<p>The first pattern, which to the best of my knowledge doesn&#8217;t have a name, you have a method in the local scope which accepts an implicit parameter, and effectively allows you to pass a single in-scope object to multiple methods without needing to repeat yourself. I&#8217;m going to dub this the &#8220;<em>quiet-my-scope pattern</em>&#8220;, because it reduces  the visual chatter that would otherwise be caused by passing the same object to multiple method calls &#8212; and I&#8217;ll just hope that name sticks.</p>
<p>The second is the famous pimp-my-lib pattern, which is well-known throughout Scalaland to be used to &#8220;add&#8221; methods to externally-defined classes.</p>
<p>Using implicits we have these two API patterns to choose from &#8212; they are equivalent, in so far as an implementation of either one can be mechanically transformed in to the other, only style and convenience being a differentiator between them. You might find it useful to keep around this alternative to the pimp-my-lib pattern, as in some APIs it will yield more readable, more convenient code.</p>
<p>Try it out: the next time you find yourself augmenting an existing class with methods using the pimp-my-lib pattern, take 5 minutes to refactor your solution to methods with implicit parameters (using the sample Thing code above as a guide). You won&#8217;t lose much time in the exercise, and you may find you&#8217;ll end up with better code.</p>
<p>Similarly, the next time you find yourself defining a method with an implicit parameter, take 5 minutes to work through what a refactored solution with a pimp-my-lib style method would look like. You may like the result quite a bit more than what you started with.</p>
<div id="_mcePaste" class="mcePaste" style="position: absolute; left: -10000px; top: 9px; width: 1px; height: 1px; overflow: hidden;">
<p>def addedFeatureToSomeThing(implicit Thing t) = &#8230;</p>
<p>&nbsp;</p>
</div>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.blumenfeld-maso.com%2F2011%2F05%2Fscala-refactoring-quiet-my-scope-with-an-implicit-parameter-vs-pimp-my-lib-with-an-implicit-conversion-method%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.blumenfeld-maso.com%2F2011%2F05%2Fscala-refactoring-quiet-my-scope-with-an-implicit-parameter-vs-pimp-my-lib-with-an-implicit-conversion-method%2F&amp;source=bmaso&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.blumenfeld-maso.com/2011/05/scala-refactoring-quiet-my-scope-with-an-implicit-parameter-vs-pimp-my-lib-with-an-implicit-conversion-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scala Word of the Day: For Loops (All 4 Kinds!)</title>
		<link>http://www.blumenfeld-maso.com/2010/01/scala-word-of-the-day-for-loops-all-4-kinds/</link>
		<comments>http://www.blumenfeld-maso.com/2010/01/scala-word-of-the-day-for-loops-all-4-kinds/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 06:36:54 +0000</pubDate>
		<dc:creator>Brian Maso</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala Word of the Day]]></category>
		<category><![CDATA[for loop]]></category>
		<category><![CDATA[iterable]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[non-strict]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://www.blumenfeld-maso.com/?p=90</guid>
		<description><![CDATA[You think you know how to define a for loop, do you? Scala has 4 different kinds. 1. Traditional for loop. Loops through the members of an Iteratable. The result of the loop block is Unit. Not surprisingly, this loop prints out the members of the collection, in order that they are returned by the [...]]]></description>
			<content:encoded><![CDATA[<p>You think you know how to define a <em>for loop</em>, do you? Scala has <em><strong>4</strong></em> different kinds.</p>
<hr />1. <strong>Traditional for loop</strong>. Loops through the members of an Iteratable. The result of the loop block is <code>Unit</code>.</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="pascal" style="font-family:monospace;"><span class="kw1">for</span><span class="br0">&#40;</span>mbr &lt;<span class="sy0">-</span> collection<span class="br0">&#41;</span> <span class="coMULTI">{
  println(mbr)
}</span></pre></div></div>
<p>Not surprisingly, this loop prints out the members of the collection, in order that they are returned by the collection&#8217;s iterator. In fact, this loop construct is translated exactly to</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="pascal" style="font-family:monospace;">collection.<span class="me1">foreach</span><span class="br0">&#40;</span>mbr <span class="sy0">=</span>&gt; println<span class="br0">&#40;</span>mbr<span class="br0">&#41;</span><span class="br0">&#41;</span></pre></div></div>
<hr />2. <strong>For&#8230;Yield Loop</strong>. Use the yield keyword within the body of a for loop. The loop block has a result which is an iterator of the yielded results. Thus the for loop can act as the RHS of an assignment.</p>
<div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="pascal" style="font-family:monospace;">val transformed <span class="sy0">=</span> <span class="kw1">for</span><span class="br0">&#40;</span>mbr &lt;<span class="sy0">-</span> collection<span class="br0">&#41;</span> <span class="coMULTI">{
  yield transform(mbr)
}</span></pre></div></div>
<p>Each time through this example loop, the result of the loop is yielded out. All the yielded results are collected in to a single iterator. This type of for loop is syntactic sugar for the following statement</p>
<div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="pascal" style="font-family:monospace;">val transformed <span class="sy0">=</span> collection.<span class="me1">map</span><span class="br0">&#40;</span>mbr <span class="sy0">=</span>&gt; transform<span class="br0">&#40;</span>mbr<span class="br0">&#41;</span><span class="br0">&#41;</span></pre></div></div>
<p>That&#8217;s right, a simple for&#8230;yield loop is just syntactic sugar for <code>Iteratable.map()</code>.</p>
<hr /><strong>Interlude: Watch Out for Infinite Iterables</strong></p>
<p>Infinite (or extremely large) iterables, usually implemented as <code>Stream</code>s or <code>Range</code>s, are a bit tricky. For example, it probably wouldn&#8217;t surprise you to learn that the following traditional for loop will not terminate (at least not in your lifetime):</p>
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="pascal" style="font-family:monospace;"><span class="kw1">for</span><span class="br0">&#40;</span>i &lt;<span class="sy0">-</span> 0 <span class="kw1">to</span> <span class="nu0">100000000000</span><span class="br0">&#41;</span> println<span class="br0">&#40;</span>i<span class="br0">&#41;</span></pre></div></div>
<p>But executing this next for&#8230;yield loop does <em>not</em> cause an indefinite wait &#8212; it works fine! (Go ahead, try it out!)</p>
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_8"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#codesyntax_8"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_8" onClick="javascript:wpsh_code(8)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_8" onClick="javascript:wpsh_print(8)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="pascal" style="font-family:monospace;">val numberStrings <span class="sy0">=</span> <span class="kw1">for</span><span class="br0">&#40;</span>i &lt;<span class="sy0">-</span> 0 <span class="kw1">to</span> <span class="nu0">100000000000</span><span class="br0">&#41;</span> yield <span class="br0">&#40;</span><span class="st0">&quot;&quot;</span> <span class="sy0">+</span> i<span class="br0">&#41;</span></pre></div></div>
<p>How can this be so? You need to know that <code>o to 10000000000</code> results in a <code>Range</code> object, which is a non-strict iterator. That is, it is an iterator that calculates the &#8220;next&#8221; value as you iterate through it, rather than pre-computing all members up front and storing them in memory when the <code>Range</code> is created.</p>
<p>Iteratable functions that yield a scalar result, like <code>foldLeft()</code> or <code>length()</code> or any <a href="http://www.blumenfeld-maso.com/2010/01/scala-word-of-the-day-catamorphism/">catamorphic function</a>, are not safe to use with non-strict iterators, because these methods must iterate over the entire iteratable&#8217;s contents to produce a result. The <code>foreach()</code> method falls in to this unsafe category &#8212; <code>foreach()</code>&#8216;s return type is Unit, which is considered scalar.</p>
<p>Remember the traditional for loop is just syntactic sugar for a <code>foreach()</code> call, and this explains why looping over the range with a traditional loop causes an indefinite wait.</p>
<p>Iteratable functions that yield results of similar size to the input, such as <code>map()</code> and <code>flatMap()</code>, are perfectly safe to use with non-strict iterators. These methods themselves yield non-strict iterators, and don&#8217;t need to iterate over the source iterator&#8217;s contents to yield a result.</p>
<p>The for&#8230;yield loop is just syntactic sugar for a <code>map()</code> call, and this explains why looping over the <code>Range</code> with a for&#8230;yield loop doesn&#8217;t cause an indefinite wait.</p>
<p><strong>End of Interlude</strong></p>
<hr />3. <strong>Guarded For&#8230;Yield Loop</strong>. Throw an <em>if</em> guard in to a for..yield loop, and its translated to a filter-map pipeline (perhaps I should say filter |&gt; map, using <a href="http://www.blumenfeld-maso.com/2009/10/pipe-operator-for-scala/">the pipe operator</a>).</p>
<div id="wpshdo_9" class="wp-synhighlighter-outer"><div id="wpshdt_9" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_9"></a><a id="wpshat_9" class="wp-synhighlighter-title" href="#codesyntax_9"  onClick="javascript:wpsh_toggleBlock(9)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_9" onClick="javascript:wpsh_code(9)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_9" onClick="javascript:wpsh_print(9)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_9" class="wp-synhighlighter-inner" style="display: block;"><pre class="pascal" style="font-family:monospace;"><span class="co1">// Using the guarded for...yield loop syntactic sugar</span>
val oddSquares <span class="sy0">=</span>
  <span class="kw1">for</span><span class="br0">&#40;</span>i &lt;<span class="sy0">-</span> 0 <span class="kw1">to</span> 10000000000 <span class="kw1">if</span> i % <span class="nu0">2</span> <span class="sy0">==</span> <span class="nu0">1</span><span class="br0">&#41;</span> yield <span class="br0">&#40;</span>i<span class="sy0">*</span>i<span class="br0">&#41;</span>
&nbsp;
<span class="co1">// Exactly the same thing as</span>
val oddSquares <span class="sy0">=</span> <span class="br0">&#40;</span>0 <span class="kw1">to</span> <span class="nu0">10000000000</span><span class="br0">&#41;</span>.<span class="me1">filter</span><span class="br0">&#40;</span>i<span class="sy0">=</span>&gt;i % <span class="nu0">2</span> <span class="sy0">==</span> <span class="nu0">1</span><span class="br0">&#41;</span>.<span class="me1">map</span><span class="br0">&#40;</span>i<span class="sy0">=</span>&gt;i<span class="sy0">*</span>i<span class="br0">&#41;</span></pre></div></div>
<p>And as the example implies, this for loop style is non-strict collection safe (because both <code>filter()</code> and <code>map()</code> are non-strict safe).</p>
<hr />4. <strong>Nested For Loop</strong>. Nesting iteration over iterators is syntactic sugar for a nested <code>flatMap()</code> call. Stare at the loop below and the form it gets translated in to by the Scala compiler for a little bit, you don&#8217;t need my explanation:</p>
<div id="wpshdo_10" class="wp-synhighlighter-outer"><div id="wpshdt_10" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_10"></a><a id="wpshat_10" class="wp-synhighlighter-title" href="#codesyntax_10"  onClick="javascript:wpsh_toggleBlock(10)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_10" onClick="javascript:wpsh_code(10)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_10" onClick="javascript:wpsh_print(10)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_10" class="wp-synhighlighter-inner" style="display: block;"><pre class="pascal" style="font-family:monospace;"><span class="co1">// Long-winded nested for loop</span>
val pairsThatSumTo100 <span class="sy0">=</span>
  <span class="kw1">for</span><span class="br0">&#40;</span>i &lt;<span class="sy0">-</span> 0 <span class="kw1">to</span> <span class="nu0">100</span>;
      j &lt;<span class="sy0">-</span> i <span class="kw1">to</span> 100 <span class="kw1">if</span> i <span class="sy0">+</span> j <span class="sy0">==</span> <span class="nu0">100</span><span class="br0">&#41;</span>
    yield Pair<span class="br0">&#40;</span>i<span class="sy0">,</span> j<span class="br0">&#41;</span>
&nbsp;
<span class="co1">// Slightly shorter but harder to read raw form that gets compiled</span>
val pairsThatSumTo100 <span class="sy0">=</span>
  <span class="br0">&#40;</span>0 <span class="kw1">to</span> 100<span class="br0">&#41;</span>.<span class="me1">flatMap</span><span class="br0">&#40;</span>i<span class="sy0">=</span>&gt;<span class="br0">&#40;</span>i <span class="kw1">to</span> <span class="nu0">100</span><span class="br0">&#41;</span>.<span class="me1">filter</span><span class="br0">&#40;</span>j<span class="sy0">=</span>&gt;i<span class="sy0">+</span>j<span class="sy0">==</span><span class="nu0">100</span><span class="br0">&#41;</span>.<span class="me1">map</span><span class="br0">&#40;</span>j<span class="sy0">=</span>&gt;Pair<span class="br0">&#40;</span>i<span class="sy0">,</span>j<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre></div></div>
<p>Note that nested for loops are also non-strict safe, because <code>filter()</code>, <code>map()</code> and <code>flatMap()</code> are all non-strict safe. Only the traditional for loop is not safe for non-strict use.
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.blumenfeld-maso.com%2F2010%2F01%2Fscala-word-of-the-day-for-loops-all-4-kinds%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.blumenfeld-maso.com%2F2010%2F01%2Fscala-word-of-the-day-for-loops-all-4-kinds%2F&amp;source=bmaso&amp;style=normal&amp;service=bit.ly&amp;hashtags=for+loop,iterable,iterator,non-strict,scala" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.blumenfeld-maso.com/2010/01/scala-word-of-the-day-for-loops-all-4-kinds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scala Word of the Day: Catamorphism</title>
		<link>http://www.blumenfeld-maso.com/2010/01/scala-word-of-the-day-catamorphism/</link>
		<comments>http://www.blumenfeld-maso.com/2010/01/scala-word-of-the-day-catamorphism/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 04:31:27 +0000</pubDate>
		<dc:creator>Brian Maso</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala Word of the Day]]></category>

		<guid isPermaLink="false">http://www.blumenfeld-maso.com/?p=75</guid>
		<description><![CDATA[Catamorphism is the $20 word for a function that condenses a set of values down to a single value. For example, in SQL the terms grouping function or aggregator function refer to catamorphic functions, which includes SQL functions AVERAGE(), SUM(), and MEDIAN(). All of these SQL functions have in common a pattern that operates on [...]]]></description>
			<content:encoded><![CDATA[<p><strong><em>Catamorphism</em></strong> is the $20 word for a function that condenses a set of values down to a single value. For example, in SQL the terms <em>grouping function</em> or <em>aggregator function</em> refer to catamorphic functions, which includes SQL functions <code>AVERAGE()</code>, <code>SUM()</code>, and <code>MEDIAN()</code>. All of these SQL functions have in common a pattern that operates on a set of numeric values, and returns a single numeric value.</p>
<p>In functional programming, catamorphisms are embodied in the <em>folds</em> functions. I like Scala, so I&#8217;ll discuss some catamorphic function examples in found in the standard Scala library&#8217;s Iterable trait.</p>
<p>The Iterable trait in Scala represents the concept of an ordered series of same-typed values. This trait is common to all the collection types (Lists, Seqs, and Streams, and many other &#8220;series like&#8221; things). The Iterable trait defines several catamorphic functions &#8212; functions which reduce the series of values down to a single value.</p>
<p>Perhaps the simplest to start with is the <code>forall()</code> and <code>exists()</code> methods. These methods each take a Boolean test operation, applies it to each member of the Iterable, and returns the logical AND (<code>forall()</code>), or OR (<code>exists</code>) of the test values. Despite the fact that they act differently, both methods have the same type signature, because both are Boolean catamorphisms:</p>
<div id="wpshdo_11" class="wp-synhighlighter-outer"><div id="wpshdt_11" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_11"></a><a id="wpshat_11" class="wp-synhighlighter-title" href="#codesyntax_11"  onClick="javascript:wpsh_toggleBlock(11)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_11" onClick="javascript:wpsh_code(11)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_11" onClick="javascript:wpsh_print(11)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_11" class="wp-synhighlighter-inner" style="display: block;"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span class="kw1">trait</span></a> Iterable<span class="br0">&#91;</span>A<span class="br0">&#93;</span> <span class="br0">&#123;</span>
  ...
  <a href="http://scala-lang.org"><span class="kw1">def</span></a> forall<span class="br0">&#40;</span>test<span class="sy0">:</span> <span class="br0">&#40;</span>A<span class="br0">&#41;</span> <span class="sy0">=&gt;</span> Boolean<span class="br0">&#41;</span><span class="sy0">:</span> Boolean <span class="sy0">=</span> ...
  <a href="http://scala-lang.org"><span class="kw1">def</span></a> find<span class="br0">&#40;</span>test<span class="sy0">:</span> <span class="br0">&#40;</span>A<span class="br0">&#41;</span> <span class="sy0">=&gt;</span> Boolean<span class="br0">&#41;</span><span class="sy0">:</span> Boolean <span class="sy0">=</span> ...
  ...
<span class="br0">&#125;</span></pre></div></div>
<p>Event more generic are the <code>foldLeft()</code> and <code>foldRight()</code> methods in Iterable. <code>foldRight()</code> is really just a slight tweak on <code>foldLeft()</code>, so I&#8217;ll just explain left-hand version and for the right-hand version I&#8217;ll just do some&#8230; erm&#8230; hand-waving.</p>
<p>The <code>foldLeft()</code> method receives an aggregator function used to combine each member of the Iterable successively in to an aggregated value. <code>foldLeft()</code> starts with an initial value, which it combines with the first Iterable value using the aggregator function. The result is then aggregated with the second Iterable value; and then the third, and so on, until the entire Iterable has been reduce down to a single aggregated value. Thus <code>foldLeft()</code> is a catamorphism, as it combines and reduces the series of values represented by an Iterable down to a single value.</p>
<p style="text-align: center;"><img class="aligncenter" title="fodlLeft Sequence Diagram" src="http://www.websequencediagrams.com/cgi-bin/cdraw?lz=Q2xpZW50IENvZGUtPmZvbGRMZWZ0OgoAAggtPmluaXRpYWxWYWx1ZTogYSA8LSBnZXQgdmFsdWUKbG9vcAogICAgACYKSXRlcmFibGU6IG4AJQdOZXh0ABYPYWdncmVnYXRvckZ1bmMAUAdjb21iaW5lKGEsIG4pCmVuZAB7CwCBHAs6IHJldHVybmVkAIB_BiA8LSBh&amp;s=default" alt="" width="512" height="214" /></p>
<p><code>foldRight()</code> is the same as <code>foldLeft()</code> &#8212; the only difference being which end of the series each starts with. The <code>foldLeft()</code> function starts with the head of the series (traditionally drawn to the left of the tail &#8212; hence the name) and proceeds to the coda; <code>foldRight()</code> starts with the series coda and proceeds to the head.</p>
<div id="wpshdo_12" class="wp-synhighlighter-outer"><div id="wpshdt_12" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_12"></a><a id="wpshat_12" class="wp-synhighlighter-title" href="#codesyntax_12"  onClick="javascript:wpsh_toggleBlock(12)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_12" onClick="javascript:wpsh_code(12)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_12" onClick="javascript:wpsh_print(12)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_12" class="wp-synhighlighter-inner" style="display: block;"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span class="kw1">trait</span></a> Iterable<span class="br0">&#91;</span>A<span class="br0">&#93;</span> <span class="br0">&#123;</span>
  ...
  <a href="http://scala-lang.org"><span class="kw1">def</span></a> foldLeft<span class="br0">&#91;</span>B<span class="br0">&#93;</span> <span class="br0">&#40;</span>initialValue<span class="sy0">:</span> B<span class="br0">&#41;</span><span class="br0">&#40;</span>aggregatorFunc<span class="sy0">:</span> <span class="br0">&#40;</span>B, A<span class="br0">&#41;</span> <span class="sy0">=&gt;</span> B<span class="br0">&#41;</span><span class="sy0">:</span> B <span class="sy0">=</span> ...
  <a href="http://scala-lang.org"><span class="kw1">def</span></a> foldRight<span class="br0">&#91;</span>B<span class="br0">&#93;</span><span class="br0">&#40;</span>initialValue<span class="sy0">:</span> B<span class="br0">&#41;</span><span class="br0">&#40;</span>aggregatorFunc<span class="sy0">:</span> <span class="br0">&#40;</span>B, A<span class="br0">&#41;</span> <span class="sy0">=&gt;</span> B<span class="br0">&#41;</span><span class="sy0">:</span> B <span class="sy0">=</span> ...
  ...
<span class="br0">&#125;</span></pre></div></div>
<p>If you think for a second, you can see how <code>forall()</code> and <code>exists()</code> could be implemented using <code>foldLeft()</code>:</p>
<div id="wpshdo_13" class="wp-synhighlighter-outer"><div id="wpshdt_13" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_13"></a><a id="wpshat_13" class="wp-synhighlighter-title" href="#codesyntax_13"  onClick="javascript:wpsh_toggleBlock(13)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_13" onClick="javascript:wpsh_code(13)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_13" onClick="javascript:wpsh_print(13)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_13" class="wp-synhighlighter-inner" style="display: block;"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span class="kw1">trait</span></a> Iterable<span class="br0">&#91;</span>A<span class="br0">&#93;</span> <span class="br0">&#123;</span>
  ...
  <a href="http://scala-lang.org"><span class="kw1">def</span></a> foldLeft<span class="br0">&#91;</span>B<span class="br0">&#93;</span> <span class="br0">&#40;</span>initialValue<span class="sy0">:</span> B<span class="br0">&#41;</span><span class="br0">&#40;</span>aggregatorFunc<span class="sy0">:</span> <span class="br0">&#40;</span>B, A<span class="br0">&#41;</span> <span class="sy0">=&gt;</span> B<span class="br0">&#41;</span><span class="sy0">:</span> B <span class="sy0">=</span> ...
  ...
  <a href="http://scala-lang.org"><span class="kw1">def</span></a> forall<span class="br0">&#40;</span>test<span class="sy0">:</span> <span class="br0">&#40;</span>A<span class="br0">&#41;</span> <span class="sy0">=&gt;</span> Boolean<span class="br0">&#41;</span><span class="sy0">:</span> Boolean <span class="sy0">=</span>
      foldLeft<span class="br0">&#40;</span><a href="http://scala-lang.org"><span class="kw1">true</span></a><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="sy0">_</span> <span class="sy0">&amp;&amp;</span> test<span class="br0">&#40;</span><span class="sy0">_</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
  <a href="http://scala-lang.org"><span class="kw1">def</span></a> exists<span class="br0">&#40;</span>test<span class="sy0">:</span> <span class="br0">&#40;</span>A<span class="br0">&#41;</span> <span class="sy0">=&gt;</span> Boolean<span class="br0">&#41;</span><span class="sy0">:</span> Boolean <span class="sy0">=</span>
      foldLeft<span class="br0">&#40;</span><a href="http://scala-lang.org"><span class="kw1">false</span></a><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="sy0">_</span> || test<span class="br0">&#40;</span><span class="sy0">_</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
  ...
<span class="br0">&#125;</span></pre></div></div>
<p>In addition to <code>forall()</code> and <code>exists()</code> there are other catamorphic convenience functions in Iterable, and as it turns out each of them could be re-implemented using <code>foldLeft()</code> (or <code>foldRight()</code>) as well. For example, <code>mkString()</code> creates a string representation of the Iterable series by combining each member&#8217;s <code>toString()</code> value, along with a prefix, a suffix, and a list separator string.</p>
<div id="wpshdo_14" class="wp-synhighlighter-outer"><div id="wpshdt_14" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_14"></a><a id="wpshat_14" class="wp-synhighlighter-title" href="#codesyntax_14"  onClick="javascript:wpsh_toggleBlock(14)" title="Click to show/hide code block">Code block</a></td><td align="right"><a href="#codesyntax_14" onClick="javascript:wpsh_code(14)" title="Show code only"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_14" onClick="javascript:wpsh_print(14)" title="Print code"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://www.blumenfeld-maso.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_14" class="wp-synhighlighter-inner" style="display: block;"><pre class="scala" style="font-family:monospace;"><a href="http://scala-lang.org"><span class="kw1">trait</span></a> Iterable<span class="br0">&#91;</span>A<span class="br0">&#93;</span> <span class="br0">&#123;</span>
  ...
  <a href="http://scala-lang.org"><span class="kw1">def</span></a> foldLeft<span class="br0">&#91;</span>B<span class="br0">&#93;</span> <span class="br0">&#40;</span>initialValue<span class="sy0">:</span> B<span class="br0">&#41;</span><span class="br0">&#40;</span>aggregatorFunc<span class="sy0">:</span> <span class="br0">&#40;</span>B, A<span class="br0">&#41;</span> <span class="sy0">=&gt;</span> B<span class="br0">&#41;</span><span class="sy0">:</span> B <span class="sy0">=</span> ...
  ...
  <a href="http://scala-lang.org"><span class="kw1">def</span></a> mkString<span class="br0">&#40;</span>prefix<span class="sy0">:</span> String, separator<span class="sy0">:</span> String, suffix<span class="sy0">:</span> String<span class="br0">&#41;</span><span class="sy0">:</span> String <span class="sy0">=</span>
      foldLeft<span class="br0">&#40;</span>prefix<span class="br0">&#41;</span><span class="br0">&#40;</span><span class="sy0">_</span> + separator + <span class="sy0">_</span><span class="br0">&#41;</span> + suffix
  ...
<span class="br0">&#125;</span></pre></div></div>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.blumenfeld-maso.com%2F2010%2F01%2Fscala-word-of-the-day-catamorphism%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.blumenfeld-maso.com%2F2010%2F01%2Fscala-word-of-the-day-catamorphism%2F&amp;source=bmaso&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.blumenfeld-maso.com/2010/01/scala-word-of-the-day-catamorphism/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

