<?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>The Free Geek &#187; clojure</title>
	<atom:link href="http://freegeek.in/blog/tag/clojure/feed/" rel="self" type="application/rss+xml" />
	<link>http://freegeek.in/blog</link>
	<description>The Chronicles of Nerd-nia</description>
	<lastBuildDate>Fri, 03 Jun 2011 05:15:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>10 Clojure One Liners to Impress Your Friends</title>
		<link>http://freegeek.in/blog/2011/06/10-clojure-one-liners/</link>
		<comments>http://freegeek.in/blog/2011/06/10-clojure-one-liners/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 11:38:13 +0000</pubDate>
		<dc:creator>Baishampayan</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://freegeek.in/blog/?p=111</guid>
		<description><![CDATA[I saw an interesting post today aptly titled 10 Scala One Liners to Impress Your Friends and then someone followed up with another blog post titled 10 CoffeeScript One Liners to Impress Your Friends. Those two posts show programming tasks (most of them trivial to solve in modern programming languages) being accomplished in about 1 [...]]]></description>
			<content:encoded><![CDATA[<p>I saw an interesting post today aptly titled <a title="10 Scala One Liners to Impress Your Friends" href="http://solog.co/47/10-scala-one-liners-to-impress-your-friends/">10 Scala One Liners to Impress Your Friends</a> and then someone followed up with another blog post titled <a title="10 CoffeeScript One Liners to Impress Your Friends" href="http://ricardo.cc/2011/06/02/10-CoffeeScript-One-Liners-to-Impress-Your-Friends.html">10 CoffeeScript One Liners to Impress Your Friends</a>. Those two posts show programming tasks (most of them trivial to solve in modern programming languages) being accomplished in about 1 line of code each.</p>
<p>I thought it&#8217;d be quite appropriate if someone ported those examples to my favourite programming language <a title="Clojure Programming Language" href="http://clojure.org">Clojure</a>, so here they are -</p>
<h2>1. Multiple Each Item in a List by 2</h2>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>map #<span style="color: #66cc66;">&#40;</span>* <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>range <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h2>2. Sum a List of Numbers</h2>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>reduce<span style="color: #66cc66;"> + </span><span style="color: #66cc66;">&#40;</span>range <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">1001</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h2>3. Verify if Word Exists in a String</h2>
<p>I used a regex here, because I believe that&#8217;s the right way to do it.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>def tweet <span style="color: #ff0000;">&quot;This is an example tweet talking about clojure and emacs.&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def regex <span style="color: #66cc66;">&#40;</span>re-pattern <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> str <span style="color: #66cc66;">&#40;</span>interpose <span style="color: #ff0000;">&quot;|&quot;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;clojure&quot;</span> <span style="color: #ff0000;">&quot;logic&quot;</span> <span style="color: #ff0000;">&quot;compojure&quot;</span> <span style="color: #ff0000;">&quot;emacs&quot;</span> <span style="color: #ff0000;">&quot;macros&quot;</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;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>re-seq regex tweet<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; Gives me the actual matches instead of just true/false</span></pre></div></div>

<p>As suggested by a commentator, this problem can be solved without using regex by leveraging Clojure&#8217;s sets.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>def tweet <span style="color: #ff0000;">&quot;This is an example tweet talking about clojure and emacs.&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def is-word? <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;clojure&quot;</span> <span style="color: #ff0000;">&quot;logic&quot;</span> <span style="color: #ff0000;">&quot;compojure&quot;</span> <span style="color: #ff0000;">&quot;emacs&quot;</span> <span style="color: #ff0000;">&quot;macros&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">not</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil</span>? <span style="color: #66cc66;">&#40;</span>some is-word? <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>split tweet <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> <span style="color: #808080; font-style: italic;">; Returns true/false</span></pre></div></div>

<h2>4. Read a File</h2>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>def file-text <span style="color: #66cc66;">&#40;</span>slurp <span style="color: #ff0000;">&quot;data.txt&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; Reads the whole file</span>
<span style="color: #66cc66;">&#40;</span>def file-lines <span style="color: #66cc66;">&#40;</span>clojure<span style="color: #66cc66;">.</span>contrib<span style="color: #66cc66;">.</span>io/read-lines <span style="color: #ff0000;">&quot;data.txt&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; Reads as a sequence of lines</span></pre></div></div>

<p>Since Clojure Contrib has been deprecated for future Clojure releases, <code>clojure.contrib.io/read-lines</code> can be rewritten as <code>(line-seq (clojure.java.io/reader (clojure.java.io/file “data.txt”)))</code> in Clojure 1.3 onwards. Thanks to Aaron for pointing it out.</p>
<h2>5. Happy Birthday to You</h2>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>doseq <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">l</span> <span style="color: #66cc66;">&#40;</span>map #<span style="color: #66cc66;">&#40;</span>str <span style="color: #ff0000;">&quot;Happy Birthday &quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;dear Rich&quot;</span> <span style="color: #ff0000;">&quot;to You&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>range <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>println <span style="color: #b1b100;">l</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Alternate version -</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;">dotimes</span> <span style="color: #66cc66;">&#91;</span>n <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;Happy Birthday &quot;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> n <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;dear Rich&quot;</span> <span style="color: #ff0000;">&quot;to You&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h2>6. Filter List of Numbers</h2>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>partition-by #<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">49</span> <span style="color: #cc66cc;">58</span> <span style="color: #cc66cc;">76</span> <span style="color: #cc66cc;">82</span> <span style="color: #cc66cc;">88</span> <span style="color: #cc66cc;">90</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h2>7. Fetch and Parse XML Web Service</h2>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>clojure<span style="color: #66cc66;">.</span>xml/parse <span style="color: #ff0000;">&quot;http://search.twitter.com/search.atom?&amp;q=clojure&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h2>8. Find Maximum (or Minimum) in a List</h2>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>reduce <span style="color: #b1b100;">max</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">14</span> <span style="color: #cc66cc;">35</span> -<span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">46</span> <span style="color: #cc66cc;">98</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>reduce <span style="color: #b1b100;">min</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">14</span> <span style="color: #cc66cc;">35</span> -<span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">46</span> <span style="color: #cc66cc;">98</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;; Now both together</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>juxt #<span style="color: #66cc66;">&#40;</span>reduce <span style="color: #b1b100;">max</span> <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#41;</span> #<span style="color: #66cc66;">&#40;</span>reduce <span style="color: #b1b100;">min</span> <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">14</span> <span style="color: #cc66cc;">35</span> -<span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">46</span> <span style="color: #cc66cc;">98</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; Returns [98 -7]</span></pre></div></div>

<h2>9. Parallel Processing</h2>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;; Assuming process-line to be a CPU intensive function that operates on a line</span>
<span style="color: #66cc66;">&#40;</span>pmap process-line lines<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">; Note the &quot;p&quot; in front of map</span></pre></div></div>

<h2>10. Sieve of Eratosthenes</h2>
<p>I don&#8217;t I have a sufficiently good (in terms of performance &amp; beauty) one line implementation of SoE. I would recommend checking out Christophe Grand&#8217;s treatise on the subject titled <a title="Everybody Loves Sieve of Eratosthenes" rel="bookmark" href="http://clj-me.cgrand.net/2009/07/30/everybody-loves-the-sieve-of-eratosthenes/">Everybody loves the Sieve of Eratosthenes</a> for a great discussion on writing real world prime sieves in Clojure.</p>
<h2>11. Solve FizzBuzz</h2>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>map #<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span>zero? <span style="color: #66cc66;">&#40;</span>mod <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;FizzBuzz&quot;</span> <span style="color: #66cc66;">&#40;</span>zero? <span style="color: #66cc66;">&#40;</span>mod <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;Fizz&quot;</span> <span style="color: #66cc66;">&#40;</span>zero? <span style="color: #66cc66;">&#40;</span>mod <span style="color: #66cc66;">%</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot;Buzz&quot;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">else</span> <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>range <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">101</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<h2>Conclusion</h2>
<p>Can&#8217;t conclude anything from these examples, really. Clojure is an extremely powerful and succinct programming language. Learn it, write some code in it and then decide for yourself.</p>
<p>Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://freegeek.in/blog/2011/06/10-clojure-one-liners/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Clojure Protocols &amp; Datatypes &#8212; A sneak peek</title>
		<link>http://freegeek.in/blog/2010/05/clojure-protocols-datatypes-a-sneak-peek/</link>
		<comments>http://freegeek.in/blog/2010/05/clojure-protocols-datatypes-a-sneak-peek/#comments</comments>
		<pubDate>Mon, 31 May 2010 13:06:58 +0000</pubDate>
		<dc:creator>Baishampayan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[Free Software]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://freegeek.in/blog/?p=91</guid>
		<description><![CDATA[Clojure 1.2 introduces two very remarkable features &#8211; Protocols and Datatypes. Clojure is defined in terms of abstractions and various implementations of those abstractions. For example, vectors, maps, lists, sets in Clojure implement the sequence abstraction which lets us treat any of those data structures as sequences. Until recently it was not possible feasible to [...]]]></description>
			<content:encoded><![CDATA[<p>Clojure 1.2 introduces two very remarkable features &#8211; <a href="http://clojure.org/protocols">Protocols</a> and <a href="http://clojure.org/datatypes">Datatypes</a>. Clojure is defined in terms of abstractions and various implementations of those abstractions. For example, vectors, maps, lists, sets in Clojure implement the sequence abstraction which lets us treat any of those data structures as sequences.<br />
Until recently it was not <del datetime="2010-06-02T03:21:30+00:00">possible</del> <ins datetime="2010-06-02T03:21:30+00:00">feasible</ins> to define and implement such core abstractions in Clojure itself; one had to drop down to Java (or C#) for those tasks, but not anymore <img src='http://freegeek.in/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Clojure 1.2 now has excellent facilities for defining and implementing similar abstractions in a highly dynamic manner while maintaining fantastic performance characteristics.<br />
In this post, I will give you a brief overview of these new features and will show you how they are useful.</p>
<p><strong>Protocols</strong></p>
<p>Protocols in Clojure are similar to Java Interfaces, though not quite. Basically a protocol is a contract, a set of functionalities without any implementation. Let&#8217;s consider a simple protocol &#8211;</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defprotocol Fly
  <span style="color: #ff0000;">&quot;A simple protocol for flying&quot;</span>
  <span style="color: #66cc66;">&#40;</span>fly <span style="color: #66cc66;">&#91;</span>this<span style="color: #66cc66;">&#93;</span> <span style="color: #ff0000;">&quot;Method to fly&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>So here we have a trivial protocol Fly which declares a method fly which takes one argument &#8216;this&#8217; (which is actually the type implementing the protocol itself). In case of all methods defined via Protocols, the first argument is always the implementing type itself. The name &#8216;this&#8217; is just a convention; it could be &#8216;self&#8217;, etc. or anything.</p>
<p>When we declared the Fly protocol, two new vars were created. One is &#8216;Fly&#8217;, the protocol itself, and the other is &#8216;fly&#8217; which is a polymorphic function that will get called when we execute it on an implementation of Fly.</p>
<p>Right now, if you try to execute the method &#8216;fly&#8217; on any object, you will get an exception because no types are implementing that protocol yet, which brings us to the next topic, DataTypes.</p>
<p><strong>DataTypes</strong></p>
<p>Traditionally in Clojure whenever we wanted to have some kind of record or a property-only Class, we used maps or struct-maps. Those serve the purpose perfectly well in most cases but the problem was that those maps didn&#8217;t have any type information attached to them. As a result, we had to put some extra keys in maps to help us determine the type of a record before we could dispatch methods. There were some obvious performance limitations too; being vanilla maps, they were never as fast as Plain Old Java Objects (POJOs). Enter <a href="http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/deftype">deftype</a> and its cousin <a href="http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/defrecord">defrecord</a>.</p>
<p>In Clojure 1.2 we can define our own types using defrecord like this -</p>

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

<p>Boom! We have a custom type, Bird with two fields, name and species. We can now instantiate a Bird like this -</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>def crow <span style="color: #66cc66;">&#40;</span>Bird<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;Crow&quot;</span> <span style="color: #ff0000;">&quot;Corvus corax&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We can access the fields of the Bird instance by treating it like<br />
a normal map -</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user› <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">nom</span> crow<span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;Crow&quot;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user› <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">species</span> crow<span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;Corvus corax&quot;</span></pre></div></div>

<p>We can also add/remove/modify keys in a record like we would do with a<br />
normal map.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>def sparrow <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> crow <span style="color: #66cc66;">:</span><span style="color: #555;">nom</span> <span style="color: #ff0000;">&quot;Sparrow&quot;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">species</span> <span style="color: #ff0000;">&quot;Passer domesticus&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This will create a new immutable instance of Bird with different data. Note that since Clojure records are persistent and immutable, the original crow instance is not affected.</p>
<p>Now to make the Bird fly. We already have a protocol called Fly. We need to implement the protocol so that our birds can actually fly. One way to do that is to put the protocol implementation inline with the record definition itself -</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defrecord Bird <span style="color: #66cc66;">&#91;</span>nom species<span style="color: #66cc66;">&#93;</span>
  Fly
  <span style="color: #66cc66;">&#40;</span>fly <span style="color: #66cc66;">&#91;</span>this<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>str <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">nom</span> this<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot; flies...&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>So easy, right? If we now create another instance of Bird, it will actually be able to fly -</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user› <span style="color: #66cc66;">&#40;</span>def kiwi <span style="color: #66cc66;">&#40;</span>Bird<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;Kiwi&quot;</span> <span style="color: #ff0000;">&quot;Apteryx australis&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
#'user/kiwi
user› <span style="color: #66cc66;">&#40;</span>fly kiwi<span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;Kiwi flies...&quot;</span></pre></div></div>

<p>Great! But what happens to the Crow, and Sparrow? We created those instances when the Bird record didn&#8217;t have any implementation of the Fly protocol. You might face similar issues when you don&#8217;t have control over the code which defines the record/class. You will need to extend those types dynamically with implementation of a protocol. Enter <a href="http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/extend-type">extend-type</a>. extend-type (and its cousin <a href="http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/extend-protocol">extend-protocol</a>) allows us to implement protocols on pre-existing types. Consider the following example -</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defprotocol Walk
  <span style="color: #ff0000;">&quot;A simple protocol to make birds walk&quot;</span>
  <span style="color: #66cc66;">&#40;</span>walk <span style="color: #66cc66;">&#91;</span>this<span style="color: #66cc66;">&#93;</span> <span style="color: #ff0000;">&quot;Birds want to walk too!&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>extend-type Bird
  Walk
  <span style="color: #66cc66;">&#40;</span>walk <span style="color: #66cc66;">&#91;</span>this<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>str <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">nom</span> this<span style="color: #66cc66;">&#41;</span> <span style="color: #ff0000;">&quot; walks too...&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We just added an implementation of the Walk protocol to the existing type Bird. All new Bird instances created from now on will be able to Walk and Fly.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user› <span style="color: #66cc66;">&#40;</span>def hummingbird <span style="color: #66cc66;">&#40;</span>Bird<span style="color: #66cc66;">.</span> <span style="color: #ff0000;">&quot;Hummingbird&quot;</span> <span style="color: #ff0000;">&quot;Selasphorus rufus&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
user› <span style="color: #66cc66;">&#40;</span>fly hummingbird<span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;Hummingbird flies...&quot;</span>
user› <span style="color: #66cc66;">&#40;</span>walk hummingbird<span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;Hummingbird walks too...&quot;</span></pre></div></div>

<p>Cool, right? At times you might require a anonymous  object which implements some protocol or interface. You could utilise those objects in cases where you just need an object which implements a given protocol but you don&#8217;t care about its type. Clojure 1.2 gives you <a href="http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/reify">reify</a>. reify allows us to create one-off anonymous objects which implement one or more protocols.</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user› <span style="color: #66cc66;">&#40;</span>fly <span style="color: #66cc66;">&#40;</span>reify Fly <span style="color: #66cc66;">&#40;</span>fly <span style="color: #66cc66;">&#91;</span>_<span style="color: #66cc66;">&#93;</span> <span style="color: #ff0000;">&quot;Swine flu...&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;Swine flu...&quot;</span></pre></div></div>

<p>Woah! Clojure can make Pigs fly <img src='http://freegeek.in/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Jokes apart, what we just did was very interesting. We just created an anonymous type which implements the Fly protocol and called the fly method on it; and it flu[sic] <img src='http://freegeek.in/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>We could implement multiple protocols in the same reify statement too,<br />
like this -</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>def pig <span style="color: #66cc66;">&#40;</span>reify
                Fly <span style="color: #66cc66;">&#40;</span>fly <span style="color: #66cc66;">&#91;</span>_<span style="color: #66cc66;">&#93;</span> <span style="color: #ff0000;">&quot;Swine flu...&quot;</span><span style="color: #66cc66;">&#41;</span>
                Walk <span style="color: #66cc66;">&#40;</span>walk <span style="color: #66cc66;">&#91;</span>_<span style="color: #66cc66;">&#93;</span> <span style="color: #ff0000;">&quot;Pig-man walking...&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
user› <span style="color: #66cc66;">&#40;</span>fly pig<span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;Swine flu...&quot;</span>
user› <span style="color: #66cc66;">&#40;</span>walk pig<span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;Pig-man walking...&quot;</span></pre></div></div>

<p>Beautiful. reify is quite similar to proxy and it is now recommended to use reify instead of proxy wherever possible because reify is much faster than proxy.</p>
<p>Before I finish off, let me explain the differences between defrecord and deftype. defrecord creates a new type and implements a few core Clojure interfaces like that of the persistent map, hashcode, keyword accessors, etc. If you are using deftype, Clojure will not implicitly implement any interface not provided by the user. In short, if you are using deftype, you will have to implement your own accessors, hashcode, etc. In most cases defrecord should suffice, but in other cases like when you need mutable fields, use deftype.</p>
<p>There is some in-depth explanation of Protocols and Datatypes on the <a href="http://clojure.org/">Clojure website</a> which you should consult if you need more information.</p>
<p><strong>Bonus Material</strong></p>
<p>Making Java Strings fly and walk <img src='http://freegeek.in/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user› <span style="color: #66cc66;">&#40;</span>extend-type java<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>String
                   Fly <span style="color: #66cc66;">&#40;</span>fly <span style="color: #66cc66;">&#91;</span>this<span style="color: #66cc66;">&#93;</span> <span style="color: #ff0000;">&quot;See me fly?&quot;</span><span style="color: #66cc66;">&#41;</span>
                   Walk <span style="color: #66cc66;">&#40;</span>walk <span style="color: #66cc66;">&#91;</span>this<span style="color: #66cc66;">&#93;</span> <span style="color: #ff0000;">&quot;Yes, that's me walking!&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #b1b100;">nil</span>
user› <span style="color: #66cc66;">&#40;</span>walk <span style="color: #ff0000;">&quot;foo&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;Yes, that's me walking!&quot;</span>
user› <span style="color: #66cc66;">&#40;</span>fly <span style="color: #ff0000;">&quot;bar&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;See me fly?&quot;</span></pre></div></div>

<p>PS &#8211; I wrote this today because I was sitting at home, sick. There are possibly some mistakes in this post; in which case, please let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://freegeek.in/blog/2010/05/clojure-protocols-datatypes-a-sneak-peek/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>Clojure Course in Pune</title>
		<link>http://freegeek.in/blog/2010/05/clojure-course-in-pune/</link>
		<comments>http://freegeek.in/blog/2010/05/clojure-course-in-pune/#comments</comments>
		<pubDate>Thu, 27 May 2010 12:21:14 +0000</pubDate>
		<dc:creator>Baishampayan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[pune]]></category>

		<guid isPermaLink="false">http://freegeek.in/blog/?p=89</guid>
		<description><![CDATA[There is a possibility of me conducting a Clojure course/workshop in Pune. In the course I will cover Clojure from ground up, teaching how to build real-world applications in Clojure. If you are (or your friend is) interested, please take part in this short survey which will help me understand what is needed.]]></description>
			<content:encoded><![CDATA[<p>There is a possibility of me conducting a Clojure course/workshop in Pune. In the course I will cover Clojure from ground up, teaching how to build real-world applications in Clojure.</p>
<p>If you are (or your friend is) interested, please take part in <a href="http://pune.clojure.in/">this short survey</a> which will help me understand what is needed.</p>
]]></content:encoded>
			<wfw:commentRss>http://freegeek.in/blog/2010/05/clojure-course-in-pune/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Slides from my Clojure talk at GNUnify 2010</title>
		<link>http://freegeek.in/blog/2010/02/slides-from-my-clojure-talk-at-gnunify-2010/</link>
		<comments>http://freegeek.in/blog/2010/02/slides-from-my-clojure-talk-at-gnunify-2010/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 07:51:42 +0000</pubDate>
		<dc:creator>Baishampayan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[Free Software]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[talk]]></category>

		<guid isPermaLink="false">http://freegeek.in/blog/?p=86</guid>
		<description><![CDATA[I gave an intro talk about Clojure at GNUnify 2010, Pune today. It was supposed to be a very basic talk on Clojure aimed at Java programmers. Here are the slides - Introduction to Clojure View more presentations from Baishampayan Ghose.]]></description>
			<content:encoded><![CDATA[<p>I gave an intro talk about Clojure at <a href="http://gnunify.in/">GNUnify 2010</a>, Pune today. It was supposed to be a very basic talk on Clojure aimed at Java programmers. Here are the slides -</p>
<div id="__ss_3214693" style="width: 425px; text-align: left;"><a style="font: 14px Helvetica,Arial,Sans-serif; display: block; margin: 12px 0 3px 0; text-decoration: underline;" title="Introduction to Clojure" href="http://www.slideshare.net/zaph0d/introduction-to-clojure">Introduction to Clojure</a><object style="margin: 0px;" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="355" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="src" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=clojure-100218040639-phpapp01&amp;rel=0&amp;stripped_title=introduction-to-clojure" /><param name="allowfullscreen" value="true" /><embed style="margin: 0px;" type="application/x-shockwave-flash" width="425" height="355" src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=clojure-100218040639-phpapp01&amp;rel=0&amp;stripped_title=introduction-to-clojure" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<div style="font-size: 11px; font-family: tahoma,arial; height: 26px; padding-top: 2px;">View more <a style="text-decoration: underline;" href="http://www.slideshare.net/">presentations</a> from <a style="text-decoration: underline;" href="http://www.slideshare.net/zaph0d">Baishampayan Ghose</a>.</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://freegeek.in/blog/2010/02/slides-from-my-clojure-talk-at-gnunify-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Downloading a bunch of files in parallel using Clojure Agents</title>
		<link>http://freegeek.in/blog/2009/10/downloading-a-bunch-of-files-in-parallel-using-clojure-agents/</link>
		<comments>http://freegeek.in/blog/2009/10/downloading-a-bunch-of-files-in-parallel-using-clojure-agents/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 08:24:49 +0000</pubDate>
		<dc:creator>Baishampayan</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[agents]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://freegeek.in/blog/?p=80</guid>
		<description><![CDATA[I suddenly needed to download around 3000 files from the Internet. I had the urls in a sequence and I was thinking about a nice way to download the files in parallel. The idea of using Clojure Agents came naturally to my mind and I was thinking about writing an Agent based HTTP client in [...]]]></description>
			<content:encoded><![CDATA[<p>I suddenly needed to download around 3000 files from the Internet. I had the urls in a sequence and I was thinking about a nice way to download the files in parallel.</p>
<p>The idea of using <a title="Clojure" href="http://clojure.org/">Clojure</a> <a title="Clojure Agents" href="http://clojure.org/agents">Agents</a> came naturally to my mind and I was thinking about writing an Agent based HTTP client in Clojure. I asked around on the Clojure IRC channel and the very helpful Stuart Sierra pointed me towards <a href="http://richhickey.github.com/clojure-contrib/http.agent-api.html">clojure.contrib.http.agent<br />
</a><br />
Indeed, c.c.http.agent seemed to be exactly what I had in my mind <img src='http://freegeek.in/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The API seemed to be straightforward enough and I got cracking immediately. I came up with something like this &#8211;</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">;;; downloader.clj -- Parallel Downloader -*- Clojure -*-</span>
<span style="color: #808080; font-style: italic;">;;; Time-stamp: &quot;2009-10-06 13:38:57 ghoseb&quot;</span>
<span style="color: #808080; font-style: italic;">;;; Author: Baishampayan Ghose </span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>ns downloader
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">require</span> <span style="color: #66cc66;">&#91;</span>clojure<span style="color: #66cc66;">.</span>contrib<span style="color: #66cc66;">.</span>http<span style="color: #66cc66;">.</span>agent <span style="color: #66cc66;">:</span><span style="color: #555;">as</span> h<span style="color: #66cc66;">&#93;</span>
            <span style="color: #66cc66;">&#91;</span>clojure<span style="color: #66cc66;">.</span>contrib<span style="color: #66cc66;">.</span>duck-streams <span style="color: #66cc66;">:</span><span style="color: #555;">as</span> d<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
A vector of vectors containing the file <span style="color: #b1b100;">name</span> <span style="color: #b1b100;">and</span> the URL
<span style="color: #66cc66;">&#40;</span>def url-data <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;file1&quot;</span> <span style="color: #ff0000;">&quot;http://some.domain/file1.xml&quot;</span><span style="color: #66cc66;">&#93;</span>
               <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;file2&quot;</span> <span style="color: #ff0000;">&quot;http://some.domain/file2.xml&quot;</span><span style="color: #66cc66;">&#93;</span>
               <span style="color: #808080; font-style: italic;">; Many many more :)</span>
               <span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn download
  <span style="color: #ff0000;">&quot;Download the data in the given URL using HTTP Agents
   Args:
     file-name - The file name to save the data in
     url - The URL to fetch
  &quot;</span>
  <span style="color: #66cc66;">&#91;</span>file-<span style="color: #b1b100;">name</span> url<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>h/http-agent url
                <span style="color: #66cc66;">:</span><span style="color: #555;">handler</span> <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>agnt<span style="color: #66cc66;">&#93;</span>
                           <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>fname file-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#93;</span>  <span style="color: #808080; font-style: italic;">; File name in a closure</span>
                             <span style="color: #66cc66;">&#40;</span>with-open <span style="color: #66cc66;">&#91;</span>w <span style="color: #66cc66;">&#40;</span>d/writer fname<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
                               <span style="color: #66cc66;">&#40;</span>d/copy <span style="color: #66cc66;">&#40;</span>h/stream agnt<span style="color: #66cc66;">&#41;</span> w<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn download-all
  <span style="color: #ff0000;">&quot;Download all the URLs
   Args:
     url-data - A vector of vectors containing the file name and the url
  &quot;</span>
  <span style="color: #66cc66;">&#91;</span>url-data<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>doseq <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>file-<span style="color: #b1b100;">name</span> url<span style="color: #66cc66;">&#93;</span> url-data<span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span>download file-<span style="color: #b1b100;">name</span> url<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>download-all url-data<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This looked fine and worked with a small set of urls. But when I ran it on the full-blown set of URLs, the server bailed out because of too many concurrent requests. The reason being the fact that http.agent uses send-off to dispatch action to the agents and send-off can end up using a potentially very large thread-pool.</p>
<p>Surely I needed to somehow make sure that only a limited number of files are downloaded in parallel and start downloading more when those are done.</p>
<p>To achieve that, I did this &#8211;</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>def partitioned-data <span style="color: #66cc66;">&#40;</span>partition <span style="color: #cc66cc;">15</span> url-data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; 15 being the max parallel downloads</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn download-all2
  <span style="color: #ff0000;">&quot;Download all the files, step by step
   Args:
     p-url-data - Partitioned url data
  &quot;</span>
  <span style="color: #66cc66;">&#91;</span>p-url-data<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>doseq <span style="color: #66cc66;">&#91;</span>url-data p-url-data<span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>agnts <span style="color: #66cc66;">&#40;</span>map #<span style="color: #66cc66;">&#40;</span>download <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>second <span style="color: #66cc66;">%</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> url-data<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
      <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> await agnts<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: #808080; font-style: italic;">; Wait till the agents finish</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>download-all2 partitioned-data<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>What did I just do? I simply partitioned the data set by the number of parallel downloads I wanted to do, and then modified the download-all function to take the partitioned data, dispatch agents on one partition and wait for them to finish, and then move on to the next partition.</p>
<p>Simple, yet very beautiful.</p>
]]></content:encoded>
			<wfw:commentRss>http://freegeek.in/blog/2009/10/downloading-a-bunch-of-files-in-parallel-using-clojure-agents/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Setting up Emacs &amp; Clojure with Emacs Starter Kit</title>
		<link>http://freegeek.in/blog/2009/08/setting-up-emacs-clojure-with-emacs-starter-kit/</link>
		<comments>http://freegeek.in/blog/2009/08/setting-up-emacs-clojure-with-emacs-starter-kit/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 21:21:55 +0000</pubDate>
		<dc:creator>Baishampayan</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[jvm]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://freegeek.in/blog/?p=63</guid>
		<description><![CDATA[Note: This blog post is no longer accurate as a lot of things have changed since I wrote this post. Please check this document for working instructions. Clojure is a very modern functional programming language which runs on the JVM. It&#8217;s a very cleanly designed Lisp dialect and has all the features that any useful [...]]]></description>
			<content:encoded><![CDATA[<p><ins><strong>Note:</strong> This blog post is no longer accurate as a lot of things have changed since I wrote this post. Please check <a title="Clojure Project on Assembla" href="http://www.assembla.com/wiki/show/clojure/Getting_Started_with_Emacs">this document</a> for working instructions.</ins></p>
<p><a href="http://clojure.org">Clojure</a> is a very modern functional programming language which runs on the <a href="http://en.wikipedia.org/wiki/JVM">JVM</a>. It&#8217;s a very cleanly designed <a href="http://en.wikipedia.org/wiki/Lisp_(programming_language)">Lisp</a> dialect and has all the features that any useful programming language would require. You can learn more about Clojure on the official website, <a href="http://en.wikibooks.org/wiki/Clojure_Programming">the wikibook</a>, <a href="http://java.ociweb.com/mark/clojure/article.html">a comprehensive article by R. Mark Volkmann</a> or by buying <a href="http://www.amazon.com/Programming-Clojure-Pragmatic-Programmers-Halloway/dp/1934356336/ref=sr_1_1?ie=UTF8&amp;qid=1249074313&amp;sr=8-1">the awesome book by Stuart Halloway</a>.</p>
<p>Now to setup <a href="http://en.wikipedia.org/wiki/Emacs">Emacs</a> as a Clojure IDE. If you are new to Emacs then don&#8217;t worry as we will take the easiest way to set it all up. If you are a Emacs veteran, then keep your own dotemacs and the bunch of customisations that you have done in all these years aside and follow along.</p>
<p>Installing Emacs is easy. On an <a href="http://ubuntu.com">Ubuntu</a> system, do this -</p>
<p><code>$ sudo apt-get install emacs-snapshot-gtk</code></p>
<p>That will install Emacs 23 for you. You will also need a few other things -</p>
<p><code>$ sudo apt-get install sun-java6-jdk ant git-core</code></p>
<p>You will need the JDK and <a href="http://ant.apache.org/">Ant</a> to build and run Clojure (it runs on the JVM, remember?) and <a href="http://git-scm.com/">Git</a> to fetch the Clojure an other related libraries.</p>
<p>Now to setup Emacs, we will use a brilliant set of Emacs Lisp libraries aptly named <a href="http://github.com/technomancy/emacs-starter-kit/">Emacs Starter Kit</a> (ESK). To get it, do this -</p>
<p><code>$ git clone git://github.com/technomancy/emacs-starter-kit.git ~/.emacs.d</code></p>
<p>That will get the ESK and will put it in your <code>~/.emacs.d</code> (existing Emacs users, please move your <code>~/.emacs</code> and <code>~/.emacs.d</code> to some other location before doing this. You can get back your old settings later).</p>
<p>What ESK does is that it provides a bunch of sane defaults for pretty much everything in Emacs. You can safely use it and get a very usable Emacs setup almost immediately.</p>
<p>You can now launch Emacs by going to <code>Applications &gt; Accessories</code> and selecting Emacs Snapshot Gtk. Once Emacs has started up, install the Emacs Clojure libraries by typing <code>M-x package-list-packages</code>. That will start up a buffer with a bunch of Emacs packages. Go to the line that says <code>clojure-mode</code> in it and press <code>i</code>. Then press <code>x</code> to install the package. Once that&#8217;s done you are ready to install Clojure. Now type <code>M-x clojure-install</code> and it will prompt you for a directory to install Clojure. Just press Enter for now and let it proceed. You can always change these things later.</p>
<p>That will fetch the Clojure runtime and also a few other useful libraries like <code>clojure-contrib</code> and will install them inside <code>~/src</code>.</p>
<p>Once that is done, you can type <code>M-x slime</code> to start Clojure inside your Emacs. Play around with it and learn the various key-bindings for <a href="http://common-lisp.net/project/slime/">Slime</a> and <a href="http://www.emacswiki.org/emacs/ParEdit">Paredit</a> which is a fantastic system for doing Lisp development. Paredit is so good that it makes all those parentheses just vanish in thin air. But yes, it takes a bit getting used to which you can achieve with some practice. One tip is to type <code>C-h m</code> in Emacs to see the documentation of all the modes that are currently active. This gives a good overview of all the key-bindings and the functions bound to those keys.</p>
<p>To develop slightly more complex Clojure applications, you need to use different dependencies with your own code. Since Java has its own <a href="http://en.wikipedia.org/wiki/Classpath">classpath</a> related quirkiness it can become a slightly complicated task. But then, there is cool function in Emacs Starter Kit called <code>clojure-project</code> which will make your life easy.</p>
<p>To use it all you need to do is to follow this simple directory structure and the rest will be taken care of automagically. The directory structure should be like this -<br />
<code><br />
myproject<br />
|-- lib<br />
|-- src<br />
|-- target<br />
|   |-- classes<br />
|   `-- dependency<br />
`-- test<br />
</code></p>
<p>Copy the Clojure &amp; Clojure Contrib (or any other dependency) JAR files into <code>lib</code> (you also alternately use a build system like <a href="http://maven.apache.org/">Maven</a> to manage dependencies for you in which case you should consider configuring the build system to copy the dependencies into <code>target/dependency</code>). Keep all your code inside <code>src</code> and tests inside <code>test</code>. The <code>src</code> and <code>test</code> directories should contain namespaces like <code>foo.bar</code> &amp; <code>foo.bar.test</code> respectively (you can later use <code>clojure-test-mode</code> to automatically pick up the tests and run them quickly). So when you run <code>M-x clojure-project</code>, it will ask you for a directory name. Give it the name of the project directory and boom! Emacs will launch Slime with the right classpath settings.</p>
<p>If you want to customise any Emacs setting, just create a file with your current system login id called <code>&lt;username.el&gt;</code> inside <code>~/.emacs.d</code> and it will be automatically loaded when you restart Emacs. If you use additional libraries, create a directory with your username inside the same directory and everything inside will be in the Emacs load-path. Nifty, right?</p>
<p>So that&#8217;s all required to setup Emacs for Clojure. Join the Clojure <a href="http://groups.google.com/group/clojure">mailing list</a> to communicate with the Clojure community. Do check the Emacs <a href="http://emacswiki.org">Wiki</a> in case you have any queries about Emacs.</p>
<p>Above all, have fun. That&#8217;s what we are here for.</p>
<p>By the way, I am <a href="http://twitter.com/ghoseb">@ghoseb</a> on Twitter and I think <a href="http://twitter.com/ghoseb">you should follow me</a> <img src='http://freegeek.in/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>UPDATE: Fixed a factual inconsistency regarding the paths used for <code>clojure-project</code>. Thanks to <a href="http://technomancy.us/">Phil</a> (the creator of ESK &amp; <code>clojure-project</code>) for pointing this out!</p>
]]></content:encoded>
			<wfw:commentRss>http://freegeek.in/blog/2009/08/setting-up-emacs-clojure-with-emacs-starter-kit/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.269 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-05-08 20:26:12 -->

