<?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>Mark Needham &#187; Clojure</title>
	<atom:link href="http://www.markhneedham.com/blog/category/clojure/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Sat, 31 Jul 2010 07:06:21 +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>Clojure: My first attempt at a macro</title>
		<link>http://www.markhneedham.com/blog/2009/12/12/clojure-my-first-attempt-at-a-macro/</link>
		<comments>http://www.markhneedham.com/blog/2009/12/12/clojure-my-first-attempt-at-a-macro/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 17:53:37 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1916</guid>
		<description><![CDATA[I'm up to the chapter on using macros in Stuart Halloway's 'Programming Clojure' book and since I've never used a language which has macros in before I thought it'd be cool to write one. In reality there's no reason to create a macro to do what I want to do but I wanted to keep [...]]]></description>
			<content:encoded><![CDATA[<p>I'm up to the chapter on using macros in Stuart Halloway's '<a href="http://www.amazon.com/gp/product/1934356336?ie=UTF8&#038;tag=marneesblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=1934356336">Programming Clojure</a>' book and since I've never used a language which has macros in before I thought it'd be cool to write one.</p>
<p>In reality there's no reason to create a macro to do what I want to do but I wanted to keep the example simple so I could try and understand exactly how macros work.</p>
<p>I want to create a macro which takes in one argument and then prints hello and the person's name.</p>
<p>In the book Halloway suggests that we should start with the expression that we want to end up with, so this is what I want:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;Hello&quot;</span> person<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>My first attempt to do that was:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> say-hello <span style="color: #66cc66;">&#91;</span>person<span style="color: #66cc66;">&#93;</span>
  println <span style="color: #ff0000;">&quot;Hello&quot;</span> person<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I made the mistake of forgetting to include the brackets around the 'println' expression so it doesn't actually pass '"Hello"' and 'person' to 'println'. Instead each symbol is evaluated individually.</p>
<p>When we evaluate this in the REPL we therefore don't quite get what we want:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (say-hello &quot;mark&quot;)          
&quot;mark&quot;</pre></div></div>

<p>Expanding the macro results in:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (macroexpand-1 '(say-hello &quot;Mark&quot;))
&quot;Mark&quot;</pre></div></div>

<p>Which is the equivalent of doing this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (eval (do println &quot;hello&quot; &quot;Mark&quot;)) 
&quot;Mark&quot;</pre></div></div>

<p>As I <a href="http://www.markhneedham.com/blog/2009/12/12/clojure-forgetting-the-brackets/">wrote previously</a> this is because 'do' evaluates each argument in order and then returns the last one which in this case is "Mark".</p>
<p>I fixed that mistake and got the following:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> say-hello <span style="color: #66cc66;">&#91;</span>person<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;Hello&quot;</span> person<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Which returns the right result&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>say-hello <span style="color: #ff0000;">&quot;Mark&quot;</span><span style="color: #66cc66;">&#41;</span>
Hello Mark
<span style="color: #b1b100;">nil</span></pre></div></div>

<p>&#8230;but actually evaluated the expression rather than expanding it because I didn't escape it correctly:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (macroexpand-1 '(say-hello &quot;Mark&quot;))
Hello Mark
nil</pre></div></div>

<p>After these failures I decided to try and change one of the examples from the book instead of my trial and error approach.</p>
<p>One approach used is to build a list of Clojure symbols inside the macro definition:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> say-hello <span style="color: #66cc66;">&#91;</span>person<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> println <span style="color: #ff0000;">&quot;hello&quot;</span> person<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (macroexpand-1 '(say-hello &quot;Mark&quot;))
(#&lt;core$println__5440 clojure.core$println__5440@681ff4&gt; &quot;hello&quot; &quot;Mark&quot;)</pre></div></div>

<p>This is pretty much what we want and although the <a href="http://twitter.com/ajlopez/statuses/6540996368">'println' symbol has been evaluated at macro expansion time</a> it doesn't actually make any difference to the way the macro works.</p>
<p>We can fix that by escaping 'println' so that it won't be evaluated until evaluation time:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> say-hello <span style="color: #66cc66;">&#91;</span>person<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> 'println <span style="color: #ff0000;">&quot;hello&quot;</span> person<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (macroexpand-1 '(say-hello &quot;Mark&quot;))
(println &quot;hello&quot; &quot;Mark&quot;)</pre></div></div>

<p>I thought it should also be possible to quote(') the whole expression instead of building up the list:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> say-hello <span style="color: #66cc66;">&#91;</span>person<span style="color: #66cc66;">&#93;</span> 
  '<span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;hello&quot;</span> person<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This expands correctly but when we try to use it this happens:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (say-hello &quot;Mark&quot;)
java.lang.Exception: Unable to resolve symbol: person in this context</pre></div></div>

<p>The problem is that when we use quote there is no evaluation of any of the symbols in the expression so <a href="http://twitter.com/patrickdlogan/statuses/6536320695">the symbol 'person' is only evaluated at runtime and since it hasn't been bound to any value</a> we end up with the above error.</p>
<p>If we want to use the approach of non evaluation then we need to make use of the backquote(`) which stops evaluation of anything unless it's preceded by a ~.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> a <span style="color: #66cc66;">&#91;</span>person<span style="color: #66cc66;">&#93;</span>
  `<span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;hello&quot;</span> ~person<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This allows us to  evaluate 'person' at expand time and replace it with the appropriate value.</p>
<p>In hindsight the approach I took to write this macro was pretty ineffective although it's been quite interesting to see all the different ways that I've found to mess up the writing of one!</p>
<p>Thanks to <a href="http://twitter.com/ajlopez">A. J. Lopez</a>, <a href="http://twitter.com/patrickdlogan">Patrick Logan</a> and <a href="http://twitter.com/fogus">fogus</a> for helping me to understand all this a bit better than I did to start with!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/12/12/clojure-my-first-attempt-at-a-macro/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Clojure: Forgetting the brackets</title>
		<link>http://www.markhneedham.com/blog/2009/12/12/clojure-forgetting-the-brackets/</link>
		<comments>http://www.markhneedham.com/blog/2009/12/12/clojure-forgetting-the-brackets/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 17:51:19 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1913</guid>
		<description><![CDATA[I've been playing around with macros over the last few days and while writing a simple one forgot to include the brackets to make it evaluate correctly: &#40;defmacro say-hello &#91;person&#93; println &#34;Hello&#34; person&#41; This macro doesn't even expand like I thought it would: user=&#62; (macroexpand-1 '(say-hello blah)) blah That seemed a bit strange to me [...]]]></description>
			<content:encoded><![CDATA[<p>I've been playing around with macros over the last few days and while writing a simple one forgot to include the brackets to make it evaluate correctly:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> say-hello <span style="color: #66cc66;">&#91;</span>person<span style="color: #66cc66;">&#93;</span>
  println <span style="color: #ff0000;">&quot;Hello&quot;</span> person<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This macro doesn't even expand like I thought it would:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (macroexpand-1 '(say-hello blah))
blah</pre></div></div>

<p>That seemed a bit strange to me but I eventually realised that I'd missed off the brackets around 'println' and the arguments following it which would have resulted in 'println' being evaluated with those arguments.</p>
<p>I was a bit curious as to why that happened so I tried the following expression without any brackets to see what would happen:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; println &quot;hello&quot; &quot;mark&quot;
#&lt;core$println__5440 clojure.core$println__5440@681ff4&gt;
&quot;mark&quot;
&quot;random&quot;</pre></div></div>

<p>It seems to just evaluate each thing individually and when we put this type of expression into a function definition the function will do the same thing but also return the last thing evaluated:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn say-hello <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> println <span style="color: #ff0000;">&quot;hello&quot;</span> <span style="color: #ff0000;">&quot;mark&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (say-hello)
&quot;mark&quot;</pre></div></div>

<p><a href="http://twitter.com/ajlopez/statuses/6563641565">A. J. Lopez pointed out</a> that this is quite like <a href="http://www.delorie.com/gnu/docs/elisp-manual-21/elisp_125.html">progn</a> in other LISPs and is the same as doing the following:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (do println &quot;hello&quot; &quot;mark&quot;)
&quot;mark&quot;</pre></div></div>

<p><a href="http://clojure.org/special_forms#toc3">do</a> is defined as follows:</p>
<blockquote><p>
(do exprs*)<br />
Evaluates the expressions in order and returns the value of the last. If no expressions are supplied, returns nil.
</p></blockquote>
<p>The way to write a function which passes those two arguments to 'println' is of course to put brackets around the statement:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn say-hello <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;hello&quot;</span> <span style="color: #ff0000;">&quot;mark&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (say-hello) 
hello mark
nil</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/12/12/clojure-forgetting-the-brackets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clojure: when-let macro</title>
		<link>http://www.markhneedham.com/blog/2009/12/09/clojure-when-let-macro/</link>
		<comments>http://www.markhneedham.com/blog/2009/12/09/clojure-when-let-macro/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 16:41:47 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1903</guid>
		<description><![CDATA[In my continued playing around with Clojure I came across the 'when-let' macro. 'when-let' is used when we want to bind an expression to a symbol and only execute the body provided as the second argument to the macro if that symbol evaluates to true. As I wrote previously, a value of 'false' or 'nil' [...]]]></description>
			<content:encoded><![CDATA[<p>In my continued playing around with Clojure I came across the '<a href="http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/when-let">when-let</a>' macro.</p>
<p>'when-let' is used when we want to bind an expression to a symbol and only execute the body provided as the second argument to the macro if that symbol evaluates to true.</p>
<p>As I <a href="http://www.markhneedham.com/blog/2009/11/20/clojure-a-few-things-ive-been-tripping-up-on/">wrote previously</a>, a value of 'false' or 'nil' would result in the second argument not being evaluated. </p>
<p>A simple example of using 'when-let' would be:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>when-<span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>a <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;The value of a is:&quot;</span> a<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is the definition:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> when-<span style="color: #b1b100;">let</span>
  <span style="color: #ff0000;">&quot;bindings =&gt; binding-form test
&nbsp;
  When test is true, evaluates body with binding-form bound to the value of test&quot;</span>
  <span style="color: #66cc66;">&#91;</span>bindings <span style="color: #66cc66;">&amp;</span> body<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>assert-args when-<span style="color: #b1b100;">let</span>
     <span style="color: #66cc66;">&#40;</span>vector? bindings<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;a vector for its binding&quot;</span>
     <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span> <span style="color: #66cc66;">&#40;</span>count bindings<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;exactly 2 forms in binding vector&quot;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>form <span style="color: #66cc66;">&#40;</span>bindings <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> tst <span style="color: #66cc66;">&#40;</span>bindings <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    `<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>temp# ~tst<span style="color: #66cc66;">&#93;</span>
       <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> temp#
         <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>~form temp#<span style="color: #66cc66;">&#93;</span>
           ~@body<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></td></tr></table></div>

<p>The 'assert-args' call at the beginning of the macro is quite interesting. </p>
<p>Two assertions are stated:</p>
<ul>
<li>The first argument should be a vector</li>
<li>That vector should contain exactly two forms</li>
</ul>
<p>I've not used dynamic languages very much before but it seems like this is one way for a dynamic language to fail fast by checking that the arguments are as expected. In a static language that would be a compile time check.</p>
<p>Line 9 is quite interesting as we know that 'bindings' will be a vector so we can take the '0th' and '1st' elements from it and bind them to 'form' and 'tst' respectively. I didn't quite pick up on the first few times I read it.</p>
<p>On line 10 it makes use of 'auto-gensym' to create a unique name which begins with 'temp' and is bound to the value of 'tst' which in the simple example provided would be the value '2&#8242;. As I understand it the name would be something like 'temp__304&#8242; or something similarly random!</p>
<p>'when 2&#8242; evaluates to true which means that we execute the body provided as the second argument.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (when-let [a 2] (println &quot;The value of a is:&quot; a))
The value of a is: 2
nil</pre></div></div>

<p>This is a bit of a contrived example of using the construct and it seems to be properly used when we're getting a value out of a list and want to check whether or not we've reached the end of that list or not. If we have then eventually we'll have a value of 'nil' bound by the 'let' and then we'll know we're finished.</p>
<p>An example of where the body wouldn't be evaluated is:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (when-let [a nil] (println &quot;This won't get printed&quot;))
nil</pre></div></div>

<p>I don't really understand why we need to bind 'form' to 'temp' on the second last line as it doesn't seem like the value is used?  I'm sure there's probably something I'm missing there so if anyone could point it out that'd be cool!</p>
<p>As I understand it, the '~@body' on the last line is called the 'splicing unquote' and it allows the individual values in 'body' to be put into the template started at '`(let [temp# ~tst]' individually rather than just being put in there as a list.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/12/09/clojure-when-let-macro/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Clojure: Unit testing in the REPL</title>
		<link>http://www.markhneedham.com/blog/2009/12/06/clojure-unit-testing-in-the-repl/</link>
		<comments>http://www.markhneedham.com/blog/2009/12/06/clojure-unit-testing-in-the-repl/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 17:28:05 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1896</guid>
		<description><![CDATA[One thing which I think is great about coding with F# is the quick feedback that we can get by defining and then testing out functions in the REPL. We can do the same thing in Clojure but it's even better because we can also define and run unit tests which I think is pretty [...]]]></description>
			<content:encoded><![CDATA[<p>One thing which I think is great about coding with F# is the <a href="http://www.markhneedham.com/blog/2009/07/20/coding-quick-feedback/">quick feedback that we can get by defining and then testing out functions in the REPL</a>.</p>
<p>We can do the same thing in Clojure but it's even better because we can also define and run unit tests which I think is pretty neat.</p>
<p>Nurullah Akkaya has a <a href="http://nakkaya.com/2009/11/18/unit-testing-in-clojure/">good post which describes how to use clojure.test</a>, a testing framework written by <a href="http://stuartsierra.com/software/clojure-stuff">Stuart Sierra</a> so I've been using that to define some tests cases for the <a href="http://www.markhneedham.com/blog/2009/11/30/clojure-parsing-an-rss-feed/">little RSS feed parser that I'm writing</a>.</p>
<p>To use clojure.test straight out the box you need the <a href="http://github.com/richhickey/clojure">latest version of the clojure source code</a> as Stuart Sierra <a href="http://stuartsierra.com/software/clojure-stuff">points out on his website</a>.</p>
<p>I ran the ant task for the project and then launched the REPL pointing to the 'alpha snapshot' jar instead of the '1.0.0&#8242; jar and it seems to work fine.</p>
<p>I managed to break the 'get-title' function while playing with it before so I thought that would be a good one to try out the tests in the REPL with.</p>
<p>This function is supposed to strip out the name and the following colon which appears in every title and just show the title of the blog post.</p>
<p>I originally had this definition:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn get-title <span style="color: #66cc66;">&#91;</span>title<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>second <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">&#40;</span>re-seq #<span style="color: #ff0000;">&quot;.*:<span style="color: #000099; font-weight: bold;">\s</span>(.*)&quot;</span> title<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>I hadn't realised that this strips from the last colon in the string and therefore returns the wrong result for some inputs.</p>
<p>I created the following tests:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>use 'clojure<span style="color: #66cc66;">.</span>test<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>deftest test-get-title
  <span style="color: #66cc66;">&#40;</span>is <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;Clojure - It's awesome&quot;</span>  <span style="color: #66cc66;">&#40;</span>get-title <span style="color: #ff0000;">&quot;Mark Needham: Clojure - It's awesome&quot;</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>is <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;A Book: Book Review&quot;</span> <span style="color: #66cc66;">&#40;</span>get-title <span style="color: #ff0000;">&quot;Mark Needham: A Book: Book Review&quot;</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>We can run those with the following function:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>run-tests<span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">FAIL in (test-get-title) (NO_SOURCE_FILE:19)
expected: (= &quot;A Book: Book Review&quot; (get-title &quot;Mark Needham: A Book: Book Review&quot;))
  actual: (not (= &quot;A Book: Book Review&quot; &quot;Book Review&quot;))
&nbsp;
Ran 1 tests containing 2 assertions.
1 failures, 0 errors.</pre></div></div>

<p>Changing the function helps solve the problem:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn- get-title <span style="color: #66cc66;">&#91;</span>title<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>second <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">&#40;</span>re-seq #<span style="color: #ff0000;">&quot;[a-zA-Z0-9 ]+:<span style="color: #000099; font-weight: bold;">\s</span>(.*)&quot;</span> title<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>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Ran 1 tests containing 2 assertions.
0 failures, 0 errors.</pre></div></div>

<p>We can also run the assertions directly without having to call 'run-tests':</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>is <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;A Book: Book Review&quot;</span> <span style="color: #66cc66;">&#40;</span>get-title <span style="color: #ff0000;">&quot;Mark Needham: A Book: Book Review&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">true</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>is <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;Something Else&quot;</span> <span style="color: #66cc66;">&#40;</span>get-title <span style="color: #ff0000;">&quot;Mark Needham: A Book: Book Review&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">expected: (= &quot;Something Else&quot; (get-title &quot;Mark Needham: A Book: Book Review&quot;))
  actual: (not (= &quot;Something Else&quot; &quot;A Book: Book Review&quot;))
false</pre></div></div>

<p>Nurullah has <a href="http://nakkaya.com/2009/11/18/unit-testing-in-clojure/">more detail in his post about how to integrate tests into a build</a> although I don't need to do that just yet!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/12/06/clojure-unit-testing-in-the-repl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clojure: Parsing an RSS feed</title>
		<link>http://www.markhneedham.com/blog/2009/11/30/clojure-parsing-an-rss-feed/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/30/clojure-parsing-an-rss-feed/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 08:33:55 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1885</guid>
		<description><![CDATA[I've been playing around with a little script in Clojure to parse the ThoughtWorks Blogs RSS feed and then create a tweet for each of them which contains a link to the blog post and the person's Twitter ID if they have one. It's not finished yet but I'm finding the way that we parse [...]]]></description>
			<content:encoded><![CDATA[<p>I've been playing around with a little script in Clojure to parse the <a href="http://blogs.thoughtworks.com/rss20.xml">ThoughtWorks Blogs RSS feed</a> and then create a tweet for each of them which contains a link to the blog post and the person's Twitter ID if they have one.</p>
<p>It's not finished yet but I'm finding the way that we parse documents like this in Clojure quite intriguing.</p>
<p>The xml to parse looks roughly like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;rss version=&quot;2.0&quot;&gt; 
	&lt;channel&gt; 
 		...
		&lt;item&gt; 
			&lt;title&gt;Simon Brunning: Links for 2009-11-27 [del.icio.us]&lt;/title&gt; 
			&lt;link&gt;http://feedproxy.google.com/~r/SmallValuesOfCool/~3/WDqeLyMA-RE/brunns&lt;/link&gt; 
		&lt;/item&gt;
		&lt;item&gt; 
			&lt;title&gt;Alex Hung: Extending iPhone battery life&lt;/title&gt; 
			&lt;link&gt;http://alexhung.vox.com/library/post/extending-iphone-battery-life.html?_c=feed-atom-full&lt;/link&gt; 
		&lt;/item&gt;
		...
	&lt;/channel&gt; 
&lt;/rss&gt;</pre></div></div>

<p>I've only included the parts of the document that I'm interested in getting.</p>
<p>Following the examples from <a href="http://www.amazon.com/Programming-Clojure-Pragmatic-Programmers-Halloway/dp/1934356336/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1259561307&#038;sr=8-1">Stuart Halloway's book</a> one approach to do this is to make use of the '<a href="http://clojure.org/api#toc674">clojure.xml.parse</a>' and '<a href="http://clojure.org/api#toc618">clojure.core.xml-seq</a>' functions to create a sequence representing the tree structure of the feed.</p>
<p>I'm used to parsing XML with XPath but that doesn't make as much sense when we have a sequence of hash maps. Instead I'm using '<a href="http://clojure.org/api#toc248">filter</a>' and '<a href="http://clojure.org/api#toc356">map</a>' to try and achieve the same outcome.</p>
<p>I found that while I was trying to work out how to use these functions together I was often trying to solve the whole problem in one go instead of breaking it down into smaller more manageable pieces.</p>
<p>I also noticed that I was using 'filter' more often than I needed to instead of filtering the data to the point that everything I wanted to extract was in the remaining data set.</p>
<p>When I was playing with F# I got into the habit of trying to minimise the number of intermediate values I created but this seemed to be making life more difficult so I've allowed myself some intermediate values for the moment!</p>
<p>The goal is to poll the ThoughtWorks RSS feed and then update the <a href="http://twitter.com/planettw">planettw</a> account with the latest blog posts. The current setup does that but doesn't include people's Twitter names in the tweet so I'm trying to sort that out.</p>
<p>This is the code I have so far:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>use '<span style="color: #66cc66;">&#91;</span>clojure<span style="color: #66cc66;">.</span>xml <span style="color: #66cc66;">:</span><span style="color: #555;">only</span> <span style="color: #66cc66;">&#40;</span>parse<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def feed <span style="color: #66cc66;">&#40;</span>xml-seq <span style="color: #66cc66;">&#40;</span>parse <span style="color: #66cc66;">&#40;</span>java<span style="color: #66cc66;">.</span>io<span style="color: #66cc66;">.</span>File<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;clojure-play/tw-blogs-rss.txt&quot;</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>
&nbsp;
<span style="color: #66cc66;">&#40;</span>def rss-entries <span style="color: #66cc66;">&#40;</span>filter #<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">:</span><span style="color: #555;">item</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">tag</span> <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> feed<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn- get-href <span style="color: #66cc66;">&#91;</span>link<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>comp <span style="color: #66cc66;">:</span><span style="color: #555;">href</span> <span style="color: #66cc66;">:</span><span style="color: #555;">attrs</span><span style="color: #66cc66;">&#41;</span> link<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn- get-<span style="color: #b1b100;">value</span> <span style="color: #66cc66;">&#91;</span>node<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">content</span> node<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn rss-link <span style="color: #66cc66;">&#91;</span>entry<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>get-<span style="color: #b1b100;">value</span> <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">&#40;</span>filter #<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">:</span><span style="color: #555;">link</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">tag</span> <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">content</span> entry<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>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn rss-title <span style="color: #66cc66;">&#91;</span>entry<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>get-<span style="color: #b1b100;">value</span> <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">&#40;</span>filter #<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">:</span><span style="color: #555;">title</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">tag</span> <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">content</span> entry<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>
&nbsp;
<span style="color: #66cc66;">&#40;</span>def rss-titles <span style="color: #66cc66;">&#40;</span>map #<span style="color: #66cc66;">&#40;</span>rss-title <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#41;</span> rss-entries<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def rss-links <span style="color: #66cc66;">&#40;</span>map #<span style="color: #66cc66;">&#40;</span>rss-link <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#41;</span> rss-entries<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn- get-author <span style="color: #66cc66;">&#91;</span>title<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>second <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">&#40;</span>re-seq #<span style="color: #ff0000;">&quot;([<span style="color: #000099; font-weight: bold;">\w</span> ]+):&quot;</span> title<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn- get-title <span style="color: #66cc66;">&#91;</span>title<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>second <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">&#40;</span>re-seq #<span style="color: #ff0000;">&quot;[a-zA-Z0-9 ]+:<span style="color: #000099; font-weight: bold;">\s</span>(.*)&quot;</span> title<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>def authors <span style="color: #66cc66;">&#40;</span>map #<span style="color: #66cc66;">&#40;</span>get-author <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#41;</span> rss-titles<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def titles <span style="color: #66cc66;">&#40;</span>map #<span style="color: #66cc66;">&#40;</span>get-title <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#41;</span> rss-titles<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn- get-display-<span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#91;</span>twitter-names real-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>twitter-<span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span>twitter-names real-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> twitter-<span style="color: #b1b100;">name</span> <span style="color: #66cc66;">&#40;</span>str <span style="color: #ff0000;">&quot;@&quot;</span> twitter-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span> real-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>def twitter-names <span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;Mark Needham&quot;</span> <span style="color: #ff0000;">&quot;markhneedham&quot;</span>
                    <span style="color: #ff0000;">&quot;Alex Hung&quot;</span> <span style="color: #ff0000;">&quot;alexhung&quot;</span>
                    <span style="color: #ff0000;">&quot;Simon Brunning&quot;</span> <span style="color: #ff0000;">&quot;brunns&quot;</span>
                    <span style="color: #ff0000;">&quot;Ola Bini&quot;</span> <span style="color: #ff0000;">&quot;olabini&quot;</span>
                    <span style="color: #ff0000;">&quot;Patrick Kua&quot;</span> <span style="color: #ff0000;">&quot;patkua&quot;</span>
                    <span style="color: #ff0000;">&quot;Marc McNeill&quot;</span> <span style="color: #ff0000;">&quot;dancingmango&quot;</span>
                    <span style="color: #ff0000;">&quot;Dahlia Bock&quot;</span> <span style="color: #ff0000;">&quot;dlbock&quot;</span>
                    <span style="color: #ff0000;">&quot;Sumeet Moghe&quot;</span> <span style="color: #ff0000;">&quot;sumeet_moghe&quot;</span>
                    <span style="color: #ff0000;">&quot;Brian Guthrie&quot;</span> <span style="color: #ff0000;">&quot;bguthrie&quot;</span>
                    <span style="color: #ff0000;">&quot;Ian Robinson&quot;</span> <span style="color: #ff0000;">&quot;iansrobinson&quot;</span>
                    <span style="color: #ff0000;">&quot;Ian Cartwright&quot;</span> <span style="color: #ff0000;">&quot;cartwrightian&quot;</span>
                    <span style="color: #ff0000;">&quot;Duncan Cragg&quot;</span> <span style="color: #ff0000;">&quot;duncancragg&quot;</span>
                    <span style="color: #ff0000;">&quot;David Cameron&quot;</span> <span style="color: #ff0000;">&quot;davcamer&quot;</span>
                    <span style="color: #ff0000;">&quot;Steven List&quot;</span> <span style="color: #ff0000;">&quot;athought&quot;</span>
                    <span style="color: #ff0000;">&quot;Philip Calcado&quot;</span> <span style="color: #ff0000;">&quot;pcalcado&quot;</span>
                    <span style="color: #ff0000;">&quot;Perryn Fowler&quot;</span> <span style="color: #ff0000;">&quot;perrynfowler&quot;</span>
                    <span style="color: #ff0000;">&quot;Jason Yip&quot;</span> <span style="color: #ff0000;">&quot;jchyip&quot;</span>
                    <span style="color: #ff0000;">&quot;Christopher Read&quot;</span> <span style="color: #ff0000;">&quot;cread&quot;</span>
                    <span style="color: #ff0000;">&quot;Jim Webber&quot;</span> <span style="color: #ff0000;">&quot;jimwebber&quot;</span>
                    <span style="color: #ff0000;">&quot;John Hume&quot;</span> <span style="color: #ff0000;">&quot;duelin_markers&quot;</span>
                    <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn- create-blog-post <span style="color: #66cc66;">&#91;</span>title link author<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">tweet</span> <span style="color: #66cc66;">&#40;</span>str title <span style="color: #ff0000;">&quot; by &quot;</span> <span style="color: #66cc66;">&#40;</span>get-display-<span style="color: #b1b100;">name</span> twitter-names author<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot; &quot;</span> link<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn create-blog-posts <span style="color: #66cc66;">&#91;</span>titles links authors<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>map #<span style="color: #66cc66;">&#40;</span>create-blog-post <span style="color: #66cc66;">%</span>1 <span style="color: #66cc66;">%</span>2 <span style="color: #66cc66;">%</span>3<span style="color: #66cc66;">&#41;</span> titles links authors<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>To use that you'd need to do this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>create-blog-posts titles rss-links authors<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Which returns a sequence of hash maps with key 'tweet' and a value of the tweet to display on Twitter:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">({:tweet &quot;Links for 2009-11-27 [del.icio.us] by @brunns http://feedproxy.google.com/~r/SmallValuesOfCool/~3/WDqeLyMA-RE/brunns&quot;} 
{:tweet &quot;Extending iPhone battery life by @alexhung http://alexhung.vox.com/library/post/extending-iphone-battery-life.html?_c=feed-atom-full&quot;} 
{:tweet &quot;Threshold Anxiety by Adrian Wible http://thoughtadrian.blogspot.com/2009/11/threshold-anxiety.html&quot;})</pre></div></div>

<p>The next step is to get this hooked up to the Twitter API.</p>
<p>There are still some things I'm unsure of when it comes to writing applications in Clojure:</p>
<ul>
<li>I'm not sure what to do with 'twitter-names'. It's pretty much a global data store so I can't decide whether to just refer to it directly inside other functions or if it should be passed in as a parameter.</li>
<li>I used 'first' quite a few times in the code to get the first value in a sequence but it doesn't feel like the code expresses the structure of the document very well.</li>
<li>What's the best way to lay out code for 'defn' expressions? I've been putting the signature on it's own line and then the implementation on other lines which seems to be the way that it's done in the <a href="http://code.google.com/p/clojure/source/browse/">Clojure source code</a> but it sometimes seems like I could just write it all on one line.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/30/clojure-parsing-an-rss-feed/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Clojure: The &#039;apply&#039; function</title>
		<link>http://www.markhneedham.com/blog/2009/11/25/clojure-the-apply-function/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/25/clojure-the-apply-function/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 01:59:11 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1874</guid>
		<description><![CDATA[In my continued playing around with Clojure I came across the 'apply' function which is used when we want to call another function with a number of arguments but have actually been given a single argument which contains the argument list. The example that I've been trying to understand is applying 'str' to a collection [...]]]></description>
			<content:encoded><![CDATA[<p>In my continued playing around with Clojure I came across the '<a href="http://clojure.org/api#toc86">apply</a>' function which is used when we want to call another function with a number of arguments but have actually been given a single argument which contains the argument list.</p>
<p>The example that I've been trying to understand is applying '<a href="http://clojure.org/api#toc537">str</a>' to a collection of values.</p>
<p>I started off with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>str <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;[1 2 3]&quot;</span></pre></div></div>

<p>This just returns the string representation of the vector that we passed it, but what we actually want is to get an output of "123&#8243;.</p>
<p>The 'apply' function allows us to do that:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> str <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;123&quot;</span></pre></div></div>

<p>That is semantically/conceptually the same as doing this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>str <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;123&quot;</span></pre></div></div>

<p>I didn't quite understand how that could work though and my assumption was that somewhere in the Clojure source the above function call would be happening.</p>
<p>The definition of 'apply' is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn <span style="color: #b1b100;">apply</span>
  <span style="color: #ff0000;">&quot;Applies fn f to the argument list formed by prepending args to argseq.&quot;</span>
  <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">arglists</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>f args* argseq<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#91;</span>#<span style="color: #66cc66;">^</span>clojure<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>IFn f <span style="color: #66cc66;">&amp;</span> args<span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span> f <span style="color: #66cc66;">&#40;</span>applyTo <span style="color: #66cc66;">&#40;</span>spread args<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>The first thing which I hadn't realised is that when you have an '&#038;' before a parameter definition then any arguments provided will be put into a list.</p>
<p>If we break down the example above we end up with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span> str <span style="color: #66cc66;">&#40;</span>applyTo <span style="color: #66cc66;">&#40;</span>spread <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The 'spread' function is defined like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn spread
  <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">private</span> true<span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#91;</span>arglist<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span>
   <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil</span>? arglist<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">nil</span>
   <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil</span>? <span style="color: #66cc66;">&#40;</span>next arglist<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>seq <span style="color: #66cc66;">&#40;</span>first arglist<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">:</span><span style="color: #555;">else</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span>first arglist<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>spread <span style="color: #66cc66;">&#40;</span>next arglist<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></td></tr></table></div>

<p>In this case we only have one item in 'arglist' so on line 6 the 'next arglist' expression evaluates to nil.</p>
<p>This means that we create a seq from the first argument of the 'arglist' which is '[1 2 3]'.</p>
<p>Working our way back up to the 'apply' function what we end up with is this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span> str <span style="color: #66cc66;">&#40;</span>applyTo <span style="color: #66cc66;">&#40;</span>seq <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 
<span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;123&quot;</span></pre></div></div>

<p>This calls through to an 'applyTo' method defined on the 'clojure.lang.IFn' interface.</p>
<p>I'm not sure which of the implementations 'str' maps to but it seems like the 'str' function would eventually be called from the Java code with each of the values in the sequence passed in as a separate argument which is pretty neat!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/25/clojure-the-apply-function/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Writing a Java function in Clojure</title>
		<link>http://www.markhneedham.com/blog/2009/11/23/writing-a-java-function-in-clojure/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/23/writing-a-java-function-in-clojure/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 10:08:20 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1868</guid>
		<description><![CDATA[A function that we had to write in Java on a project that I worked on recently needed to indicate whether there was a gap in a series of data points or not. If there were gaps at the beginning or end of the sequence then that was fine but gaps in the middle of [...]]]></description>
			<content:encoded><![CDATA[<p>A function that we had to write in Java on a project that I worked on recently needed to indicate whether there was a gap in a series of data points or not.</p>
<p>If there were gaps at the beginning or end of the sequence then that was fine but gaps in the middle of the sequence were not.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">null, 1, 2, 3 =&gt; no gaps
1, 2, 3, null =&gt; no gaps
1, null, 2, 3 =&gt; gaps</pre></div></div>

<p>The Java version looked a bit like this:</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: #000066; font-weight: bold;">boolean</span> hasGaps<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>BigInteger<span style="color: #339933;">&gt;</span> values<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Iterator<span style="color: #339933;">&lt;</span>BigInteger<span style="color: #339933;">&gt;</span> fromHead <span style="color: #339933;">=</span> values.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><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>fromHead.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> fromHead.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        fromHead.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #003399;">Collections</span>.<span style="color: #006633;">reverse</span><span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    Iterator<span style="color: #339933;">&lt;</span>BigInteger<span style="color: #339933;">&gt;</span> fromTail <span style="color: #339933;">=</span> values.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><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>fromTail.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> fromTail.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        fromTail.<span style="color: #006633;">remove</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> values.<span style="color: #006633;">contains</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We take the initial list and then remove all the null values from the beginning of it, then reverse the list and remove all the values from the end.</p>
<p>We then check if there's a null value and if there is then it would indicate there is indeed a gap in the list.</p>
<p>To write this function in Clojure we can start off by using the '<a href="http://clojure.org/api#toc237">drop-while</a>' function to get rid of the trailing nil values.</p>
<p>I started off with this attempt:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>drop-while #<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">%</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
  no-nils<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Unfortunately that gives us the following error!</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Can't take value of a macro: #'clojure.core/let (NO_SOURCE_FILE:16)</pre></div></div>

<p>It thinks we're trying to pass around the 'let' macro instead of evaluating it &#8211; I forgot to put in the brackets around the 'let'!</p>
<p>I fixed that with this next version:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>drop-while <span style="color: #b1b100;">nil</span>? <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
  no-nils<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>But again, no love:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">java<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>IllegalArgumentException<span style="color: #66cc66;">:</span> <span style="color: #b1b100;">let</span> requires an even number of forms in binding vector <span style="color: #66cc66;">&#40;</span>NO_SOURCE_FILE<span style="color: #66cc66;">:</span><span style="color: #cc66cc;">23</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The way I understand it the 'let' macro takes in a vector of bindings as its first argument and what I've done here is pass in two vectors instead of one.</p>
<p>In the bindings vector we need to ensure that there are an even number of forms so that each symbol can be bound to an expression.</p>
<p>I fixed this by putting the two vectors defined above into another vector:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>no-nils<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
  no-nils<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We can simplify that further so that we don't have nested vectors:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils <span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
  no-nils<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The next step was to make 'no-nils' a function so that I could make use of that function when the list was reversed as well:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>no-nils <span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I then wrote the rest of the function to reverse the list and then <a href="http://www.markhneedham.com/blog/2009/11/21/clojure-checking-for-a-nil-value-in-a-collection/">check the remaining list for nil</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>no-nils<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
          <span style="color: #66cc66;">&#91;</span>nils-removed<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>comp no-nils <span style="color: #b1b100;">reverse</span> no-nils<span style="color: #66cc66;">&#41;</span> x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>some <span style="color: #b1b100;">nil</span>? <span style="color: #66cc66;">&#40;</span>nils-removed <span style="color: #b1b100;">list</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>The '<a href="http://clojure.org/api#toc151">comp</a>' function can be used to compose a set of functions which is what I needed.</p>
<p>It seemed like the 'nils-removed' function wasn't really necessary so I inlined that:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn has-gaps? <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>no-nils <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>drop-while <span style="color: #b1b100;">nil</span>? x<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>some <span style="color: #b1b100;">nil</span>? <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>comp no-nils <span style="color: #b1b100;">reverse</span> no-nils<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">list</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>The function can now be used like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">user=&gt; (has-gaps? '(1 2 3))
nil
user=&gt; (has-gaps? '(nil 1 2 3))
nil
user=&gt; (has-gaps? '(1 2 3 nil))
nil
user=&gt; (has-gaps? '(1 2 nil 3))
true</pre></div></div>

<p>I'd be intrigued to know if there's a better way to do this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/23/writing-a-java-function-in-clojure/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Clojure: Checking for a nil value in a collection</title>
		<link>http://www.markhneedham.com/blog/2009/11/21/clojure-checking-for-a-nil-value-in-a-collection/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/21/clojure-checking-for-a-nil-value-in-a-collection/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 12:11:22 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1856</guid>
		<description><![CDATA[Something which I wanted to do recently was write a function that would indicate whether a collection contained a nil value. I initially incorrectly thought the 'contains?' function was the one that I wanted: &#40;contains? '&#40;1 nil 2 3&#41; nil&#41; =&#62; false I thought it would work the same as the Java equivalent but that [...]]]></description>
			<content:encoded><![CDATA[<p>Something which I wanted to do recently was write a function that would indicate whether a collection contained a nil value.</p>
<p>I initially incorrectly thought the '<a href="http://clojure.org/api#toc170">contains?</a>' function was the one that I wanted:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>contains? '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">nil</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> false</pre></div></div>

<p>I thought it would work the same as the Java equivalent but that function actually checks whether a key exists in a collection rather than a value. It's more useful when dealing with maps.</p>
<p>There's <a href="http://groups.google.com/group/clojure/browse_frm/thread/49173d05a6781f62/47084ba4eec07c26?lnk=gst&#038;q=contains%3F#47084ba4eec07c26">more discussion on the consistency of the API on the mailing list</a>.</p>
<p>Luckily the documentation guides us towards the '<a href="http://clojure.org/api#toc523">some</a>' function:</p>
<p>My first attempt was to write an anonymous function to check if there was a 'nil' in the list:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>some #<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">%</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">nil</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> true</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>some #<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">%</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>  <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #b1b100;">nil</span></pre></div></div>

<p><a href="http://twitter.com/fogus/status/5904916921">fogus showed me an even better way</a> by making use of the built in '<a href="http://clojure.org/api#toc388">nil?</a>' function:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>some <span style="color: #b1b100;">nil</span>? '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">nil</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Another approach would be to make use of the Java 'contains' method as <a href="http://twitter.com/philip_schwarz">Philip Schwarz</a> <a href="http://twitter.com/philip_schwarz/statuses/5905264987">pointed out</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>contains '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #b1b100;">nil</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> true</pre></div></div>

<p>I noticed that when you use Java methods in Clojure with collections then the result will either be 'true' or 'false' whereas when you use Clojure built in functions then it's more likely to be 'true' or 'nil'.</p>
<p>I guess this is linked to the idea that <a href="http://www.markhneedham.com/blog/2009/11/20/clojure-a-few-things-ive-been-tripping-up-on/">'nil' is false in Clojure</a> so it doesn't make much difference what the return value is.</p>
<p>When I'm using a language I've got into the habit of just trying out the API in the way that I expect it to work rather than paying a lot of attention to what the API documentation says. </p>
<p>I think this is something I'll need to work out to avoid much frustration!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/21/clojure-checking-for-a-nil-value-in-a-collection/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Clojure: A few things I&#039;ve been tripping up on</title>
		<link>http://www.markhneedham.com/blog/2009/11/20/clojure-a-few-things-ive-been-tripping-up-on/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/20/clojure-a-few-things-ive-been-tripping-up-on/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 03:11:03 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1851</guid>
		<description><![CDATA[In my continued playing with Clojure I'm noticing a few things that I keep getting confused about. The meaning of parentheses Much like Keith Bennett I'm not used to parentheses playing such an important role in the way that an expression gets evaluated. As I understand it if an expression is enclosed in parentheses then [...]]]></description>
			<content:encoded><![CDATA[<p>In my continued playing with Clojure I'm noticing a few things that I keep getting confused about.</p>
<h3>The meaning of parentheses</h3>
<p><a href="http://krbtech.wordpress.com/2009/03/16/same-temperature-converter-different-language-clojure/">Much like Keith Bennett</a> I'm not used to parentheses playing such an important role in the way that an expression gets evaluated. </p>
<p>As I understand it if an expression is enclosed in parentheses then that means it will be evaluated as a function.</p>
<p>For example I spent quite a while trying to work out why the following code kept throwing a class cast exception:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>true<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>If you run that code in the REPL you'll get the following exception because 'true' isn't a function and therefore can't be applied as such:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.lang.ClassCastException: java.lang.Boolean cannot be cast to clojure.lang.IFn (NO_SOURCE_FILE:0)</pre></div></div>

<p>If we don't want something to be treated this way then the parentheses need to disappear!</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> true <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h3>Truthyness</h3>
<p>Somewhat related to the above is understanding which expressions evaluate to 'true' or 'false'. </p>
<p>I'm told there are some edge cases but that as a general rule everything except for 'false' and 'nil' evaluates to true. </p>
<p>I think that's an idea which is more common in languages like Ruby but I'm not yet used to the idea that we can something like this and have it execute:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #ff0000;">&quot;mark&quot;</span> <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>In C# or Java I would except to have to compare "mark" to something in order for it to evaluate to a boolean result.</p>
<p>It seems like a really neat way to reduce the amount of code we have to write though so I like it so far.</p>
<h3>Character Literals</h3>
<p>I've been working through <a href="http://java.ociweb.com/mark/clojure/article.html#Syntax">Mark Volkmann's Clojure tutorial</a> and in one example he defines the following function:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>def vowel? <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> <span style="color: #ff0000;">&quot;aeiou&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I wanted to try it out to see if a certain character was a vowel so I initially did this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">=&gt;</span><span style="color: #66cc66;">&#40;</span>vowel? <span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">nil</span></pre></div></div>

<p>"a" is actually a string though which means it's an array of characters when what we really want is a single character.</p>
<p>I thought the following would be what I wanted:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>vowel? 'a'<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Instead what I got was the following exception:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">java.lang.Exception: Unmatched delimiter: )</pre></div></div>

<p>This one just turned out to be a case of me not reading <a href="http://clojure.org/reader">the manual</a> very carefully and actually the following is what I wanted:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>vowel? \a<span style="color: #66cc66;">&#41;</span>
\a</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/20/clojure-a-few-things-ive-been-tripping-up-on/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Clojure: A first look at recursive functions</title>
		<link>http://www.markhneedham.com/blog/2009/11/17/clojure-a-first-look-at-recursive-functions/</link>
		<comments>http://www.markhneedham.com/blog/2009/11/17/clojure-a-first-look-at-recursive-functions/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 01:10:37 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=1836</guid>
		<description><![CDATA[I'm working through Stuart Halloway's 'Programming Clojure' book and I just got to the section where it first mentions recursive functions. It's a simple function to countdown from a given number to zero and then return that sequence. This was one of the examples from the book: &#40;defn countdown &#91;result x&#93; &#40;if &#40;zero? x&#41; result [...]]]></description>
			<content:encoded><![CDATA[<p>I'm working through Stuart Halloway's '<a href="http://www.amazon.com/gp/product/1934356336?ie=UTF8&#038;tag=marneesblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=1934356336">Programming Clojure</a>' book and I just got to the section where it first mentions recursive functions.</p>
<p>It's a simple function to countdown from a given number to zero and then return that sequence.</p>
<p>This was one of the examples from the book:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn countdown <span style="color: #66cc66;">&#91;</span>result x<span style="color: #66cc66;">&#93;</span> 
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>zero? x<span style="color: #66cc66;">&#41;</span>
    result
    <span style="color: #66cc66;">&#40;</span>recur <span style="color: #66cc66;">&#40;</span>conj result x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>dec x<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>That function could then be called like this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>countdown <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I wanted to see what the function would look if we didn't have the empty vector as a parameter.</p>
<p>From playing around with F# and Scala my first thought would be to write the function like this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn count-down <span style="color: #66cc66;">&#91;</span>from<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>defn inner-count <span style="color: #66cc66;">&#91;</span>so-far x<span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>zero? x<span style="color: #66cc66;">&#41;</span>
      so-far
      <span style="color: #66cc66;">&#40;</span>inner-count <span style="color: #66cc66;">&#40;</span>conj so-far x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>dec x<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>inner-count <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> from<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>As the book points out a bit further on, Clojure doesn't perform automatic tail call optimisation so we end up with a stack overflow exception if we run the function with a big enough input value.</p>
<p>Clojure does optimise calls to 'recur' so it makes more sense to use that if we want to avoid that problem.</p>
<p>This is an example which makes use of that:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn count-down <span style="color: #66cc66;">&#91;</span>from<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>defn inner-count <span style="color: #66cc66;">&#91;</span>so-far x<span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>zero? x<span style="color: #66cc66;">&#41;</span>
      so-far
      <span style="color: #66cc66;">&#40;</span>recur <span style="color: #66cc66;">&#40;</span>conj so-far x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>dec x<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>inner-count <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> from<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p><a href="http://groups.google.com/group/clojure/browse_thread/thread/4e7a4bfb0d71a508?pli=1">Looking through the Clojure mailing list at a similar problem</a> I noticed that one of the suggestions was to arity overload the function to include an accumulator.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn count-down
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>from<span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span>count-down <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> from<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>so-far from<span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>zero? from<span style="color: #66cc66;">&#41;</span>
      so-far
      <span style="color: #66cc66;">&#40;</span>recur <span style="color: #66cc66;">&#40;</span>conj so-far from<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>dec from<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>Written this way it feels a little bit like Haskell or Erlang but probably not idiomatic Clojure.</p>
<p>Anyway on the next page Halloway shows a better way to do this with much less code!</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>into <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>take <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&#40;</span>iterate dec <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I noticed that in Scala the idea of using 'take' and 'drop' on streams of values seems to be quite popular so I'm intrigued as to whether I'll find the same thing with Clojure.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2009/11/17/clojure-a-first-look-at-recursive-functions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
