<?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>lexanderA &#187; Scala</title>
	<atom:link href="http://lexandera.com/tag/scala/feed/" rel="self" type="application/rss+xml" />
	<link>http://lexandera.com</link>
	<description>A blog about the web, mobile web, semantic web and mobile semantic web.</description>
	<lastBuildDate>Sun, 06 Jun 2010 18:27:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Don&#8217;t &#8212; in Scala</title>
		<link>http://lexandera.com/2009/11/dont-in-scala/</link>
		<comments>http://lexandera.com/2009/11/dont-in-scala/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 08:49:43 +0000</pubDate>
		<dc:creator>Aleksander Kmetec</dc:creator>
				<category><![CDATA[Ideas]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Acme]]></category>
		<category><![CDATA[Control flow]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Syntax]]></category>

		<guid isPermaLink="false">http://lexandera.com/?p=553</guid>
		<description><![CDATA[While browsing Reddit yesterday, I came across this thread on Perl&#8217;s Acme::Don't module. The Acme::Don't module is a joke module which adds support for &#8220;don't&#8220;, the opposite of the &#8220;do&#8221; command. The &#8220;don't&#8221; construct accepts a block of code and then DOESN&#8217;T execute it, no matter what. Now, lately I&#8217;ve been playing around with Scala [...]]]></description>
			<content:encoded><![CDATA[<p>While browsing Reddit yesterday, I came across this <a href="http://www.reddit.com/r/programming/comments/a26fe/dont/">thread on Perl&#8217;s <code>Acme::Don't</code> module</a>. The <a href="http://search.cpan.org/%7Edconway/Acme-Don-t-1.01/t.pm"><code>Acme::Don't</code></a> module is a joke module which adds support for &#8220;<code>don't</code>&#8220;, the opposite of the &#8220;<code>do</code>&#8221; command. The &#8220;<code>don't</code>&#8221; construct accepts a block of code and then DOESN&#8217;T execute it, no matter what. Now, lately I&#8217;ve been playing around with <a class="zem_slink" href="http://www.scala-lang.org" title="Scala (programming language)" rel="homepage">Scala</a> and I know that thanks to its flexible syntax it&#8217;s possible to create functions which look and behave just like control structures. This got me thinking&#8230; could I implement a useful version of &#8220;<code>don't</code>&#8220;? </p>
<p>After a couple of minutes I came up with the following 2 use cases (in addition to the original standalone don&#8217;t block):</p>
<pre name="code" class="javascript">

dont { ... }
dont { ... } unless (condition)
dont { ... } until (condition)
</pre>
<p>Implementing the <code>dont { ... }</code> part is easy. The only thing needed to make this work is a function which accepts a block of code and doesn&#8217;t do anything with it.</p>
<p>Adding support for the <code>unles/until</code> part is a bit trickier, but still easy. For this to work, the dont function must return an object which contains methods named <code>unless</code> and <code>until</code>. These methods then evaluate the condition passed to them and execute the original block of code <code>if/when</code> the condition is true.</p>
<p>It&#8217;s probably best if I just show you the code:</p>
<pre name="code" class="scala">

def dont(code: =&gt; Unit) = new DontCommand(code)

class DontCommand(code: =&gt; Unit) {
    def unless(condition: =&gt; Boolean) =
        if (condition) code

    def until(condition: =&gt; Boolean) = {
        while (!condition) {}
        code
    }
}
</pre>
<p>Now there&#8217;s nothing else left but to use it in an example:</p>
<pre name="code" class="scala">

/* This will never be executed */
dont {
    println(&quot;Hello? Can anyone hear me?&quot;);
}

/* This will only be executed if the condition is true */
dont {
    println(&quot;Yep, 2 really is greater than 1.&quot;)
} unless (2 &gt; 1) 

/* Just a helper function */
var number = 0;
def nextNumber() = {
    number += 1
    println(number)
    number
}

/* This will not be printed until the condition is met. */
dont {
    println(&quot;Done counting to 5!&quot;)
} until (nextNumber() == 5) 
</pre>
<p>Runnig the above code should print:</p>
<blockquote><p>Yep, 2 really is greater than 1.<br />
1<br />
2<br />
3<br />
4<br />
5<br />
Done counting to 5!</p></blockquote>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><img style="border: medium none ; float: right;" class="zemanta-pixie-img" alt="" src="http://img.zemanta.com/pixy.gif?x-id=72b2b28a-d74d-477d-93b7-4f614da280d5"><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://lexandera.com/2009/11/dont-in-scala/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
