<?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/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
	xmlns:media="http://search.yahoo.com/mrss/"
>

<channel>
	<title>Object Commando</title>
	<atom:link href="http://www.objectcommando.com/blog/feed/rss/" rel="self" type="application/rss+xml" />
	<link>http://www.objectcommando.com/blog</link>
	<description>languages, development and design</description>
	<lastBuildDate>Thu, 12 Aug 2010 12:07:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<!-- podcast_generator="podPress/8.8" - maintenance_release="8.8.4" -->
		<copyright>Copyright &#xA9; Object Commando 2010 </copyright>
		<managingEditor>ryan@objectcommando.com (Object Commando)</managingEditor>
		<webMaster>ryan@objectcommando.com (Object Commando)</webMaster>
		<category>posts</category>
		<itunes:keywords></itunes:keywords>
		<itunes:subtitle></itunes:subtitle>
		<itunes:summary>languages, development and design</itunes:summary>
		<itunes:author>Object Commando</itunes:author>
		<itunes:category text="Society &amp; Culture"/>
		<itunes:owner>
			<itunes:name>Object Commando</itunes:name>
			<itunes:email>ryan@objectcommando.com</itunes:email>
		</itunes:owner>
		<itunes:block>No</itunes:block>
		<itunes:explicit>no</itunes:explicit>
		<itunes:image href="http://www.objectcommando.com/blog/wp-content/plugins/podpress/images/powered_by_podpress_large.jpg" />
		<image>
			<url>http://www.objectcommando.com/blog/wp-content/plugins/podpress/images/powered_by_podpress.jpg</url>
			<title>Object Commando</title>
			<link>http://www.objectcommando.com/blog</link>
			<width>144</width>
			<height>144</height>
		</image>
		<item>
		<title>Circular Lists with Clojure</title>
		<link>http://www.objectcommando.com/blog/2010/08/12/circular-lists-with-clojure/</link>
		<comments>http://www.objectcommando.com/blog/2010/08/12/circular-lists-with-clojure/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 12:07:07 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[sequences]]></category>

		<guid isPermaLink="false">http://www.objectcommando.com/blog/?p=392</guid>
		<description><![CDATA[I was working on some code that repeatedly executed a defined set of queries against a database for a give amount of time. Think throughput testing. The list of queries was very small (like 5) but the number of times each was executed was pretty high (500+). After it executed the 5th query, I wanted [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on some code that repeatedly executed a defined set of queries against a database for a give amount of time.  Think throughput testing.  The list of queries was very small (like 5) but the number of times each was executed was pretty high (500+).  After it executed the 5th query, I wanted it to execute the first one again and continue on.  So I thought the easiest way to go about this was to have a circular-type list.  I figured in Clojure it probably wouldn&#8217;t be circular, but rather a lazy list that repeated itself.  I looked around the source a bit (the internet connection at work was down, so there was no Google searching) and I wasn&#8217;t coming up with anything.  Not finding anything that fit my needs, I thought about it and made a couple of attempts at a solution.  I was pretty unhappy with the solutions I was coming up with.  I went back to the Clojure source trying to figure out how best to implement this and I stumbled on <em>cycle</em>.  It was exactly what I needed.  What struck me about cycle, was how concise and simple it was.  First, it&#8217;s use:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">take</span> <span style="color: #cc66cc;">10</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cycle</span> <span style="color: #66cc66;">&#40;</span>list <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: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">;=&gt; (1 2 3 4 1 2 3 4 1 2)</span></pre></div></div>

<p>It takes the collection passed in and creates a lazy sequence of the items in the list repeated infinitely.  I would keep consuming items from this infinite list for some defined amount of time and then stop:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">doseq</span> <span style="color: #66cc66;">&#91;</span>query <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cycle</span> <span style="color: #66cc66;">&#40;</span>query<span style="color: #66cc66;">-</span>list<span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> 
               :<span style="color: #555;">while</span> <span style="color: #66cc66;">&#40;</span>keep<span style="color: #66cc66;">-</span>going? @run<span style="color: #66cc66;">-</span>state<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
    <span style="color: #66cc66;">&#40;</span>execute<span style="color: #66cc66;">-</span>query query<span style="color: #66cc66;">...</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>In the above code, run-state is an atom that is updated once a particular point of time has been reached and then the query run completes.  What struck me about the cycle function, was how simple it was:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> <span style="color: #b1b100;">cycle</span>
  <span style="color: #ff0000;">&quot;Returns a lazy (infinite!) sequence of repetitions of the 
   items in coll.&quot;</span>
  <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">added</span> <span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#91;</span>coll<span style="color: #66cc66;">&#93;</span> 
 <span style="color: #66cc66;">&#40;</span>lazy<span style="color: #66cc66;">-</span>seq 
          <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when-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;">concat</span> s <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cycle</span> s<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>It&#8217;s a very concise function.  Here&#8217;s a rundown of what it does.  The <em>lazy-seq</em> macro takes as it&#8217;s body an expression that yields a sequence (or <em>nil</em>).  It&#8217;s lazy, so it doesn&#8217;t actually fully realize the sequence.  Inside the <em>lazy-seq</em> call is a <em>when-let</em> which just ensures that what is passed in is a collection that has at least one element in it.  If it is not a collection of at least one element, the <em>lazy-seq</em> is just <em>nil</em>.  If it does, the sequence that the <em>lazy-seq</em> function is looking for comes from the <em>concat</em> statement.    <em>Concat</em> is simple, it takes one or more collections and returns a single collection containing all of the elements in each passed in collection:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">concat</span> <span style="color: #66cc66;">&#40;</span>list <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: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>list <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">6</span> <span style="color: #cc66cc;">7</span> <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">; =&gt;(1 2 3 4 5 6 7 8)</span></pre></div></div>

<p>With that info, the <em>concat</em> in <em>cycle</em> glues together the entire collection that was passed in, followed by a recursive call to itself (passing in the same collection).  So with that, if the same <em>1, 2, 3, 4</em> list is passed in to <em>cycle</em>, the last line looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">concat</span> <span style="color: #66cc66;">&#40;</span>list <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: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cycle</span> <span style="color: #66cc66;">&#40;</span>list <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: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>After the first four items are consumed, the recursive invocation of cycle will happen, again producing (list 1 2 3 4) followed by another recursive invocation.  Since it&#8217;s lazy and infinite, this can continue until the query execution time is up, whether it&#8217;s 5 seconds or 5 hours.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.objectcommando.com/blog/2010/08/12/circular-lists-with-clojure/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Mocking Clojure Protocols with Atticus</title>
		<link>http://www.objectcommando.com/blog/2010/06/13/mocking-clojure-protocols-with-atticus/</link>
		<comments>http://www.objectcommando.com/blog/2010/06/13/mocking-clojure-protocols-with-atticus/#comments</comments>
		<pubDate>Mon, 14 Jun 2010 02:14:46 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[atticus]]></category>
		<category><![CDATA[mocks]]></category>
		<category><![CDATA[protocols]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.objectcommando.com/blog/?p=351</guid>
		<description><![CDATA[Atticus is a mocking library written by Hugo Duncan. For more information on Atticus, you can see Hugo&#8217;s blog post from May here. I have added the ability to mock protocols to Atticus and would like some feedback as to the best approach for binding the mocked protocol instance. There&#8217;s a survey below, but first [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/hugoduncan/atticus">Atticus</a> is a mocking library written by <a href="http://hugoduncan.org">Hugo Duncan</a>.  For more information on Atticus, you can see Hugo&#8217;s blog post from May <a href="http://hugoduncan.org/post/2010/mocking_clojure_functions_with_atticus.xhtml">here</a>.  I have added the ability to mock protocols to Atticus and would like some feedback as to the best approach for binding the mocked protocol instance.  There&#8217;s a survey below, but first a little background on the implementation. </p>
<h2>Mocking Protocols</h2>
<p>What makes the protocol instances a bit tough to mock is that they&#8217;re not just straight Clojure functions.  They dip a bit into Java and a bit into Clojure.  With that in mind, using Atticus without modification, wouldn&#8217;t allow the mocking of those protocols.  Below is an example of some code that uses this new functionality:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defprotocol Squared 
  <span style="color: #66cc66;">&#40;</span>square <span style="color: #66cc66;">&#91;</span>impl x<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">deftest</span> mock<span style="color: #66cc66;">-</span>protocol<span style="color: #66cc66;">-</span>test
  <span style="color: #66cc66;">&#40;</span>expects
   <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>instance Squared
	      <span style="color: #66cc66;">&#40;</span>square <span style="color: #66cc66;">&#91;</span>impl y<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>once <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span> y y<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;">&#93;</span>
   <span style="color: #66cc66;">&#40;</span>is <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">9</span> <span style="color: #66cc66;">&#40;</span>square instance <span style="color: #cc66cc;">3</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></td></tr></table></div>

<p>Originally I had a marker function name <em>mock-protocol-instance</em> which didn&#8217;t serve much purpose and was a bit awkward.  Talking with Hugo, I switched it to the above syntax.  The first item in that list is <em>instance</em> and is the symbol that the mocked protocol instance is bound to.  The next item is the protocol followed by functions.  The <em>(once&#8230;)</em> wrapped around the body of the function is existing functionality in Atticus that expands into the code to ensure the function was called exactly once.  An example of mocking a regular function in Atticus is below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">deftest</span> test<span style="color: #66cc66;">-</span>cube
  <span style="color: #66cc66;">&#40;</span>expects
   <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>cube <span style="color: #66cc66;">&#91;</span>y<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>once <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span> y y y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
   <span style="color: #66cc66;">&#40;</span>is <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">8</span> <span style="color: #66cc66;">&#40;</span>cube <span style="color: #cc66cc;">2</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></td></tr></table></div>

<p>The difference in this code and the first is that the binding syntax is familiar because it is similar to letfn.  Below is an example of letfn:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">letfn</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>add5 <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> x <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#40;</span>subtract5 <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-</span> x <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">7</span> <span style="color: #66cc66;">&#40;</span>add5 <span style="color: #66cc66;">&#40;</span>subtract5 <span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>In the style above, the first argument is the function name and so anything that refers to <em>add5</em> in the body of the letfn gets the function bound above in the <em>letfn</em>.  This <em>letfn</em> type of binding makes sense for Atticus when mocking functions.  They both have similar goals, binding a function temporarily.  Where this is a little more tricky is in mocking the protocol.  In the first example above, the first element in the list is special.  It&#8217;s the symbol bound to the protocol instance.  This is really more appropriate for a let style of binding.  Where one element is the symbol and the other is an expression.  Unfortunately, switching to a let style binding for the <em>expects</em> macro will make the syntax a little more cumbersome for mocking functions because you would have to add &#8220;fn&#8221;.  This would probably look something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">deftest</span> mock<span style="color: #66cc66;">-</span>protocol<span style="color: #66cc66;">-</span>test<span style="color: #66cc66;">-</span>alt
  <span style="color: #66cc66;">&#40;</span>expects
    <span style="color: #66cc66;">&#91;</span>instance <span style="color: #66cc66;">&#40;</span>Squared
	       <span style="color: #66cc66;">&#40;</span>square <span style="color: #66cc66;">&#91;</span>impl y<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>once <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span> y y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     cube <span style="color: #66cc66;">&#40;</span>fn <span style="color: #66cc66;">&#91;</span>y<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>once <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span> y y y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span>
        <span style="color: #66cc66;">&#40;</span>is <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">9</span> <span style="color: #66cc66;">&#40;</span>square instance <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#40;</span>is <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">8</span> <span style="color: #66cc66;">&#40;</span>cube y <span style="color: #cc66cc;">2</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></td></tr></table></div>

<p>The above is just spit balling, but the key point is that expects would use a let binding style.  The first is a let style binding of the protocol instance and the second is binding a function.</p>
<h2>Put it to a vote!</h2>
<p>The question is, which one is better?  Is sticking with the letfn binding and the brevity that it allows worth it even though the protocol mocking is a bit different?  The letfn style is the first and second code examples above.  Or is it confusing enough to warrant a little extra code around mocking functions (the example immediately above).  Is there another approach that would be better?  Below is just a quick survey on which one is preferable.  Thanks for giving your input!<br />
<br/><br />
<iframe src="http://spreadsheets.google.com/embeddedform?formkey=dDB3ZFc3YVlVamx6aXo0cGctMTg2Z3c6MQ" width="460" height="600" frameborder="0" marginheight="0" marginwidth="0">Loading&#8230;</iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.objectcommando.com/blog/2010/06/13/mocking-clojure-protocols-with-atticus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clojure Futures</title>
		<link>http://www.objectcommando.com/blog/2010/06/10/clojure-futures/</link>
		<comments>http://www.objectcommando.com/blog/2010/06/10/clojure-futures/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 22:04:51 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Clojure]]></category>

		<guid isPermaLink="false">http://www.objectcommando.com/blog/?p=335</guid>
		<description><![CDATA[Futures in Clojure are basically a way that you can execute some bit of code on a background thread. I was using it as a way to allow timeouts for long running code. In this entry I&#8217;ll give a run down on how to use futures. Future Basics It is pretty easy to start using [...]]]></description>
			<content:encoded><![CDATA[<p>Futures in Clojure are basically a way that you can execute some bit of code on a background thread.  I was using it as a way to allow timeouts for long running code.  In this entry I&#8217;ll give a run down on how to use futures.</p>
<h2>Future Basics</h2>
<p>It is pretty easy to start using futures in Clojure.  Most of the function calls start with (future&#8230;).  First I&#8217;ll start by creating a future that calls a long running sleep function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> f 
  <span style="color: #66cc66;">&#40;</span>future
    <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;Starting to sleep...&quot;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>Thread<span style="color: #66cc66;">/</span>sleep <span style="color: #cc66cc;">600000</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;Done sleeping.&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>There are two ways via Clojure to create a future, the first is with the macro <em>future</em> (used above), the other is the <em>future-call</em> function.  The <em>future</em> macro is just syntax sugar around <em>future-call</em>.  Whatever you pass in to <em>future</em> with be wrapped in a no-arg function and passed to <em>future-call</em>.  The <em>future-call</em> function just invokes the passed in function on background type of thread.  The above code, even though it sleeps for an hour will return immediately.  The object returned is a future (intentionally not getting into details about what is returned right now).  This returned future allows you to peek inside the execution of the code passed in and get information like is it done, or has it been canceled:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>future<span style="color: #66cc66;">-</span>done? f<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> false
&nbsp;
<span style="color: #66cc66;">&#40;</span>future<span style="color: #66cc66;">-</span>cancelled? f<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> false</pre></td></tr></table></div>

<p>You can then decide to cancel the future:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>future<span style="color: #66cc66;">-</span>cancel f<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span>true
&nbsp;
<span style="color: #66cc66;">&#40;</span>future<span style="color: #66cc66;">-</span>cancel f<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span>false</pre></td></tr></table></div>

<p>The above will return false when it was not able to cancel the future.  As an example, if we try to cancel the future again, it will return false, because it has already been canceled.  Another set of useful functions is getting the value returned by the executing future.  Let&#8217;s say we&#8217;re still computing Fibonacci numbers in the slow, recursive way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> fib <span style="color: #66cc66;">&#91;</span>x<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">zero?</span> x<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">0</span>
            <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> x<span style="color: #66cc66;">&#41;</span> <span style="color: #cc66cc;">1</span>
            :<span style="color: #555;">else</span>
	    <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">+</span> <span style="color: #66cc66;">&#40;</span>fib <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-</span> x <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>fib <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">-</span> x <span style="color: #cc66cc;">2</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></td></tr></table></div>

<p>Computing <em>(fib 40)</em> on my laptop takes about 10 seconds.  Below is code to execute the code in a future and then use deref to pull the value out:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> f <span style="color: #66cc66;">&#40;</span>future<span style="color: #66cc66;">-</span>call #<span style="color: #66cc66;">&#40;</span>fib <span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> #'user<span style="color: #66cc66;">/</span>f
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">time</span> @f<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;Elapsed time: 8075.452326 msecs&quot;</span>
<span style="color: #cc66cc;">102334155</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">time</span> @f<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;Elapsed time: 0.082342 msecs&quot;</span>
<span style="color: #cc66cc;">102334155</span></pre></td></tr></table></div>

<p>The first deref (@) call will block until the future has completed running and then return the value.  So timing the deref, it took about 8 seconds until returning the value.  But if it has already computed the value, it will just return that computed value.  So the second deref returns very quickly.</p>
<h2>Timeouts</h2>
<p>One thing that is hidden from users of futures in Clojure is what is actually returned when calling <em>(future&#8230;)</em>.  I say that it&#8217;s hidden because when you use <em>future-cancel</em>, <em>future-done?</em> etc, it doesn&#8217;t matter what kind of object is returned from the future function call.  It&#8217;s some object that you are able to pass to these other functions and it just works.  Where you actually do need to know more about the implementation of this is when you want to have your future timeout.  The object returned by <em>future-call</em> is an implementation of java.util.concurrent.Future.  With this information, you can use the Java APIs for the Future.  Below is some code that creates a future that will timeout before it finishes:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> f <span style="color: #66cc66;">&#40;</span>future<span style="color: #66cc66;">-</span>call #<span style="color: #66cc66;">&#40;</span>fib <span style="color: #cc66cc;">50</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: #66cc66;">.</span>get f <span style="color: #cc66cc;">10</span> <span style="color: #66cc66;">&#40;</span>java<span style="color: #66cc66;">.</span>util<span style="color: #66cc66;">.</span>concurrent<span style="color: #66cc66;">.</span>TimeUnit<span style="color: #66cc66;">/</span>SECONDS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>The last line does the same thing that we did before when we deref&#8217;d the future, but this time there is a timeout on how long we will wait for that deref to happen.  If the timeout is reached (in this case 10 seconds), it will throw a java.util.concurrent.TimeoutException.  Armed with the knowledge that the future is actually a java.util.concurrent.Future, the deref is just a more Clojure way of calling the get method on the future:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> f <span style="color: #66cc66;">&#40;</span>future<span style="color: #66cc66;">-</span>call #<span style="color: #66cc66;">&#40;</span>fib <span style="color: #cc66cc;">40</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> #'user<span style="color: #66cc66;">/</span>f
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">time</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>get f<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;Elapsed time: 10713.176672 msecs&quot;</span>
<span style="color: #cc66cc;">102334155</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">time</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">.</span>get f<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span> <span style="color: #ff0000;">&quot;Elapsed time: 0.075569 msecs&quot;</span>
<span style="color: #cc66cc;">102334155</span></pre></td></tr></table></div>

<h2>Hey, where did my console output go?</h2>
<p>I use Emacs and Slime for my development environment.  One thing that I noticed was that code running in a future does not write to the console like code running from the Slime REPL.  This is because code running in the Slime REPL gets the bound variables from the REPL thread, whereas the future code runs in a different thread that does not have those variables bound.  <em>bound-fn</em> makes it easy to fix this problem:</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="clojure" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> f <span style="color: #66cc66;">&#40;</span>future<span style="color: #66cc66;">-</span>call #<span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;Hello World&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span>#'user<span style="color: #66cc66;">/</span>f
&nbsp;
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> f <span style="color: #66cc66;">&#40;</span>future<span style="color: #66cc66;">-</span>call <span style="color: #66cc66;">&#40;</span>bound<span style="color: #66cc66;">-</span>fn <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;Hello World&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">=&gt;</span>Hello World
#'user<span style="color: #66cc66;">/</span>f</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.objectcommando.com/blog/2010/06/10/clojure-futures/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Easy Java Interop with Clojure</title>
		<link>http://www.objectcommando.com/blog/2010/05/17/easy-java-interop-with-clojure/</link>
		<comments>http://www.objectcommando.com/blog/2010/05/17/easy-java-interop-with-clojure/#comments</comments>
		<pubDate>Tue, 18 May 2010 03:13:47 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[AWS]]></category>
		<category><![CDATA[EC2]]></category>
		<category><![CDATA[java interop]]></category>

		<guid isPermaLink="false">http://www.objectcommando.com/blog/?p=321</guid>
		<description><![CDATA[This past week I started writing some code to work with Amazon EC2 Instances. I started using the JClouds library . It&#8217;s a great library for spinning up public AMIs in a cloud neutral way, however didn&#8217;t it do everything that I needed. Some of the things I needed were EC2 specific, so that&#8217;s not [...]]]></description>
			<content:encoded><![CDATA[<p>This past week I started writing some code to work with Amazon EC2 Instances.  I started using the <a href="http://code.google.com/p/jclouds/">JClouds</a> library .  It&#8217;s a great library for spinning up public AMIs in a cloud  neutral way, however didn&#8217;t it do everything that I needed.  Some of the things I needed were EC2 specific, so that&#8217;s not so surprising.  I fell back to the <a href="http://aws.amazon.com/sdkforjava/">AWS SDK for Java</a>, which basically just wraps calls to the Amazon web services.  Using that library, I wrote some Clojure functions that wrapped the Java calls to do what I needed.  Examples of what I needed would be to start up an existing EC2 EBS backed instance, stop an EBS instance and determine what state an EBS VM is in.  This led to Clojure code that would build up some request objects and interpret some response POJOs.  The API is a little awkward, even using Java.  Starting, stopping and describing an instance via the API all require one thing, one or more instance ids.  In the Amazon API, they have created a separate class for each (DescribeInstancesRequest,  StopInstancesRequest and StartInstancesRequest) and have those classes include a method where you <em>setInstanceIds</em> rather than just calling a method and passing a list of Strings (or something similar).  Working with this API helped me learn more about the Clojure Java Interop functions.</p>
<h3>dot dot</h3>
<p>The first feature that made my life easier was <strong>..</strong> .  This is a  macro for chaining Java calls.  It takes code that in Java would look like this:</p>
<pre>
instance.getState().getName()
</pre>
<p>And puts a similar feel in Clojure:</p>
<pre>
(.. instance getState getName)
</pre>
<p>Without this macro, you would have to:</p>
<pre>
(.getName (.getState instance))
</pre>
<p>It takes the return value of the first part (in the inner expression above) and passes it into the second.  The code above works great for chained method calls, but doesn&#8217;t help much with side affects.</p>
<h3>doto</h3>
<p>I found <strong>doto</strong> useful when I needed to call setters in constructing POJOs.  Calls to determine the status of a running EC2 instance returns an object graph of several nested objects and were particularly awkward to test, since there were a decent amount of objects to construct.  Before I realized doto could help me, I had code that looked like below:</p>
<pre>
(defn single-instance-result-example []
    (let [reservation-list (ArrayList.)
           instance-list (ArrayList.)
           reservation (Reservation.)
           instance (Instance.)
           instance-result (DescribeInstancesResult.)]
       (.setInstanceId instance "testinstance")
       (.add instance-list instance)
       (.setInstances reservation instance-list)
       (.add reservation-list reservation)
       (.setReservations instance-result reservation-list)
       instance-result))
</pre>
<p>I then transformed this into some code that used some nested <strong>dotos</strong></p>
<pre>
(defn single-instance-result-example []
    (doto (DescribeInstancesResult.)
        (.setReservations
        (doto (ArrayList.)
            (.add
                (doto (Reservation.)
	            (.setInstances
	                (doto (ArrayList.)
	                    (.add
                                (doto (Instance.)
                                    (.setInstanceId "testinstance")))))))))))
</pre>
<p>I must admit that the dotos here took some time to get comfortable with.  Some interesting things to note is that the above does not include all of the intermediate references to objects.  In the first example above I always passed the objects in, such as (.setInstanceId instance &#8220;testinstance&#8221;).  This is no longer necessary with doto.  The intermediate let-bound variables are also not necessary.  I seeing the above code, I felt like I still had some room for improvement.  In the above example and in other areas of my code I was seeing a common pattern: </p>
<pre>
(doto (ArrayList.)
    (.add (doto...))
    (.add (doto...))
     ...)
</pre>
<p>So I created a quick macro that I called doto-list that would bundle that piece up: </p>
<pre>
(defmacro doto-list [&#038; forms]
     `(doto (ArrayList.)
         ~@(map (fn [item] `(.add ~item)) forms)))
</pre>
<p>Which then made the function look like:</p>
<pre>
(defn single-instance-result-example []
    (doto (DescribeInstancesResult.)
        (.setReservations
        (doto-list
            (doto (Reservation.)
	        (.setInstances
	            (doto-list
	                (doto (Instance.)
	                    (.setInstanceId "test")))))))))
</pre>
<p>Which I think is a nice improvement when I&#8217;m creating a decent amount of ArrayLists.  </p>
<h3>memfn</h3>
<p>The next piece I used integration with the AWS APIs was <strong>memfn</strong>.  The call to <em>describeInstances</em> returns a List of <em>Instance</em> objects which have a few fields I&#8217;m interested in.  There are quite a few fields on the Instance object (20+) and I was only interested in a few.  Furthermore, I also did not want the callers of the functions to have to know they were dealing with a Java objects.  One was <strong>bean</strong>, which transforms an object into a map of it&#8217;s bean properties.  It seemed like it would work for me, but would pull over fields I cared about and a lot of ones that I didn&#8217;t.  It would also require knowledge of the Java object and the generated map structure.  I thought a better way to do this would be memfn.  The memfn macro takes an method name (and optionally arguments) and returns a function that takes an object as a parameter.  The function then invokes the method on that object when called. It basically translates the above call into:</p>
<pre>
((memfn getPublicIpAddress) some-object)
</pre>
<p>This seemed closer to what I wanted, but what I really wanted was a function called &#8220;public-ip&#8221; that you could pass an instance to.  So I ended up attaching a name to the memfn function that was returned by creating a little macro:</p>
<pre>
(defmacro defmemfn
     [name method-name &#038; args]
     `(def ~name (memfn ~method-name ~@args)))
</pre>
<p>A call to defmemfn looks like:</p>
<pre>
(defmemfn public-ip getPublicIpAddress)
</pre>
<p>And asking for a public ip looks like:</p>
<pre>
(public-ip instance)
</pre>
<h3>Up Next &#8211; Testing</h3>
<p>In conclusion, I think that working with the AWS APIs was easy thanks to Clojure&#8217;s great Java integration.  Testing this code was also much easier than I thought which I&#8217;ll post next.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.objectcommando.com/blog/2010/05/17/easy-java-interop-with-clojure/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Narrowing the Scope of Globals with Let</title>
		<link>http://www.objectcommando.com/blog/2010/05/09/narrowing-the-scope-of-globals-with-let/</link>
		<comments>http://www.objectcommando.com/blog/2010/05/09/narrowing-the-scope-of-globals-with-let/#comments</comments>
		<pubDate>Mon, 10 May 2010 03:54:49 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[OnLisp]]></category>

		<guid isPermaLink="false">http://www.objectcommando.com/blog/?p=278</guid>
		<description><![CDATA[I&#8217;ve been reading OnLisp by Paul Graham. It&#8217;s about becoming a better Lisp programmer. It&#8217;s written for Common Lisp, but I have found quite a bit of it carries over into Clojure. One interesting code snippit I found in the book was on using let when two functions required the same value. Previously I would [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been reading <a href="http://www.paulgraham.com/onlisp.html">OnLisp</a> by Paul Graham.  It&#8217;s about becoming a better Lisp programmer.  It&#8217;s written for Common Lisp, but I have found quite a bit of it carries over into Clojure.  One interesting code snippit I found in the book was on using let when two functions required the same value.  Previously I would have done this through a def, like below:</p>
<pre name="code" class="clojure">
(def step-by 7)
(defn increment [x] (+ x step-by))
(defn decrement [x] (- x step-by))
</pre>
<p>Paul Graham instead approaches it like:</p>
<pre name="code" class="clojure">
 (let [step-by 7]
	   (defn increment1 [x] (+ x step-by))
	   (defn decrement1 [x] (- x step-by)))
</pre>
<p>Obviously the example above is trivial, however there are times that shared immutable data is necessary.  Database connection properties, connection URI information etc.  I often have a small number of functions that need that sort of data.  For these kinds of situations, I like this second approach.  It more narrowly scopes the variable and I can&#8217;t think of any drawbacks.  I think this also highlights a difference in approach from imperative languages and functional languages in general.  Paul (on page 30 of  <a href="http://www.paulgraham.com/onlisp.html">OnLisp</a>) describes imperative language code structure as more &#8220;solid and blockish&#8221; whereas functional code tends to be more &#8220;fluid&#8221;.  The first example above fits the blockish imperative model where you define your variables and functions at the same level.  This is exactly how I would go about it in Java.  I&#8217;ve noticed that with the Clojure code I&#8217;ve been writing, and thinking back to code I&#8217;ve written in OCaml, it is definitely more fluid, a less rigid block structure.  I&#8217;ve gone through about 6 chapters of the book thus far and am looking forward to reading through the chapters on macros.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.objectcommando.com/blog/2010/05/09/narrowing-the-scope-of-globals-with-let/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Installing Parliament on Ubuntu</title>
		<link>http://www.objectcommando.com/blog/2010/04/25/installing-parliament-on-ubuntu/</link>
		<comments>http://www.objectcommando.com/blog/2010/04/25/installing-parliament-on-ubuntu/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 02:00:47 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Semantic Web]]></category>
		<category><![CDATA[Parliament]]></category>
		<category><![CDATA[Revelytix]]></category>
		<category><![CDATA[triple store]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.objectcommando.com/blog/?p=279</guid>
		<description><![CDATA[What is Parliament? Parliament is an open source triple store that is an improved version of DAMLDB. There is some good information on using the triple in the User&#8217;s Guide. What&#8217;s interesting about Parliament is how it stores the triples. Relational databases are a more common implementation of an RDF store, but Parliament goes a [...]]]></description>
			<content:encoded><![CDATA[<h3>What is Parliament?</h3>
<p><a href="http://parliament.semwebcentral.org">Parliament</a> is an open source triple store that is an improved version of <a href="http://www.daml.org/2001/09/damldb/">DAMLDB</a>.  There is some good information on using the triple in the <a href="http://parliament.semwebcentral.org/ParliamentUserGuide.pdf">User&#8217;s Guide</a>.  What&#8217;s interesting about Parliament is how it stores the triples.  Relational databases are a more common implementation of an RDF store, but Parliament goes a different way.  Parliament takes linked list style of approach  It uses BerkeleyDB for storing the URI values and then stores the triple of references in a linked list.  For more information on their approach, there&#8217;s a great paper on it than can be found <a href="http://parliament.semwebcentral.org/ISWC2009ParliamentPaper.pdf">here</a>.</p>
<h3>Building Parliament for Ubuntu</h3>
<p>Parliament has binaries on it&#8217;s website for Windows and Mac, but none for Ubuntu (or any other Linux distro).  Parliament is written in C++ and Java, so make sure you have the g++ package and the JDK installed.  You&#8217;ll also need to make sure you have a Subversion client installed to get the source and Ant installed to build the Java code.  Below are the steps I went through to get Parliament to run on Linux:</p>
<ol>
<li>Build Boost Jam</li>
<ul>
<li>Download and unzip <a href="http://sourceforge.net/projects/boost/files/boost-jam/3.1.17/">Boost Jam</a></li>
<li>Run build.sh in the boost-jam directory</li>
<li>Put jam executable in your PATH</li>
</ul>
<li>Build the Boost C++ Libraries</li>
<ul>
<li> Download and unzip <a href="http://sourceforge.net/projects/boost/files/boost/1.42.0/">Boost</a></li>
<li>cd into the boost directory and build boost with the following command (modify accordingly if your not on a 64 bit system)
<pre>
bjam -q --build-dir=linux/build --stagedir=linux/stage
--layout=versioned --with-test architecture=combined address-model=64
variant=debug,release threading=multi link=shared runtime-link=shared
stage
</pre>
</li>
</ul>
<li>Build and Install BerkleyDB</li>
<ul>
<li>Download and unzip [http://www.oracle.com/technology/software/products/berkeley-db/htdocs/popup/db/4.7.25/db-targz.html BerkleyDB] 4.7.x</li>
<li>cd into <berkeley_install_dir>/build/unix and type ../dist/configure</li>
<li>run make</li>
<li> run sudo make install</li>
</ul>
<li>Create the following environment variables (if you don&#8217;t already have them</li>
<ul>
<li>JAVA_HOME=/usr/lib/jvm/java-6-sun</li>
<li>BOOST_ROOT=/path/to/boost/boost_1_42_0</li>
<li>BDB_HOME=/usr/local/BerkeleyDB.4.7/</li>
<li>BOOST_BUILD_PATH=$BOOST_ROOT/tools/build/v2</li>
<li>BOOST_TEST_LOG_LEVEL=message</li>
</ul>
<li>Building Parliament</li>
<ul>
<li>Checkout the Parliament source: <em>svn checkout &#8211;username anonsvn https://projects.semwebcentral.org/svn/parliament/trunk</em></li>
<li>Copy the parliament_dir/doc/Linux/*.jam files to ~/</li>
<li>The Parliament build uses pushd and popd, which is not build into /bin/dash (which is where /bin/sh is symlinked in Ubuntu).  To fix this, I changed the /bin/sh symlink to /bin/bash
<li>Copy build.properties.template from the Parliament source directory to build.properties</li>
<li>Comment the various build architectures (for Mac and Windows) and make sure the below line in uncommented <em>nativeBuildParams=toolset=gcc-4.4 address-model=64 variant=release</em></li>
</ul>
<li>Source Changes</li>
<ul>
<li>When I tried to build Parliament the first time, I received an error that the method <em>remove</em> could not be found when compiling Parliament/KbCore/FileHandle.cpp.  It was due to the lines of code below:
<pre name="code" class="C++">
#if defined(PARLIAMENT_WINDOWS)
	if (!DeleteFile(filePath.c_str()))
#else
	if (remove(filePath.c_str()) == -1)
#endif
</pre>
<p>I added an include to the top of the file:</p>
<pre name="code" class="C++">
#if !defined(PARLIAMENT_WINDOWS)
#	include &lt;errno.h&gt;
#	include &lt;fcntl.h&gt;
#	include &lt;sys/stat.h&gt;
#      include &lt;stdio.h&gt; //&lt;-- Added this
</pre>
<p>to fix the problem.</p>
<li>The and build file includes the Mac environment variable for the C libraries, but not the Linux ones.  I changed the build.xml in the Parliament directory to include either the Linux or Mac environment variable depending on the build architecture:
<pre name="code" class="xml">
	&lt;condition property="libraryEnvVariable" value="DYLD_LIBRARY_PATH"&gt; &lt;!-- Line 290 --&gt;
			&lt;os family="mac"/&gt;
		&lt;/condition&gt;
		&lt;condition property="libraryEnvVariable" value="LD_LIBRARY_PATH"&gt;
			&lt;and&gt;
				&lt;os family="unix"/&gt;
				&lt;not&gt;&lt;os family="mac"/&gt;&lt;/not&gt;
			&lt;/and&gt;
		&lt;/condition&gt;
...
 &lt;env key="${libraryEnvVariable}" path="${artifactsDir}/${nativeArtifactsDir}"/&gt; &lt;!-- Line 302 --&gt;
</pre>
</ul>
<li>From the source tree root, run ant</li>
<li>Copy Parliament-v2.6.7.0-InsertPlatformHere.zip from the target directory to your install directory (can be anywhere)</li>
<li>In the install directory copy all of the files from gcc-4.4/release/64/ to the ParliamentKB directory</li>
<li>Run the StartParliament.sh script to start Parliament</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.objectcommando.com/blog/2010/04/25/installing-parliament-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clojure Protocols Part 3</title>
		<link>http://www.objectcommando.com/blog/2010/04/12/clojure-protocols-part-3/</link>
		<comments>http://www.objectcommando.com/blog/2010/04/12/clojure-protocols-part-3/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 12:12:22 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Languages]]></category>

		<guid isPermaLink="false">http://www.objectcommando.com/blog/?p=253</guid>
		<description><![CDATA[Recently there have been some changes to the Clojure Protocols code out on Github. Not huge changes, but enough that the examples I wrote from Part 1 and Part 2 will no longer work. I thought I&#8217;d finish out my protocol blog entries by showing how I used it and include the new syntax. I [...]]]></description>
			<content:encoded><![CDATA[<p>Recently there have been some changes to the Clojure Protocols code out on Github.  Not huge changes, but enough that the examples I wrote from <a href="http://www.objectcommando.com/blog/2010/03/26/clojure-protocols-part-1/">Part 1</a> and <a href="http://www.objectcommando.com/blog/2010/03/29/clojure-protocols-part-2/">Part 2</a> will no longer work.  I thought I&#8217;d finish out my protocol blog entries by showing how I used it and include the new syntax.  I also have a better understanding on how reify can be used (thanks <a href="http://kotka.de/blog/">Meikel</a>) and will include some of that.  First the goal of protocol usage.  I have been working on some comparisons and evaluations of triplestores.  Triplestores can be used to store RDF data which is a series of subject/predicate (or property)/object triples.  There are many triplestores out there and of the triplestores that are out there, many have several interfaces.  For example, Oracle has a JDBC interface that uses stored procedures and a Jena API that incorporates pieces of the Jena framework.  This was some pretty low hanging fruit from an abstraction perspective.  Whether inserting a new triple in Oracle JDBC, Jena (with Oracle) or one of the other triplestore impelementations, on the surface, it is the same.  Take this subject, predicate and object and store it.  The same could be said for querying it with SPARQL or deleting entries.  I ended up with a protocol named <em>TriplestoreOperations</em> like below:</p>
<pre>
(ns revelytix.triplestore-operations)

(defprotocol TriplestoreOperations
  "Interface for the various operations allowed by a triple store"
  (create-graph [impl graph-name] "Creates a new graph of name graph-name")
  (delete-graph [impl graph-name] "Deletes graph graph-name if graph exists")
  (insert-quad [impl graph-name subject predicate object]
    "Creates a new triple, data is assumed to be a full URI")
  ;;...)
</pre>
<p>This syntax is the same.  The first argument is used to pass in the implementation of <em>TriplestoreOperations</em>.  The <em>graph-name</em> or <em>model</em> in Oracle terms, is what is going to hold the triples.  The protocol exists in one namespace (called triplestore-operations above) and the implementations of the interfaces are in separate namespaces.  The first is an Oracle JDBC implementation of <em>TriplestoreOperations</em>.  It&#8217;s parameterized by the database connection details and the name of the table to store the data in.</p>
<pre>
(ns oracle.oracle-jdbc
  (:use clojure.contrib.sql
	triplestore-operations))

(deftype OracleJdbcOperations [db table-name]  TriplestoreOperations
  (delete-graph [impl graph-name]
	(let [drop-model-string (create-sql-string DROP-MODEL-SQL graph-name)
	      drop-table-string (create-sql-string DROP-TABLE-SQL table-name)]
	  (with-connection db
	      (with-open [drop-model-statement (.prepareCall (connection) drop-model-string)]
		(do
		  (drop-entailment-if-exists db graph-name "RDFS")
		  (.execute drop-model-statement)
		  (do-commands drop-table-string))))))
  (create-graph [impl graph-name]
      (let [createModelString (create-sql-string CREATE-MODEL-SQL graph-name table-name)
	    createTableString (create-sql-string CREATE-TABLE-SQL table-name)]
	(do (with-connection db
	      (with-open [createModelStatement
                                    (.prepareCall (connection) createModelString)]
		(do-commands createTableString)
		(.execute createModelStatement))))))
  (insert-quad [impl graph-name subject predicate object]
	       (create-family-triple table-name db graph-name subject predicate object))
  ;;...)

  (defn create-oracle-jdbc-triplestore-instance [table-name]
           (OracleJdbcOperations *oracle-jdbc-props* table-name)) ;;Awkward see below
</pre>
<p>One difference between the above code and the code in Part 1 or Part 2 is that the implementation parameter in the previous version of deftype disappeared.  So the <em>create-graph</em> function above would have had only had a single parameter.  I like the change, I found the original code a little confusing, wondering where the first parameter went etc.  The next implementation of the <em>TriplestoreOperations</em> protocol was a Jena implementation of the protocol.  The below code makes use of the <a href="http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/reify">reify</a> function and feels a little more idomatic Clojure and less like the implementation of a protocol is something special and different from just functions.  I like the refiy syntax over deftype and I&#8217;ve been moving my code over to use it.   I&#8217;m going to cut a decent portion of the implementation below because it mostly calls Java APIs and is a bit noisy:</p>
<pre>
(ns jena-operations
  (:use triplestore-operations)
  ;;...)

(defn create-jena-operations-instance [jena-support-impl]
  (reify TriplestoreOperations
	  (create-graph [impl modelString] nil)
	  (delete-graph [impl modelString]
			(with-triplestore-connection ;...)
	  (insert-quad [impl modelString subject predicate object]
		       (with-triplestore-connection ;;...)
          ;;...))
</pre>
<p>The reify function call above also creates a new instance of the protocol <em>TriplestoreOperations</em> with the functions defined in line.  There&#8217;s also not a need to create an instance of the type like is being done in the previous example.  The end result, deftype or reify from a functionality perspective is the same, there&#8217;s just a different way to get there.  Reading through some of the docs, it looks like reify is more dynamic and deftype results in generated code.  One difference between Jena and the Oracle JDBC interface is that graphs don&#8217;t need to be created explicitly using Jena, so that method does nothing.  The above code is slightly different as well in that the implementation parameter no longer disappears.  Another interesting part is that the JenaOperations instance is parametrized by another protocol called <em>JenaSupport</em>.  What I have found is that many vendors support the Jena APIs, but they implement it slightly different.  It&#8217;s definitely not as pluggable as something like JDBC.  This JenaOperations implementation is generic for the Jena APIs and is used by several triplestores with Jena implementations.  The <em>JenaSupport</em> protocol abstracts things like getting a Jena connection, creating the correct implementation of <em>Model</em> etc which is different from implementation to implementation.</p>
<h3>Development Gotchas</h3>
<p>I have found a few issues when developing Clojure code that uses protocols.  I&#8217;m using <a href="http://github.com/technomancy/leiningen">Leiningen</a> and  <a href="http://github.com/technomancy/leiningen/tree/master/lein-swank/">Lein Swank</a> for development of the code.  First I found that if I had AOT compilation enabled, and had run <em>lein install</em>, the protocol definition results in compiled code in the classes directory of the project.  Where this caused a problem was when I tried to change a protocol definition.  I&#8217;d make a change in Emacs, load the file with the updated protocol code and behaviour of the code would be such that I made no change to the protocol at all.  What was happening was the old version of the code, the one that had the interface code generated, was still on the class path in the classes directory.  Removing that code (through <em>lein clean</em> or something similar) allowed my changes to take affect.  This problem stumped me for a couple of hours.  I can avoid this entirely by just not using the AOT compilation (I don&#8217;t really need it) but others might not.</p>
<p>Another gotcha I found was in the loading of files that use implementations of protocols.  In the example above, let&#8217;s say I have a test file (I&#8217;ll call it test-A) that executes functions from <em>TriplestoreOperations</em> on the <em>JenaOperations</em> implementation that in turn uses the Oracle implementation of <em>JenaSupport</em>.  Just loading test-A.clj file does not cause the loading of the Jena implementation of the TriplestoreOperations, or the Oracle version of JenaSupport.  Rather it just complains that there is not an implementation of <em>TriplestoreOperations</em> for &#8216;nil&#8217;.  Loading those files individually fixes the problem, it just doesn&#8217;t do that automatically for me.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.objectcommando.com/blog/2010/04/12/clojure-protocols-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clojure Protocols Part 2</title>
		<link>http://www.objectcommando.com/blog/2010/03/29/clojure-protocols-part-2/</link>
		<comments>http://www.objectcommando.com/blog/2010/03/29/clojure-protocols-part-2/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 04:08:34 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[clojure protocols]]></category>
		<category><![CDATA[defprotocol]]></category>
		<category><![CDATA[deftype]]></category>
		<category><![CDATA[records]]></category>

		<guid isPermaLink="false">http://www.objectcommando.com/blog/?p=234</guid>
		<description><![CDATA[Stale code warning There have been small changes to the protocols code in Clojure. The below post is still useful, but a few details of the example code is different. See part 3 for the updated syntax. Clojure Protocols Part 2 This is the second in the series of blog entries on Clojure protocols. The [...]]]></description>
			<content:encoded><![CDATA[<h3>Stale code warning</h3>
<p>There have been small changes to the protocols code in Clojure.  The below post is still useful, but a few details of the example code is different.  See <a href="http://www.objectcommando.com/blog/2010/04/12/clojure-protocols-part-3/">part 3</a> for the updated syntax.</p>
<h3>Clojure Protocols Part 2</h3>
<p>This is the second in the series of blog entries on Clojure protocols.  The first can be found <a href="http://www.objectcommando.com/blog/2010/03/26/clojure-protocols-part-1/">here</a>.  This entry continues by using protocols to implement Java interfaces and reify interfaces/protocols inline in a function invocation.  First I&#8217;ll use reify to define an implementation of the <em>TextOutput</em> interface in-line of the function call.  I&#8217;ll change the italics syntax to the MediaWiki italics format:</p>
<pre>
(println (output-string (reify TextOutput
		      (output-string [x] (str "''" x "''"))) "stuff"))
''stuff''
</pre>
<p>The acceptable things to <em>reify</em> are Interfaces (in Java) protocols or Object.  I&#8217;ve not yet find a use for <em>reify</em> in code that I have written.  One of the things that can be passed to <em>reify</em> are regular Java interfaces.  This can also be passed to <em>deftype</em> to define Clojure implementations of Java interfaces.  An implementation of <em>Comparator</em> looks like below:</p>
<pre>
(deftype ThreeCompare [] java.util.Comparator
	       (compare [o1 o2]
			(cond (= o1 3) -1
			      (= o2 3) 1
			      :else (.compareTo o1 o2)))
	       (equals [other] (isa? other ThreeCompare)))
</pre>
<p>The deftype above implements the protocol <em>java.util.Comparator</em> that when sorting a list of numbers will always put any values of <em>3</em> first in the list followed by the rest in ascending order.  This can be used like any Java implementation of <em>Comparator</em>:</p>
<pre>
(def java-list (java.util.ArrayList. (list 1 2 3 4 5 6 7 8)))
(java.util.Collections/sort java-list (ThreeCompare))
(println java-list)
<ArrayList [3, 1, 2, 4, 5, 6, 7, 8]>
</pre>
<p>A nice side benefit of <em>deftype</em> is something that reminded me of <a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual003.html#toc7">records</a> in OCaml:</p>
<pre>
(deftype Point [x y])
(defn midpoint [point1 point2]
	    (Point (/ (+ (:x point1) (:x point2)) 2)
		   (/ (+ (:y point1) (:y point2)) 2)))
(println (midpoint (Point -1 2) (Point 3 -6)))
#:Point{:x 1, :y -2}
</pre>
<p>The above code defines a new type <em>Point</em>, a midpoint function that takes two points and return a new Point that represents the midpoint of the two points.</p>
<h3>Default Implementations</h3>
<p>One feature I was looking for when I first incorporated protocols into some existing Clojure code was the concept of a default implementation of a protocol within a namespace.  I think this would be a pretty typical usage of a protocol, you might have several implementations, but generally you&#8217;re only working with one at a time.  In my case, I was testing three implementations of a protocol for an integration test.  I wanted to run the same tests on all three implementations of the protocol.  This presented a problem because the deftest macro doesn&#8217;t allow passed in parameters and yet each function that I called needed to be parametrized based on the implementation I was testing.  I first attacked this problem with a bound variable and then just had all functions called on the protocol use the bound variable as their implementation.  Then when a switch to another implementation was needed, I&#8217;d change the implementation assigned to the bound variable.  This worked for me because it was just test code, but I think this will come up more in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.objectcommando.com/blog/2010/03/29/clojure-protocols-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Clojure Protocols Part 1</title>
		<link>http://www.objectcommando.com/blog/2010/03/26/clojure-protocols-part-1/</link>
		<comments>http://www.objectcommando.com/blog/2010/03/26/clojure-protocols-part-1/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 21:52:27 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Polymorphism]]></category>

		<guid isPermaLink="false">http://www.objectcommando.com/blog/?p=222</guid>
		<description><![CDATA[Stale code warning There have been small changes to the protocols code in Clojure. The below post is still useful, but a few details of the example code is different. See part 3 for the updated syntax. Clojure Protocols Protocols are a new feature in Clojure, set to be released in the next version. They [...]]]></description>
			<content:encoded><![CDATA[<h3>Stale code warning</h3>
<p>There have been small changes to the protocols code in Clojure.  The below post is still useful, but a few details of the example code is different.  See <a href="http://www.objectcommando.com/blog/2010/04/12/clojure-protocols-part-3/">part 3</a> for the updated syntax.</p>
<h3>Clojure Protocols</h3>
<p>Protocols are a new feature in Clojure, set to be released in the next version.  They provide polymorphism in a very Clojure-ish way.  I think it&#8217;s a great lightweight polymorphism implementation that has a lot of potential.  In true Clojure style I think it meets the polymorphism objective and yet doesn&#8217;t need to totally change the way you already write your code in Clojure.  I&#8217;m breaking this entry into more than one piece to show some different ways that Clojure protocols can be used.  Because it&#8217;s so new, there are not a lot of docs out there on it, but Rich does some good documentation on the macros themselves.  If you want to try these examples, make sure you&#8217;re running off of the 1.2 version of Clojure (from <a href="http://clojars.org/">Clojars</a> or a local build from the <a href="http://github.com/richhickey/clojure">Clojure git repo</a>).  First I&#8217;ll start by defining a simple protocol:</p>
<pre>
(defprotocol TextOutput
	  (output-string [x string]))
</pre>
<p>In Java terms, I&#8217;m defining a TextOutput interface (actually a Java interface <strong>is</strong> being created, but more on that later), that has a single function named <em>output-string</em> that includes no implementation details.  The input to this function is a little tricky though.  I specified a parameter <em>x</em> and another one called <em>string</em>.  The first parameter will be used to pass the implementation of the interface into the function.  You don&#8217;t need to write code to handle the parameter <em>x</em> and when you write your implementation, you&#8217;ll act like it doesn&#8217;t exist.  A wiki type text output of an italics string would look like:</p>
<pre>
(deftype ItalicsOutput [] TextOutput
	       (output-string [string] (str "_" string "_")))
</pre>
<p>I have begun thinking about this in Java terms as a class <em>ItalicsOutput</em> that implements the <em>TextOutput</em> interface.  Here in the output-string function, I only specify one parameter (not two).  Next you can use this implementation with the following code:</p>
<pre>
(output-string (ItalicsOutput) "stuff")
"_stuff_"
</pre>
<p>I&#8217;m telling Clojure I want it to execute the <em>output-string function</em>, on the implementation <em>(ItalicsOutput)</em> (more in this below) with the argument <em>&#8220;stuff&#8221;</em>.  I think that below is a little more readable:</p>
<pre>
(def italics-impl (ItalicsOutput))
(output-string italics-impl "stuff")
"_stuff_"
</pre>
<p>Which just assigns the instantiated implementation to a variable which can then be used.  These implementations can also have parameters, like:</p>
<pre>
(deftype PrefixedOutput [prefix-string] TextOutput
	       (output-string [string] (str prefix-string " " string)))
</pre>
<p>I think passing a variable in makes the instantiation step make a little more sense:</p>
<pre>
(def prefix-with-more (PrefixedOutput "more"))
(output-string prefix-with-more "stuff")
"more stuff"
</pre>
<p>Both implementations can be used together as well:</p>
<pre>
(defn print-all []
	(let [italics-impl (ItalicsOutput)
	     prefix-with-more (PrefixedOutput "more")]
	     (println (output-string italics-impl "stuff"))
	     (println (output-string prefix-with-more "stuff"))))
</pre>
<p>With output that would look like:</p>
<pre>
(print-all)
_stuff_
more stuff
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.objectcommando.com/blog/2010/03/26/clojure-protocols-part-1/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>SICP &#8211; Chapter 1</title>
		<link>http://www.objectcommando.com/blog/2010/03/21/sicp-chapter-1/</link>
		<comments>http://www.objectcommando.com/blog/2010/03/21/sicp-chapter-1/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 03:45:55 +0000</pubDate>
		<dc:creator>Ryan</dc:creator>
				<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Languages]]></category>

		<guid isPermaLink="false">http://www.objectcommando.com/blog/?p=215</guid>
		<description><![CDATA[I have began reading through Structure and Interpretation of Computer Programs through a study group (a spawn from the Lambda Lounge). A classic computer science textbook, I&#8217;ve wanted to read it for a while now, and I&#8217;m amazed that thus far I have avoided reading it. Maybe because it&#8217;s older is the reason I missed [...]]]></description>
			<content:encoded><![CDATA[<p>I have began reading through <a href="http://mitpress.mit.edu/sicp/">Structure and Interpretation of Computer Programs</a> through a study group (a spawn from the <a href="http://lambdalounge.org">Lambda Lounge</a>).  A classic computer science textbook, I&#8217;ve wanted to read it for a while now, and I&#8217;m amazed that thus far I have avoided reading it.  Maybe because it&#8217;s older is the reason I missed the SICP cut-off.  I have to say, I&#8217;m impressed with the pace of the book.  It&#8217;s partially a function of the language, but I really like how the text gets the the <em>bare metal</em>, in that it builds everything from the ground up.  Scheme allows it to do this in that many things that are syntax in other languages (like basic arithmetic operations) are not syntax in Scheme.  Languages like Java have operations like addition and division built into the syntax of the language.  My answers to the exercises in the textbook are <a href="http://github.com/senior/sicp">here</a>.  They are written in Clojure and I&#8217;ve been pretty surprised at how closely Clojure code corresponds with Scheme code.</p>
<h2>The Good Stuff</h2>
<p>Like I described above, Abelson and Sussman getting to the bare metal in terms of Scheme I think is a real benefit.  I&#8217;m sure it took a lot of restraint to not use the fancy macros or functions early on and start small.  I really liked the way that they described the benefits of tail recursion.  I have been asked on several occasions to give such a description.  I have to say their approach using visuals is much better than mine.  I will definitely be borrowing theirs when asked that question in the future.  Building on that I though that the exercises that were in 1.2 did a good job of covering how to go about making something tail recursive.  I think their coverage of higher order functions was thorough and look forward to them revisiting the flexibility of this in the coming chapters.</p>
<h2>The Bad Stuff</h2>
<p>I thought their coverage of recurrences and asymptotic growth was particularly bad.  I think recurrences are a very tough topic and section 1.22 barely skimmed the surface enough to give an exercise like 1.13.  Maybe the students at MIT had a prerequisite that covered that or something, but I spent many hours in grad school trying to understand recurrences and I know I would have been drowning with such a light coverage of the topic.  Maybe asymptotic growth will be covered in depth in another section, but I though that just skimmed the surface as well.  The only other negative comment I have is that the amount of math makes the book less approachable.  I know why they did it, it&#8217;s the only base that could be built upon easily for them to use their bare metal sort of approach.  I also don&#8217;t think you need a very extensive math background to read it, they don&#8217;t come in expecting a whole lot.  But math is intimidating to many people and just having something that smells and looks like hard math will turn people away.</p>
<h2>Conclusion</h2>
<p>In conclusion, chapter 1 from SICP has been worth the time and definitely a good start for a foundation in computer science.  I think that the first chapter is a good read for any software developer.  I&#8217;m looking forward to continuing with the rest of the book.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.objectcommando.com/blog/2010/03/21/sicp-chapter-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
