<?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, 11 Feb 2012 10:56:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Clojure: Casting to a Java class&#8230;or not!</title>
		<link>http://www.markhneedham.com/blog/2011/12/31/clojure-casting-to-a-java-class-or-not/</link>
		<comments>http://www.markhneedham.com/blog/2011/12/31/clojure-casting-to-a-java-class-or-not/#comments</comments>
		<pubDate>Sat, 31 Dec 2011 17:47:47 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3888</guid>
		<description><![CDATA[I have a bit of Java code for working out the final destination of a URL assuming that there might be one redirect which looks like this: private String resolveUrl&#40;String url&#41; &#123; try &#123; HttpURLConnection con = &#40;HttpURLConnection&#41; &#40;new URL&#40;url&#41;.openConnection&#40;&#41;&#41;; con.setInstanceFollowRedirects&#40;false&#41;; con.connect&#40;&#41;; int responseCode = con.getResponseCode&#40;&#41;; &#160; if &#40;String.valueOf&#40;responseCode&#41;.startsWith&#40;&#34;3&#34;&#41;&#41; &#123; return con.getHeaderField&#40;&#34;Location&#34;&#41;; &#125; &#125; catch [...]]]></description>
			<content:encoded><![CDATA[<p>I have a bit of Java code for <a href="http://stackoverflow.com/questions/2659000/java-how-to-find-the-redirected-url-of-a-url">working out the final destination of a URL</a> assuming that there might be one redirect which looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> resolveUrl<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">HttpURLConnection</span> con <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">HttpURLConnection</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    con.<span style="color: #006633;">setInstanceFollowRedirects</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    con.<span style="color: #006633;">connect</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;">int</span> responseCode <span style="color: #339933;">=</span> con.<span style="color: #006633;">getResponseCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>responseCode<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> con.<span style="color: #006633;">getHeaderField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> url<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">return</span> url<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I need to cast to <cite>HttpURLConnection</cite> on the first line so that I can make the call to <cite>setInstanceFollowRedirects</cite> which isn&#8217;t available on <cite>URLConnection</cite>.</p>
<p>I wanted to write some similar code in Clojure and my first thought was that I needed to work out how to do the cast, which I didn&#8217;t know how to do.</p>
<p>I then remembered that Clojure is actually dynamically typed so there isn&#8217;t any need &#8211; as long as the object has the method that we want to call on it everything will be fine.</p>
<p>In this case we end up with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> resolve<span style="color: #66cc66;">-</span>url <span style="color: #66cc66;">&#91;</span>url<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>con <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">..</span> <span style="color: #66cc66;">&#40;</span>new URL url<span style="color: #66cc66;">&#41;</span> openConnection<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">doall</span>
     <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>setInstanceFollowRedirects con false<span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>connect con<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>startsWith <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">str</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>getResponseCode con<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;3&quot;</span><span style="color: #66cc66;">&#41;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>getHeaderField con <span style="color: #ff0000;">&quot;Location&quot;</span><span style="color: #66cc66;">&#41;</span>
      url<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Which can be simplified to this:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> resolve<span style="color: #66cc66;">-</span>url <span style="color: #66cc66;">&#91;</span>url<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>con <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">doto</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">..</span> <span style="color: #66cc66;">&#40;</span>new URL url<span style="color: #66cc66;">&#41;</span> openConnection<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>setInstanceFollowRedirects false<span style="color: #66cc66;">&#41;</span>
                  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>connect<span style="color: #66cc66;">&#41;</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> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>startsWith <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">str</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>getResponseCode con<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;3&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>getHeaderField con <span style="color: #ff0000;">&quot;Location&quot;</span><span style="color: #66cc66;">&#41;</span>
    url<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/12/31/clojure-casting-to-a-java-class-or-not/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Leiningen: Using goose via a local Maven repository</title>
		<link>http://www.markhneedham.com/blog/2011/12/27/leiningen-using-goose-via-a-local-maven-repository/</link>
		<comments>http://www.markhneedham.com/blog/2011/12/27/leiningen-using-goose-via-a-local-maven-repository/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 12:48:17 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[goose]]></category>
		<category><![CDATA[leiningen]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3842</guid>
		<description><![CDATA[I&#8217;ve been playing around a little bit with goose &#8211; a HTML content/article extractor &#8211; originally in Java but later in clojure where I needed to work out how to include goose and all its dependencies via Leiningen. goose isn&#8217;t included in a Maven repository so I needed to create a local repository, something which [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around a little bit with <a href="https://github.com/jiminoc/goose">goose</a> &#8211; a HTML content/article extractor &#8211; originally in Java but later in clojure where I needed to work out how to include goose and all its dependencies via <a href="https://github.com/technomancy/leiningen">Leiningen</a>.</p>
<p>goose isn&#8217;t included in a Maven repository so I needed to create a local repository, something which I&#8217;ve got stuck on in the past.</p>
<p>Luckily <a href="http://www.pgrs.net/2011/10/30/using-local-jars-with-leiningen/">Paul Gross has written a cool blog post</a> explaining how his team got past this problem.</p>
<p>Following the instructions from Paul&#8217;s post this is how I got goose playing nicely with clojure:</p>
<p>Inside my clojure project:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/Users/mneedham/github/android/text-extraction $ mkdir maven_repository</pre></div></div>

<p>I then ran the following command from where I had goose checked out on my machine:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">mvn install:install-file -Dfile=target/goose-2.1.6.jar -DartifactId=goose -Dversion=2.1.6 -DgroupId=goose -Dpackaging=jar -DlocalRepositoryPath=/Users/mneedham/github/android/text-extraction/maven_repository -DpomFile=pom.xml</pre></div></div>

<p>I added the repository and goose dependency to my project.clj file which now looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">(defproject textextraction &quot;0.1.0&quot;
  :description &quot;Extract text from urls&quot;
  :dependencies [[org.clojure/clojure &quot;1.2.0&quot;],
		 [org.clojure/clojure-contrib &quot;1.2.0&quot;],
		 [ring/ring-jetty-adapter &quot;0.3.11&quot;],
         [compojure &quot;0.6.4&quot;]
         [goose &quot;2.1.6&quot;]]
  :dev-dependencies [[swank-clojure &quot;1.2.1&quot;]]
  :repositories {&quot;local&quot; ~(str (.toURI (java.io.File. &quot;maven_repository&quot;)))}
  :main textextraction.main)</pre></div></div>

<p>I then run:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/Users/mneedham/github/android/text-extraction $ lein run</pre></div></div>

<p>And goose and all its dependencies are included in the &#8216;lib&#8217; directory.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/12/27/leiningen-using-goose-via-a-local-maven-repository/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Clojure: Getting caught out by lazy collections</title>
		<link>http://www.markhneedham.com/blog/2011/07/31/clojure-getting-caught-out-by-lazy-collections/</link>
		<comments>http://www.markhneedham.com/blog/2011/07/31/clojure-getting-caught-out-by-lazy-collections/#comments</comments>
		<pubDate>Sun, 31 Jul 2011 21:40:35 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3693</guid>
		<description><![CDATA[Most of the work that I&#8217;ve done with Clojure has involved running a bunch of functions directly in the REPL or through Leiningen&#8217;s run target which led to me getting caught out when I created a JAR and tried to run that. As I mentioned a few weeks ago I&#8217;ve been rewriting part of our [...]]]></description>
			<content:encoded><![CDATA[<p>Most of the work that I&#8217;ve done with Clojure has involved running a bunch of functions directly in the REPL or through Leiningen&#8217;s <cite>run</cite> target which led to me getting caught out when I created a JAR and tried to run that.</p>
<p>As I <a href="http://www.markhneedham.com/blog/2011/07/10/clojure-language-as-thought-shaper/">mentioned a few weeks ago</a> I&#8217;ve been rewriting part of our system in Clojure to see how the design would differ and a couple of levels down the Clojure version comprises of applying a map function over a collection of documents.</p>
<p>The code in question originally looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>ns aim<span style="color: #66cc66;">.</span>main <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">gen-class</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn import-zip-file <span style="color: #66cc66;">&#91;</span>zipFile working-dir<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>xml-files <span style="color: #66cc66;">&#40;</span>filter xml-file? <span style="color: #66cc66;">&#40;</span>unzip zipFile working-dir<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span>map import-document xml-files<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 -main <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&amp;</span> args<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>import-zip-file <span style="color: #ff0000;">&quot;our/file.zip&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;/tmp/unzip/to/here&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Which led to absolutely nothing happening when run like this!</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ lein uberjar &amp;&amp; java -jar my-project-0.1.0-standalone.jar</pre></div></div>

<p>I originally assumed that I had something wrong in the code but my colleague Uday reminded me that collections in Clojure are lazily evaluated and there was nothing in the code that would force the evaluation of ours.</p>
<p>In this situation we had to wrap the <cite>map</cite> with a <cite><a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/doall">doall</a></cite> in order to force evaluation of the collection:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>ns aim<span style="color: #66cc66;">.</span>main <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">gen-class</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn import-zip-file <span style="color: #66cc66;">&#91;</span>zip-file working-dir<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>xml-files <span style="color: #66cc66;">&#40;</span>filter xml-file? <span style="color: #66cc66;">&#40;</span>unzip zip-file working-dir<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span>doall <span style="color: #66cc66;">&#40;</span>map import-document xml-files<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 -main <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&amp;</span> args<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>import-zip-file <span style="color: #ff0000;">&quot;our/file.zip&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;/tmp/unzip/to/here&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>When we run the code in the REPL or through &#8216;lein run&#8217; the code is being eagerly evaluated as far as I understand it which is why we see a different behaviour than when we run it on its own.</p>
<p>I also got caught out on another occasion where I tried to pass around a collection of input streams which I&#8217;d retrieved from a zip file only to realise that when the code which used the input stream got evaluated the ZIP file was no longer around!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/07/31/clojure-getting-caught-out-by-lazy-collections/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Clojure: Creating XML document with namespaces</title>
		<link>http://www.markhneedham.com/blog/2011/07/20/clojure-creating-xml-document-with-namespaces/</link>
		<comments>http://www.markhneedham.com/blog/2011/07/20/clojure-creating-xml-document-with-namespaces/#comments</comments>
		<pubDate>Wed, 20 Jul 2011 20:28:17 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3673</guid>
		<description><![CDATA[As I mentioned in an earlier post we&#8217;ve been parsing XML documents with the Clojure zip-filter API and the next thing we needed to do was create a new XML document containing elements which needed to be inside a namespace. We wanted to end up with a document which looked something like this: &#60;root&#62; &#60;mynamespace:foo [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in an earlier post we&#8217;ve been <a href="http://www.markhneedham.com/blog/2011/07/16/clojure-extracting-child-elements-from-an-xml-document-with-zip-filter/">parsing XML documents with the Clojure zip-filter API</a> and the next thing we needed to do was create a new XML document containing elements which needed to be inside a namespace.</p>
<p>We wanted to end up with a document which looked something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&lt;root&gt;
&lt;mynamespace:foo xmlns:mynamespace=&quot;http://www.magicalurlfornamespace.com&quot;&gt;
	&lt;mynamespace:bar&gt;baz&lt;/mynamespace:bar&gt;
&lt;/mynamespace:foo&gt;
&lt;/root&gt;</pre></div></div>

<p>We can make use of <cite>lazy-xml/emit</cite> to output an XML string from *some sort of input?* by wrapping it inside <cite>with-out-str</cite> like so:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>require '<span style="color: #66cc66;">&#91;</span>clojure<span style="color: #66cc66;">.</span>contrib<span style="color: #66cc66;">.</span>lazy-xml <span style="color: #66cc66;">:</span><span style="color: #555;">as</span> lxml<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defn xml-string <span style="color: #66cc66;">&#91;</span>xml-zip<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>with-out-str <span style="color: #66cc66;">&#40;</span>lxml/emit xml-zip<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I was initially confused about how we&#8217;d be able to create a map representing name spaced elements to pass to <cite>xml-string</cite> but it turned out to be reasonably simple.</p>
<p>To create a non namespaced XML string we might pass <cite>xml-string</cite> the following map:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>xml-string <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">tag</span> <span style="color: #66cc66;">:</span><span style="color: #555;">root</span> <span style="color: #66cc66;">:</span><span style="color: #555;">content</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">tag</span> <span style="color: #66cc66;">:</span><span style="color: #555;">foo</span> <span style="color: #66cc66;">:</span><span style="color: #555;">content</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">tag</span> <span style="color: #66cc66;">:</span><span style="color: #555;">bar</span> <span style="color: #66cc66;">:</span><span style="color: #555;">content</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;baz&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Which gives us this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?&gt;
&lt;root&gt;
	&lt;foo&gt;
		&lt;bar&gt;baz&lt;/bar&gt;
	&lt;/foo&gt;
&lt;/root&gt;&quot;</pre></div></div>

<p>Ideally I wanted to prepend <cite>:foo</cite> and <cite>:bar</cite> with &#8216;:mynamespace&#8221; but I thought that wouldn&#8217;t work since that type of syntax would be invalid in Ruby and I thought it&#8217;d be the same in Clojure.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">mneedham@Administrators-MacBook-Pro-5.local ~$ irb
&gt;&gt; { :mynamespace:foo &quot;bar&quot; }
SyntaxError: compile error
(irb):1: odd number list for Hash
{ :mynamespace:foo &quot;bar&quot; }
               ^
(irb):1: syntax error, unexpected ':', expecting '}'
{ :mynamespace:foo &quot;bar&quot; }
               ^
(irb):1: syntax error, unexpected '}', expecting $end
	from (irb):1
&gt;&gt;</pre></div></div>

<p>In fact it isn&#8217;t so we can just do this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>xml-string <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">tag</span> <span style="color: #66cc66;">:</span><span style="color: #555;">root</span> 
  <span style="color: #66cc66;">:</span><span style="color: #555;">content</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">tag</span> <span style="color: #66cc66;">:</span><span style="color: #555;">mynamespace</span><span style="color: #66cc66;">:</span><span style="color: #555;">foo</span> <span style="color: #66cc66;">:</span><span style="color: #555;">attrs</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">xmlns</span><span style="color: #66cc66;">:</span><span style="color: #555;">meta</span> <span style="color: #ff0000;">&quot;http://www.magicalurlfornamespace.com&quot;</span><span style="color: #66cc66;">&#125;</span> 
              <span style="color: #66cc66;">:</span><span style="color: #555;">content</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">tag</span> <span style="color: #66cc66;">:</span><span style="color: #555;">mynamespace</span><span style="color: #66cc66;">:</span><span style="color: #555;">bar</span> <span style="color: #66cc66;">:</span><span style="color: #555;">content</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;baz&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;&lt;?xml version=\&quot;1.0\&quot; encoding=\&quot;UTF-8\&quot;?&gt;
&lt;root&gt;
&lt;mynamespace:foo xmlns:meta=\&quot;http://www.magicalurlfornamespace.com&quot;&gt;
	&lt;mynamespace:bar&gt;baz&lt;/mynamespace:bar&gt;
&lt;/mynamespace:foo&gt;
&lt;/root&gt;&quot;</pre></div></div>

<p>As a refactoring step, since I had to append the namespace to a lot of tags, I was able to make use of the <cite>keyword</cite> function to do so:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn tag <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">name</span> <span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">tag</span> <span style="color: #66cc66;">&#40;</span>keyword <span style="color: #66cc66;">&#40;</span>str <span style="color: #ff0000;">&quot;mynamespace&quot;</span> <span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">content</span> <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&gt; (tag :Foo &quot;hello&quot;)  
{:tag :mynamespace:Foo, :content [&quot;hello&quot;]}</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/07/20/clojure-creating-xml-document-with-namespaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clojure: Extracting child elements from an XML document with zip-filter</title>
		<link>http://www.markhneedham.com/blog/2011/07/16/clojure-extracting-child-elements-from-an-xml-document-with-zip-filter/</link>
		<comments>http://www.markhneedham.com/blog/2011/07/16/clojure-extracting-child-elements-from-an-xml-document-with-zip-filter/#comments</comments>
		<pubDate>Sat, 16 Jul 2011 22:19:47 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3665</guid>
		<description><![CDATA[I&#8217;ve been following Nurullah Akkaya&#8217;s blog post about navigating XML documents using the Clojure zip-filter API and I came across an interesting problem in a document I&#8217;m parsing which goes beyond what&#8217;s covered in his post. Nurullah provides a neat zip-str function which we can use to convert an XML string into a zipper object: [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been following <a href="http://nakkaya.com/2009/12/07/zipping-xml-with-clojure/">Nurullah Akkaya&#8217;s blog post</a> about navigating XML documents using the Clojure <a href="http://richhickey.github.com/clojure-contrib/zip-filter-api.html">zip-filter</a> API and I came across an interesting problem in a document I&#8217;m parsing which goes beyond what&#8217;s covered in his post.</p>
<p>Nurullah provides a neat <cite>zip-str</cite> function which we can use to convert an XML string into a zipper object:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>require '<span style="color: #66cc66;">&#91;</span>clojure<span style="color: #66cc66;">.</span>zip <span style="color: #66cc66;">:</span><span style="color: #555;">as</span> zip<span style="color: #66cc66;">&#93;</span> '<span style="color: #66cc66;">&#91;</span>clojure<span style="color: #66cc66;">.</span>xml <span style="color: #66cc66;">:</span><span style="color: #555;">as</span> xml<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>use '<span style="color: #66cc66;">&#91;</span>clojure<span style="color: #66cc66;">.</span>contrib<span style="color: #66cc66;">.</span>zip-filter<span style="color: #66cc66;">.</span>xml<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn zip-str <span style="color: #66cc66;">&#91;</span>s<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>zip/xml-zip <span style="color: #66cc66;">&#40;</span>xml/parse <span style="color: #66cc66;">&#40;</span>java<span style="color: #66cc66;">.</span>io<span style="color: #66cc66;">.</span>ByteArrayInputStream<span style="color: #66cc66;">.</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>getBytes s<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>The fragment of the document I&#8217;m parsing looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>def test-doc <span style="color: #66cc66;">&#40;</span>zip-str <span style="color: #ff0000;">&quot;&lt;?xml version='1.0' encoding='UTF-8'?&gt;
&lt;root&gt;
  &lt;Person&gt;
    &lt;FirstName&gt;Charles&lt;/FirstName&gt;
    &lt;LastName&gt;Kubicek&lt;/LastName&gt;
  &lt;/Person&gt;
  &lt;Person&gt;
    &lt;FirstName&gt;Mark&lt;/FirstName&gt;
    &lt;MiddleName&gt;H&lt;/MiddleName&gt;
    &lt;LastName&gt;Needham&lt;/LastName&gt;
  &lt;/Person&gt;	
&lt;/root&gt;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I wanted to be able to get the full names of each of the people such that I&#8217;d have a collection which looked like 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: #ff0000;">&quot;Charles Kubicek&quot;</span> <span style="color: #ff0000;">&quot;Mark H Needham&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>My initial thinking was to get all the child elements of the <cite>Person</cite> element and operate on those:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>require '<span style="color: #66cc66;">&#91;</span>clojure<span style="color: #66cc66;">.</span>contrib<span style="color: #66cc66;">.</span>zip-filter <span style="color: #66cc66;">:</span><span style="color: #555;">as</span> zf<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>xml-<span style="color: #66cc66;">&gt;</span> test-doc <span style="color: #66cc66;">:</span><span style="color: #555;">Person</span> zf/children text<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Unfortunately that gives back all the names in one collection like so:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Charles&quot;</span> <span style="color: #ff0000;">&quot;Kubicek&quot;</span> <span style="color: #ff0000;">&quot;Mark&quot;</span> <span style="color: #ff0000;">&quot;H&quot;</span> <span style="color: #ff0000;">&quot;Needham&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Since it&#8217;s not mandatory to have a <cite>MiddleName</cite> element it&#8217;s not possible to work out which names go with which person!</p>
<p>A bit of googling led me to <a href="http://stackoverflow.com/questions/2057797/how-do-i-combine-results-from-zip-filter-queries-on-an-xml-tree-in-clojure">stackoverflow</a> where Timothy Pratley suggests that we need to get up to the <cite>Person</cite> element and then pick each of the child elements individually.</p>
<p>We can do that by mapping over the collection with a function which creates a vector for each <cite>Person</cite> containing all their names.</p>
<p>In pseudo-code this is what we want to do:</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>map magic-<span style="color: #b1b100;">function</span> <span style="color: #66cc66;">&#40;</span>xml-<span style="color: #66cc66;">&gt;</span> test-doc <span style="color: #66cc66;">:</span><span style="color: #555;">Person</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Charles&quot;</span> <span style="color: #ff0000;">&quot;Kubicek&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Mark&quot;</span> <span style="color: #ff0000;">&quot;H&quot;</span> <span style="color: #ff0000;">&quot;Needham&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Timothy suggests the <cite>juxt</cite> function which is defined like so:</p>
<blockquote><p>
juxt</p>
<p>Takes a set of functions and returns a fn that is the juxtaposition of those fns.  The returned fn takes a variable number of args, and returns a vector containing the result of applying each fn to the args (left-to-right).
</p></blockquote>
<p>A simple use of <cite>juxt</cite> could be to create some values containing my name:</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;">&#40;</span>juxt #<span style="color: #66cc66;">&#40;</span>str <span style="color: #66cc66;">%</span> <span style="color: #ff0000;">&quot; loves Clojure&quot;</span><span style="color: #66cc66;">&#41;</span> #<span style="color: #66cc66;">&#40;</span>str <span style="color: #66cc66;">%</span> <span style="color: #ff0000;">&quot; loves Scala&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;Mark&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Which returns:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Mark loves Clojure&quot;</span> <span style="color: #ff0000;">&quot;Mark loves Scala&quot;</span><span style="color: #66cc66;">&#93;</span></pre></div></div>

<p>We can use <cite>juxt</cite> to build the collection of names and then use <cite><a href="http://clojuredocs.org/clojure_core/clojure.string/join">clojure.string/join</a></cite> to separate them with a space.</p>
<p>The code to do this ends up looking like this:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>require '<span style="color: #66cc66;">&#91;</span>clojure<span style="color: #66cc66;">.</span>string <span style="color: #66cc66;">:</span><span style="color: #555;">as</span> str<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn get-names <span style="color: #66cc66;">&#91;</span>doc<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>-<span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #66cc66;">&#40;</span>xml-<span style="color: #66cc66;">&gt;</span> doc <span style="color: #66cc66;">:</span><span style="color: #555;">Person</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>map <span style="color: #66cc66;">&#40;</span>juxt #<span style="color: #66cc66;">&#40;</span>xml1-<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">%</span> <span style="color: #66cc66;">:</span><span style="color: #555;">FirstName</span> text<span style="color: #66cc66;">&#41;</span> #<span style="color: #66cc66;">&#40;</span>xml1-<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">%</span> <span style="color: #66cc66;">:</span><span style="color: #555;">MiddleName</span> text<span style="color: #66cc66;">&#41;</span> #<span style="color: #66cc66;">&#40;</span>xml1-<span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">%</span> <span style="color: #66cc66;">:</span><span style="color: #555;">LastName</span> text<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>map <span style="color: #66cc66;">&#40;</span>partial filter seq<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#40;</span>map <span style="color: #66cc66;">&#40;</span>partial str/join <span style="color: #ff0000;">&quot; &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 use a <cite>filter</cite> on the second last line to get rid of any nil values in the vector (e.g. no middle name) and then combine the names on the last line. </p>
<p>We can then call the function:</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>get-names test-doc<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Charles Kubicek&quot;</span> <span style="color: #ff0000;">&quot;Mark H Needham&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/07/16/clojure-extracting-child-elements-from-an-xml-document-with-zip-filter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clojure: Language as thought shaper</title>
		<link>http://www.markhneedham.com/blog/2011/07/10/clojure-language-as-thought-shaper/</link>
		<comments>http://www.markhneedham.com/blog/2011/07/10/clojure-language-as-thought-shaper/#comments</comments>
		<pubDate>Sun, 10 Jul 2011 22:21:16 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3654</guid>
		<description><![CDATA[I recently read an interesting article by Tom Van Cutsem where he describes some of the goals that influence the design of programming languages and one which stood out to me is that of viewing &#8216;language as a thought shaper&#8217;: Language as thought shaper: to induce a paradigm shift in how one should structure software [...]]]></description>
			<content:encoded><![CDATA[<p>I recently read <a href="http://soft.vub.ac.be/~tvcutsem/whypls.html">an interesting article by Tom Van Cutsem</a> where he describes some of the goals that influence the design of programming languages and one which stood out to me is that of viewing &#8216;language as a thought shaper&#8217;:</p>
<blockquote><p>
Language as thought shaper: to induce a paradigm shift in how one should structure software (changing the &#8220;path of least resistance&#8221;).
</p></blockquote>
<blockquote><p>
To quote <a href="http://www.cs.yale.edu/quotes.html">Alan Perlis</a>: &#8220;a language that doesn&#8217;t affect the way you think about programming, is not worth knowing.&#8221;</p>
<p>The goal of a thought shaper language is to change the way a programmer thinks about structuring his or her program.
</p></blockquote>
<p>I&#8217;ve been rewriting part of the current system that I&#8217;m working on in Clojure in my spare time to see how the design would differ and it&#8217;s interesting to see that it&#8217;s quite different.</p>
<p>The part of the system I&#8217;m working on needs to extract a bunch of XML files from ZIP files and then import those into the database.</p>
<p>From a high level the problem can be described as follows:</p>
<ul>
<li>Get all files in specified directory</li>
<li>Find only the ZIP files</li>
<li>Find the XML files in those ZIP files</li>
<li>Categorise the XML files depending on whether we can import them</li>
<li>Add an additional section to good files to allow for easier database indexing</li>
<li>Import the new version of the files into the database</li>
</ul>
<p>Clojure encourages a design based around processing lists and this problem seems to fit that paradigm very neatly.</p>
<p>We can make use of the <cite><a href="http://clojuredocs.org/clojure_core/clojure.core/-%3E%3E">->></a></cite> macro to chain together a bunch of functions originally acting on the specified directory to allow us to achieve this.</p>
<p>At the moment this is what the entry point of the code looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn parse-directory <span style="color: #66cc66;">&#91;</span>dir<span style="color: #66cc66;">&#93;</span>                                                                                                                                                                           
  <span style="color: #66cc66;">&#40;</span>-<span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #66cc66;">&#40;</span>all-files-in dir<span style="color: #66cc66;">&#41;</span>                                                                                         
       <span style="color: #66cc66;">&#40;</span>filter #<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">..</span> <span style="color: #66cc66;">&#40;</span>canonical-path <span style="color: #66cc66;">%</span>1<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>endsWith <span style="color: #ff0000;">&quot;.zip&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>mapcat <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>file<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>extract file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>                                                                         
       <span style="color: #66cc66;">&#40;</span>filter <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>entry<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span> <span style="color: #66cc66;">&#40;</span>entry <span style="color: #66cc66;">:</span><span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>endsWith <span style="color: #ff0000;">&quot;.xml&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>                                                                                                                                                 
       <span style="color: #66cc66;">&#40;</span>map #<span style="color: #66cc66;">&#40;</span>categorise <span style="color: #66cc66;">%</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 design of the Scala code is a bit different even though the language constructs exist to make a similar design possible.</p>
<p>The following are some of the classes involved:</p>
<ul>
<li>ImportManager &#8211; finds the XML files in the ZIP files, delegates to DocumentMatcher</li>
<li>DeliveryManager &#8211; gets all the ZIP files from specified directory</li>
<li>DocumentMatcher &#8211; checks if XML document matches any validation rules and wraps in appropriate object</li>
<li>ValidDocument/InvalidDocument &#8211; wrap the XML document and upload to database in the case of the former</li>
<li>ValidationRule &#8211; checks if the document can be imported into the system</li>
</ul>
<p>It was interesting to me that when I read the Scala code the problem appeared quite complicated whereas in Clojure it&#8217;s easier to see the outline of what the program does.</p>
<p>I think is because we&#8217;re trying to shoe horn a <a href="http://eaipatterns.com/PipesAndFilters.html">pipes and filters</a> problems into objects which leaves us with a design that feels quite unnatural.</p>
<p>I originally learnt this design style while playing around with F# a couple of years ago and it seems to work reasonably well in most functional languages.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/07/10/clojure-language-as-thought-shaper/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Clojure: Equivalent to Scala&#8217;s flatMap/C#&#8217;s SelectMany</title>
		<link>http://www.markhneedham.com/blog/2011/07/03/clojure-equivalent-to-scalas-flatmapcs-selectmany/</link>
		<comments>http://www.markhneedham.com/blog/2011/07/03/clojure-equivalent-to-scalas-flatmapcs-selectmany/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 22:50:47 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=3646</guid>
		<description><![CDATA[I&#8217;ve been playing around with Clojure a bit over the weekend and one thing I got stuck with was working out how to achieve the functionality provided by Scala&#8217;s flatMap or C#&#8217;s SelectMany methods on collections. I had a collection of zip files and wanted to transform that into a collection of all the file [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with Clojure a bit over the weekend and one thing I got stuck with was working out how to achieve the functionality provided by Scala&#8217;s <cite>flatMap</cite> or C#&#8217;s <cite>SelectMany</cite> methods on collections.</p>
<p>I had a collection of zip files and wanted to transform that into a collection of all the file entries in those files.</p>
<p>If we just use <cite>map</cite> then we&#8217;ll end up with a collection of collections which is more difficult to deal with going forward.</p>
<p>In Scala we&#8217;d do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">collection</span>.<span style="color: #000000;">JavaConversions</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">val</span> zip1 <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> ZipFile<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> File<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;/Users/mneedham/Documents/my-zip-file.zip&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">val</span> zip2 <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> ZipFile<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> File<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;/Users/mneedham/Documents/my-zip-file2.zip&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
List<span style="color: #F78811;">&#40;</span>zip1, zip2<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">flatMap</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000000;">entries</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>I was originally make using of <cite>map</cite> followed by <cite>flatten</cite> but I learnt from my colleague <a href="http://fragmental.tw/">Phil Calcado</a> that the function I wanted is <cite><a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/mapcat">mapcat</a></cite> which leads to this solution:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>def zip1 <span style="color: #66cc66;">&#40;</span>new ZipFile <span style="color: #66cc66;">&#40;</span>file <span style="color: #ff0000;">&quot;/Users/mneedham/Documents/my-zip-file.zip&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>def zip2 <span style="color: #66cc66;">&#40;</span>new ZipFile <span style="color: #66cc66;">&#40;</span>file <span style="color: #ff0000;">&quot;/Users/mneedham/Documents/my-zip-file2.zip&quot;</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>mapcat <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>file<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>enumeration-seq <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>entries file<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">list</span> zip1 zip2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I also learnt about the various functions available to create sequences, such as <cite>enumeration-seq</cite> from other types which are listed at <a href="http://clojure.org/sequences">the bottom of this page</a>.</p>
<p>Scala uses implicit conversions to do that and presumably you&#8217;d hide away the conversion in a helper function in Clojure.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2011/07/03/clojure-equivalent-to-scalas-flatmapcs-selectmany/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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&#8217;m up to the chapter on using macros in Stuart Halloway&#8217;s &#8216;Programming Clojure&#8216; book and since I&#8217;ve never used a language which has macros in before I thought it&#8217;d be cool to write one. In reality there&#8217;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&#8217;m up to the chapter on using macros in Stuart Halloway&#8217;s &#8216;<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>&#8216; book and since I&#8217;ve never used a language which has macros in before I thought it&#8217;d be cool to write one.</p>
<p>In reality there&#8217;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&#8217;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 &#8216;println&#8217; expression so it doesn&#8217;t actually pass &#8216;&#8221;Hello&#8221;&#8216; and &#8216;person&#8217; to &#8216;println&#8217;. Instead each symbol is evaluated individually.</p>
<p>When we evaluate this in the REPL we therefore don&#8217;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 &#8216;do&#8217; evaluates each argument in order and then returns the last one which in this case is &#8220;Mark&#8221;.</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&#8217;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">&#8216;println&#8217; symbol has been evaluated at macro expansion time</a> it doesn&#8217;t actually make any difference to the way the macro works.</p>
<p>We can fix that by escaping &#8216;println&#8217; so that it won&#8217;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(&#8216;) 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 &#8216;person&#8217; is only evaluated at runtime and since it hasn&#8217;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&#8217;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 &#8216;person&#8217; 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&#8217;s been quite interesting to see all the different ways that I&#8217;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&#8217;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&#8217;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&#8217;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&#8217;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&#8217;d missed off the brackets around &#8216;println&#8217; and the arguments following it which would have resulted in &#8216;println&#8217; 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 &#8216;println&#8217; 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 &#8216;when-let&#8216; macro. &#8216;when-let&#8217; 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 &#8216;false&#8217; or &#8216;nil&#8217; [...]]]></description>
			<content:encoded><![CDATA[<p>In my continued playing around with Clojure I came across the &#8216;<a href="http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/when-let">when-let</a>&#8216; macro.</p>
<p>&#8216;when-let&#8217; 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 &#8216;false&#8217; or &#8216;nil&#8217; would result in the second argument not being evaluated. </p>
<p>A simple example of using &#8216;when-let&#8217; 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 &#8216;assert-args&#8217; 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&#8217;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 &#8216;bindings&#8217; will be a vector so we can take the &#8217;0th&#8217; and &#8217;1st&#8217; elements from it and bind them to &#8216;form&#8217; and &#8216;tst&#8217; respectively. I didn&#8217;t quite pick up on the first few times I read it.</p>
<p>On line 10 it makes use of &#8216;auto-gensym&#8217; to create a unique name which begins with &#8216;temp&#8217; and is bound to the value of &#8216;tst&#8217; which in the simple example provided would be the value &#8217;2&#8242;. As I understand it the name would be something like &#8216;temp__304&#8242; or something similarly random!</p>
<p>&#8216;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&#8217;re getting a value out of a list and want to check whether or not we&#8217;ve reached the end of that list or not. If we have then eventually we&#8217;ll have a value of &#8216;nil&#8217; bound by the &#8216;let&#8217; and then we&#8217;ll know we&#8217;re finished.</p>
<p>An example of where the body wouldn&#8217;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&#8217;t really understand why we need to bind &#8216;form&#8217; to &#8216;temp&#8217; on the second last line as it doesn&#8217;t seem like the value is used?  I&#8217;m sure there&#8217;s probably something I&#8217;m missing there so if anyone could point it out that&#8217;d be cool!</p>
<p>As I understand it, the &#8216;~@body&#8217; on the last line is called the &#8216;splicing unquote&#8217; and it allows the individual values in &#8216;body&#8217; to be put into the template started at &#8216;`(let [temp# ~tst]&#8216; 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>6</slash:comments>
		</item>
	</channel>
</rss>

