<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress.com" -->
<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/"
	>

<channel>
	<title>clojure &amp;laquo; WordPress.com Tag Feed</title>
	<link>http://en.wordpress.com/tag/clojure/</link>
	<description>Feed of posts on WordPress.com tagged "clojure"</description>
	<pubDate>Mon, 30 Nov 2009 12:45:06 +0000</pubDate>

	<generator>http://en.wordpress.com/tags/</generator>
	<language>en</language>

<item>
<title><![CDATA[Building a Clojure Web application with Incanter, Compojure, and Leiningen]]></title>
<link>http://incanter-blog.org/2009/11/29/incanter-webapp/</link>
<pubDate>Sun, 29 Nov 2009 23:04:15 +0000</pubDate>
<dc:creator>liebke</dc:creator>
<guid>http://incanter-blog.org/2009/11/29/incanter-webapp/</guid>
<description><![CDATA[This post will demonstrate how to build a simple Clojure Web application, with Compojure, that will ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>This post will demonstrate how to build a simple Clojure Web application, with Compojure, that will use Incanter to generate a sample from a normal distribution with parameters (size, mean, and standard deviation) provided by the user, and then return a PNG image of the histogram of the data. </p>
<p>This example is simple, but it demonstrates how to dynamically generate an Incanter chart and pass it directly to the Web client as a PNG image.</p>
<p>For more information on the  <a href="http://github.com/weavejester/compojure">Compojure</a> Web framework, visit its <a href="http://github.com/weavejester/compojure">Github repository</a>, its <a href="http://groups.google.com/group/compojure">Google group</a>, its <a href="http://en.wikibooks.org/wiki/Compojure">WikiBooks site</a>, and this <a href="http://groups.google.com/group/compojure/msg/9b9c01a737242b58">quick tutorial</a>.</p>
<p>I will use the  <a href="http://github.com/technomancy/leiningen">Leiningen</a> build tool to download dependencies, compile the application, and package it up as jar file. For more information on installing and using Leiningen, see my <a href="http://incanter-blog.org/2009/11/20/leiningen-clojars/">previous post</a>.</p>
<p>Let&#8217;s start off by creating a project directory, I&#8217;ll call it <code>incanter-webapp</code>, and give it a <code>src</code> subdirectory. Then create the following Leiningen <a href="https://gist.github.com/4ea90cd6a4abff160b94">project.clj</a> file in the project directory. </p>
<pre><code>(defproject incanter-webapp "0.1.0"
  :description "A simple Incanter web-app."
  :dependencies [[incanter "0.9.0"]
                 [org.clojars.ato/compojure "0.3.1"]]
  :main simple_web_app)</code></pre>
<p>This project file includes two dependencies, Incanter and Compojure, and indicates that the <code>-main</code> function for the project is in the file <code>simple_web_app.clj</code> in the <code>src</code> subdirectory of the project. </p>
<p>Next create the file called <a href="https://gist.github.com/01e4022f6fde08fc5f4b">simple_web_app.clj</a> in the <code>src</code> subdirectory. </p>
<pre><code>(ns simple-web-app
  (:gen-class)
  (:use [compojure]
        [compojure.http response]
        [incanter core stats charts])
  (:import (java.io ByteArrayOutputStream
                    ByteArrayInputStream)))</code></pre>
<p>Note that the namespace for this file is <code>simple-web-app</code> with hyphens instead of the underscores used in the file name or in the :main line of the <code>project.clj</code> file. </p>
<p>In Compojure, you need to define routes, which in this case associate the function <code>sample-form</code> with &#8220;/&#8221;, and the function <code>gen-samp-hist-png</code> with &#8220;/sample-normal&#8221;.</p>
<pre><code>(defroutes webservice
  (GET "/"
    sample-form)
  (GET "/sample-normal"
    (gen-samp-hist-png request
                       (params :size)
                       (params :mean)
                       (params :sd))))</code></pre>
<p>The function <code>gen-samp-hist-png</code> takes four arguments, a request object, the sample size, the population mean, and the population standard deviation, and then updates the Compojure response object to include an Incanter histogram of a sample from a normal distribution with the given parameters.</p>
<p>Now define the <code>gen-samp-hist-png</code> function; start by converting the string-arguments, passed in from the <code>params</code> object, into numbers.</p>
<pre><code>(defn gen-samp-hist-png
  [request size-str mean-str sd-str]
    (let [size (if (nil? size-str)
                 1000
                 (Integer/parseInt size-str))
          m (if (nil? mean-str)
              0
              (Double/parseDouble mean-str))
          s (if (nil? sd-str)
              1
              (Double/parseDouble sd-str)))</code></pre>
<p>Each argument has a default value, 1000 for size, 0 for mean, and 1 for the standard deviation (sd).</p>
<p>Next generate the sample from the normal distribution using Incanter's <code>sample-normal</code> function with the given size, mean, and standard deviation, and then create a histogram of the resulting data.</p>
<pre><code>
          samp (sample-normal size
                              :mean m
                              :sd s)
          chart (histogram
                  samp
                  :title "Normal Sample"
                  <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_mad.gif' alt=':x' class='wp-smiley' /> -label (str "sample-size = " size
                                ", mean = " m
                                ", sd = " s))</code></pre>
<p>I've used the <code>x-label</code> option of the <code>histogram</code> function to customize the x-axis label so that it includes the given sample size, mean, and standard deviation.</p>
<p>Next, we need to convert the histogram into a byte array, and feed that into a <code>ByteArrayInputStream</code> that can be passed to Compojure's <code>update-response</code> function, along with a header that sets the Content-Type to "image/png".</p>
<p><code>
<pre>
        out-stream (ByteArrayOutputStream.)
        in-stream (do
                    (save chart out-stream)
                    (ByteArrayInputStream.
                      (.toByteArray out-stream)))
        header {:status 200
                :headers {"Content-Type" "image/png"}}]
    (update-response request
                     header
                     in-stream)))</code></pre>
<p>The above code is the key to sending a dynamically generated chart directly to the client. In addition to a filename, an <code>OutputStream</code> can be passed to Incanter&#8217;s <code>save</code> function; in this case, we use a <code>ByteArrayOutputStream</code>, which is then converted to a byte array that is used to initialize the <code>ByteArrayInputStream</code> that is passed to <code>update-response</code>.</p>
<p>We can create a Web form for submitting requests to <code>/sample-normal</code>. The following function is a helper function that creates a simple html page skeleton. </p>
<pre><code>(defn html-doc
  [title &#38; body]
  (html
    (doctype :html4)
    [:html
      [:head
        [:title title]]
      [:body
       [:div
	[:h2
	 [:a {:href "/"}
          "Generate a normal sample"]]]
        body]]))</code></pre>
<p>Now define the <code>sample-form</code> function, which will generate a Web form using <code>html-doc</code>.  This function was associated with &#8220;/&#8221; earlier using the <code>defroutes</code> macro.</p>
<pre><code>(def sample-form
  (html-doc "sample-normal histogram"
    (form-to [:get "/sample-normal"]
      "sample size: " (text-field {:size 4} :size)
      "mean: " (text-field {:size 4} :mean)
      "sd: " (text-field {:size 4} :sd)
      (submit-button "view"))))</code></pre>
<p>Now, we need to create a <code>-main</code> function that will be called when the <code>incanter-webapp.jar</code> is executed. This function will call Compojure&#8217;s <code>run-server</code> function, starting the built-in Jetty server on port 8080.</p>
<pre><code>(defn -main [&#38; args]
  (run-server {:port 8080}
    "/*" (servlet webservice)))</code></pre>
<p>Now we can use Leiningen to download and install all the necessary dependencies, including Incanter and Compojure, and then build and package the app.</p>
<pre><code>$ lein deps
$ lein compile
$ lein uberjar</code></pre>
<p>Finally, to start the server, run</p>
<pre><code>$ java -jar incanter-webapp.jar </code></pre>
<p>and visit <code>http://localhost:8080</code> to submit a request, or go directly to the sample-normal app by constructing a URL like the following:</p>
<pre><code>http://localhost:8080/sample-normal?size=500&#38;mean=100&#38;sd=10</code></pre>
<p>The complete code for this example can be found <a href="https://gist.github.com/01e4022f6fde08fc5f4b">here</a>, and the Leiningen project file can be found <a href="https://gist.github.com/4ea90cd6a4abff160b94">here</a>.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Emacs, Clojure, and Japanese]]></title>
<link>http://vaelen.org/2009/11/28/emacs-clojure-and-japanese/</link>
<pubDate>Sat, 28 Nov 2009 07:26:04 +0000</pubDate>
<dc:creator>Andrew</dc:creator>
<guid>http://vaelen.org/2009/11/28/emacs-clojure-and-japanese/</guid>
<description><![CDATA[This might be proof that I&#8217;m crazy: I&#8217;m working on a project for my NLP class that invol]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>This might be proof that I&#8217;m crazy:</p>
<p><a href="http://vaelen.wordpress.com/files/2009/11/emacs-jp-clojure.png"><img class="alignnone size-full wp-image-121" title="Emacs, Clojure, and Japanese" src="http://vaelen.wordpress.com/files/2009/11/emacs-jp-clojure.png" alt="" width="568" height="356" /></a></p>
<p><!--more--></p>
<p>I&#8217;m working on a <a href="http://github.com/vaelen/jpdv">project</a> for my NLP class that involves generating a <a href="http://en.wikipedia.org/wiki/Vector_space_model" target="_blank">semantic vector space</a> for Japanese text, and I decided that this might be a good time to learn one of the LISP dialects.  I&#8217;ve been looking at <a href="http://clojure.org/" target="_blank">Clojure</a> for a while now, but I hadn&#8217;t taken the time to learn it before.  I must say, I&#8217;m quite impressed so far.  The fact that reading a Japanese XML document into a data structure &#8220;just works&#8221; without any tweaking is pretty nice.  I&#8217;m still coming to grips with functional programming, but I&#8217;m liking it so far.  And the best thing as far as I&#8217;m concerned is that Clojure code can easily be made <a href="http://clojure.org/concurrent_programming" target="_blank">massively parallel</a> thanks to all of its data types being immutable (ala Erlang).  That will help me out a lot when I need to run my code on multi-core / multi-processor machines (which I often have to do because of the sheer amount of data being crunched.)</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Optimising Mandelbrot]]></title>
<link>http://developmentjungle.wordpress.com/2009/11/24/optimising-mandelbrot/</link>
<pubDate>Tue, 24 Nov 2009 22:55:31 +0000</pubDate>
<dc:creator>alex</dc:creator>
<guid>http://developmentjungle.wordpress.com/2009/11/24/optimising-mandelbrot/</guid>
<description><![CDATA[In my previous post I showed how I created a Mandelbrot fractal renderer in Clojure, however its per]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><img src="http://developmentjungle.wordpress.com/files/2009/11/clojure-mandelbrot-set-preview.png" alt="" title="Clojure Mandelbrot Set" width="222" height="155" class="alignright size-full wp-image-15" />In my previous post I showed how I created a <a href="/2009/11/14/rendering-the-mandelbrot-set-in-clojure/">Mandelbrot fractal renderer in Clojure</a>, however its performance left a lot to be desired. Optimising code is actually a very good way to learn more about a new language as it teaches you what is going on under the hood. In this post, I will go through the steps I took to drastically improve the application&#8217;s performance.</p>
<h2>Inspecting the Code</h2>
<p>The first step when optimising code (after <a href="http://c2.com/cgi/wiki?PrematureOptimization"><em>not</em> optimising</a> of course) is to inspect the code&#8217;s logic and find quick fixes to any obvious potential performance areas. The logic of the Mandelbrot code however is fairly easy to reason about as we have built it up from the mathematical formula. Also, looking for obvious performance issues is difficult in a language you are trying to learn, so it&#8217;s time to break out the tools.<br />
<!--more--></p>
<h2>Profiling</h2>
<p>First of all, let&#8217;s see how long it takes to render our fractal with the current implementation:</p>
<pre class="brush: javafx;">
user=&#62; (time (my-mandelbrot))
&#34;Elapsed time: 29340.692742 msecs&#34;
nil
</pre>
<p>Note, I have undone the premature optimisation I made last time and made the rendering single threaded by replacing <code>pmap</code> with <code>map</code>. This is important when profiling as it ensures we can properly determine which parts of the code are taking the longest. On a quad core cpu, I found that using <code>pmap</code> gave only a 2x speed increase which shows there is quite a lot of overhead in Clojure when processing on multiple threads.</p>
<p>Now at this stage, we have no idea what is taking so long so let&#8217;s run the app through a profiler. As Clojure is hosted on the JVM, it allows us to take advantage of many tools that would normally be used for Java applications. I attached the <a href="https://visualvm.dev.java.net/">VisualVM</a> profiling tool (which now comes with Java 6) to my application and then called <code>my-mandelbrot</code> again from the REPL. This is what I got after running for a few minutes:</p>
<p><img class="aligncenter size-full wp-image-55" title="VisualVM profile of render" src="http://developmentjungle.wordpress.com/files/2009/11/profile-render.png" alt="" width="623" height="298" /></p>
<p>What strikes you when digging through a Clojure call tree is just how deep it can go. It can also be difficult to relate the Java method calls we see back to the original Clojure code. Here I have expanded the calls made within the <code>render</code> function. You can see the two main consumers of CPU time are calls to <code>count</code> and <code>vec</code>. This is surprising as if you have a look at the original <code>render</code> function code, there&#8217;s not a call to either the <code>count</code> or <code>vec</code> functions to be seen:</p>
<pre class="brush: javafx;">
(defn render [xstart ystart xsize ysize width height max-iters wr]
  (dorun
    (map (fn [pixel]
            (let [[x y] pixel]
              (.setPixel wr x y
                (int-array (coord-colour
                  (get-coord (double x) (double y) xstart ystart xsize ysize width height)
                  max-iters)))))
           (get-pixels width height))))
</pre>
<p>In fact, <code>count</code> is called from within the <code>coord-colour</code> function:</p>
<pre class="brush: javafx;">
(defn coord-colour
  &#34;Returns a colour for which to draw the given coordinate. If the coordinate
  is within the mandelbrot set, black is returned. Otherwise, a colour within
  a gradient is given based on the number of iterations of the mandelbrot set
  that have been evaluated.&#34;
  [[xcoord ycoord] max-iters]
  (let [num-iters (count (take max-iters (take-while #(&#60;= (mag %) 4) (mandelformula xcoord ycoord))))]
    (if (= max-iters num-iters)
      *set-colour*
      (iter-colour num-iters max-iters))))
</pre>
<p>The call to <code>vec</code> is deeper still within the calls to <code>iter-colour</code> and <code>grad-colour</code>:</p>
<pre class="brush: javafx;">
(defn grad-colour
  &#34;Returns the colour that is the given fraction of the way between
  the first and second colours given. Returns as a vector of three
  integers between 0 and 255.&#34;
  [colA colB frac]
  (vec (map #(+ (* frac (- %2 %1)) %1) colA colB)))

(defn iter-colour
  &#34;Returns the colour needed to paint a point with the given number
  of iterations&#34;
  [num-iters max-iters]
  (grad-colour *grad-colour-a* *grad-colour-b*
    (/ (double num-iters) max-iters)))
</pre>
<p>The reason we see the <code>count</code> and <code>vec</code> calls at the level of the <code>render</code> function is that Clojure (or perhaps the hotspot) appears to inline certain function calls into the calling function. You can see further evidence of this in the above screenshot if you have a look at the <code>divide</code>, <code>add</code> and <code>multiply</code> methods. These are the operations performed by the <code>get-coord</code> function:</p>
<pre class="brush: javafx;">
(defn get-coord
  &#34;Returns the coordinates of the given pixel in the complex plane&#34;
  [x y xstart ystart xsize ysize width height]
  [(+ xstart (* (/ x width) xsize))
   (+ ystart (* (/ y height) ysize))])
</pre>
<p>You should also see that at the time of the snapshot I took above, the number of invocations of each of the mathematical operations was around 67k which is twice the number of invocations of the <code>count</code> function which makes sense as you can see they are called once for each of the x and y coordinates.</p>
<p>Let&#8217;s dig deeper into the <code>count</code> function.</p>
<p><a href="http://developmentjungle.wordpress.com/files/2009/11/profile-count.png"><img class="aligncenter size-full wp-image-57" title="VisualVM profile of count (click to enlarge)" src="http://developmentjungle.wordpress.com/files/2009/11/profile-count-crop.png" alt="" width="632" height="381" /></a></p>
<p>As you expand the call tree, and if you ignore the <code>clojure.lang</code> methods, you&#8217;ll see calls to <code>take</code>, <code>take-while</code>, <code>iterate</code> and finally <code>mandelformula</code>. Here&#8217;s another mystery of the Clojure call stack: we have a call to <code>iterate</code> before the call to <code>mandelformula</code> and yet in our code we can see iterate is called <em>within</em> the <code>mandelformula</code> function.</p>
<pre class="brush: javafx;">
(defn mandelformula [xcoord ycoord]
  &#34;Returns an infinite sequence of vectors containing the values of
  successive iterations of the mandelbrot formula, given a point on
  the complex plane.&#34;
  (iterate
    #(vec (map + (mandel %) [xcoord ycoord]))
    [xcoord ycoord]))
</pre>
<p>As far as I can tell, the reason for this is that <code>mandelformula</code> returns a lazy sequence which is not actually evaluated until it is needed. So <code>iterate</code> is used to get the next value in the sequence, while <code>mandelformula</code> actually does the job of calculating what the next value should be.</p>
<p><a href="http://developmentjungle.wordpress.com/files/2009/11/profile-mandelformula.png"><img class="aligncenter size-full wp-image-59" title="Visual VM profile of mandelformula (click to enlarge)" src="http://developmentjungle.wordpress.com/files/2009/11/profile-mandelformula-crop.png" alt="" width="659" height="396" /></a></p>
<p>Drilling down into the <code>mandelformula</code> function we open up a whole can of method calls and it gets suddenly very difficult to map back to the original Clojure code. However we can see there seem to be a large number of sequences created and calls to methods in the <code>clojure.lang.Numbers</code> class taking <code>Objects</code> as their parameters. If we could get the formula to only use primitive types, this could potentially remove a lot of object creation and garbage collection. However, before we start re-working the code, let&#8217;s look at another profiling technique available to us in Clojure.</p>
<h2>Using Clojure Contrib&#8217;s Profile Macros</h2>
<p><a href="http://stuartsierra.com/">Stuart Sierra</a> has made a vast number of contributions to Clojure including the <a href="http://richhickey.github.com/clojure-contrib/profile-api.html">profile</a> library which provides two macros <code>prof</code> and <code>profile</code> to measure the execution time of any block of code. Clojure&#8217;s macro system makes what would normally be a cumbersome task in other languages extremely easy. Let&#8217;s put profiling in our <code>coord-colour</code> function:</p>
<pre class="brush: javafx; highlight: [3,4];">
(defn coord-colour
  [[xcoord ycoord] max-iters]
  (let [num-iters (prof :mandel (count (take max-iters (take-while #(&#60;= (mag %) 4) (mandelformula xcoord ycoord)))))]
    (prof :colour (if (= max-iters num-iters)
      *set-colour*
      (iter-colour num-iters max-iters)))))
</pre>
<p>We simply wrap the block of code we want to profile around a call to the <code>prof</code> macro and specify a keyword to identify that block of code. Calling the <code>profile</code> macro will then summarise the time taken to evaluate each call to those <code>prof</code> blocks and print a bunch of statistics.</p>
<pre class="brush: javafx;">
user=&#62; (time (profile (my-mandelbrot)))
  Name      mean       min       max     count       sum
colour      4875      1117   1091200    300000  1462510112
mandel     71729      2514 100182286    300000 21518722246
&#34;Elapsed time: 31456.673404 msecs&#34;
</pre>
<p>Here we can see that the total time taken by the <code>mandel</code> section of the <code>coord-colour</code> function is around 21 seconds (the values above are in nanoseconds) which gives it 68% of the total running time. The time taken to calculate the gradient colour is about 5% of the total running time. These are quite different statistics from what we saw with VisualVM which gave figures of 60% for the <code>count</code> function and 20% for the calls to <code>vec</code>. The discrepancy is most likely due to the fact that the program did not run to completion when I profiled it with VisualVM. The time taken by the <code>count</code> function depends very much on the number of iterations it executes and during the first 25% of the render, most of its calls finish within 4-5 iterations.</p>
<p>In any case, now that we know the source of the slow performance, let&#8217;s turn our attention to optimisation.</p>
<h2>Back to Primitives</h2>
<p>If we want to eliminate boxed numbers while calculating each term of the Mandelbrot formula, then unfortunately that means we will also have to stop being lazy. It&#8217;s not possible to create a lazy sequence of primitives (just yet). This means we will need to calculate all the terms of the Mandelbrot formula in a single function before returning the number of iterations taken.</p>
<pre class="brush: javafx;">
(defn mandelformula [x0 y0 max-iters]
  &#34;Applies the mandelbrot formula until max-iters iterations
  are reached, or the magnitude of Z exceeds 2&#34;
  (let [x0 (double x0)
        y0 (double y0)
        max-iters (int max-iters)]
    (loop [x (double x0)
           y (double y0)
           n (int 0)]
      (if (== n max-iters)
        n
        (let [mag (+ (* x x) (* y y))]
          (if (&#62;= mag (double 4))
            n
            (let [new-x (+ x0 (- (* x x) (* y y)))
                  new-y (+ y0 (* (double 2) (* x y)))]
              (recur new-x new-y (inc n)))))))))
</pre>
<p>Here is the newly improved function. Note how I explicitly <a href="http://clojure.org/java_interop#toc37">coerce</a> the function parameters to doubles using a <code>let</code>. The intermediate values for each iteration are now defined in a <code>loop</code> and again explicitly coerced. It can be tricky in Clojure to force primitives &#8211; there&#8217;s no static type checking as in Java of course &#8211; but Clojure will throw an exception if you try to pass non-primitive values to a loop that uses coerced parameters. This helps detect the times when Clojure magically autoboxes your primitives behind your back. (One place where I tripped up was thinking that mathematical operators that take multiple arguments would return a primitive when passed primitives, in fact, (* (int 1) (int 2) (int 3)) will return an Integer whereas (* (int 1) (int 2)) returns an <code>int</code>. This is why the calculation of <code>new-y</code> above involves two calls to *).</p>
<p>In the above function, I&#8217;m taking care of calculating all the terms of the mandelbrot formula up to the point where <code>z</code> &#62; 2 or <code>max-iters</code> is reached. This is a bit more than the original <code>mandelformula</code> function so the <code>coord-colour</code> function simplifies to:</p>
<pre class="brush: javafx;">
(defn coord-colour
  &#34;Returns a colour for which to draw the given coordinate. If the coordinate
  is within the mandelbrot set, black is returned. Otherwise, a colour within
  a gradient is given based on the number of iterations of the mandelbrot set
  that have been evaluated.&#34;
  [[xcoord ycoord] max-iters]
  (let [num-iters (mandelformula xcoord ycoord max-iters)]
    (if (= max-iters num-iters)
      *set-colour*
      (iter-colour num-iters max-iters)
      )))
</pre>
<p>Let&#8217;s see what kind of performance we get now.</p>
<pre class="brush: javafx;">
user=&#62; (time (profile (my-mandelbrot)))
  Name      mean       min       max     count       sum
colour      5179      1117   1593220    300000  1553876060
mandel      1787      1396    137448    300000  536248035
&#34;Elapsed time: 10177.274766 msecs&#34;
</pre>
<p>Wow, that&#8217;s a 40x speed improvement for the <code>mandel</code> part of our code. This shows how significant keeping object allocations to a minimum can be. However, the two parts of the code we&#8217;re profiling only make up about 2 seconds out of a total of 10 seconds of runtime. Let&#8217;s move our profiling to the <code>render</code> function:</p>
<pre class="brush: javafx; highlight: [5,6,7];">
(defn render [xstart ystart xsize ysize width height max-iters wr]
  (dorun
    (map (fn [pixel]
            (let [[x y] pixel]
              (prof :setPixel (.setPixel wr x y
                (prof :int-array (int-array
                  (prof :coord-colour (coord-colour
                    (get-coord (double x) (double y) xstart ystart xsize ysize width height)
                    max-iters))))))))
           (get-pixels width height))))
</pre>
<p>And run the render again:</p>
<pre class="brush: javafx;">
user=&#62; (time (profile (my-mandelbrot)))
        Name      mean       min       max     count       sum
coord-colour      6632      3073   1690997    300000  1989613029
   int-array     11402      7263   1706641    300000  3420617714
    setPixel     30688     21790   2191620    300000  9206612546
&#34;Elapsed time: 11283.777407 msecs&#34;
</pre>
<p>You can see that <code>setPixel</code> is most costly function, but this is not really a surprise given the other two calls are nested within it. However, even taking the runtime of the nested calls into account, the <code>.setPixel</code> call is taking nearly 6 out of the total 11 seconds (the reason we have an extra 1s of runtime after this run is most likely due to the overhead of profiling another block of code). </p>
<h2>Removing Reflection</h2>
<p>We can see that <code>.setPixel</code> is called on a binding called <code>wr</code> but Clojure (nor us) has no idea what type wr might be. In fact Clojure has to use reflection to determine which method <code>.setPixel</code> refers to. Let&#8217;s turn on reflection warning and load the program again:</p>
<pre class="brush: javafx;">
Reflection warning, D:\dev\clojure\...\mandelbrot.clj:87 - call to setPixel can't be resolved.
Reflection warning, D:\dev\clojure\...\mandelbrot.clj:87 - call to setPixel can't be resolved.
Reflection warning, D:\dev\clojure\...\mandelbrot.clj:99 - call to drawImage can't be resolved.
Reflection warning, D:\dev\clojure\...\mandelbrot.clj:105 - call to setPreferredSize can't be resolved.
Reflection warning, D:\dev\clojure\...\mandelbrot.clj:106 - call to add can't be resolved.
Reflection warning, D:\dev\clojure\...\mandelbrot.clj:117 - reference to field getRaster can't be resolved.
Reflection warning, D:\dev\clojure\...\mandelbrot.clj:122 - reference to field repaint can't be resolved.
1:1 user=&#62;
</pre>
<p>We can see that with reflection warning turned on, Clojure complains that it cannot resolve the call to <code>setPixel</code>. We can fix this of course with a type hint, however we must also remember to coerce the parameters into the types expected by the <code>setPixel</code> method:</p>
<pre class="brush: javafx; highlight: [1,5];">
(defn render [xstart ystart xsize ysize width height max-iters #^WritableRaster wr]
  (dorun
    (map (fn [pixel]
            (let [[x y] pixel]
              (prof :setPixel (.setPixel wr (int x) (int y)
                (prof :int-array (int-array
                  (prof :coord-colour (coord-colour
                    (get-coord (double x) (double y) xstart ystart xsize ysize width height)
                    max-iters))))))))
           (get-pixels width height))))
</pre>
<p>Running the program again we notice the reflection warning has gone (the others remain but we don&#8217;t care about them as their calls are not made within a loop), and we get a fairly significant speed up:</p>
<pre class="brush: javafx;">
user=&#62; (time (profile (my-mandelbrot)))
        Name      mean       min       max     count       sum
coord-colour      6770      3072  108868865    300000  2031060305
   int-array     12263      6704  221714492    300000  3679041832
    setPixel     16247     10057  221746898    300000  4874399625
&#34;Elapsed time: 6739.067116 msecs&#34;
</pre>
<p>This now means that the <code>setPixel</code>, <code>int-array</code> and <code>coord-colour</code> sections now take 17%, 25% and 30% of the total elapsed time respectively. These proportions seem pretty even so it&#8217;s possible that our profiling representing a significant portion of that elapsed time. Let&#8217;s run again, this time without making the call to the <code>profile</code> macro:</p>
<pre class="brush: javafx;">
user=&#62; (time (my-mandelbrot))
&#34;Elapsed time: 1668.624085 msecs&#34;
</pre>
<p>That&#8217;s quite a big drop and it shows how significant the overhead of profiling can be but I&#8217;m hoping that this can be reduced with optimisations to the profiling macros themselves in the future. </p>
<p>We saw earlier how the section of code for calculating a colour gradient was taking about 75% of the time of the <code>mandelformula</code> function. It&#8217;s possible this function could be sped up with more primitive type arithmetic but let&#8217;s try a different, more functional method of optimisation.</p>
<h2>Memoization</h2>
<p>The <code>iter-colour</code> function takes a number between 1 and max-iter and returns a corresponding colour. The fact that there is only a small, fixed number of possible inputs to the function and the output is always the same for a given input, makes it a perfect candidate for <a href="http://clojure.org/api#toc370">memoization</a>. We can simply redefine the function as a memoized version of itself:</p>
<pre class="brush: javafx;">
user=&#62; (def iter-colour (memoize iter-colour ))
#'user/iter-colour
user=&#62; (time (my-mandelbrot))
&#34;Elapsed time: 954.501759 msecs&#34;
</pre>
<p>We have now managed to improve overall performance by over 30 times. It&#8217;s possible we could repeat these techniques such as inlining the get-pixels function or making the get-coord function use primitives to get a further performance boost. However, as cgrand says, <a href="http://clj-me.cgrand.net/2009/11/18/an-optimization-job-is-never-done/">an optimisation job is never done</a> and for now I believe the performance is &#8220;good enough&#8221;.</p>
<p>The full optimised code can be found <a href="http://github.com/alexspurling/clojure-projects/blob/master/Mandelbrot/src/fast-mandelbrot.clj">on github</a>. Please feel free to submit your own performance optimisations to improve the code even further <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>We&#8217;ve seen how to use profiling tools to peer beneath Clojure&#8217;s dynamic lispy surface and used this knowledge to dramatically improve the performance of our fractal renderer by taking advantage of function inlining, primitive coersions, elimination of reflection and memoization. Hopefully these techniques will be useful to you when improving the performance of your own Clojure programs!</p>
<h3>References</h3>
<p>Inspiration on how to write a Mandelbrot fractal renderer the &#8216;right&#8217; way: <a href="http://briancarper.net/tag/mandelbrot">http://briancarper.net/tag/mandelbrot</a><br />
Performance tips on Clojure: <a href="http://devlog.bigmonachus.org/2009/03/performance-tips-for-clojure.html">http://devlog.bigmonachus.org/2009/03/performance-tips-for-clojure.html</a></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Dynamic Offline Reports]]></title>
<link>http://developeraspirations.wordpress.com/2009/11/23/dynamic-offline-reports/</link>
<pubDate>Tue, 24 Nov 2009 00:38:41 +0000</pubDate>
<dc:creator>jearil</dc:creator>
<guid>http://developeraspirations.wordpress.com/2009/11/23/dynamic-offline-reports/</guid>
<description><![CDATA[Many applications have the primary concern of storing and retrieving data. The raw data by itself is]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Many applications have the primary concern of storing and retrieving data. The raw data by itself is often not very useful, so an additional process is put into place to turn that data into useful information. Many applications generate these reports from the data quickly at the user&#8217;s request through either a narrow SQL select statement, in application data processing, or both. However, in larger applications where the data is too large to handle in memory at a time, processing is too heavy, and report customization is too varied, information generation needs to be pushed to its own scalable system.</p>
<p>A lot of data has various pieces of metadata associated with it naturally. Data such as what user added the record, the date it was added or modified (or both), maybe the size of the record, categories, tags, keywords, or other pieces of associate data that is used to break it up into more manageable chunks. This meta data is useful, but only if we can use it as an aggregate to generate specific information relating to the grouping of items based on this data.</p>
<p>Sometimes generating these more specific reports is as easy as adding additional WHERE or GROUP BY clauses in SQL. However, when more advanced business rules are taking place where there isn&#8217;t an easy or succinct way of extracting this information via a query, or if the query returns such a large amount of data as to cause memory issues, a different approach can be taken.</p>
<p>For instance; in an application I am currently working on we need to generate reports based on a table with about 5 million rows. The SQL queries we use can limit the amount of rows returned to perhaps a few hundred thousand for some of our larger reports. However, a lot of the data needs to be processed in application code rather than by the database itself due to some special business rules. Because of this, we end up creating a large number of objects in Java to hold these result rows. If multiple users are generating different reports we might end up holding too many of these objects in memory at a time and receive an OOM error. Also, the processing on this data can be intense enough that if the server is slammed with report requests that the entire system slows down, causing difficulties for people wanting to insert or modify data. This is the case I am in while I contemplate offline report generation.</p>
<p>The basic idea is that the main application should be concerned purely with manipulating the data of the system. That is basic CRUD stuff such as creating new records, updating them, the rare deletions, and showing single records to the user (so they can edit or delete it). We want that part of the application to remain fast, and not be effected by the purely read-only needs imposed by report generation. In order to nullify the impact, we move reporting to its own system that reads from a separate read-only replication of our production database.</p>
<p>When a report request comes in to our application, we send a request to the separate reporting system. This can be done either as a web service or maybe an RPC call. The reporting system uses its read-only copy of the data to generate the report numbers and send it back, causing no speed delay for insertion or regular operation of the main application.</p>
<p>This doesn&#8217;t solve our OOM issues however, as many drivers for our database (MySQL) return ResultSet objects with the entire contents of the results which might be too large to fit into memory. However, since we&#8217;re using a read-only list anyway we can convert the table or tables we use to process our results into flat files that can be read in on a line by line basis, perform some intermediate result processing, deallocate those lines and work on additional lines. Since our reports are mostly generating statistical data over a large data set, we can process results on that data set in parallel using multiple threads or possibly multiple computers using a Hadoop cluster.</p>
<p>By making report generation asynchronous to our applications general work flow we will free up the processing power and the connection pool that&#8217;s used to handle requests by asking users to either poll for when the result is finished or to notify the system when a report is finished and thereby avoid the instances where we use all of our available connections or resources processing reports. There is also the added possibility of continuously generating all possible report data on a separate machine or cluster to decrease access time by increasing storage requirements.</p>
<p>I&#8217;m currently researching the use of MapReduce in Hadoop for processing this flat file of all data for reports. In addition, I&#8217;m researching a few languages that are reported to be good at concurrent processing so that I can benefit from multiple cores when generating reports from our raw data. My current focus is on Scala, Erlang, and Clojure, but I do not have much to report on those areas yet. If anyone has any information those languages as far as report generation based on a largish data set (currently 5 million, but the rate of growth is fairly alarming), let me know.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Editors]]></title>
<link>http://ossareh.wordpress.com/2009/11/23/editors/</link>
<pubDate>Mon, 23 Nov 2009 23:25:18 +0000</pubDate>
<dc:creator>ossareh</dc:creator>
<guid>http://ossareh.wordpress.com/2009/11/23/editors/</guid>
<description><![CDATA[My previous post on the state of affairs with Eclipse garnered some interest. I&#8217;ve continued t]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>My previous post on the state of affairs with Eclipse garnered some interest. I&#8217;ve continued to explore the editor scene. Back in the very late 90&#8217;s and early 00&#8217;s I really started playing around with linux, at the time there was emacs and vim. Interestingly that hasn&#8217;t really changed. Back then I chose the way to be VI / VIM. Until getting into the world of lisp I hadn&#8217;t looked back. Clojure has me pretty deeply interested in Emacs.</p>
<p>Emacs has shown me a different way of looking at editors. It is so highly scriptable / customizable that I am slightly peeved it took me so long to start experimenting. Sure VI has a bunch of cool features, it is powerful, etc, but compared to Emacs it is like night vs day. That is not to say that Emacs is &#8220;better&#8221; than VIM &#8211; but it is a very different beast.</p>
<p>Indentation is a problem though. Over the last few weeks I threw myself head first into Emacs, I&#8217;m not good at learning any other way, and now I&#8217;m coming up against barriers in my work flow. I prefer the VIM way of letting you control your indentation. Emacs, thus far, makes too many assumptions about how you want your code to look, for LISP it nails it &#8211; but for java, javascript, html, css, etc it really doesn&#8217;t need to do anything more than highlight my text in the appropriate manner.</p>
<p>Going forth I think most programming takes place in VIM, lispy stuff takes place in emacs. The limited capacity of my brain will just have to delete everything to do with Eclipse / Netbeans / IntelliJ. Only really refactoring loses out here.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Running Programming Clojure Examples on Clojure Box]]></title>
<link>http://breadthfirst.wordpress.com/2009/11/23/running-programming-clojure-examples-on-clojure-box/</link>
<pubDate>Mon, 23 Nov 2009 18:35:51 +0000</pubDate>
<dc:creator>Dan</dc:creator>
<guid>http://breadthfirst.wordpress.com/2009/11/23/running-programming-clojure-examples-on-clojure-box/</guid>
<description><![CDATA[Stuart Halloway&#8217;s Programming Clojure book is the only game in town for learning Clojure (at l]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Stuart Halloway&#8217;s <a href="http://www.pragprog.com/titles/shcloj/programming-clojure">Programming Clojure</a> book is the only game in town for learning Clojure (at least until Luke VanderHart&#8217;s Practical Clojure is released), and <a href="http://clojure.bighugh.com/">Clojure Box</a> is the easiest way to get Clojure running on Windows with a good IDE, if you don&#8217;t already have cygwin installed, but they don&#8217;t quite play nicely out of the box.</p>
<p>Craig McDaniel has <a href="http://www.mail-archive.com/clojure@googlegroups.com/msg13563.html"> some generally useful code</a> for Emacs/Clojure, and that&#8217;s almost enough to get things working. With the following changes incorporated, all of the examples work for me:</p>
<pre>
(<font color="#a020f0">defun</font> <font color="#0000ff">reset-swank</font> ()
  <font color="#8b2252">"Because changing swank-clojure-extra-classpaths is not enough to force a new instance of slime to use it."</font>
  (interactive)
  (setq slime-lisp-implementations
        (assq-delete-all 'clojure slime-lisp-implementations))
  (add-to-list 'slime-lisp-implementations
               `(clojure ,(swank-clojure-cmd) <font color="#7a378b">:init</font> swank-clojure-init) t))

(<font color="#a020f0">defun</font> <font color="#0000ff">run-slime</font> (dir)
  (interactive <font color="#8b2252">"Project directory: "</font>)
  (cd dir)
  (<font color="#a020f0">when</font> (file-directory-p <font color="#8b2252">"~/.clojure"</font>) (directory-files <font color="#8b2252">"~/.clojure"</font> t <font color="#8b2252">".jar$"</font>))
    (make-directory <font color="#8b2252">"classes"</font>))
  (setq swank-clojure-extra-classpaths '(<font color="#8b2252">"src"</font> <font color="#8b2252">"classes"</font> <font color="#8b2252">"lib/*"</font> <font color="#8b2252">""</font>))
  (reset-swank)
  (slime))</pre>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Clojure: Return values]]></title>
<link>http://dahliabock.wordpress.com/2009/11/22/clojure-return-values/</link>
<pubDate>Sun, 22 Nov 2009 14:10:28 +0000</pubDate>
<dc:creator>Dahlia Bock</dc:creator>
<guid>http://dahliabock.wordpress.com/2009/11/22/clojure-return-values/</guid>
<description><![CDATA[Like in Ruby, there is no explicit call in a function to return values, the last expression of a fun]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Like in Ruby, there is no explicit call in a function to return values, the last expression of a function is evaluated and used as its return value.</p>
<p>Consider the following 2 functions:</p>
<pre class="textmate-source blackboard"><span class="text text_plain"><span class="meta meta_paragraph meta_paragraph_text">(defn hello-with-println [name]
</span>    <span class="meta meta_paragraph meta_paragraph_text">(println "Hello," name))
</span>    

<span class="meta meta_paragraph meta_paragraph_text">(defn hello-with-str [name]
</span>    <span class="meta meta_paragraph meta_paragraph_text">(str "Hello, " name))</span></span></pre>
<p>The first function when evaluated on the REPL (read-eval-print-loop), which is an interactive programming environment, returns the following:</p>
<pre class="textmate-source blackboard"><span class="text text_plain">
user=&#62; (hello-with-println "punk")
Hello, punk
nil</span></pre>
<p>The second function returns:</p>
<pre class="textmate-source blackboard"><span class="text text_plain">
user=&#62; (hello-with-str "munk")
"Hello, munk"
</span></pre>
<p>At first glance, these two functions seem to be doing almost the same thing &#8211; takes in a string input, which is a name, and outputs a string that says Hello to that name. However, <code>hello-with-println</code> returns <code>nil</code> while <code>hello-with-str</code> returns the evaluation of the <code>str</code> function.</p>
<p>This can be proven further with special variables in the REPL. When working within the REPL, the results of evaluating the three most recent expressions are stored in the special variables <code>*1</code>, <code>*2</code> and <code>*3</code>. If I call <code>hello-with-println</code> twice with the inputs &#8220;munk&#8221; and &#8220;punk&#8221; and use the special variables *1 and *2 to get the results of those expressions, I get the following:</p>
<pre class="textmate-source blackboard"><span class="text text_plain">
user=&#62; (str *1 " and " *2)
" and "
</span></pre>
<p>If I do the same with <code>hello-with-str</code>, I get the following output:</p>
<pre class="textmate-source blackboard"><span class="text text_plain">
user=&#62; (str *1 " and " *2)
"Hello, punk and Hello, munk"
</span></pre>
<p>When <code>hello-with-println</code> was used, there were no return values so the special variables were empty unlike in <code>hello-with-str</code>.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Between thought and expression]]></title>
<link>http://transfixedbutnotdead.com/2009/11/21/between-thought-and-expression/</link>
<pubDate>Sat, 21 Nov 2009 10:38:42 +0000</pubDate>
<dc:creator>draegtun</dc:creator>
<guid>http://transfixedbutnotdead.com/2009/11/21/between-thought-and-expression/</guid>
<description><![CDATA[Comparing computer languages can usually be interesting, sometimes informative, often amusing and in]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Comparing computer languages can usually be interesting, sometimes informative, often amusing and in lots of cases just damn annoying!</p>
<p>Still this recent <a href="http://news.ycombinator.com/item?id=881642">Hacker News post</a> about <a href="http://www.bestinclass.dk/index.php/2009/10/python-vs-clojure-evolving/">Python vs Clojure</a> did catch my eye</a>.</p>
<p>Despite the fact the author of the blog post had pulled the Python code from <a href="http://pyeuler.wikidot.com/problems-1-10">answers</a> to <a href="http://projecteuler.net">Project Euler</a> the pythonista&#8217;s quite correctly cried foul because it wasn&#8217;t well written idiomatic python example and so was an unfair comparison.</p>
<p>There&#8217;s a lot of code out there posted on those intertubes over the years.  However languages and practises evolve but unfortunately intertube posts can remain set in stone!  Perl is often weighed down by its past history on the intertubes.  One good way forward is just to produce more Modern Perl posts.</p>
<p>Anyway i digress, so moving on lets take a look at the <a href="http://projecteuler.net/index.php?section=problems&#38;id=4">Euler 4 &#8211; Finding Palindroms</a>.  This was the Clojure example that was put forward in the blog post:</p>
<pre class="brush: plain;">
(reduce max
    (filter #(let [s (str %)]
               (= (seq s) (reverse s)))
            (for [x (range 100 1000)
                  y (range 100 1000)]
              (* x y))))
</pre>
<p>Now in reply to this a <a href="http://blog.hackers-cafe.net/2009/10/re-python-vs-clojure.html">much more idiomatic python example</a> was provided:</p>
<pre class="brush: python;">
print max(s for s in (x * y
    for x in range(111, 1000)
    for y in range(x, 1000))
    if list(str(s)) == list(reversed(str(s))))
</pre>
<p>This uses Python&#8217;s list comprehension.  Definitely a very powerful construct.  However i do find i get lost very quickly when trying to follow long nested comprehension&#8217;s <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>In this same Python post there is also mention of a <a href="http://twitter.com/yukihiro_matz/status/4901341641">twitter post</a> by <a href="http://en.wikipedia.org/wiki/Yukihiro_Matsumoto">Yukihiro Matz</a>, the creator of Ruby.   Matz provided the following Ruby example:</p>
<pre class="brush: ruby;">
p [*100..1000].product([*100..1000]).map{&#124;x,y&#124; x*y}.select{&#124;s&#124;s=s.to_s; s==s.reverse}.max
</pre>
<p>Despite being formatted for twitter (ie. one line) i found it easy to follow through the logic.</p>
<p>Ruby &#38; Python are not the only languages that can produce an elegant solution!  So not to be out done here is a Perl example.  In fact its the exact same code repeated three times but formatted differently: </p>
<pre class="brush: perl;">
use Modern::Perl;
use List::Util q(max);

# twitter one liner
say max grep { $_ eq reverse $_ } map { my $x = $_; map { $x * $_ } 100..1000 } 100..1000;

# again in lispy style format
say
  max
    grep { $_ eq reverse $_ }
      map {
        my $x = $_;
        map { $x * $_ } 100..1000;
      } 100..1000;

# and finally in my preferred more perlish alignment
say max
    grep { $_ eq reverse $_ }
    map {
        my $x = $_;
        map { $x * $_ } 100..1000;
    } 100..1000;
</pre>
<p>The perl code speaks for itself.. its wonderfully clear and succinct.  Also its fast, each version runs in under a second on my machine!</p>
<p>/I3az/</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Loving Clojure]]></title>
<link>http://devender.wordpress.com/2009/11/18/loving-clojure/</link>
<pubDate>Thu, 19 Nov 2009 02:02:15 +0000</pubDate>
<dc:creator>devender</dc:creator>
<guid>http://devender.wordpress.com/2009/11/18/loving-clojure/</guid>
<description><![CDATA[I seem to be liking Clojure &#8230; Let me backup a bit, in the last couple of weeks I have been deb]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>I seem to be liking Clojure &#8230;</p>
<p>Let me backup a bit, in the last couple of weeks I have been debating between picking up Scala or Clojure (don&#8217;t get me wrong Ruby is still my favorite).</p>
<p>I always wanted to pick up a functional programming language so I dabbled a bit with Erlang and Haskell, liked Haskell a lot but without much practice it kind of died (sad times)  and Scala seems too much like Java, yeah I know it seems to have a bigger crowd than Clojure and there are a lot of big names behind it.</p>
<p>Maybe that&#8217;s exactly why I choose Clojure (since its the underdog), or cause it is different enough from Java or simply cause it has a better syntax and seems more elegant (apparently Clojure has better integration with Java, don&#8217;t quote me on it), anyways I decided to learn Clojure.</p>
<p>Peepcode has a nice <a href="http://peepcode.com/products/functional-programming-with-clojure">screen-cast</a> to get you started off on Clojure. If you are on the Mac there is a nice <a href="http://github.com/nullstyle/clojure-tmbundle">bundle</a> for TextMate and anywhere else Netbeans with the <a href="http://enclojure.org/">enclojure</a> plugin seems to be the best.</p>
<p>On a side note it seems more and more that Netbeans has the latest and greatest plugins for everything, then comes IntelliJ and finally eclipse, what&#8217;s going on with eclipse ? has it reached its peak and now it will start dropping off ? but on the flip side there seems to be more and more apps built on top of the Eclipse RCP like Xmind, so is Eclipse no longer going to be the leader of the IDE and just become a platform for building RCPs. This of course depends  on what Oracle is going to do with NetBeans, I really hope they give the same amount of love to NetBeans as Sun did.</p>
<p>Ok getting back to Clojure, don&#8217;t get your panties in a bunch when you see all those parenthesis, it is just the layout that is shocking, indent it well and it is no more than what you are used to.</p>
<p>Here&#8217;s an example</p>
<blockquote><p>(defn fac<br />
&#8220;Returns the factorial of n, which must be a positive integer.&#8221;<br />
[n]<br />
(if (= n 1)<br />
1<br />
(* n (fac (- n 1)))<br />
)</p></blockquote>
<p>Is same as</p>
<blockquote><p>(defn fac [n] (if (= n 1) 1 (* n (fac (- n 1)))))</p></blockquote>
<p>But the first one is a lot more easier on the eyes (even brain?) than the second one. Most examples that you see look like the second one and it frightens people, don&#8217;t let that stop you take my word and go for it.</p>
<p>Clojure seems to be very easy to pick up, things seem very intuitive, like the other day I was wondering, how to return a default value from a map if the key is not found and there is was right there in the api.</p>
<blockquote><p>(map key default-value)</p></blockquote>
<p>So simple! I was easily able to extend the examples that came with the peepcode screencast. Anyways I have started on this path, let&#8217;s see where it goes.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Clojure: Getting back to the basics - Lexical Scoping]]></title>
<link>http://dahliabock.wordpress.com/2009/11/18/clojure-getting-back-to-the-basics-lexical-scoping/</link>
<pubDate>Wed, 18 Nov 2009 21:50:10 +0000</pubDate>
<dc:creator>Dahlia Bock</dc:creator>
<guid>http://dahliabock.wordpress.com/2009/11/18/clojure-getting-back-to-the-basics-lexical-scoping/</guid>
<description><![CDATA[Functional programming has always been a nightmare for me and it started off in college with a class]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Functional programming has always been a nightmare for me and it started off in college with a class called <a href="http://www.cs.cmu.edu/~me/212/schedule.html" target="_blank"><em>15-212: Principles of Programming</em></a>, which was taught in <a href="http://www.amazon.com/Introduction-Programming-International-Computer-Science/dp/0201398206" target="_blank">SML</a>. Almost everything that was taught in that class went over my head and it&#8217;s a miracle that I managed to get a <em>B</em> for it. When I graduated, I swore I never wanted to have anything to do with functional programming ever again, until a few of my coworkers started talking about languages like <a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/" target="_blank">F#</a>, <a href="http://www.scala-lang.org/" target="_blank">Scala</a> and <a href="http://clojure.org" target="_blank">Clojure</a> last year. I have tried to avoid it for as long as I can, but it has proven inevitable.</p>
<p>In starting to read and play with Clojure, I have had to learn a lot of basic concepts that I might or might not have known before. I will attempt to highlight those concepts as I stumble upon them and illustrate them with code snippets that I find or ones that I wrote myself. Pardon the juvenile approach, I find I remember things better if I write them down or explain it to someone.</p>
<p><strong><em>Lexical Scoping</em></strong> &#8211; with lexical or static scope, a variable always refers to its top-level environment. You can always determine a value of a variable based on just reading the code, substituting variable bindings as you go. Of course most programming languages work this way, I just never knew there was a term for it.</p>
<p>Here&#8217;s some code (<a href="http://java.ociweb.com/mark/clojure/article.html" target="_blank">taken from this Clojure tutorial by R. Mark Volkmann</a>) that illustrates lexical scoping and the usages of <code>def</code>, <code>let</code> and <code>binding</code>.</p>
<pre class="textmate-source blackboard"><span class="text text_plain"><span class="meta meta_paragraph meta_paragraph_text">(def v 1) ; v is a global binding
</span>
<span class="meta meta_paragraph meta_paragraph_text">(defn f1 []
</span>  <span class="meta meta_paragraph meta_paragraph_text">(println "f1: v =" v))
</span>
<span class="meta meta_paragraph meta_paragraph_text">(defn f2 []
</span>  <span class="meta meta_paragraph meta_paragraph_text">(println "f2: before let v =" v)
  (let [v 2]
</span>    <span class="meta meta_paragraph meta_paragraph_text">(println "f2: in let, v =" v)
    (f1))
</span>  <span class="meta meta_paragraph meta_paragraph_text">(println "f2: after let v =" v))
</span>
<span class="meta meta_paragraph meta_paragraph_text">(defn f3 []
</span>  <span class="meta meta_paragraph meta_paragraph_text">(println "f3: before binding v =" v)
  (binding [v 3]
</span>    <span class="meta meta_paragraph meta_paragraph_text">(println "f3: in binding, v =" v)
    (f1))
</span>  <span class="meta meta_paragraph meta_paragraph_text">(println "f3: after binding v =" v))
</span>
<span class="meta meta_paragraph meta_paragraph_text">(defn f4 []
</span> <span class="meta meta_paragraph meta_paragraph_text">(def v 4))
</span></span></pre>
<p>Calling <code>f2</code> spits out the following output:</p>
<pre class="textmate-source blackboard"><span class="text text_plain">
<span class="meta meta_paragraph meta_paragraph_text">f2: before let v = 1</span>
<span class="meta meta_paragraph meta_paragraph_text">f2: in let, v = 2</span>
<span class="meta meta_paragraph meta_paragraph_text">f1: v = 1</span>
<span class="meta meta_paragraph meta_paragraph_text">f2: after let v = 1</span></span></pre>
<p>The value of <code>v</code> is 2 only within the <code>let</code> form, and functions that are called within the body cannot see the local bindings of <code>let</code>.</p>
<p>Calling <code>f3</code>:</p>
<pre class="textmate-source blackboard"><span class="text text_plain">
<span class="meta meta_paragraph meta_paragraph_text">f3: before binding v = 1</span>
<span class="meta meta_paragraph meta_paragraph_text">f3: in binding, v = 3</span>
<span class="meta meta_paragraph meta_paragraph_text">f1: v = 3</span>
<span class="meta meta_paragraph meta_paragraph_text">f3: after binding v = 1</span></span></pre>
<p>The <code>binding</code> macro is similar to <code>let</code>, but it creates thread-local values temporarily for existing global bindings. These new values are seen within the form and the functions that are called from within that form.</p>
<p>Calling <code>f4: (println "after calling f4, v =" v)</code>:<br />
<code>after calling f4, v = 4</code></p>
<p>This actually changes the value of the global binding. Now calling <code>f2</code> and <code>f3</code> will have the following output:</p>
<pre class="textmate-source blackboard"><span class="text text_plain">
<span class="meta meta_paragraph meta_paragraph_text">f2: before let v = 4</span>
<span class="meta meta_paragraph meta_paragraph_text">f2: in let, v = 2</span>
<span class="meta meta_paragraph meta_paragraph_text">f1: v = 4</span>
<span class="meta meta_paragraph meta_paragraph_text">f2: after let v = 4</span>
<span class="meta meta_paragraph meta_paragraph_text">nil</span>
<span class="meta meta_paragraph meta_paragraph_text">f3: before binding v = 4</span>
<span class="meta meta_paragraph meta_paragraph_text">f3: in binding, v = 3</span>
<span class="meta meta_paragraph meta_paragraph_text">f1: v = 3</span>
<span class="meta meta_paragraph meta_paragraph_text">f3: after binding v = 4</span>
<span class="meta meta_paragraph meta_paragraph_text">nil</span></span></pre>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Rendering the Mandelbrot Set in Clojure]]></title>
<link>http://developmentjungle.wordpress.com/2009/11/14/rendering-the-mandelbrot-set-in-clojure/</link>
<pubDate>Sat, 14 Nov 2009 20:08:11 +0000</pubDate>
<dc:creator>alex</dc:creator>
<guid>http://developmentjungle.wordpress.com/2009/11/14/rendering-the-mandelbrot-set-in-clojure/</guid>
<description><![CDATA[Clojure is a functional language that brings dynamic types and lisp syntax to the JVM. It has a numb]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><img class="alignright size-full wp-image-15" title="Clojure Mandelbrot Set" src="http://developmentjungle.wordpress.com/files/2009/11/clojure-mandelbrot-set-preview.png" alt="Clojure Mandelbrot Set" width="222" height="155" /><a href="http://www.clojure.org">Clojure</a> is a functional language that brings dynamic types and lisp syntax to the JVM. It has a number of features that make it interesting which will be the subject of another post but for now, let&#8217;s dive right into some code.</p>
<h2>The Mandelbrot Set</h2>
<p>The <a href="http://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot set</a> is an example of a fractal which is basically something that can be described using a simple formula and yet exhibit fascinating levels of detail and beauty. I thought it would be  a good application to get me started learning Clojure.<br />
<!--more--><br />
The Mandelbrot formula is:</p>
<p><strong>Z = Z<sup>2</sup> + C</strong></p>
<p>Where <strong>Z</strong> is a complex number and <strong>C</strong> is a constant. To find out whether or not <strong>C</strong> is in the set, we apply the formula iteratively until the magnitude of <strong>Z</strong> becomes larger than 2, or we reach a certain number of maximum iterations.</p>
<p>Most 2D fractals are described mathematically using complex numbers which are just like regular (real) numbers but with an added 2nd dimension. They can be represented by two regular numbers so another way to describe the formula above might be:</p>
<p><strong>[x, y] = [x, y]<sup>2</sup> + [a, b]</strong></p>
<p>Where <strong>[a, b]</strong> is equivalent to <strong>C</strong> and represents the coordinates on the complex plane we&#8217;re interested in. By applying the formula we can find out whether or not <strong>C</strong> is in the Mandelbrot set. If it is, we draw it with a black pixel, otherwise with some other colour depending the number of iterations taken by the formula.</p>
<h2>Defining a Functional Mandelbrot Set</h2>
<p>Let&#8217;s try to write this in Clojure code. First of all, how do you square a complex number? First let&#8217;s represent <strong>Z</strong> as a vector of regular numbers <strong>[x, y]</strong>. To square them we just apply the formula:</p>
<p><strong>[x, y]<sup>2</sup> = [x<sup>2</sup> - y<sup>2</sup>, 2xy]</strong></p>
<p><strong> </strong>In Clojure, it looks like this:</p>
<pre class="brush: javafx;">
(defn mandel [[x y]]
  [(- (* x x) (* y y)) (* 2 x y)])
</pre>
<p>But this represents just half of the Mandelbrot formula, we need to add that constant<strong> C</strong> and also somehow call our <code>mandel</code> function iteratively. Normally, in an imperative language, this would be easy. We just assign C to our starting value, then calculate Z in a for loop. In Clojure, it&#8217;s a bit different:</p>
<pre class="brush: javafx;">
(defn mandelformula [xcoord ycoord]
  (iterate
    #(vec (map + (mandel %) [xcoord ycoord]))
    [xcoord ycoord]))
</pre>
<p>This function uses the <code><a href="http://clojure.org/api#toc318">iterate</a></code> function to create successive calls to the <code>mandel</code> function passing our initial starting value <strong>[xcoord, ycoord]</strong> which represents <strong>C</strong> in the original formula. The <code>mandel</code> function is actually wrapped in an anonymous function which adds the return value to the initial value hence giving us the <strong>Z</strong><strong><sup>2</sup></strong><strong> + C</strong>. The reason we have to use <code><a href="http://clojure.org/api#toc356">map</a><code> is that we cannot directly add the two vectors together. The individual <strong>x</strong> and <strong>y</strong> values must be added separately and then constructed back into a vector with the <code>vec</code> function.</p>
<p>That's quite a dense piece of code but I think represents the mathematical version of the formula fairly well. It's certainly not the fastest implementation but the goal of functional programming is to favour correctness and readability rather than performance. The great thing about this implementation is that that <code>iterate</code> function returns a lazy infinite sequence. This is very much like the mathematical definition of the formula given at the start; it says nothing about how many times we're actually going to keep applying the formula.</p>
<p>The next part of the definition of the set says that we need to apply the formula to a given point until either the magnitude of <strong>Z</strong> becomes greater than 2, or we reach a given number of iterations.</p>
<pre class="brush: javafx;">
(defn mag [[x y]]
 (+ (* x x) (* y y)))

(take 50 (take-while #(&#60;= (mag %) 4) (mandelformula xcoord ycoord)))
</pre>
<p>Firstly, we define the function <code>mag</code> which returns the magnitude (actually, the magnitude squared) of the given vector pair. Next we use this function with the <code><a href="http://clojure.org/api#toc562">take-while</a></code> function so we'll only evaluate the terms of the mandelbrot formula while the value of each term is less than or equal to 4 (we've squared 2 to avoid having to perform an expensive square root in the <code>mag</code> function). <code>take-while</code> returns another lazy sequence (which might also be infinite) so we use <code><a href="http://clojure.org/api#toc560">take</a></code> to restrict the number of terms evaluated to 50.</p>
<p>Now we're pretty much done with all the maths stuff. All we need to do is count the number of terms calculated and from that number, calculate a colour.</p>
<pre class="brush: javafx;">
(defn coord-colour
  [[xcoord ycoord] max-iters]
  (let [num-iters (count (take max-iters (take-while #(&#60;= (mag %) 4) (mandelformula xcoord ycoord))))]
    (if (= max-iters num-iters)
    *set-colour*
    (iter-colour num-iters max-iters))))
</pre>
<p>This function takes a coordinate, a defined number of maximum iterations and returns the colour that the coordinate should be rendered with by applying the Mandelbrot formula. You can see that we're assigning <strong>num-iters</strong> to the count of the terms returned by our sequence calculated earlier. We then compare <strong>num-iters</strong> with <strong>max-iters</strong>, if they are equal, then the given coordinate is likely to be in the Mandelbrot set so we return <strong>*set-colour*</strong>. If <strong>num-iters</strong> is less than <strong>max-iters</strong> then the coordinate cannot be within the set so we pass the values to another function <code>iter-colour</code> which applies a gradient to the colours.</p>
<h2>Calculating a Colour Gradient</h2>
<p>First of all we define three vectors representing the colours we want to draw with as RGB values.</p>
<pre class="brush: javafx;">
;; Colours used to draw the set
(def *grad-colour-a* [255, 255, 0]) ;yellow
(def *grad-colour-b* [0, 0, 255]) ;blue
(def *set-colour* [0, 0, 0]) ;black
</pre>
<p>We want to calculate a gradient colour such that if <strong>num-iters</strong> is 0, we return yellow, if it is equal to <strong>max-iters</strong> we return blue and if it is somewhere in between, we return a mix between the two colours.</p>
<pre class="brush: javafx;">
(defn grad-colour
  &#34;Returns the colour that is the given fraction of the way between
  the first and second colours given. Returns as a vector of three
  integers between 0 and 255.&#34;
  [colA colB frac]
  (vec (map #(+ (* frac (- %2 %1)) %1) colA colB)))

(defn iter-colour
  &#34;Returns the colour needed to paint a point with the given number
  of iterations&#34;
  [num-iters max-iters]
  (grad-colour *grad-colour-a* *grad-colour-b*
    (/ (double num-iters) max-iters)))
</pre>
<p>The <code>iter-colour</code> function calculates the number of iterations as a fraction of the maximum number of iterations to give a value between 0 and 1 and passes this along with the two colours to the <code>grad-colour</code> function. This function simply calculates the difference between each of the R, G and B values of the two colours and weights it by multiplying by <strong>frac</strong>. Again, similar to how we calculate the sum of a vector in the <code>mandelformula</code> function, we use <code>map</code> to apply this calculation to each of the three values in the two vectors.</p>
<h2>Surfing the Complex Plane</h2>
<p>Now we have all we need to calculate the colour of a given coordinate on the complex plane, we're close to being able to render a fractal in Clojure! But how do we go from the pixels you find on your screen to the mathematical coordinates of the complex plane? Clearly, we need another function.</p>
<pre class="brush: javafx;">
(defn get-coord
  &#34;Returns the coordinates of the given pixel in the complex plane&#34;
  [x y xstart ystart xsize ysize width height]
  [(+ xstart (* (/ x width) xsize))
  (+ ystart (* (/ y height) ysize))])
</pre>
<p>This function takes the x and y <code>pixel</code> coordinates and returns the corresponding <code>complex</code> coordinates. The other parameters are the complex coordinate of the top-left of our screen (<strong>xstart</strong> and <strong>ystart</strong>), the complex dimensions of the area we're drawing (<strong>xsize</strong> and <strong>ysize</strong>) and the dimensions of the area we are drawing in pixels (<strong>width</strong> and <strong>height</strong>).</p>
<p>Now all we need is a way to get all the pixels in our drawing area as vectors to pass on to the this function.</p>
<pre class="brush: javafx;">
(defn get-pixels [width height]
  (for [y (range height) x (range width)]
    [x y]))
</pre>
<p>This function returns a lazy sequence of pixel coordinates for the given width and height. We just need to map the <code>get-coord</code> function onto this sequence and we'll have a lazy sequence of <em>all</em> the complex coordinates in our drawing area. Then all that's left is to get the colour of each of those complex coordinates (according the mandelbrot formula) and assign the colour to the corresponding pixel.</p>
<pre class="brush: javafx;">
(defn render [xstart ystart xsize ysize width height max-iters wr]
  (dorun
    (pmap (fn [pixel]
            (let [[x y] pixel]
              (.setPixel wr x y
                (int-array (coord-colour
                  (get-coord (double x) (double y) xstart ystart xsize ysize width height)
                  max-iters)))))
          (get-pixels width height))))
</pre>
<p>Here we are in the heart of the fractal renderer. Note that we use <code><a href="http://clojure.org/api#toc429">pmap</a></code> to process the sequence of pixels which works just like <code>map</code> but runs across multiple threads which allows us to make the most of multi-core system and render each pixel in parallel. Also, we must use <code><a href="http://clojure.org/api#toc220">dorun</a></code> because <code>get-pixels</code> returns a lazy sequence, so <code>pmap</code> is forced to actually process each pixel (rather than return a lazy sequence as it would do normally). Finally, the colour returned by each call to <code>coord-colour</code> is converted into a Java int[] array so that it can be passed to the <code>.setPixel</code> function of <strong>wr</strong> which is an instance of a <code>WritableRaster</code>.</p>
<p>Note also how we <code>coerce</code> the pixel values of <strong>x</strong> and <strong>y</strong> into doubles. This is because by default, Clojure will use arbitrary precision using Java's BigDecimal class which would incur a huge performance hit when it came to applying the formula. Coercing to double ensures we use either double primitives or boxed Double objects.</p>
<h2>Setting up the Swing Stuff</h2>
<p>All the remaining code is to initialise the Java Swing components needed to display the image generated.</p>
<pre class="brush: javafx;">
(defn get-img [width height]
  (BufferedImage. width height (BufferedImage/TYPE_INT_RGB)))

(defn get-panel [width height img]
  (proxy [JPanel] [] (paint [g] (.drawImage g img 0 0 (Color/red) nil))))

(defn construct-frame [width height panel]
  &#34;Creates and displays a JFrame of the given dimensions with
  the panel added to it&#34;
  (let [frame (JFrame.)]
    (.setPreferredSize panel (Dimension. width height))
    (doto frame
      (.add panel)
      .pack
      (.setLocationRelativeTo nil)
      .show)))

(defn mandelbrot [xstart ystart xsize ysize width height max-iters]
  &#34;Returns a function to render the mandelbrot set with the
  given parameters on a frame&#34;
  (let [img (get-img width height)
        panel (get-panel width height img)
        wr (.getRaster img)]
    (construct-frame width height panel)
    (fn []
      (do
        (render xstart ystart xsize ysize width height max-iters wr)
        (.repaint panel)))))
</pre>
<p>We have defined a number of functions for creating the Swing components and also a function for putting them all together called <code>mandelbrot</code>. Notice, rather than call <code>render</code> immediately, the <code>mandelbrot</code> creates and returns an anonymous function instead. This function is actually a <code>closure</code> which means we can call it repeatedly without having to keep passing in the parameters for the window size and number of iterations. When running a program in a REPL, being able to call individual parts of your code can be very handy.</p>
<p>Finally, we assign the function to a var, and then call it!</p>
<pre class="brush: javafx;">
(def my-mandelbrot (mandelbrot -2 -1.25 3 2.5 1200 1000 50))

(future (my-mandelbrot))
</pre>
<p>We wrap the call to <code>my-mandelbrot</code> in a <code><a href="http://clojure.org/api#toc268">future</a></code> which returns immediately and then executes the function passed to it in a separate thread. The reason for this is that rendering the fractal can take a significant amount of time. Using a <code>future</code> allows you to continue to use the REPL while the rendering happens in the background which can be helpful for debugging or general development. After about 30 seconds or so, you'll see something like this:</p>
<p><img class="aligncenter size-full wp-image-17" title="Clojure Mandelbrot Set" src="http://developmentjungle.wordpress.com/files/2009/11/clojure-mandelbrot-set.png" alt="Mandelbrot Set rendered in Clojure" width="608" height="534" /></p>
<h2>Conclusion</h2>
<p>We've seen how to create a working fractal rendering program by building on mathematical formulas and making use of Clojure features such as lazy, infinite sequences, parallel processing and closures to do it. These features allow us to keep the code close to the problem domain and hopefully, improve its readability and correctness. In the <a href="/2009/11/24/optimising-mandelbrot/">next post</a> we'll see how to profile the renderer and <em>drastically</em> improve its performance.</p>
<p>The full source code can be found <a href="http://github.com/alexspurling/clojure-projects/blob/master/Mandelbrot/src/mandelbrot.clj">here on github</a>.</p>
<p>Stay tuned for more Clojure goodness in the future!</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Clojure Changing My Mind]]></title>
<link>http://invisibleblocks.wordpress.com/2009/11/13/clojure-changing-my-mind/</link>
<pubDate>Fri, 13 Nov 2009 22:15:17 +0000</pubDate>
<dc:creator>Daniel Bernier</dc:creator>
<guid>http://invisibleblocks.wordpress.com/2009/11/13/clojure-changing-my-mind/</guid>
<description><![CDATA[As good as Processing is, it&#8217;s still java. I&#8217;ve slowly been learning clojure, and trying]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>As good as <a href="http://processing.org">Processing</a> is, it&#8217;s still java.  I&#8217;ve slowly been learning <a href="http://clojure.org">clojure</a>, and trying to use it with Processing, via <a href="http://github.com/rosado/clj-processing">clj-processing</a>. While <a href="http://clojure.org/Reference">the clojure docs</a> and <a href="http://www.amazon.com/gp/product/1934356336?ie=UTF8&#38;tag=invisblock-20&#38;linkCode=as2&#38;camp=1789&#38;creative=390957&#38;creativeASIN=1934356336">the book</a> are both good, I&#8217;m still in that flounder-around stage, trying out tiny experiments to understand how things work.</p>
<p>I wanted a function to make saw-step sequences: (1 2 3 2 1 2 3 2 &#8230;).  You give it the low bound, the high bound, and the step-size.  For example:</p>
<ul>
<li><code>(saw-step 1 4 1)</code> gives <code>(1 2 3 4 3 2 1 2 3 4 3 ...)</code></li>
<li><code>(saw-step 0 15 5)</code> gives <code>(0 5 10 15 10 5 0 5 ...)</code></li>
<li><code>(saw-step 0 5 2)</code> gives <code>(0 2 4 5 3 1 0 2 4 5 ...)</code></li>
</ul>
<p>I&#8217;ve coded this kind of thing up in ruby, javascript, or java a dozen times, though it&#8217;s usually inside a loop, not returning a list.  Something like this:</p>
<pre class="brush: jscript;">
var min = 3;
var max = 7;
var step = 2;

var n = min;
while(shouldContinue()) {
    doSomethingWith(n);
    n += step;
    if (n+step &#62; max &#124;&#124; n-step &#60; min) {
        step *= -1;  // turn around
    }
}
</pre>
<p>My first try in clojure took a similar tack.  I never got it right, but it was something like this:</p>
<pre style="font-size:1.2em;">(defn saw-step
  [min max step-size]
    (let [step (ref step-size)]
      (iterate
       (fn [x]
         (if (or ( min (- x @step)))
           (dosync (alter step -)))
         (+ @step x))
         min)))</pre>
<p>It&#8217;s a disaster &#8212; the parentheses don&#8217;t even match &#8212; but you get the gist.  I was trying to cram the same imperative algorithm into clojure: keep some mutable state, add <code>step</code> to it, and when you reach the edges, mutate <code>step</code> to change direction.  I kept getting bugs where it would go one step beyond the edge, or it would start working its way back from one edge, only to turn around again, and bounce against the edge forever.</p>
<p>I gave up and went to bed, but a few days later, I had better luck.</p>
<pre style="font-size:1.2em;">(defn saw-step
  [min max step]
     (cycle
      (into (vec (range min max step)) ; vec, so (into) adds to end
	    (for [x
		  (iterate #(- % step) max)
		  :while (&#62; x min)] x))))</pre>
<p>The first not-entirely-wrong step I made was to try breaking the list of numbers I wanted into two parts: the going-up numbers, and the coming-down numbers.  (0 2 4 5 3 1) is just (0 2 4) + (5 3 1).  The <code>(range min max step)</code> part gives you the going-ups, and the <code>(for [x (iterate ...)</code> stuff is the going-downs, as a list comprehension.</p>
<p>(One mistake I made was trying <code>(range max min step)</code> for the going-downs, which yields an empty list; another was using <code>(iterate dec max)</code>, which never ends, because it keeps decrementing into the negatives.  I found my way out with the list comprehension, but I bet there&#8217;s a better way.)</p>
<p>Once you have those two lists, you can use <code>into</code> to add each item from the second list to the first, giving you (0 2 4 5 3 1).  That goes into <code>cycle</code> for a lazy, infinite sequence.</p>
<p>The solution&#8217;s not too bad: a saw-step is a cycle of the going-ups, followed by the going-downs.  The code looks about that way.</p>
<p>(It occurred to me after that I could always use a default step of 1, and pipe the result through a scaling map.  That would give me the bounds I wanted, with the extra benefit of evenly-spaced steps.  Maybe I&#8217;ll remove <code>step</code> later.)</p>
<p><a href="http://www.cs.yale.edu/quotes.html">Alan Perlis</a> said, &#8220;A language that doesn&#8217;t affect the way you think about programming, is not worth knowing.&#8221;  He also said, &#8220;The only difference(!) between Shakespeare and you was the size of his idiom list &#8211; not the size of his vocabulary.&#8221;  Clojure&#8217;s changing my mind, a bit at a time.</p>
<p><em> </em></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Podcast #001]]></title>
<link>http://strictlyprofessional.wordpress.com/2009/11/13/podcast-001/</link>
<pubDate>Fri, 13 Nov 2009 14:05:09 +0000</pubDate>
<dc:creator>Chas Emerick</dc:creator>
<guid>http://strictlyprofessional.wordpress.com/2009/11/13/podcast-001/</guid>
<description><![CDATA[This is the inaugural Strictly Professional podcast, hosted by Chas Emerick and joined by Gerard Gua]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>This is the inaugural Strictly Professional podcast, hosted by <a href="http://muckandbrass.com">Chas Emerick</a> and joined by <a href="http://receivebacon.org/">Gerard Gualberto</a>, <a href="http://dougsprojects.com">Doug Martin</a>, <a href="http://twitch.posterous.com">Chris Miles</a>, and <a href="http://loufranco.com/">Lou Franco</a>.</p>
<p>You don&#8217;t know us, but we&#8217;re  software developers (and occasional software company founders) that have met through the <a href="http://wmassdevs.com">Western Massachusetts Developers Group</a>&#8217;s biweekly meetings.  We can all jabber on forever, and it seemed like a crime to deprive the rest of the world of our wit and wisdom.</p>
<p>Notes &#38; Topics:</p>
<ul>
<li>Chas thought a good starting topic would be <a href="http://www.inc.com/magazine/20091101/does-slow-growth-equal-slow-death.html">Joel Spolsky&#8217;s recent article on Inc.com</a>, where he laments the &#8220;slow growth path&#8221; that FogCreek has taken over the years, and <a href="http://37signals.com/svn/posts/2002-bug-tracking-isnt-a-network-effect-business">DHH&#8217;s response to it</a>.  This leads into a discussion about bug trackers, what is and is not a network business, and other various nonsense, including a sidebar about the</li>
<li>Lou mentioned Chas&#8217; recent blog post, &#8220;<a href="http://muckandbrass.com/web/display/~cemerick/2009/10/23/Reducing+purchase+anxiety+is+a+feature">Reducing purchase anxiety is a feature</a>&#8220;, and how you&#8217;d better offer refunds, because your customers will get them if they want them anyway.</li>
<li>On the way to talking about why we&#8217;re software developers and what the nature of work is, Lou ran us through the outlines of Noam Wasserman&#8217;s <a title="Noam's canonical blog post on the subject" href="http://founderresearch.blogspot.com/2005/11/rich-versus-king-core-concept.html" target="_blank">&#8220;Rich or King&#8221; choice</a>.</li>
<li>Doug exposes top-secret national security information.</li>
<li>We trail out with some brief software language philosophy.  Miles mentioned Rich Hickey&#8217;s near-metaphysical discussion of time (first in <a href="http://wiki.jvmlangsummit.com/Clojure_Keynote">Rich&#8217;s keynote presentation</a> at this year&#8217;s JVM Language Summit [<a href="http://wiki.jvmlangsummit.com/images/a/ab/HickeyJVMSummit2009.pdf">slides</a>], and then in the Artima interview <a href="http://www.artima.com/articles/hickey_on_time.html">Time is the New Memory</a>), and then said that Brainfuck is philosophically anarchist existentialism&#8230;which makes sense to me.</li>
</ul>
<p>Comments, criticism, raves?  Comment below, and maybe we&#8217;ll do another one.</p>
<p><span style='text-align:left;display:block;'><p><object type='application/x-shockwave-flash' data='http://wordpress.com/wp-content/plugins/audio-player/player.swf' width='290' height='24' id='audioplayer1'><param name='movie' value='http://wordpress.com/wp-content/plugins/audio-player/player.swf' /><param name='FlashVars' value='&amp;bg=0xf8f8f8&amp;leftbg=0xeeeeee&amp;lefticon=0x666666&amp;rightbg=0xcccccc&amp;rightbghover=0x999999&amp;righticon=0x666666&amp;righticonhover=0xffffff&amp;text=0x666666&amp;slider=0x666666&amp;track=0xFFFFFF&amp;border=0x666666&amp;loader=0x9FFFB8&amp;soundFile=http%3A%2F%2Fs3.amazonaws.com%2Fstrictly-professional%2Fsp-podcast-001.mp3' /><param name='quality' value='high' /><param name='menu' value='false' /><param name='bgcolor' value='#FFFFFF' /></object></p></span></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Setting up a Clojure Project with Maven]]></title>
<link>http://reminiscential.wordpress.com/2009/11/11/setting-up-a-clojure-project-with-maven/</link>
<pubDate>Wed, 11 Nov 2009 19:32:20 +0000</pubDate>
<dc:creator>jchiu1106</dc:creator>
<guid>http://reminiscential.wordpress.com/2009/11/11/setting-up-a-clojure-project-with-maven/</guid>
<description><![CDATA[In this blog post I&#8217;m going to record my recent experience in setting up a Clojure project usi]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>In this blog post I&#8217;m going to record my recent experience in setting up a Clojure project using the <a href="http://github.com/talios/clojure-maven-plugin">clojure-maven-plugin</a>.</p>
<h1>Clojure-Maven-Plugin</h1>
<p>First you need to compile the plugin from source:<br />
<code>git clone git://github.com/talios/clojure-maven-plugin.git</code><br />
<code>cd clojure-maven-plugin</code><br />
<code>mvn install</code><br />
Of course, you will need to have Maven2 installed already.</p>
<p>After that, the compiled plugin jar will be in your maven local repository. Create a pom.xml file to use the plugin. I&#8217;m using the pom.xml from my project <a href="http://github.com/kevinjqiu/pci-clj">Programming Collective Intelligence</a> as an example:</p>
<pre class="brush: xml;">
&#60;project xmlns=&#34;http://maven.apache.org/POM/4.0.0&#34; xmlns:xsi=&#34;http://www.w3.org/2001/XMLSchema-instance&#34;
    xsi:schemaLocation=&#34;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&#34;&#62;
    &#60;modelVersion&#62;4.0.0&#60;/modelVersion&#62;
    &#60;groupId&#62;pci&#60;/groupId&#62;
    &#60;artifactId&#62;pci&#60;/artifactId&#62;
    &#60;name&#62;Programming Collective Intelligence&#60;/name&#62;
    &#60;version&#62;0.0.1-SNAPSHOT&#60;/version&#62;

    &#60;build&#62;
        &#60;plugins&#62;
            &#60;plugin&#62;
                &#60;groupId&#62;com.theoryinpractise&#60;/groupId&#62;
                &#60;artifactId&#62;clojure-maven-plugin&#60;/artifactId&#62;
                &#60;version&#62;1.2-SNAPSHOT&#60;/version&#62;
            &#60;/plugin&#62;
        &#60;/plugins&#62;
    &#60;/build&#62;

&#60;/project&#62;
</pre>
<p>Also, setting up clojure-lang and clojure-contrib (optional, but nice to have) as dependencies as follows:</p>
<pre class="brush: xml;">
    &#60;dependencies&#62;
        &#60;dependency&#62;
            &#60;groupId&#62;org.clojure&#60;/groupId&#62;
            &#60;artifactId&#62;clojure-lang&#60;/artifactId&#62;
            &#60;version&#62;1.1.0-alpha-SNAPSHOT&#60;/version&#62;
        &#60;/dependency&#62;
        &#60;dependency&#62;
            &#60;groupId&#62;org.clojure&#60;/groupId&#62;
            &#60;artifactId&#62;clojure-contrib&#60;/artifactId&#62;
            &#60;version&#62;1.0-SNAPSHOT&#60;/version&#62;
        &#60;/dependency&#62;
    &#60;/dependencies&#62;
</pre>
<p>Clojure builds haven&#8217;t reached Maven central repository yet, so you need to build Clojure from source, and install it into your local repository by using<br />
<code>mvn install:install-file -Dfile=clojure-lang.jar -DgroupId=org.clojure -DartifactId=clojure-lang -Dversion=1.1.0-alpha-SNAPSHOT -Dpackaging=jar</code></p>
<p>Do the same for clojure-contrib.</p>
<p>You can also create a directory in under your project root and have pom.xml looking for artifacts in that directory as well. I found this to be useful for artifacts that are not in the central repository. To do so, add repository definition in pom.xml:</p>
<pre class="brush: xml;">
    &#60;repositories&#62;
        &#60;repository&#62;
            &#60;id&#62;lib-repo&#60;/id&#62;
            &#60;name&#62;lib-m2-repository&#60;/name&#62;
            &#60;url&#62;file://${basedir}/lib-repo&#60;/url&#62;
            &#60;layout&#62;legacy&#60;/layout&#62;
            &#60;snapshots&#62;
                &#60;enabled&#62;false&#60;/enabled&#62;
            &#60;/snapshots&#62;
            &#60;releases&#62;
                &#60;checksumPolicy&#62;ignore&#60;/checksumPolicy&#62;
            &#60;/releases&#62;
        &#60;/repository&#62;
    &#60;/repositories&#62;
</pre>
<p>Afterwards, you need to setup your directory structure as follows:</p>
<pre class="brush: css;">
${project_root}
-- lib-repo
    -- org.clojure
        -- jars
            * clojure-lang-1.1.0-alpha-SNAPSHOT.jar
            * clojure-contrib-1.1.0-alpha-SNAPSHOT.jar
        -- poms
            * clojure-lang-1.1.0-alpha-SNAPSHOT.pom
            * clojure-contrib-1.1.0-alpha-SNAPSHOT.pom
</pre>
<p>The pom files can be found in your local repository after you&#8217;ve done mvn install:install-file.</p>
<p>If you want to run clojure:repl goal, you&#8217;d better add jline to your dependency as well:</p>
<pre class="brush: xml;">
&#60;dependencies&#62;
  ...
        &#60;dependency&#62;
            &#60;groupId&#62;jline&#60;/groupId&#62;
            &#60;artifactId&#62;jline&#60;/artifactId&#62;
            &#60;version&#62;0.9.94&#60;/version&#62;
        &#60;/dependency&#62;

  ...
&#60;/dependencies&#62;
</pre>
<p>By convention, the plugin compiles everything under ${project_root}/src/main/clojure and ${project_root}/src/test/clojure.</p>
<p>It&#8217;s hard to imagine working in a language like Clojure without automated unit tests. Fortunately, clojure-maven-plugin has the clojure:test goal which runs unit tests. All you need to do is telling the plugin where&#8217;s the entry point of your unit test. Add the following configuration in the build section:</p>
<pre class="brush: xml;">
    &#60;build&#62;
        &#60;plugins&#62;
            &#60;plugin&#62;
                &#60;groupId&#62;com.theoryinpractise&#60;/groupId&#62;
                &#60;artifactId&#62;clojure-maven-plugin&#60;/artifactId&#62;
                &#60;version&#62;1.2-SNAPSHOT&#60;/version&#62;
                &#60;configuration&#62;
                    &#60;testScript&#62;src/test/clojure/pci/test.clj&#60;/testScript&#62;
                &#60;/configuration&#62;
            &#60;/plugin&#62;
        &#60;/plugins&#62;
    &#60;/build&#62;
</pre>
<p>The test script looks like the following:</p>
<pre class="brush: css;">
(ns my-namespace
  (:use clojure.contrib.test-is
          test-module-1
          test-module-2))
(run-tests 'test-module-n)
</pre>
<p>There you have it! The sample files can be found in my <a href="http://github.com/kevinjqiu/pci-clj">pci-clj</a> project on GitHub.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[String manipulation with Clojure]]></title>
<link>http://andrewpatzer.wordpress.com/2009/11/11/string-manipulation-with-clojure/</link>
<pubDate>Wed, 11 Nov 2009 18:08:42 +0000</pubDate>
<dc:creator>andrewpatzer</dc:creator>
<guid>http://andrewpatzer.wordpress.com/2009/11/11/string-manipulation-with-clojure/</guid>
<description><![CDATA[As I continue learning more about Clojure, I decided to start re-writing some common routines that I]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>As I continue learning more about Clojure, I decided to start re-writing some common routines that I&#8217;ve created in Java, but this time use Clojure and try to do it the idiomatic way, and use more of a functional approach.</p>
<p>My first attempt was to build a function that gives the reverse-complement of a strand of DNA. Basically, you take a string of characters, reverse it, and then swap out the letters to give you your result. The letters consist of &#8216;a&#8217;, &#8216;c&#8217;, &#8216;t&#8217;, and &#8216;g&#8217;. You need to swap the letters like this: [a-&#62;t] [t-&#62;a] [c-&#62;g] [g-&#62;c].</p>
<p>Here&#8217;s my first pass:</p>
<pre>
(defn rev-comp
     "Reverses the given DNA sequence and swaps
      nucleotides to produce the reverse complement"
    [dna]
    (let [mappings {\a \t \t \a \c \g \g \c}]
        (apply str (replace mappings (reverse dna)))
)</pre>
<pre>user=&#62; (rev-comp "acttggccaaaaat")
"atttttggccaagt"
</pre>
<p>This is my first real Clojure function beyond the &#8220;Hello World&#8221; stuff, so I&#8217;d be interested in hearing tips of how this could be better.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Programming Collective Intelligence in Clojure]]></title>
<link>http://reminiscential.wordpress.com/2009/11/10/programming-collective-intelligence-in-clojure/</link>
<pubDate>Wed, 11 Nov 2009 01:13:15 +0000</pubDate>
<dc:creator>jchiu1106</dc:creator>
<guid>http://reminiscential.wordpress.com/2009/11/10/programming-collective-intelligence-in-clojure/</guid>
<description><![CDATA[They say the best way to learn a new programming language is by programming in it. Therefore I]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>They say the best way to learn a new programming language is by programming in it. Therefore I&#8217;m starting this project converting algorithms in the book <a href="http://oreilly.com/catalog/9780596529321">Programming Collective Intelligence</a> into Clojure, while learning the best practices and language idioms during the process.</p>
<p>I&#8217;ve created a GitHub <a href="http://github.com/kevinjqiu/pci-clj">project</a> for this. I&#8217;m not sure how far I&#8217;m able to go but let&#8217;s see.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Clojure]]></title>
<link>http://kotsokalis.wordpress.com/2009/11/06/clojure/</link>
<pubDate>Fri, 06 Nov 2009 21:23:52 +0000</pubDate>
<dc:creator>CK</dc:creator>
<guid>http://kotsokalis.wordpress.com/2009/11/06/clojure/</guid>
<description><![CDATA[Even before I actually study it in detail, Clojure becomes my latest fixation. A Lisp-based function]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Even before I actually study it in detail, <a href="http://clojure.org/">Clojure</a> becomes my latest fixation. A Lisp-based functional, general-purpose language, which produces JVM bytecode and has access to Java libraries? Sounds like a dream come true. I never liked Java, and as a matter of fact, I consider myself a Java-dyslexic. No matter how much I tried in the past, I never got around learning enough of it to use further than &#8220;Hello, world&#8221;s. Its syntactic resemblance to C, with which I am (was?) quite proficient, didn&#8217;t help much. Nevertheless, the breadth and depth of libraries that exist in Java are mind-boggling, and the ability to use them with a different language is just great. I know there are other JVM-based languages, e.g. <a href="http://www.scala-lang.org/">Scala</a>, but somehow after reading introductory material they never enticed me enough. Also, I really don&#8217;t know how come, although a big fan of <a href="http://python.org/">Python</a> I never tried <a href="http://jython.org/">Jython</a>. I assume I just preferred the real thing &#8212; Python also has an extensive and compelling set of libraries.</p>
<p>In any case, I&#8217;ll try to get a closer look at Clojure and come back with a more complete opinion.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Cambridge Clojure Meetup 10th Nov]]></title>
<link>http://jimdowning.wordpress.com/2009/11/04/cambridge-clojure-meetup-10th-nov/</link>
<pubDate>Wed, 04 Nov 2009 11:32:32 +0000</pubDate>
<dc:creator>jimdowning</dc:creator>
<guid>http://jimdowning.wordpress.com/2009/11/04/cambridge-clojure-meetup-10th-nov/</guid>
<description><![CDATA[I&#8217;ll be going to the weekly Cambridge Clojure Meetup next Tuesday, 10th November, 2009. The ti]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>I&#8217;ll be going to the weekly Cambridge Clojure Meetup next Tuesday, 10th November, 2009. </p>
<p>The time place is the usual, 8pm at the Kingston Arms. </p>
<p><iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.uk/maps?f=q&amp;#38;source=s_q&amp;#38;hl=en&amp;#38;geocode=&amp;#38;q=kingston arms, cambridge&amp;#38;sll=53.800651,-4.064941&amp;#38;sspn=17.025991,46.538086&amp;#38;ie=UTF8&amp;#38;z=14&amp;#38;iwloc=A&amp;#38;cid=7908658995767794566&amp;#38;ll=52.20787,0.144196&amp;#38;output=embed&amp;#38;w=425&amp;#38;h=350"></iframe><br /><small><a href="http://maps.google.co.uk/maps?f=q&amp;#38;source=s_q&amp;#38;hl=en&amp;#38;geocode=&amp;#38;q=kingston arms, cambridge&amp;#38;sll=53.800651,-4.064941&amp;#38;sspn=17.025991,46.538086&amp;#38;ie=UTF8&amp;#38;z=14&amp;#38;iwloc=A&amp;#38;cid=7908658995767794566&amp;#38;ll=52.20787,0.144196&amp;#38;source=embed&amp;#38;w=425&amp;#38;h=350" style="color:#0000FF;text-align:left">View Larger Map</a></small></p>
<p>Update: I&#8217;ve set up an <a href="http://upcoming.yahoo.com/event/4842358">upcoming entry</a> for next week&#8217;s meeting. </p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Incanter Cheat Sheet]]></title>
<link>http://incanter-blog.org/2009/10/22/incanter-cheat-sheet/</link>
<pubDate>Thu, 22 Oct 2009 04:10:42 +0000</pubDate>
<dc:creator>liebke</dc:creator>
<guid>http://incanter-blog.org/2009/10/22/incanter-cheat-sheet/</guid>
<description><![CDATA[I&#8217;ve created an Incanter cheat sheet (pdf or LaTex) based on Steve Tayon&#8217;s Clojure cheat]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>I&#8217;ve created an Incanter cheat sheet (<a href="http://incanter.org/docs/incanter-cheat-sheet.pdf">pdf</a> or <a href="http://github.com/liebke/incanter/blob/master/docs/incanter-cheat-sheet.tex">LaTex</a>) based on Steve Tayon&#8217;s Clojure cheat sheet (<a href="http://cloud.github.com/downloads/richhickey/clojure/clojure-cheat-sheet-a4-grey.pdf">pdf</a> or <a href="http://clojure.org/cheatsheet">html</a>). Click on the image below to see the complete pdf.</p>
<p><b>Updated:</b> Trimmed some redundancies, got the sheet down to a single page.</p>
<p><a href="http://incanter.org/docs/incanter-cheat-sheet.pdf"><img src="http://incanter.wordpress.com/files/2009/10/incanter-cheat-sheet-pict.jpg" /></a></p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Explore Clojure: Building a Bifid cipher]]></title>
<link>http://reminiscential.wordpress.com/2009/10/20/explore-clojure-building-a-bifid-cipher/</link>
<pubDate>Tue, 20 Oct 2009 17:28:09 +0000</pubDate>
<dc:creator>jchiu1106</dc:creator>
<guid>http://reminiscential.wordpress.com/2009/10/20/explore-clojure-building-a-bifid-cipher/</guid>
<description><![CDATA[Lately I&#8217;ve been teaching myself Clojure, a Lisp dialect on the JVM platform. I still love Erl]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>Lately I&#8217;ve been teaching myself <a href="http://clojure.org">Clojure</a>, a Lisp dialect on the JVM platform. I still love Erlang and still learning it, but Clojure has a special draw for me being a JVM language and its Lisp roots. I studied Scheme (another Lisp dialect) in my college years and deemed it purely academic. However, Clojure has the potential of changing this and bring the expressiveness of Lisp and the power of functional programming to the Java world.</p>
<p>The best way to learn a language is to use it to solve some non-trivial practical problems. I stumbled <a href="http://programmingpraxis.com/2009/10/13/bifid-cipher/">this</a> problem on <a href="http://programmingpraxis.com">Programming Praxis</a>. It&#8217;s about building a <a href="http://en.wikipedia.org/wiki/Bifid_cipher">Bifid cipher</a>, which I thought could be a good practical problem for me to solve in Clojure.</p>
<h1>Polybius Square</h1>
<p>Bifid cipher is based on <a href="http://en.wikipedia.org/wiki/Polybius_square">Polybius Square</a>, which looks like this:</p>
<p>1 2 3 4 5<br />
1 B G W K Z<br />
2 Q P N D S<br />
3 I O A X E<br />
4 F C L U M<br />
5 T H Y V R</p>
<p>Every encodable letter is given a vector value, which is the coordinate in this square. For example, X=[3 4], H=[5 2]. The first task would be building the Polybius square in Clojure.</p>
<p>My function <code>polybius-square</code> is going to take 2 parameters: <code>charset</code> is a list of encodable characters, and <code>size</code> is the size of the Polybius square.</p>
<pre class="brush: css;">
(defn polybius-square
  &#34;Polybius square by the given charset and size&#34;
  [charset square-size]
  (map #(vector %1 [%2 %3]) charset
    (for [x (range 1 (+ 1 square-size)), y (range 1 (+ 1 square-size))] x)
    (take (* square-size square-size) (cycle (range 1 (+ 1 square-size))))))
</pre>
<p>Let&#8217;s see how this works. Suppose you&#8217;re calling this function with<br />
<code>(polybius-square "ABCDE" 3)</code>, <code>charset</code> is bound to &#8220;ABCDE&#8221; and <code>size</code> is bound to 3. What I want is a seq of vectors in the form of: <code>(\Letter [x y])</code> where x and y are the coordinates. It&#8217;s natural to use map, with the function-to-map taking three parameters and vectorize them. That&#8217;s what the anonymous function does:<br />
<code>#(vector %1 [%2 %3])</code></p>
<p>%1 is the letter, %2 and %3 are the coordinates. I want the coordinate sequence to be like this: [1 1], [1 2], [1 3], [2 1], [2 2], [2 3]. In imperative language, you&#8217;d use a nested for loop, but we&#8217;ve established that&#8217;s ugly. We want the parameters fed to <code>map</code> to be of the following:<br />
<code>"ABCDEFGHJI" (1 1 1 2 2 2 3 3 3) (1 2 3 1 2 3 1 2 3 1 2 3)</code></p>
<p>To generate the first list, we use list comprehension:<br />
<code>(for [ x (range 1 (+ 1 square-size)), y (range 1 (+ 1 square-size))] x)</code><br />
It reads: for every <code>x</code> in range 1 to square-size+1 (because &#8220;range&#8221; is right-exclusive) and for every <code>y</code> in the same range, output the value of x and take the whole output as the list. Because the binding of y comes later, and Lisp evaluates from right to left, the result of this list comprehension is (1 1 1 2 2 2 3 3 3)</p>
<p>Now let&#8217;s look at the other list: (1 2 3 1 2 3 1 2 3) &#8211; a cyclic list. Fortunately, Clojure has a built-in cycle function, which returns a lazy sequence of cycles of a collection. We want to call it with <code>cycle '(1 2 3)</code>, so it&#8217;s going to be <code>cycle (range 1 (+ 1 square-size))</code>. Because cycle will be evaluated to an infinite sequence, we have to tell it when to stop.<br />
When you see the square as a flat list, it&#8217;s clear that the cycle should stop when it reaches square-size^2, hence <code>(take (* square-size square-size) (cycle (range 1 (+ 1 square-size))))</code>.</p>
<h1>The codec maps</h1>
<p>After the Polybius square is built, we have the bases for our encryption. However, it&#8217;s not user-friendly: it&#8217;s a flat list of tuples. We want two maps for fast searching from letters to their encoding forms (<code>letter-2-code</code>) and from transposed vectors to the letters (<code>transposed-2-letter</code>).</p>
<pre class="brush: css;">
(defn- letter-2-code
  &#34;Produce a mapping between letters and their codes&#34;
  [square]
  (letfn [(helper [the-square the-map]
          (if (empty? the-square)
            the-map
            (let [the-item (first the-square)
                  the-key (first the-item)
                  the-number (second the-item)]
              (recur (rest the-square) (assoc the-map the-key the-number)))))]
    (helper square (empty hash-map))))
</pre>
<p>The strategy here is to walk through the list, add each item to a <code>hash-map</code>. Here we define an inner function, which is basically a recursive helper. The function itself is pretty straightforward. Take note that in Clojure, when you need to do tail recursion, always use <code>recur</code> instead of calling the function by its name. This is because JVM cannot do automatic tail cut optimization (TCO), but Clojure does the optimization through the <code>recur</code> keyword. After the binding, we kick off the recursion by calling it with the initial list.</p>
<p>In the similar fashion, we have:</p>
<pre class="brush: css;">
(defn- transposed-2-letter
  &#34;Produce a mapping between the transposed codes to letter&#34;
  [square]
  (letfn [(helper [the-square the-map]
            (if (empty? the-square)
              the-map
              (let [the-item (first the-square)
                    the-key (second the-item)
                    the-letter (first the-item)]
                (recur (rest the-square) (assoc the-map the-key the-letter)))))]
    (helper square (empty hash-map))))
</pre>
<h1>Encode</h1>
<p>Now comes the encode function. It&#8217;s important to note that the encoding will be different if a different polybius square is provided. Therefore, both <code>encode</code> and <code>decode</code> functions take the Polybius square as parameter.</p>
<p>During the encoding phase, we first convert every character in the message into a vector, then reorganize the vectors. To better illustrate, we use an example:<br />
Suppose we have the following mapping:<br />
<code>{I [3 3], H [3 2], G [3 1], F [2 3], E [2 2], D [2 1], C [1 3], B [1 2], A [1 1]}</code><br />
and we want to encode the word &#8220;CEDE&#8221;<br />
CEDE =&#62; ([1 3] [2 2] [2 1] [2 2]), then we want to transpose the &#8220;matrix&#8221; composed of the above vectors, so it would be like:<br />
([1 2] [2 2] [3 2] [1 2]), and finally looking up every item in the transposed-2-letter mapping<br />
<code>{[3 3] I, [3 2] H, [3 1] G, [2 3] F, [2 2] E, [2 1] D, [1 3] C, [1 2] B, [1 1] A}</code><br />
=&#62;BEHB</p>
<p>To get a mapping of message to vectors, we again map a function to the list:<br />
<code>(map #(get l2c % %) message)</code> (l2c has been bound)<br />
Then we apply the interleave function to the result, this gives:<br />
<code>(1 2 2 2 3 2 1 2)</code>. This is good but we want a list of vectors instead of a flat list. Clojure provides <code>partition</code> function, which does exactly it:<br />
<code>(partition 2 '(1 2 2 2 3 2 1 2))</code><br />
gives:<br />
<code>([1 2] [2 2] [3 2] [1 2])</code></p>
<p>Then, we want to lookup every item in the result in the transposed-to-letter map and get a list of mapped characters. To achieve this, again, we use <code>map</code>, providing a different mapping:<br />
<code>(map #(get tr2l % %) ...)</code> (tr2l is bound)<br />
This returns a list of characters. We want a string, so we concatenate the characters by applying <code>str</code> function:<br />
<code>(apply str (map #(get tr2l % %) ...))</code></p>
<p>Here&#8217;s the complete code:</p>
<pre class="brush: css;">
(defn encode
  &#34;Return the message encoded by the specified polybius-square&#34;
  [message polybius-square]
  (let [l2c (letter-2-code polybius-square)
        tr2l (transposed-2-letter polybius-square)]
    (apply str
      (map #(get tr2l % %) (partition 2 (apply interleave (map #(get l2c % %) message)))))))
</pre>
<p>Here you see the expressiveness of Clojure: all operations are chained together and formed in expression.</p>
<p>The <code>decode</code> function behaves similarly. The only trick is we need to split the translated code list in two. We use <code>partition</code> method again, with <code>size</code> being half of the entire list.</p>
<pre class="brush: css;">
(defn decode
  &#34;Return the decoded message using the specified polybius-square&#34;
  [encoded polybius-square]
  (let [l2c (letter-2-code polybius-square)
        c2l (transposed-2-letter polybius-square)
        codes (apply concat (map #(get l2c % %) encoded))
        len (count codes)
        parts (partition (/ len 2) codes)]
    (apply str (map #(get c2l %1 %1) (map #(vector %1 %2) (first parts) (second parts))))))
</pre>
<h1>Putting it together</h1>
<p>Now with all the pieces are in place, we can put it together by writing a simple test:</p>
<pre class="brush: css;">
(defn -main
  &#34;A simple test case entry point&#34;
  []
  (let [square (polybius-square &#34;ABCDEFGHIKLMNOPQRSTUVWXYZ&#34; 5)]
    (do (println (encode &#34;BIFIDCIPHER&#34; square))
      (println (decode (encode &#34;BIFIDCIPHER&#34; square) square)))))
</pre>
<p>Output:<br />
<code><br />
BGAHFRQTOXW<br />
BIFIDCIPHER<br />
</code></p>
<p>Have the complete source code here at <a href="http://github.com/kevinjqiu/bifid-clj">http://github.com/kevinjqiu/bifid-clj</a>, I&#8217;m sure there are a lot to be improved of the above implementation of the Bifid cipher. Especially currently I have no elegant way to solve the situation where the letter to be encoded is not in the Polybius square. Also, I&#8217;m sure my code isn&#8217;t entirely the idiomatic Clojure. If you have any suggestions, don&#8217;t hesitate to write a comment.</p>
<p>Although I have been learning Clojure for 3 days, I&#8217;m already enjoying its expressiveness and the clean look. As a Java programmer, Clojure also gives me the benefit of full access to the Java land. The potential is endless!</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 8: 1000 digit number]]></title>
<link>http://sidhantgodiwala.wordpress.com/2009/10/18/problem-8-1000-digit-number/</link>
<pubDate>Sun, 18 Oct 2009 06:57:45 +0000</pubDate>
<dc:creator>Sidhant Godiwala</dc:creator>
<guid>http://sidhantgodiwala.wordpress.com/2009/10/18/problem-8-1000-digit-number/</guid>
<description><![CDATA[Find the largest product of 5 consecutive digits of a 1000 digit number This can be solved very eleg]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><strong>Find the largest product of 5 consecutive digits of a 1000 digit number</strong></p>
<p>This can be solved very elegantly in Clojure, the 1000 digit number can be found <a title="1000 digit number" href="http://projecteuler.net/index.php?section=problems&#38;id=8">here</a>.</p>
<pre>(def number-string "73167...")

(def digits (map #(- (int %) 48) number-string))

(apply max (map * digits (drop 1 digits) (drop 2 digits) (drop 3 digits) (drop 4 digits)))</pre>
<p><em>number-string</em> stores the 1000 digit number as a string</p>
<p><em>digits</em> creates a list of digits from <em>number-string</em>, 48 is the ASCII value for the character &#8216;0&#8242;, 49 for &#8216;1&#8242; and so on. Subtracting 48 from the character gives the digit value.</p>
<p><em>map</em> runs only till one of the list arguments terminates so the last line is valid</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Statistical Learning in Clojure Part 1: LDA &amp; QDA Classifiers]]></title>
<link>http://incanter-blog.org/2009/10/18/lda-qda/</link>
<pubDate>Sun, 18 Oct 2009 03:57:52 +0000</pubDate>
<dc:creator>liebke</dc:creator>
<guid>http://incanter-blog.org/2009/10/18/lda-qda/</guid>
<description><![CDATA[This will hopefully be the first of a series of posts based on a book that has substantially influen]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p>This will hopefully be the first of a series of posts based on a book that has substantially influenced me over the last several years, <a href="http://www.amazon.com/gp/product/0387848576?ie=UTF8&#38;tag=statisprograw-20&#38;linkCode=as2&#38;camp=1789&#38;creative=9325&#38;creativeASIN=0387848576">The Elements of Statistical Learning</a> (EoSL) by Hastie, Tibshirani, and Friedman (I went and got a degree in statistics essentially for the purposes of better understanding this book). Best of all, the pdf version of EoSL is now available free of charge at the <a href="http://www-stat.stanford.edu/~tibs/ElemStatLearn/">book&#8217;s website</a>, along with data, code, errata, and more.</p>
<p>This post will demonstrate the use of <a href="http://en.wikipedia.org/wiki/Linear_discriminant_analysis">Linear Discriminant Analysis</a> and <a href="http://en.wikipedia.org/wiki/Quadratic_classifier">Quadratric Discriminant Analysis</a> for classification, as described in chapter 4, &#8220;Linear Methods for Classification&#8221;, of EoSL. </p>
<p>I will implement the classifiers in Clojure and Incanter, and use the same data set as EoSL to train and test them. The data has 11 different classes, each representing a vowel sound, and 10 predictors, each representing processed audio information captured from eight male and seven female speakers. Details of the data are available <a href="http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/vowel.info">here</a>. The data is partitioned into a <a href="http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/vowel.train">training set</a> and a <a href="http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/vowel.test">test set</a>.</p>
<p>The task is to classify each of the N observations as one of the (K=11) vowel sounds using the (p=10) predictors. I will do this using a Bayes classifier built from both linear- and quadratic-discriminant functions. The Bayes classifier works by estimating the probability that an observation, x, belongs to each class, k, and then selects the class with the highest probability. Appropriately, Bayes rule is used to calculate the conditional probability , or posterior probability, for each class.</p>
<pre>(eq 4.7) <img src='http://l.wordpress.com/latex.php?latex=P%28G%3Dk%26%23124%3BX%3Dx%29+%3D+%5Cfrac%7BP%28X%3Dx%26%23124%3BG%3Dk%29%5Cpi_k%7D%7B%5Csum_%7Bl%3D1%7D%5E%7BK%7D+P%28X%3Dx%26%23124%3BG%3Dl%29%5Cpi_l%7D+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='P(G=k&#124;X=x) = \frac{P(X=x&#124;G=k)\pi_k}{\sum_{l=1}^{K} P(X=x&#124;G=l)\pi_l} ' title='P(G=k&#124;X=x) = \frac{P(X=x&#124;G=k)\pi_k}{\sum_{l=1}^{K} P(X=x&#124;G=l)\pi_l} ' class='latex' /> </pre>
<p>Where, <img src='http://l.wordpress.com/latex.php?latex=%5Cpi_k+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\pi_k ' title='\pi_k ' class='latex' /> is the prior probability of class k. If we model each class density as multivariate-normal, then <img src='http://l.wordpress.com/latex.php?latex=P%28X%3Dx%26%23124%3BG%3Dk%29+%3D+f_k%28x%29+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='P(X=x&#124;G=k) = f_k(x) ' title='P(X=x&#124;G=k) = f_k(x) ' class='latex' />, where </p>
<pre>(eq 4.8) <img src='http://l.wordpress.com/latex.php?latex=f_%7Bk%7D%28x%29+%3D+%5Cfrac%7B1%7D%7B%282%5Cpi%29%5E%7Bp%2F2%7D%26%23124%3B%5CSigma_k%26%23124%3B%5E%7B1%2F2%7D%7De%5E%7B-%5Cfrac%7B1%7D%7B2%7D%28x-%5Cmu_k%29%5ET%5CSigma_%7Bk%7D%5E%7B-1%7D%28x-%5Cmu_k%29%7D+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='f_{k}(x) = \frac{1}{(2\pi)^{p/2}&#124;\Sigma_k&#124;^{1/2}}e^{-\frac{1}{2}(x-\mu_k)^T\Sigma_{k}^{-1}(x-\mu_k)} ' title='f_{k}(x) = \frac{1}{(2\pi)^{p/2}&#124;\Sigma_k&#124;^{1/2}}e^{-\frac{1}{2}(x-\mu_k)^T\Sigma_{k}^{-1}(x-\mu_k)} ' class='latex' /> </pre>
<p>and the posterior probability becomes</p>
<pre>(eq 4.7) <img src='http://l.wordpress.com/latex.php?latex=P%28G%3Dk%26%23124%3BX%3Dx%29+%3D+%5Cfrac%7Bf_%7Bk%7D%28x%29%5Cpi_k%7D%7B%5Csum_%7Bl%3D1%7D%5E%7BK%7Df_l%28x%29%5Cpi_l%7D+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='P(G=k&#124;X=x) = \frac{f_{k}(x)\pi_k}{\sum_{l=1}^{K}f_l(x)\pi_l} ' title='P(G=k&#124;X=x) = \frac{f_{k}(x)\pi_k}{\sum_{l=1}^{K}f_l(x)\pi_l} ' class='latex' /> </pre>
<p>Since the denominator is the same for each class, we can ignore it, and if we take the log of the numerator and ignore the constant, <img src='http://l.wordpress.com/latex.php?latex=%282%5Cpi%29%5E%7Bp%2F2%7D+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='(2\pi)^{p/2} ' title='(2\pi)^{p/2} ' class='latex' />, in the density function, we end up with the quadratic discriminant function:</p>
<pre>(eq 4.12) <img src='http://l.wordpress.com/latex.php?latex=%5Cdelta_%7Bk%7D%28x%29%3D-%5Cfrac%7B1%7D%7B2%7Dlog%26%23124%3B%5CSigma_%7Bk%7D%26%23124%3B-%5Cfrac%7B1%7D%7B2%7D%28x-%5Cmu_k%29%5ET%5CSigma_%7Bk%7D%5E%7B-1%7D%28x-%5Cmu_k%29%2Blog%5Cpi_k+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\delta_{k}(x)=-\frac{1}{2}log&#124;\Sigma_{k}&#124;-\frac{1}{2}(x-\mu_k)^T\Sigma_{k}^{-1}(x-\mu_k)+log\pi_k ' title='\delta_{k}(x)=-\frac{1}{2}log&#124;\Sigma_{k}&#124;-\frac{1}{2}(x-\mu_k)^T\Sigma_{k}^{-1}(x-\mu_k)+log\pi_k ' class='latex' /></pre>
<p>If we further assume that the covariance matrices, <img src='http://l.wordpress.com/latex.php?latex=%5CSigma_k+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\Sigma_k ' title='\Sigma_k ' class='latex' /> are equal for each cluster, then more terms cancel out and we end up with the linear discriminant function,</p>
<pre>(eq 4.10) <img src='http://l.wordpress.com/latex.php?latex=%5Cdelta_%7Bk%7D%28x%29%3Dx%5ET%5CSigma%5E%7B-1%7D%5Cmu_k-%5Cfrac%7B1%7D%7B2%7D%5Cmu_k%5ET%5CSigma%5E%7B-1%7D%5Cmu_k%2Blog%5Cpi_k+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\delta_{k}(x)=x^T\Sigma^{-1}\mu_k-\frac{1}{2}\mu_k^T\Sigma^{-1}\mu_k+log\pi_k ' title='\delta_{k}(x)=x^T\Sigma^{-1}\mu_k-\frac{1}{2}\mu_k^T\Sigma^{-1}\mu_k+log\pi_k ' class='latex' /> </pre>
<p>Classification is then performed with these discriminant functions using the following decision rule:</p>
<pre>(eq 4.10a) <img src='http://l.wordpress.com/latex.php?latex=G%28x%29%3Dargmax_k%5Cdelta_k%28x%29+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='G(x)=argmax_k\delta_k(x) ' title='G(x)=argmax_k\delta_k(x) ' class='latex' /></pre>
<p>In other words, select the class, k, with the highest score for each observation.</p>
<p><b>Implementing the LDA Classifier</b></p>
<p>Start by loading the necessary Incanter libraries and the data sets from the EoSL website:</p>
<pre><code>(use '(incanter core stats charts io))
(def training (to-matrix
                (read-dataset "http://bit.ly/464h4h"
                              :header true)))
(def testing (to-matrix
               (read-dataset "http://bit.ly/1btCei"
                             :header true)))
</code></pre>
<p>Define the fixed parameters:</p>
<pre><code>(def K 11)
(def p 10)
(def N (nrow training))
(def group-counts (map nrow (group-by training 1)))
</code></pre>
<p>Since we don&#8217;t know the parameters of the multivariate-normal distributions used to model each cluster, we need to estimate them with the training data. First, estimate the prior probabilities for each cluster</p>
<pre>(eq 4.10b) <img src='http://l.wordpress.com/latex.php?latex=%5Chat%5Cpi_k%3DN_k%2FN+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\hat\pi_k=N_k/N ' title='\hat\pi_k=N_k/N ' class='latex' /></pre>
<p>where <img src='http://l.wordpress.com/latex.php?latex=N_k+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='N_k ' title='N_k ' class='latex' /> is the number of observations in class k, and <img src='http://l.wordpress.com/latex.php?latex=N+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='N ' title='N ' class='latex' /> is the total number of observations.</p>
<pre><code>(def prior-probs (div group-counts N))
</code></pre>
<p>Next, estimate the centroids for each cluster</p>
<pre>(eq 4.10c) <img src='http://l.wordpress.com/latex.php?latex=%5Chat%5Cmu_k%3D%5Csum_%7Bg_i%3Dk%7Dx_i%2FN_k+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\hat\mu_k=\sum_{g_i=k}x_i/N_k ' title='\hat\mu_k=\sum_{g_i=k}x_i/N_k ' class='latex' /> </pre>
<pre><code>(def cluster-centroids
  (matrix
    (for [x_k (group-by training 1 :cols (range 2 12))]
      (map mean (trans x_k)))))
</code></pre>
<p>Finally, estimate the covariance matrix to be used for all clusters,</p>
<pre>(eq 4.10d) <img src='http://l.wordpress.com/latex.php?latex=%5Chat%5CSigma%3D%5Csum_%7Bk%3D1%7D%5EK%5Csum_%7Bg_i%3Dk%7D%28x_i-%5Chat%5Cmu_k%29%28x_i-%5Chat%5Cmu_k%29%5ET%2F%28N-K%29+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\hat\Sigma=\sum_{k=1}^K\sum_{g_i=k}(x_i-\hat\mu_k)(x_i-\hat\mu_k)^T/(N-K) ' title='\hat\Sigma=\sum_{k=1}^K\sum_{g_i=k}(x_i-\hat\mu_k)(x_i-\hat\mu_k)^T/(N-K) ' class='latex' /></pre>
<pre><code>(def cluster-cov-mat
  (let [groups (group-by training 1 :cols (range 2 12))]
    (reduce plus
      (map (fn [group centroid n]
        (reduce plus
                (map #(div
                        (mmult (trans (minus % centroid))
                               (minus % centroid))
                        (- N K))
                     group)))
             groups cluster-centroids group-counts))))
</code></pre>
<p>and its inverse:</p>
<pre><code>(def inv-cluster-cov-mat (solve cluster-cov-mat))</code></pre>
<p>With the parameters estimated, now define the linear discriminant function based on equation 4.10 from EoSL:</p>
<pre>(eq 4.10) <img src='http://l.wordpress.com/latex.php?latex=%5Cdelta_%7Bk%7D%28x%29%3Dx%5ET%5CSigma%5E%7B-1%7D%5Cmu_k-%5Cfrac%7B1%7D%7B2%7D%5Cmu_k%5ET%5CSigma%5E%7B-1%7D%5Cmu_k%2Blog%5Cpi_k+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\delta_{k}(x)=x^T\Sigma^{-1}\mu_k-\frac{1}{2}\mu_k^T\Sigma^{-1}\mu_k+log\pi_k ' title='\delta_{k}(x)=x^T\Sigma^{-1}\mu_k-\frac{1}{2}\mu_k^T\Sigma^{-1}\mu_k+log\pi_k ' class='latex' /> </pre>
<pre><code>(defn ldf [x Sigma-inv mu_k pi_k]
  (+ (mmult x Sigma-inv (trans mu_k))
     (- (mult 1/2 (mmult mu_k Sigma-inv (trans mu_k))))
     (log pi_k)))
</code></pre>
<p>Differences in the equation, as implemented in the code, and equation 4.10 are because most of the arguments to the <code>ldf</code> function above are row vectors, instead of the column vectors used in eq 4.10.</p>
<p>Next, define a function that returns a matrix of linear discriminate scores, where rows are observations and columns are classes,</p>
<pre><code>(defn calculate-scores
  ([data inv-cov-mat centroids priors]
    (matrix
      (pmap (fn [row]
             (pmap (partial ldf row inv-cov-mat)
                   centroids
                   priors))
           (sel data :cols (range 2 12))))))
</code></pre>
<p>and calculate the scores for the training data</p>
<pre><code>(def training-lda-scores
  (calculate-scores training
                    inv-cluster-cov-mat
                    cluster-centroids
                    prior-probs))
</code></pre>
<p>Now, create a function that returns the index (i.e. class) with the maximum score for a given row (i.e. observation) of the scoring matrix,</p>
<pre><code>(defn max-index
  ([x]
    (let [max-x (reduce max x)
          n (length x)]
      (loop [i 0]
        (if (= (nth x i) max-x)
          i
          (recur (inc i)))))))
</code></pre>
<p>We can get a vector of the predicted class for every observation in the training set with the following line of code:</p>
<pre><code>(map max-index training-lda-scores)</code></pre>
<p>To determine the error rate of the classifier, create the following function,</p>
<pre><code>(defn error-rate [data scores]
  (/ (sum (map #(if (= %1 %2) 0 1)
               (sel data :cols 1)
               (plus 1 (map max-index scores))))
     (nrow data)))
</code></pre>
<p>and apply it to the score matrix for the training data,</p>
<pre><code>(error-rate training training-lda-scores)</code></pre>
<p>which returns an error rate of 0.316. </p>
<p>Now let&#8217;s see how the classifier performs on the test data. Start by calculating the linear discriminant score matrix,</p>
<pre><code>(def testing-lda-scores
  (calculate-scores testing
                    inv-cluster-cov-mat
                    cluster-centroids
                    prior-probs))
</code></pre>
<p>and then calculate the error-rate,</p>
<pre><code>(error-rate testing testing-lda-scores)</code></pre>
<p>which is 0.556. As expected the error rate is higher on the test data than the training data.</p>
<p><strong>Implementing the QDA Classifier</strong> </p>
<p>In order to use QDA to classify the data, we need to estimate a unique covariance matrix for each of the K classes, and write the quadratic discriminant function.</p>
<p>First, estimate the covariance matrices,</p>
<pre>(eq 4.10e) <img src='http://l.wordpress.com/latex.php?latex=%5Chat%5CSigma_k%3D%5Csum_%7Bg_i%3Dk%7D%28x_i-%5Chat%5Cmu_k%29%28x_i-%5Chat%5Cmu_k%29%5ET%2F%28N_k-1%29+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\hat\Sigma_k=\sum_{g_i=k}(x_i-\hat\mu_k)(x_i-\hat\mu_k)^T/(N_k-1) ' title='\hat\Sigma_k=\sum_{g_i=k}(x_i-\hat\mu_k)(x_i-\hat\mu_k)^T/(N_k-1) ' class='latex' /></pre>
<pre><code>(def cluster-cov-mats
  (let [groups (group-by training 1 :cols (range 2 12))]
    (map (fn [group centroid n]
      (reduce plus
              (map #(div
                      (mmult (trans (minus % centroid))
                             (minus % centroid))
                      (dec n))
                   group)))
           groups cluster-centroids group-counts)))
</code></pre>
<p>and their inverses.</p>
<pre><code>(def inv-cluster-cov-mats (map solve cluster-cov-mats))</code></pre>
<p>Next, create a function that implements the quadratic discriminant function:</p>
<pre>(eq 4.12) <img src='http://l.wordpress.com/latex.php?latex=%5Cdelta_%7Bk%7D%28x%29%3D-%5Cfrac%7B1%7D%7B2%7Dlog%26%23124%3B%5CSigma_%7Bk%7D%26%23124%3B-%5Cfrac%7B1%7D%7B2%7D%28x-%5Cmu_k%29%5ET%5CSigma_%7Bk%7D%5E%7B-1%7D%28x-%5Cmu_k%29%2Blog%5Cpi_k+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\delta_{k}(x)=-\frac{1}{2}log&#124;\Sigma_{k}&#124;-\frac{1}{2}(x-\mu_k)^T\Sigma_{k}^{-1}(x-\mu_k)+log\pi_k ' title='\delta_{k}(x)=-\frac{1}{2}log&#124;\Sigma_{k}&#124;-\frac{1}{2}(x-\mu_k)^T\Sigma_{k}^{-1}(x-\mu_k)+log\pi_k ' class='latex' /></pre>
<pre><code>(defn qdf [x Sigma_k Sigma-inv_k mu_k pi_k]
  (+ (- (mult 1/2 (log (det Sigma_k))))
     (- (mult 1/2 (mmult (minus x mu_k)
                         Sigma-inv_k
                         (trans (minus x mu_k)))))
     (log pi_k)))
</code></pre>
<p>Then calculate the quadratic discriminant score matrix and error rates for the training and test data, which are 0.0113 and 0.528 respectively. Notice that the error rate is substantially lower on the training data than it was with LDA (0.316), but the error rate on the test data is not much better than that achieved by LDA (0.556).</p>
<p><b>Improving Performance</b></p>
<p>Hastie, Tibshirani, and Friedman suggested performance of the calculations can be improved by first performing an Eigenvalue decomposition of the covariance matrices.</p>
<pre><img src='http://l.wordpress.com/latex.php?latex=%5CSigma_k%3DU_kD_kU_k%5ET+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\Sigma_k=U_kD_kU_k^T ' title='\Sigma_k=U_kD_kU_k^T ' class='latex' /></pre>
<p>where <img src='http://l.wordpress.com/latex.php?latex=U_k+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='U_k ' title='U_k ' class='latex' /> is the matrix of eigenvectors, and <img src='http://l.wordpress.com/latex.php?latex=D_k+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='D_k ' title='D_k ' class='latex' /> is a diagonal matrix of positive eigenvalues <img src='http://l.wordpress.com/latex.php?latex=d_%7Bkl%7D+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='d_{kl} ' title='d_{kl} ' class='latex' />. </p>
<pre><code>(def Sigma-decomp
  (map decomp-eigenvalue cluster-cov-matrices ))
(def D (map #(diag (:values %)) Sigma-decomp))
(def U (map #(:vectors %) Sigma-decomp))
</code></pre>
<p>Making the following substitutions to <img src='http://l.wordpress.com/latex.php?latex=%5Cdelta_k%28x%29+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\delta_k(x) ' title='\delta_k(x) ' class='latex' />,</p>
<pre>(a) <img src='http://l.wordpress.com/latex.php?latex=%28x-%5Chat%5Cmu_k%29%5ET%5Chat%5CSigma_k%5E%7B-1%7D%28x-%5Chat%5Cmu_k%29%3D%5BU_k%5ET%28x-%5Chat%5Cmu_k%29%5D%5ETD_k%5E%7B-1%7D%5BU_k%5ET%28x-%5Chat%5Cmu_k%29%5D+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='(x-\hat\mu_k)^T\hat\Sigma_k^{-1}(x-\hat\mu_k)=[U_k^T(x-\hat\mu_k)]^TD_k^{-1}[U_k^T(x-\hat\mu_k)] ' title='(x-\hat\mu_k)^T\hat\Sigma_k^{-1}(x-\hat\mu_k)=[U_k^T(x-\hat\mu_k)]^TD_k^{-1}[U_k^T(x-\hat\mu_k)] ' class='latex' /></pre>
<pre>(b) <img src='http://l.wordpress.com/latex.php?latex=log%26%23124%3B%5Chat%5CSigma_k%26%23124%3B%3D%5Csum_l+log%28d_%7Bkl%7D%29+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='log&#124;\hat\Sigma_k&#124;=\sum_l log(d_{kl}) ' title='log&#124;\hat\Sigma_k&#124;=\sum_l log(d_{kl}) ' class='latex' /></pre>
<p>results in the following quadratic discriminant function:</p>
<pre>(eq 4.12) <img src='http://l.wordpress.com/latex.php?latex=%5Cdelta_%7Bk%7D%28x%29%3D-%5Cfrac%7B1%7D%7B2%7D%5Csum_l+log%28d_%7Bkl%7D%29-%5Cfrac%7B1%7D%7B2%7D%5BU_k%5ET%28x-%5Chat%5Cmu_k%29%5D%5ETD_k%5E%7B-1%7D%5BU_k%5ET%28x-%5Chat%5Cmu_k%29%5D%2Blog%5Cpi_k+&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\delta_{k}(x)=-\frac{1}{2}\sum_l log(d_{kl})-\frac{1}{2}[U_k^T(x-\hat\mu_k)]^TD_k^{-1}[U_k^T(x-\hat\mu_k)]+log\pi_k ' title='\delta_{k}(x)=-\frac{1}{2}\sum_l log(d_{kl})-\frac{1}{2}[U_k^T(x-\hat\mu_k)]^TD_k^{-1}[U_k^T(x-\hat\mu_k)]+log\pi_k ' class='latex' /></pre>
<pre><code>(defn qdf [x D_k U_k mu_k pi_k]
  (+ (- (mult 1/2 (sum (map log (diag D_k)))))
     (- (mult 1/2
              (mmult (trans (mmult (trans U_k)
                                   (trans (minus x mu_k))))
                     (solve D_k)
                     (mmult (trans U_k)
                            (trans (minus x mu_k))))))
     (log pi_k)))
</code></pre>
<p>This version of the quadratic discriminant function is a bit faster than the previous version, while producing the exact same results. The performance improvement should be greater with larger data sets.</p>
<p>The complete code for this post can be found <a href="https://gist.github.com/afc8a7e35b3ed6286ee9">here</a>.</p>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 6: Sums of Squares of Sums]]></title>
<link>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-6/</link>
<pubDate>Wed, 14 Oct 2009 09:34:23 +0000</pubDate>
<dc:creator>Sidhant Godiwala</dc:creator>
<guid>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-6/</guid>
<description><![CDATA[Difference between sum of squares and square of sums Find the difference between the sum of squares ]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><strong>Difference between sum of squares and square of sums</strong></p>
<p>Find the difference between the sum of squares and the square of the sum of the integers 1 to 100</p>
<p>Using the functions <em>map</em> and <em>reduce</em>, this becomes trivial to implement</p>
<pre>(defn sqr [x]
    (* x x))

(let [terms (range 1 101)]
    (- (reduce + (map sqr terms)) (sqr (reduce + terms))))</pre>
<blockquote><p><em>map</em> takes a function and 1 or more lists and generates a new list of terms which are a result of the application of the function on each of the terms in the list/lists</p>
<pre>(map + '(1 2 3) '(1 2 3))
=&#62; (2 4 6)</pre>
</blockquote>
</div>]]></content:encoded>
</item>
<item>
<title><![CDATA[Problem 5: Lowest Common Multiple]]></title>
<link>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-5/</link>
<pubDate>Wed, 14 Oct 2009 08:48:24 +0000</pubDate>
<dc:creator>Sidhant Godiwala</dc:creator>
<guid>http://sidhantgodiwala.wordpress.com/2009/10/14/problem-5/</guid>
<description><![CDATA[Find the smallest number divisible by each of the numbers 1 to 20 The requirement is to find the Low]]></description>
<content:encoded><![CDATA[<div class='snap_preview'><p><strong>Find the smallest number divisible by each of the numbers 1 to 20</strong></p>
<p>The requirement is to find the <a title="Lowest Common Multiple" href="http://en.wikipedia.org/wiki/Least_common_multiple">Lowest Common Multiple</a> (LCM)of the numbers 1 to 20</p>
<p>We can very easily find the LCM using the  <a title="Greatest Common Divisor" href="http://en.wikipedia.org/wiki/Greatest_common_divisor">Greatest Common Divisor</a> (GCD) using the formula</p>
<pre>lcm(x, y) = (x * y) / gcd(x, y)</pre>
<p>A very fast method to calculate GCD is using <a title="Euclid's Algorithm" href="http://en.wikipedia.org/wiki/Euclidean_algorithm">Euclid&#8217;s algorithm</a></p>
<pre>gcd(x, y) = gcd(y, x mod y)</pre>
<p>Implementing this in Clojure</p>
<pre>(defn gcd [x y]
    (cond
	(zero? x) y
	(zero? y) x
	:else (recur y (mod x y))))

(defn lcm [x y]
    (/ (* x y) (gcd x y)))

(reduce lcm (range 1 21))</pre>
<p><em>gcd</em> calculates the greatest common divisor of 2 integers</p>
<pre>(gcd 6 8 )
=&#62; 2</pre>
<p><em>lcm</em> calculates the lowest common multiple of 2 integers</p>
<pre>(lcm 6 8 )
=&#62; 24</pre>
</div>]]></content:encoded>
</item>

</channel>
</rss>
