<?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>christopherowen.id.au &#187; programming</title>
	<atom:link href="http://christopherowen.id.au/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://christopherowen.id.au/blog</link>
	<description>Stuff cubed plus one</description>
	<lastBuildDate>Wed, 18 Aug 2010 23:21:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>The Happy Toggler</title>
		<link>http://christopherowen.id.au/blog/2010/01/11/the-happy-toggler/</link>
		<comments>http://christopherowen.id.au/blog/2010/01/11/the-happy-toggler/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 12:23:49 +0000</pubDate>
		<dc:creator>Christopher Owen</dc:creator>
				<category><![CDATA[nerd]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://christopherowen.id.au/blog/?p=156</guid>
		<description><![CDATA[So I've been writing a lot of boolean toggling code recently, and it's been making me sad. Can't we make it happier?]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been writing a lot of UI code lately, and one of the bread-and-butter operations in UI code is toggling boolean state, usually in response to a button press. Now, what does toggling code look like? </p>
<p>Say I have an object that likes to transition solely between happy states every time a certain event occurs. Here&#8217;s how I might handle it:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"> theObject.<span style="color: #006633;">happy</span> <span style="color: #339933;">=</span> <span style="color: #339933;">!</span>theObject.<span style="color: #006633;">happy</span><span style="color: #339933;">;</span></pre></div></div>

<p>There. Simple huh? Something you probably learn how to do in an introductory programming course. It might be surprising to you, but someone <a href="http://stackoverflow.com/questions/224311/cleanest-way-to-toggle-a-boolean-variable-in-java">asked a question on how to do this on Stack Overflow</a>. The other alternative provided in the answer is the somewhat more cerebral:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">theObject.<span style="color: #006633;">happy</span> <span style="color: #339933;">^=</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span></pre></div></div>

<p>Thanks Spock. Of course if your language doesn&#8217;t have an assigning XOR operator this option is ruled out. If you value clarity of code, or you simply don&#8217;t want less brainy maintainers pestering you about what your code is doing, you rule this option out as well.</p>
<p>So I&#8217;ve been writing a lot of code recently of the first form, and it&#8217;s been making me sad. There&#8217;s a strong feeling of non object oriented-ness about it. I mean, to toggle the state of an object I have to:</p>
<ol>
<li>ask it what its current state is;
<li>flip that state locally;
<li>and then tell the object to take on this new state.
</ol>
<p>Bleh! Why the hell should I care what state it currently is in? I just want to toggle it! Shouldn&#8217;t I just be able to ask the object to toggle it&#8217;s own state; perhaps of even multiple properties in a batch sequence? </p>
<p>Naturally, nobody wants to write a toggling method for every writable boolean property of their objects. So either there needs to be automatic support in the platform or some ability to provide a general toggling abstraction.</p>
<p>One such example of a crude toggling abstraction would be a Ruby module defined like the following.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> Toggler
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">class</span> ToggleHelper
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>target<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@target</span> = target
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> method_missing<span style="color:#006600; font-weight:bold;">&#40;</span>sym<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#0066ff; font-weight:bold;">@target</span>.<span style="color:#9900CC;">toggle</span><span style="color:#006600; font-weight:bold;">&#40;</span>sym<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">def</span> toggle; <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> toggle<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>sym<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> ToggleHelper.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> sym.<span style="color:#9900CC;">empty</span>?
&nbsp;
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;#{sym}=&quot;</span>, !<span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>sym<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Such a module could be mixed-in at the appropriate levels of an inheritance hierarchy, or maybe even at the root level if you&#8217;re game.</p>
<p>With the mix-in mixed-in, toggling code would be more concise and less repetitious:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">the_object.<span style="color:#9900CC;">toggle</span>.<span style="color:#9900CC;">happy</span>
the_object.<span style="color:#9900CC;">toggle</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:happy</span><span style="color:#006600; font-weight:bold;">&#41;</span>
the_object.<span style="color:#9900CC;">toggle</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:wanted</span>, <span style="color:#ff3333; font-weight:bold;">:happy</span>, <span style="color:#ff3333; font-weight:bold;">:virtuous</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Doesn&#8217;t a declarative syntax such as this feel much more happy?</p>
]]></content:encoded>
			<wfw:commentRss>http://christopherowen.id.au/blog/2010/01/11/the-happy-toggler/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Java developers have 99 problems&#8230;</title>
		<link>http://christopherowen.id.au/blog/2009/08/26/java-developers-have-99-problems/</link>
		<comments>http://christopherowen.id.au/blog/2009/08/26/java-developers-have-99-problems/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 13:49:11 +0000</pubDate>
		<dc:creator>Christopher Owen</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://christopherowen.id.au/blog/?p=119</guid>
		<description><![CDATA[Java developers have many problems but safe resource acquisition and release isn't one.]]></description>
			<content:encoded><![CDATA[<p>&#8230; but this ain&#8217;t one.</p>
<p>Today Yahuda Katz posted his <a href="http://yehudakatz.com/2009/08/24/my-10-favorite-things-about-the-ruby-language/">10 favourite things about the ruby language</a>. His list certainly reflects most of the things I find very appealing about the language. The sixth item highlights Ruby&#8217;s excellent support for blocks and lambdas and an argument is mounted that when performing file operations in languages without them, programmers are <q>forced to use an inline “ensure” block every in the same lexical scope that they originally opened the file in, to ensure that the resource is closed.</q> As is often the case the comparison is made to Java, but as any seasoned Java developer will tell you (and this point has probably be made many, many times) it just isn&#8217;t true. Java supports a safer and comparable idiom via the anonymous inner class. </p>
<p>The example given is a very succinct Ruby method to print out the lines of a text file. Ruby&#8217;s <kbd>File.open</kbd> ensures the file is properly closed after the block has completed, normally or otherwise:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> run<span style="color:#006600; font-weight:bold;">&#40;</span>input<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>input, <span style="color:#996600;">&quot;r&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
    f.<span style="color:#9900CC;">each_line</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> line <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The Java version listed does the same but, predictably, is far more verbose and laborious:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> in<span style="color: #009900;">&#41;</span> 
<span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">FileNotFoundException</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003399;">File</span> input <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>in<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003399;">String</span> line<span style="color: #339933;">;</span> Scanner reader <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Scanner<span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>reader.<span style="color: #006633;">hasNextLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>reader.<span style="color: #006633;">nextLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span> reader.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>However no Java developer that values their sanity is ever going to sprinkle this pattern through their code base any time they want to use a File and ensure that it is closed properly. Here&#8217;s what they are going to use instead:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> Processor<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">void</span> process<span style="color: #009900;">&#40;</span>T target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Scanner</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FileSlurper <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> slurp<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> fileName, Processor<span style="color: #339933;">&lt;</span>Scanner<span style="color: #339933;">&gt;</span> processor<span style="color: #009900;">&#41;</span> 
    <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">FileNotFoundException</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003399;">File</span> input <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">File</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Scanner reader <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      reader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Scanner<span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      processor.<span style="color: #006633;">process</span><span style="color: #009900;">&#40;</span>reader<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span>
      reader.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Once that infrastructure is in place, the Java implementation of the example becomes:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">FileSlurper.<span style="color: #006633;">slurp</span><span style="color: #009900;">&#40;</span>input, <span style="color: #000000; font-weight: bold;">new</span> Processor<span style="color: #339933;">&lt;</span>Scanner<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> process<span style="color: #009900;">&#40;</span>Scanner reader<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>reader.<span style="color: #006633;">hasNextLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>reader.<span style="color: #006633;">nextLine</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>No one can argue that this is as pretty or convenient as the Ruby version, but it does ensure that file opening and closing is handled correctly without any effort from the client code. The code in the anonymous inner class can even access variables in the current lexical context, as with the Ruby block, with the caveat that they must be declared <kbd>final</kbd> (not quite a lexical closure!). This pattern, in conjunction with the <kbd>Processor</kbd> interface may be used any time there is a need for similar resource acquisition and clean&ndash;up procedures, as might be the case for locks or database connections.</p>
<p>Lambdas and blocks are very useful constructs, and the anonymous inner class can act as an acceptable, if far from ideal, analogue. What an anonymous inner class can&#8217;t do is act as a co&ndash;routine; this is a truly powerful feature of Ruby&#8217;s blocks and associated method invocation features that Java can&#8217;t directly compete with.</p>
<p>One thing&#8217;s for certain: the Ruby community won&#8217;t win over Java developers by giving examples of deficiencies where none exist.</p>
]]></content:encoded>
			<wfw:commentRss>http://christopherowen.id.au/blog/2009/08/26/java-developers-have-99-problems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript reduce</title>
		<link>http://christopherowen.id.au/blog/2008/07/08/javascript-reduce/</link>
		<comments>http://christopherowen.id.au/blog/2008/07/08/javascript-reduce/#comments</comments>
		<pubDate>Tue, 08 Jul 2008 13:20:47 +0000</pubDate>
		<dc:creator>Christopher Owen</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[javascript reduce]]></category>

		<guid isPermaLink="false">http://christopherowen.id.au/blog/?p=42</guid>
		<description><![CDATA[Tonight Matt posted a Javascript solution to a problem he saw described elsewhere. Here's a version that I whipped up.]]></description>
			<content:encoded><![CDATA[<p>Tonight <a href="http://www.mattryall.net">Matt</a> posted a <a href="http://mattryall.net/blog/2008/07/dustins-programming-problem">JavaScript solution to a problem he saw described</a> elsewhere. Here&#8217;s a version that I whipped up to use JavaScript 1.8&#8242;s new <a href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Objects:Array:reduce">Array.prototype.reduce function</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> arrVal <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;d&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;e&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;e&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;e&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;e&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;e&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;f&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;e&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;f&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;e&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;f&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;f&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;f&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;f&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> group<span style="color: #009900;">&#40;</span>previousValue<span style="color: #339933;">,</span> currentValue<span style="color: #339933;">,</span> index<span style="color: #339933;">,</span> arr<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>index <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span>  <span style="color: #339933;">&amp;&amp;</span> arr<span style="color: #009900;">&#91;</span>index <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> currentValue<span style="color: #009900;">&#41;</span>
		<span style="color: #000066; font-weight: bold;">return</span> previousValue.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">concat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>previousValue.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">concat</span><span style="color: #009900;">&#40;</span>currentValue<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> previousValue.<span style="color: #660066;">concat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>currentValue<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> format<span style="color: #009900;">&#40;</span>previousValue<span style="color: #339933;">,</span> currentValue<span style="color: #339933;">,</span> index<span style="color: #339933;">,</span> arr<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> previousValue.<span style="color: #660066;">concat</span><span style="color: #009900;">&#40;</span>
		currentValue.<span style="color: #660066;">length</span> <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">3</span> <span style="color: #339933;">?</span> 
		currentValue.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
		currentValue.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> 
			<span style="color: #3366CC;">&quot; &lt;span&gt;&quot;</span> <span style="color: #339933;">+</span> currentValue.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&lt;span&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000066;">print</span><span style="color: #009900;">&#40;</span>arrVal.<span style="color: #660066;">reduce</span><span style="color: #009900;">&#40;</span>group<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">reduce</span><span style="color: #009900;">&#40;</span>format<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://christopherowen.id.au/blog/2008/07/08/javascript-reduce/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Wondrous Functional Languages</title>
		<link>http://christopherowen.id.au/blog/2007/02/27/wondrous-functional-languages/</link>
		<comments>http://christopherowen.id.au/blog/2007/02/27/wondrous-functional-languages/#comments</comments>
		<pubDate>Tue, 27 Feb 2007 10:52:58 +0000</pubDate>
		<dc:creator>Christopher Owen</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://christopherowen.id.au/blog/?p=24</guid>
		<description><![CDATA[My colleague Matt wrote about a recent Ruby Quiz which emphasised short, elegant solutions. He was particularly impressed with a solution which used a property of Ruby hashes to generate a wondrous number sequence for any given integer in a single line of code. That got me thinking about the solution in Scheme, a language [...]]]></description>
			<content:encoded><![CDATA[<p>My colleague <a href="http://mattryall.net/">Matt</a> wrote about a recent <a href="http://www.rubyquiz.com/">Ruby Quiz</a> which emphasised short, elegant solutions. He was <a href="http://www.mattryall.net/article.cgi?id=282">particularly impressed</a> with a solution which used a property of Ruby hashes to generate a wondrous number sequence for any given integer in a single line of code.</p>
<p>That got me thinking about the solution in Scheme, a language I find very appealing, and Haskell, which I had started picking up a few months ago but had to abandon due to lack of time. Even so I managed to remember enough to get this basic solution within a few minutes:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">wondrous <span style="color: #339933; font-weight: bold;">::</span> <span style="color: #cccc00; font-weight: bold;">Int</span> <span style="color: #339933; font-weight: bold;">-&gt;</span> <span style="color: green;">&#91;</span><span style="color: #cccc00; font-weight: bold;">Int</span><span style="color: green;">&#93;</span>
wondrous <span style="color: red;">1</span> <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: green;">&#93;</span>
wondrous x
	<span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">even</span> x <span style="color: #339933; font-weight: bold;">=</span> x : wondrous <span style="color: green;">&#40;</span>x `<span style="font-weight: bold;">div</span>` <span style="color: red;">2</span><span style="color: green;">&#41;</span>
	<span style="color: #339933; font-weight: bold;">|</span> <span style="font-weight: bold;">otherwise</span> <span style="color: #339933; font-weight: bold;">=</span> x : wondrous <span style="color: green;">&#40;</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">*</span>x<span style="color: #339933; font-weight: bold;">+</span><span style="color: red;">1</span><span style="color: green;">&#41;</span></pre></div></div>

<p>Here is the most obvious implementation in Scheme:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>define wondrous
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">lambda</span> <span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> x <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> ‘<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>modulo x <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> x <span style="color: #66cc66;">&#40;</span>wondrous <span style="color: #66cc66;">&#40;</span>quotient x <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span>else <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> x <span style="color: #66cc66;">&#40;</span>wondrous <span style="color: #66cc66;">&#40;</span>+ <span style="color: #66cc66;">&#40;</span>* <span style="color: #cc66cc;">3</span> x<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>As you can see both of these functional languages are really suited to expressing such algorithms, particularly Haskell with its excellent pattern matching expressions. I have no doubts that there are far more elegant solutions in both languages; I&#8217;ll post them if I get suitably inspired.</p>
]]></content:encoded>
			<wfw:commentRss>http://christopherowen.id.au/blog/2007/02/27/wondrous-functional-languages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
