<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IANP.ORG &#187; Clojure</title>
	<atom:link href="http://ianp.org/tag/clojure/feed/" rel="self" type="application/rss+xml" />
	<link>http://ianp.org</link>
	<description>random thought and mutterings</description>
	<lastBuildDate>Thu, 25 Mar 2010 09:41:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Building Clojure with Maven</title>
		<link>http://ianp.org/2010/03/building-clojure-with-maven/</link>
		<comments>http://ianp.org/2010/03/building-clojure-with-maven/#comments</comments>
		<pubDate>Mon, 15 Mar 2010 18:48:51 +0000</pubDate>
		<dc:creator>Ian Phillips</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Maven]]></category>

		<guid isPermaLink="false">http://ianp.org/?p=495</guid>
		<description><![CDATA[Just a quick reminder on the steps I needed to take to build Clojure and install it in my local&#160;repository:


download the Maven Ant tasks, version 2.1.0 at the time of&#160;writing;
move them to $ANT_HOME/lib (this is /usr/share/ant/lib on Mac OS&#160;X);
cd to my Clojure download folder, run git pull if&#160;needed;
run ant -Dsnapshot.repo.dir=~/.m2/repository clean nightly-build, this will perform [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick reminder on the steps I needed to take to build <a href="http://clojure.org/">Clojure</a> and install it in my local&nbsp;repository:</p>

<ol>
<li><p>download the <a href="http://maven.apache.org/ant-tasks/download.html">Maven Ant tasks</a>, version 2.1.0 at the time of&nbsp;writing;</p></li>
<li><p>move them to <code>$ANT_HOME/lib</code> (this is <code>/usr/share/ant/lib</code> on Mac OS&nbsp;X);</p></li>
<li><p><code>cd</code> to my Clojure download folder, run <code>git pull</code> if&nbsp;needed;</p></li>
<li><p>run <code>ant -Dsnapshot.repo.dir=~/.m2/repository clean nightly-build</code>, this will perform a clean build and install it into the supplied repo; this can be anywhere, the default though is <code>/var/www/maven-snapshot-repository</code> which is probably no&nbsp;good;</p></li>
<li><p>to also install <code>clojure-contrib</code> just change to it&#8217;s download directory and run <code>mvn&nbsp;install</code>.</p></li>
</ol>

<p>Is&nbsp;simples!</p>
]]></content:encoded>
			<wfw:commentRss>http://ianp.org/2010/03/building-clojure-with-maven/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prevalent Synchronicity</title>
		<link>http://ianp.org/2010/01/prevalent-synchronicity/</link>
		<comments>http://ianp.org/2010/01/prevalent-synchronicity/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 13:23:33 +0000</pubDate>
		<dc:creator>Ian Phillips</dc:creator>
				<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Prevalent Systems]]></category>

		<guid isPermaLink="false">http://ianp.org/?p=486</guid>
		<description><![CDATA[Maybe it’s just an idea whose time has come, but in the past few days there’ve been 2 prevalent database systems announced for Clojure: FleetDB and&#160;Persister.

Prevalent&#160;Databases

The idea behind prevalent databases has been around for a while being, if not ‘popularised’ exactly, at least pushed by the guys behind Prevayler. Here’s how they describe&#160;them:


  Prevayler [...]]]></description>
			<content:encoded><![CDATA[<p>Maybe it’s just an idea whose time has come, but in the past few days there’ve been 2 prevalent database systems announced for <a href="http://clojure.org/">Clojure</a>: <a href="http://fleetdb.org/">FleetDB</a> and&nbsp;<a href="http://github.com/SergeyDidenko/Simple-Persistence-for-Clojure">Persister</a>.</p>

<h4>Prevalent&nbsp;Databases</h4>

<p>The idea behind prevalent databases has been around for a while being, if not ‘popularised’ exactly, at least pushed by the guys behind <a href="http://www.prevayler.org/">Prevayler</a>. Here’s how they describe&nbsp;them:</p>

<blockquote>
  <p>Prevayler is an open source object persistence library for Java. It is an implementation of the Prevalent System design pattern, in which business objects are kept live in memory and transactions are journaled for system&nbsp;recovery.</p>
</blockquote>

<h4>Fleet&nbsp;DB</h4>

<p>While <a href="http://twitter.com/mmcgrana">Mark McGranaghan</a>’s Fleet DB doesn’t use the term prevalent database, but right now that’s basically what it is. The core of Fleet DB is a Clojure based append-only log based database; it provides a native clojure query language (with built in query optimiser), schema-less records, indexes, and a server with a JSON based network&nbsp;protocol.</p>

<p>For a new new project Fleet DB also has a good set of documentation and it sounds like Mark has some big plans for it in the future. As an added benefit there are also clients for the network protocol in languages other than Clojure (Ruby officially, and a set of Python bindings in&nbsp;development).</p>

<h4>Persister</h4>

<p>Sergey Didenko’s <a href="http://github.com/SergeyDidenko/Simple-Persistence-for-Clojure">Simple Persistence for Clojure</a> project is a much less ambitious offering, but with the really cool feature of being a single (255 line, ~11KB) file that you could just drop into your project and start using - that’s pretty lightweight! From the read me&nbsp;file:</p>

<blockquote>
  <p>Simple Persistence for Clojure is a journal-based persistence library for Clojure programs. It follows “Prevalent system” design&nbsp;pattern.</p>
  
  <p>The intended usage is assist you in making a prevalent system. Thus you work with your in-memory data and wrap every writing call into one of (apply-transaction*)&nbsp;macros.</p>
</blockquote>

<p>A nice feature is that the log files are just Clojure&nbsp;code:</p>

<blockquote>
  <p>Transactions are logged as a valid Clojure code, so they are easy to read and run&nbsp;separately.</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://ianp.org/2010/01/prevalent-synchronicity/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Squeeze!</title>
		<link>http://ianp.org/2010/01/squeeze/</link>
		<comments>http://ianp.org/2010/01/squeeze/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 18:38:16 +0000</pubDate>
		<dc:creator>Ian Phillips</dc:creator>
				<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://ianp.org/?p=481</guid>
		<description><![CDATA[One of the neat features of Clojure is the sequence abstraction — it makes solving a whole host of data processing tasks much easier, simply get you data into a sequence and you&#8217;ve got a huge toolbox available to work on it. Of course being a guy I’m firmly of the belief that more tools are better, [...]]]></description>
			<content:encoded><![CDATA[<p>One of the neat features of <a href="http://clojure.org/">Clojure</a> is the <a href="http://clojure.org/sequences">sequence</a> abstraction — it makes solving a whole host of data processing tasks much easier, simply get you data into a sequence and you&#8217;ve got a huge toolbox available to work on it. Of course being a guy I’m firmly of the belief that more tools are better, with that in mind let’s add another one to our&nbsp;toolbox.</p>

<p>Given a sequence the squeeze function returns another sequence with any adjacent items which match a supplied predicate merged together using a supplied function. It’s probably easier to illustrate by example, suppose I have a sequence of strings and I want to merge them together when the trailing string starts with whitespace, I can squeeze them like&nbsp;this:</p>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>squeeze #<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">and</span> <span style="color: #66cc66;">%</span>2 <span style="color: #66cc66;">&#40;</span>re-matches #<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\A</span><span style="color: #000099; font-weight: bold;">\s</span>.*&quot;</span> <span style="color: #66cc66;">%</span>2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         #<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> str <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> concat <span style="color: #66cc66;">%&amp;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
         <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;hello&quot;</span> <span style="color: #ff0000;">&quot; world.&quot;</span> <span style="color: #ff0000;">&quot;foo&quot;</span> <span style="color: #ff0000;">&quot; bar&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>Another example, given a sequence of characters (read from an <code>InputStream</code> for example), I could group them into words by squeezing then thusly (the first line is just to remind you that calling <code>seq</code> on a string produces a sequence of&nbsp;characters):</p>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>seq <span style="color: #ff0000;">&quot;Cheers, chars!&quot;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>\C \h \e \e \r \s \<span style="color: #66cc66;">,</span> \space \c \h \a \r \s \<span style="color: #66cc66;">!</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>map str/trim
         <span style="color: #66cc66;">&#40;</span>squeeze #<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">and</span> <span style="color: #66cc66;">%</span>2 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">not</span><span style="color: #66cc66;">=</span> \space <span style="color: #66cc66;">%</span>2<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                  #<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> str <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> concat <span style="color: #66cc66;">%&amp;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
                   <span style="color: #66cc66;">&#40;</span>seq <span style="color: #ff0000;">&quot;I'm sorry Dave, I can't let you do that.&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;I'm&quot;</span> <span style="color: #ff0000;">&quot;sorry&quot;</span> <span style="color: #ff0000;">&quot;Dave,&quot;</span> <span style="color: #ff0000;">&quot;I&quot;</span> <span style="color: #ff0000;">&quot;can't&quot;</span> <span style="color: #ff0000;">&quot;let&quot;</span> <span style="color: #ff0000;">&quot;you&quot;</span> <span style="color: #ff0000;">&quot;do&quot;</span> <span style="color: #ff0000;">&quot;that.&quot;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>So how does it work? Well, here&#8217;s the&nbsp;interface:</p>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn squeeze
  <span style="color: #66cc66;">&#91;</span>pred merge-fn coll<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span>squeeze- pred merge-fn coll <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>And here&#8217;s the actual function that does the work, it&#8217;s declared private because I don’t want to expose the matched parameter to the outside&nbsp;world.</p>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn- squeeze-
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>pred merge-fn coll matched<span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span>lazy-seq
      <span style="color: #66cc66;">&#40;</span>when-<span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>s <span style="color: #66cc66;">&#40;</span>seq coll<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>f <span style="color: #66cc66;">&#40;</span>first s<span style="color: #66cc66;">&#41;</span>
              s <span style="color: #66cc66;">&#40;</span>second s<span style="color: #66cc66;">&#41;</span>
              rest <span style="color: #66cc66;">&#40;</span>rest coll<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>pred f s<span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span>squeeze- pred merge-fn rest <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> f matched<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>next <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> matched <span style="color: #66cc66;">&#40;</span>merge-fn <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> f <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">reverse</span> matched<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> f<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
              <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> next <span style="color: #66cc66;">&#40;</span>squeeze- pred merge-fn rest <span style="color: #b1b100;">nil</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>I should probably point out that all of this playing around with sequences was inspired by <a href="http://fulldisclojure.blogspot.com/">Sean Devlin</a>’s <a href="http://fulldisclojure.blogspot.com/2010/01/12-fn-proposal-same-multisame.html">excellent proposal</a> for some new sequence functions for Clojure&nbsp;1.2.</p>

<p>The full code for this is available <a href="/files/2010/01/squeeze.clj">here</a> (it’s just the above, but with an added doc comment on the <code>squeeze</code> function&nbsp;definition).</p>
]]></content:encoded>
			<wfw:commentRss>http://ianp.org/2010/01/squeeze/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Working with Java Arrays</title>
		<link>http://ianp.org/2010/01/working-with-java-arrays/</link>
		<comments>http://ianp.org/2010/01/working-with-java-arrays/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 14:23:44 +0000</pubDate>
		<dc:creator>Ian Phillips</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[Interop]]></category>

		<guid isPermaLink="false">http://ianp.org/?p=473</guid>
		<description><![CDATA[One improvement that I&#8217;d like to see in Clojure is more examples in the doc strings (or maybe in a separate :example metadata item). Still, nothing to stop me building up a set of my&#160;own.

So, here are some simple examples of working with Java arrays in&#160;Clojure…

Given some sample&#160;data:


&#40;def my-list '&#40;1 2 3 4 5&#41;&#41;
&#40;def my-vector [...]]]></description>
			<content:encoded><![CDATA[<p>One improvement that I&#8217;d like to see in Clojure is more examples in the doc strings (or maybe in a separate <code>:example</code> metadata item). Still, nothing to stop me building up a set of my&nbsp;own.</p>

<p>So, here are some simple examples of working with Java arrays in&nbsp;Clojure…</p>

<p>Given some sample&nbsp;data:</p>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>def my-<span style="color: #b1b100;">list</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def my-vector <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>def my-map <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">:</span><span style="color: #555;">a</span> <span style="color: #ff0000;">&quot;apple&quot;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">b</span> <span style="color: #ff0000;">&quot;banana&quot;</span> <span style="color: #66cc66;">:</span><span style="color: #555;">c</span> <span style="color: #ff0000;">&quot;chopped liver&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>


<p>To convert to Java&nbsp;arrays:</p>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>to-<span style="color: #b1b100;">array</span> my-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span>Object<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>Ljava<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>Object<span style="color: #808080; font-style: italic;">;@962522b&gt;</span>
<span style="color: #66cc66;">&#40;</span>to-<span style="color: #b1b100;">array</span> my-vector<span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span>Object<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>Ljava<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>Object<span style="color: #808080; font-style: italic;">;@37e55794&gt;</span>
<span style="color: #66cc66;">&#40;</span>to-<span style="color: #b1b100;">array</span> my-map<span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span>Object<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>Ljava<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>Object<span style="color: #808080; font-style: italic;">;@52cd19d&gt;</span></pre></div></div>


<p>Note that this always returns <code>Object[]</code> regardless of the contents of the collection. Note also that the map isn&#8217;t flattened (the pp function used here is in&nbsp;<code>clojure.contrib.pprint</code>):</p>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>pp<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">:</span><span style="color: #555;">a</span> <span style="color: #ff0000;">&quot;apple&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">:</span><span style="color: #555;">b</span> <span style="color: #ff0000;">&quot;banana&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">:</span><span style="color: #555;">c</span> <span style="color: #ff0000;">&quot;chopped liver&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span></pre></div></div>


<p>If the array is 2-dimensional there is a corresponding&nbsp;function:</p>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>def my-vec-2d <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">8</span> <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
#'user/my-vec-2d
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>to-array-2d my-vec-2d<span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span>Object<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>Ljava<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>Object<span style="color: #808080; font-style: italic;">;@3a42f352&gt;</span>
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>pp<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #b1b100;">nil</span></pre></div></div>


<p>If you need to use a specific type of array (e.g. to pass a <code>String[]</code> into a Java method) or need to use an array with more than 3 dimensions it&#8217;s a little&nbsp;trickier:</p>


<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;">user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>into-<span style="color: #b1b100;">array</span> my-<span style="color: #b1b100;">list</span><span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span>Integer<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>Ljava<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span><span style="color: #b1b100;">Integer</span><span style="color: #808080; font-style: italic;">;@60c0c8b5&gt;</span>
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>pp<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #b1b100;">nil</span>
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>into-<span style="color: #b1b100;">array</span> my-vector<span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span>Integer<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>Ljava<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span><span style="color: #b1b100;">Integer</span><span style="color: #808080; font-style: italic;">;@2151b0a5&gt;</span>
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>pp<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #b1b100;">nil</span>
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>into-<span style="color: #b1b100;">array</span> my-map<span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span>MapEntry<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>Lclojure<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>MapEntry<span style="color: #808080; font-style: italic;">;@7ae0c7c3&gt;</span>
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>into-<span style="color: #b1b100;">array</span> <span style="color: #66cc66;">&#40;</span>vals my-map<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
#<span style="color: #66cc66;">&lt;</span>String<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>Ljava<span style="color: #66cc66;">.</span>lang<span style="color: #66cc66;">.</span>String<span style="color: #808080; font-style: italic;">;@731de9b&gt;</span>
user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>pp<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;apple&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;banana&quot;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">&quot;chopped liver&quot;</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #b1b100;">nil</span></pre></div></div>


<p>There, that should serve as a handy reference for myself for when I’m feeling&nbsp;forgetful…</p>
]]></content:encoded>
			<wfw:commentRss>http://ianp.org/2010/01/working-with-java-arrays/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Paredit.el Comes to IntelliJ</title>
		<link>http://ianp.org/2009/12/paredit-el-comes-to-intellij/</link>
		<comments>http://ianp.org/2009/12/paredit-el-comes-to-intellij/#comments</comments>
		<pubDate>Fri, 25 Dec 2009 20:21:47 +0000</pubDate>
		<dc:creator>Ian Phillips</dc:creator>
				<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Emacs]]></category>
		<category><![CDATA[IntelliJ]]></category>

		<guid isPermaLink="false">http://ianp.org/?p=465</guid>
		<description><![CDATA[I’ve been working on adding paredit.el like structural editing to the next version of the La Clojure plugin for Intellij IDEA. IDEA already does most of the paren matching stuff (automatically inserting a closing paren when you type an opening paren and so on). So far I’ve got the basic barf and slurp commands working, [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been working on adding <a href="http://mumble.net/~campbell/emacs/paredit.el">paredit.el</a> like structural editing to the next version of the <a href="http://plugins.intellij.net/plugin/?id=4050">La Clojure</a> plugin for <a href="http://www.jetbrains.com/idea/index.html">Intellij IDEA</a>. IDEA already does most of the paren matching stuff (automatically inserting a closing paren when you type an opening paren and so on). So far I’ve got the basic barf and slurp commands working, and splicing, as you can see in the screenshot&nbsp;below:</p>

<p><img src="/images/2009/12/idea-slurp-and-barf.png" alt="Slurping and Barfing" /></p>

<p>The next step is probably to make IDEA’s expand selection code be a little smarter in the face of&nbsp;s-expressions.</p>

<p>In related news: I found a good <a href="http://www.slideshare.net/mudphone/paredit-preso">introduction to paredit.el</a> on <a href="http://www.slideshare.net/">SlideShare</a> which may be of&nbsp;interest.</p>

<p>I’ll try to get the guys at IntelliJ to push out a new version of the plugin after the 9.0.1 release is out (it’s in beta&nbsp;now).</p>
]]></content:encoded>
			<wfw:commentRss>http://ianp.org/2009/12/paredit-el-comes-to-intellij/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clojure Purists?</title>
		<link>http://ianp.org/2009/12/clojure-purists/</link>
		<comments>http://ianp.org/2009/12/clojure-purists/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 14:39:25 +0000</pubDate>
		<dc:creator>Ian Phillips</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://ianp.org/?p=463</guid>
		<description><![CDATA[I’ve been following Tim Bray’s excellent Concur.next article series covering approaches to concurrency in various languages, and currently (no pun intended!) covering Clojure. The latest article talks about a super efficient log parsing implementation done by Alex Osborne an includes the following&#160;comment:


  “Lots of the performance wins come from dipping into Java-land (AtomicLongs, LinkedBlockingQueue), [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve been following <a href="http://www.tbray.org/ongoing/">Tim Bray</a>’s excellent <a href="http://www.tbray.org/ongoing/When/200x/2009/09/27/Concur-dot-next">Concur.next</a> article series covering approaches to concurrency in various languages, and currently (no pun intended!) covering Clojure. The <a href="http://www.tbray.org/ongoing/When/200x/2009/12/15/Osborne-WF2-Clojure">latest article</a> talks about a super efficient log parsing <a href="http://meshy.org/2009/12/13/widefinder-2-with-clojure.html">implementation</a> done by <a href="http://meshy.org/">Alex Osborne</a> an includes the following&nbsp;comment:</p>

<blockquote>
  <p>“Lots of the performance wins come from dipping into Java-land (AtomicLongs, LinkedBlockingQueue), which is perfectly OK, but a Clojure purist would probably see those occasions as maybe highlighting gaps in that language’s&nbsp;coverage.”</p>
</blockquote>

<p>I’d take issue with that, one of the real strengths of Clojure it that it has easy and fast access to the whole Java ecosystem. As <a href="http://clojure.org/jvm_hosted">Rich&nbsp;says</a>:</p>

<blockquote>
  <p>“Clojure is designed to be a hosted language, sharing the JVM type system, GC, threads etc. It compiles all functions to JVM bytecode. Clojure is a great Java library consumer, offering the dot-target-member notation for calls to&nbsp;Java.”</p>
</blockquote>

<p>That seems pretty clear to me. I wonder if the people claiming to be Clojure purists are all coming from a Lisp background rather than the Java&nbsp;world?</p>
]]></content:encoded>
			<wfw:commentRss>http://ianp.org/2009/12/clojure-purists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clojure Evaluation</title>
		<link>http://ianp.org/2009/12/clojure-evaluation/</link>
		<comments>http://ianp.org/2009/12/clojure-evaluation/#comments</comments>
		<pubDate>Mon, 07 Dec 2009 17:44:50 +0000</pubDate>
		<dc:creator>Ian Phillips</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Macros]]></category>

		<guid isPermaLink="false">http://ianp.org/?p=459</guid>
		<description><![CDATA[I&#8217;ve been loking at the early-access version of Manning’s forthcoming Clojure in Action book as well as some of the criticism of it. One of the complaints is that the current drafts describe macros as a run-time concept and that this is wrong. The confusion arises from the fact that Clojure (and Lisps in general) [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been loking at the early-access version of <a href="http://www.manning.com/">Manning</a>’s forthcoming <a href="http://www.manning.com/rathore/">Clojure in Action</a> book as well as some of the <a href="http://www.manning-sandbox.com/thread.jspa?threadID=35150&amp;tstart=0">criticism</a> of it. One of the complaints is that the current drafts describe macros as a run-time concept and that this is wrong. The confusion arises from the fact that <a href="http://clojure.org/">Clojure</a> (and Lisps in general) don&#8217;t follow the same path from source to execution as a more conventional programming languages like C and Java. I&#8217;ll compare four different languages to see how they differ: C, Java, a traditional Lisp compiler, and&nbsp;Clojure.</p>

<h3>C Like&nbsp;Languages</h3>

<p>This is what most people are used to, the traditional compiled language. Here, source code is read in and then parsed (this is normally broken out into multiple stages, e.g. a separate lexing stage, but for our purposes we can gloss over these details). The parser emits some form of intermediate representation, usually an abstract syntax tree (AST), this is then used by the compiler to generate executable&nbsp;code.</p>

<p><img src="/images/2009/12/c-compilation-stages.png" alt="C Like Languages" /></p>

<p>Again, this potentially glosses over some details: optimizers can world on the intermediate representation for example, or the compiler could require a separate linking stage to generate an executable. For our purposes though this is sufficient: we go from source to AST to&nbsp;executable.</p>

<p>Also, it could be argued that the C pre-processor operates on the source code before the parser sees it, but in practice the C macros system is so primitive it doesn&#8217;t really warrant being called out as a separate&nbsp;stage.</p>

<h3>Java Like&nbsp;Languages</h3>

<p><img src="/images/2009/12/java-compilation-stages.png" alt="Java Like Languages" /></p>

<p>The Java like languages differ from C in that they run on top of a virtual machine rather than being executed directly by the OS; as a reult their compiler emits ‘bytecode’ rather than a finished executable. This bytecode is then executed by the virtual machine. In all modern desktop and server virtual machines this is means just in time compiling the bytecode down to native machine&nbsp;code.</p>

<h3>Traditional&nbsp;Lisps</h3>

<p>The Lisp view of things is a little different; it&#8217;s more complicated but also more powerful. The first thing to note is that Lisp code is already basically in the form of an AST - there is no (or not much) syntax getting in the way. Next, there are 2 types of macros which are applied to Lisp code: macros and reader macros. I&#8217;ll duscuss them in the opposite order to the way they are&nbsp;applied…</p>

<p><img src="/images/2009/12/lisp-compilation-stages.png" alt="Lisp Like Languages" /></p>

<p>The standard type of Lisp macros are what most people rave about when extolling the virtues of the language: these are chunks of code that are executed after the source has been loaded into an AST (remember, Lisp source code is basically in this form already, so this just involves moving from a textual representation to something that the Lisp runtime can work with). If a node in the AST is a macro then it is evaluated as the code is loaded and the result of the evaluation is used to <em>replace the macro node in the AST</em>. Stop and think about that for a minute - this happens <em>before</em> the code is evaluated by the regular Lisp runtime, but yet at this point you already have access to the full Lisp programming language. All of this means that you can do some <a href="http://weitz.de/macros.lisp">pretty</a> <a href="http://brierwooddesign.com/2009/2/12/fun-with-lisp-macros">cool</a> <a href="http://www.paulgraham.com/avg.html">tricks</a>: how about writing your own control constructs? Writing a DSL compiler? Logging constructs that have <em>zero</em> runtime overhead when not used (but that can be switched on and off by users of the program, unlike <code>#define DEBUG 0</code> in&nbsp;C)?</p>

<p>The second, and much less common, type of macro is the <em>reader macro</em>. Reader macros operate on the character stream as it is read in, before the AST is constructed. Basically, when the reader sees a specific character (usually #) it then looks at the next character and uses that as a key into a table of functions (the read table) that tell it how to handle future input. Using reader macros it&#8217;s possible to create DSLs that don&#8217;t use s-expression syntax (s-expressions are the paren enclosed lists that Lisp is (in)famous for); or do something as simple as allowing the use of brackets to <a href="http://www.psg.com/~dlamkins/sl/chapter03-12.html">write quoted lists</a> without needing an explicit quote (i.e. writing <code>[1 2 3]</code> instead of <code>'(1 2&nbsp;3)</code>).</p>

<p>Only once all of this has finished is a traditional lisp ready to let it&#8217;s compiler go to work turning the (now macro-free) AST into executable&nbsp;code.</p>

<h3>Clojure</h3>

<p>Clojure is very similar to the traditional Lisp model, with two main differences. The first difference is the fact that, like Java, the output is bytecode which is then loaded and executed by a standard Java virtual machine. The second difference is that while Clojure does have <a href="http://clojure.org/reader#toc1">reader macros</a>, the read table isn&#8217;t exposed to user programs; that is, while it operates in the same way as a traditional Lisp there is no way for user code to alter the behaviour of the reader. This is probably good thing as Clojure includes a relatively large number of predefined reader macros including a literal syntax for lists, sets, and maps, as well as lambda-expressions (anonymous functions) and&nbsp;metadata.</p>

<h3>Summary</h3>

<p>C and Java like languages have a huge amount of syntax baked in, but don&#8217;r provide any way to modify this or to manipulate the program before it is compiled. Lisp has almost no syntax but provides a mechanism for users to add their own, and provides a mechanism to manipulate programs before they are compiled. Clojure has some syntax (more than other Lisps, but way less than C/Java/&amp;c.) and provides the same mechanism for program manipulation as other&nbsp;Lisps.</p>

<p>The OmniGraffle file for the images in this post is <a href="http://ianp.org/images/2009/12/compilation-stages.graffle.gz">available here</a> if anybody is&nbsp;interested.</p>

<p><em>Update:</em> this post is intended to compare traditional C-style languages with Lisps, it doesn&#8217;t cover, for examples, so-called scripting languages such as Perl, Python, and&nbsp;Ruby.</p>
]]></content:encoded>
			<wfw:commentRss>http://ianp.org/2009/12/clojure-evaluation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Strawman Arguments and Coding Styles</title>
		<link>http://ianp.org/2009/11/strawman-arguments-and-coding-styles/</link>
		<comments>http://ianp.org/2009/11/strawman-arguments-and-coding-styles/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 12:11:09 +0000</pubDate>
		<dc:creator>Ian Phillips</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://ianp.org/?p=443</guid>
		<description><![CDATA[So there’s this blog post over on the Best in Class blog that talks about ceremony in programming languages and compares Clojure with Java on this basis. While I’d agree with the basic premise of the article (that there is less ceremony in Clojure), I&#8217;m less keen on the way it’s presented: by way of [...]]]></description>
			<content:encoded><![CDATA[<p>So there’s <a href="http://blog.bestinclass.dk/index.php/2009/09/java-vs-clojure-lets-talk-ceremony/">this blog post</a> over on the <a href="http://blog.bestinclass.dk/">Best in Class</a> blog that talks about ceremony in programming languages and compares <a href="http://clojure.org/">Clojure</a> with <a href="http://java.sun.com/">Java</a> on this basis. While I’d agree with the basic premise of the article (that there is less ceremony in Clojure), I&#8217;m less keen on the way it’s presented: by way of a needlessly verbose strawman example. To be fair the article does kind of admit that this is what is being done, but it’s still&nbsp;annoying.</p>

<p>With this in mind let’s see how well we can do with the Java version of the code, relying on a better coding style and a couple of freely available libraries (one of the platforms much touted strengths). For the original — 28 line — version of the code I’ll refer you to the <a href="http://blog.bestinclass.dk/index.php/2009/09/java-vs-clojure-lets-talk-ceremony/">original post</a> (but warn you that it’s presented in that well known code storage format,&nbsp;PNG!).</p>

<p>The same code rewritten in a smarter manner, but still using only the core Java libraries. This gets it down to 10 lines of code and also makes the intent of the code clearer. There&#8217;s still a fair amount of ceremony about this however: the multiple imports, and all of the class and static main method&nbsp;boilerplate.</p>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Arrays</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Set</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.HashSet</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Distinct <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Set<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> distinct <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Arrays</span>.<span style="color: #006633;">asList</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">&quot;foo&quot;</span>, <span style="color: #0000ff;">&quot;bar&quot;</span>, <span style="color: #0000ff;">&quot;baz&quot;</span>, <span style="color: #0000ff;">&quot;foo&quot;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>Let&#8217;s see if we can&#8217;t do a little better with the addition of some open source libraries. Enter <a href="http://code.google.com/p/google-collections/">Google Collections</a>, a really neat library that improves the collections API from the JDK. We&#8217;re now down to 7 lines of code, and 2 of those are just closing braces! In any reasonably size program the class and main statements disappear into the noise, so we&#8217;re really saying that we have 2 import statements and a single line of code. That&#8217;s not too different from the Clojure version all things&nbsp;considered.</p>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Set</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">common</span>.<span style="color: #006633;">collect</span>.<span style="color: #006633;">Sets</span>.<span style="color: #006633;">newHashSet</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Distinct <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>... <span style="color: #006633;">args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Set<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> distinct <span style="color: #339933;">=</span> newHashSet<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;foo&quot;</span>, <span style="color: #0000ff;">&quot;bar&quot;</span>, <span style="color: #0000ff;">&quot;baz&quot;</span>, <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>It&#8217;s interesting to note that the second Java version weighs in at 10 lines of code, versus 8 for the equivalent clojure version; not much of a difference&nbsp;really.</p>

<p>I think that the benefits of Clojure come from it&#8217;s <a href="http://clojure.org/functional_programming">functional style</a>, <a href="http://clojure.org/macros">macro system</a>, and excellent <a href="http://clojure.org/concurrent_programming">concurrency support</a> — not from the fact that you can save a few lines of code here and&nbsp;there.</p>
]]></content:encoded>
			<wfw:commentRss>http://ianp.org/2009/11/strawman-arguments-and-coding-styles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Embedding Clojure (part 2)</title>
		<link>http://ianp.org/2009/04/embedding-clojure-part-2/</link>
		<comments>http://ianp.org/2009/04/embedding-clojure-part-2/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 08:20:08 +0000</pubDate>
		<dc:creator>Ian Phillips</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://ianp.org/?p=100</guid>
		<description><![CDATA[Following on from the last post, it actually turns out to be much easier to do most of the work in Clojure itself — no need for all of that tiresome messing around with Vars and Symbolss on the Java side of things! The trick is to define an abstract class in Java to set a few [...]]]></description>
			<content:encoded><![CDATA[<p>Following on from the last post, it actually turns out to be much easier to do most of the work in Clojure itself — no need for all of that tiresome messing around with <code>Var</code>s and <code>Symbols</code>s on the Java side of things! The trick is to define an abstract class in Java to set a few things up and use as a hook, than implement this in Clojure. I&#8217;ll go through both sides of this, starting with the Java&nbsp;stuff.</p>

<h3>The Abstract Class in&nbsp;Java</h3>

<p>Basically, I&#8217;m using the Java side of things to set up my text pane with a standard stylesheet (I&#8217;d like it to use a proportional font, with different colours for input, output, and error text) and to install a key listener to send commands to the Clojure repl whenever the user hits enter or&nbsp;return.</p>

<p>The basic class then,&nbsp;is</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> InteractiveConsole <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Document</span> _document <span style="color: #339933;">=</span> createStyledDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">JTextPane</span> _textpane<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">PipedWriter</span> _inWriter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PipedWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Reader</span> _in<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Writer</span> _out <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PrintWriter</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DocWriter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;output&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Writer</span> _err <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PrintWriter</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DocWriter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> InteractiveConsole<span style="color: #009900;">&#40;</span><span style="color: #003399;">JTextPane</span> textpane<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
        _textpane <span style="color: #339933;">=</span> textpane<span style="color: #339933;">;</span>
        _in <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">PipedReader</span><span style="color: #009900;">&#40;</span>_inWriter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>The <code>createStyledDocument</code> method, which I won&#8217;t include here, just sets up the style context and my colour scheme. The <code>DocWriter</code> class that is references is an trivial writer subclass that just calls <code>insertString</code> on the document with the named style. The other class that I&#8217;ll be wanting to use is a runnable so that I can launch the Clojure REPL on it&#8217;s own thread. It&#8217;s about as trivial as it gets, it just calls back into the two abstract methods that I&#8217;m going to provide to provide my Clojure code somewhere to hook&nbsp;onto.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> ConsoleRunner <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Runnable</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Map<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>String, Object<span style="color: #339933;">&gt;</span> _context<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> ConsoleRunner<span style="color: #009900;">&#40;</span>Map<span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>String, Object<span style="color: #339933;">&gt;</span> context<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      _context <span style="color: #339933;">=</span> context<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> run<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Map</span>.<span style="color: #006633;">Entry</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>String, Object<span style="color: #339933;">&gt;</span> var <span style="color: #339933;">:</span> _context.<span style="color: #006633;">entrySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            bindVariable<span style="color: #009900;">&#40;</span>var.<span style="color: #006633;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, var.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        doStart<span style="color: #009900;">&#40;</span>_in, _out, _err<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>With this I can provide a start method that installs my key listener and then launches a new thread with an instance of this runnable. The two hook methods that I&#8217;m providing&nbsp;are</p>

<ul>
<li><p><code>abstract void bindVariable(String,Object)</code> to allow me to set up some domain objects on the clojure side of things;&nbsp;and</p></li>
<li><p><code>abstract void doStart(Reader,Writer,Writer)</code> to actually start the REPL, using the provided input, output, and error&nbsp;streams.</p></li>
</ul>

<h3>The Clojure&nbsp;Implementation</h3>

<p>Turns out to be trivial as well, the implementation of <code>bindVariable</code> just interns the object passed in into the user namespace, it&#8217;s a one liner in&nbsp;Clojure.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn -bindVariable <span style="color: #66cc66;">&#91;</span>this <span style="color: #b1b100;">name</span> <span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">intern</span> 'user <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">symbol</span> <span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">value</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>


<p>The <code>doStart</code> method isn&#8217;t much more involved either, it just sets up the bindings and then launches the&nbsp;REPL.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn -doStart <span style="color: #66cc66;">&#91;</span>this #<span style="color: #66cc66;">^</span>Reader in #<span style="color: #66cc66;">^</span>Writer out #<span style="color: #66cc66;">^</span>Writer err<span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span>binding <span style="color: #66cc66;">&#91;</span>*in*  <span style="color: #66cc66;">&#40;</span>LineNumberingPushbackReader<span style="color: #66cc66;">.</span> in<span style="color: #66cc66;">&#41;</span>
              *out* out
              *err* err<span style="color: #66cc66;">&#93;</span>
        <span style="color: #66cc66;">&#40;</span>clojure<span style="color: #66cc66;">.</span>main/repl<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>


<p>Notice that here I have added type annotations so that the correct method gets implemented, without these the Clojure code compiled but then I got abstract method errors at runtime. Check out the docstring for the <code>repl</code> function as well, there are a few useful options (for example in my actual code I have an <code>:init</code> function to switch to the user namespace, and a custom&nbsp;prompt).</p>

<p>For completeness, here&#8217;s the rest of the Clojure file with the code required to inherit from the Java base&nbsp;class.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>ns com<span style="color: #66cc66;">.</span>example<span style="color: #66cc66;">.</span>ClojureConsole
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">import</span> <span style="color: #66cc66;">&#40;</span>clojure<span style="color: #66cc66;">.</span>lang LineNumberingPushbackReader<span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>java<span style="color: #66cc66;">.</span>io Reader Writer<span style="color: #66cc66;">&#41;</span>
             <span style="color: #66cc66;">&#40;</span>java<span style="color: #66cc66;">.</span>util Map<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">require</span> <span style="color: #66cc66;">&#40;</span>clojure main<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">:</span><span style="color: #555;">gen-class</span>
     <span style="color: #66cc66;">:</span><span style="color: #555;">extends</span> bg<span style="color: #66cc66;">.</span>beer<span style="color: #66cc66;">.</span>editor<span style="color: #66cc66;">.</span>InteractiveConsole
     <span style="color: #66cc66;">:</span><span style="color: #555;">init</span> init
     <span style="color: #66cc66;">:</span><span style="color: #555;">constructors</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#91;</span>javax<span style="color: #66cc66;">.</span>swing<span style="color: #66cc66;">.</span>JTextPane<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span>String javax<span style="color: #66cc66;">.</span>swing<span style="color: #66cc66;">.</span>JTextPane<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#40;</span>defn -init <span style="color: #66cc66;">&#91;</span>textpane<span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#91;</span>textpane<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>


<h3>Summary</h3>

<p>This approach has the advantage that any additional configuration can happen in the clojure code. It would also be easy, for example, to have an additional script that was always run at start up, to allow the user to customize the console further (similar to the .emacs file in&nbsp;Emacs).</p>

<p>You could also move most of the work that I&#8217;m doing in Java into the Clojure code. I haven&#8217;t done this as I may want to support multiple languages in my console and it&#8217;s nice to have a common stylesheet and keybindings (e.g. for history) across languages. Your mileage may&nbsp;vary.</p>
]]></content:encoded>
			<wfw:commentRss>http://ianp.org/2009/04/embedding-clojure-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Embedding Clojure in an Existing Application</title>
		<link>http://ianp.org/2009/04/embedding-clojure-in-an-existing-application/</link>
		<comments>http://ianp.org/2009/04/embedding-clojure-in-an-existing-application/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 10:57:37 +0000</pubDate>
		<dc:creator>Ian Phillips</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Lisp]]></category>
		<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://ianp.org/?p=99</guid>
		<description><![CDATA[I&#8217;ve been taking a look at Clojure lately, as a JVM friendly flavour of lisp I&#8217;ve got to say it looks pretty interesting. One problem that I&#8217;ve had though is that all of the documentation out there (of which there&#8217;s very little, to be honest) seems to assume that you&#8217;ll be writing/running a pure Clojure [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been taking a look at <a href="http://www.clojure.org/">Clojure</a> lately, as a JVM friendly flavour of lisp I&#8217;ve got to say it looks pretty interesting. One problem that I&#8217;ve had though is that all of the documentation out there (of which there&#8217;s very little, to be honest) seems to assume that you&#8217;ll be writing/running a pure Clojure program as you main application. There&#8217;s plenty of information about calling Java code from Clojure programs, and <em>some</em> information about extending Java classes and interfaces with Clojure code, but nothing about getting the two talking together at&nbsp;runtime.</p>

<p>So here&#8217;s how it&#8217;s&nbsp;done.</p>

<p>First off you need to set up the symbols and namespaces that you are going to need to start up a clojure&nbsp;environment.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">Symbol main <span style="color: #339933;">=</span> Symbol.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;main&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Symbol clojureMain <span style="color: #339933;">=</span> Symbol.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;clojure.main&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Symbol user <span style="color: #339933;">=</span> Symbol.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Symbol require <span style="color: #339933;">=</span> Symbol.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;require&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Namespace userNS <span style="color: #339933;">=</span> Namespace.<span style="color: #006633;">findOrCreate</span><span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Namespace clojureMainNS <span style="color: #339933;">=</span> Namespace.<span style="color: #006633;">findOrCreate</span><span style="color: #009900;">&#40;</span>clojureMain<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>Once you have these you can <em>require</em> the clojure main namespace, this is the same one that is used to run scripts or start a REPL from the comand&nbsp;line.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">Var.<span style="color: #006633;">intern</span><span style="color: #009900;">&#40;</span>RT.<span style="color: #006633;">CLOJURE_NS</span>, require<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>clojureMain<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>Then, and this is the bit that I had trouble working out, you need to bind your application&#8217;s domain model (or at least those bits of it that you want to expose) into the <em>user</em> namespace in&nbsp;Clojure.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Map</span>.<span style="color: #006633;">Entry</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>String, Object<span style="color: #339933;">&gt;</span> global <span style="color: #339933;">:</span> globals.<span style="color: #006633;">entrySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> key <span style="color: #339933;">=</span> global.<span style="color: #006633;">getKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Object</span> value <span style="color: #339933;">=</span> global.<span style="color: #006633;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Var.<span style="color: #006633;">intern</span><span style="color: #009900;">&#40;</span>userNS, Symbol.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span>, value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>Finally you&#8217;re ready to grab the <em>main</em> method and run&nbsp;it.</p>

<pre class="brush:java">
Var.intern(clojureMainNS, main).applyTo(RT.seq(new String[0]));
</pre>

<p>The emtpy string array here is emulating an emty sargument list at the command&nbsp;line.</p>

<p>Some things to note here: you need to <em>intern</em> <em>vars</em> before you can use them, even for core library features like <em>require</em>, this surprised me at first but when you remember that most Lisps are built around a very small core of special forms with everything else defined in Lisp, it makes some&nbsp;sense.</p>
]]></content:encoded>
			<wfw:commentRss>http://ianp.org/2009/04/embedding-clojure-in-an-existing-application/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
