<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>Along Came Betty</title>
	
	<link>http://blog.darevay.com</link>
	<description>You know, software and some other stuff like maybe guitar or something</description>
	<lastBuildDate>Sat, 28 Jan 2012 13:27:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/darevay" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="darevay" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>ClojureScript One Execution Flow</title>
		<link>http://blog.darevay.com/2012/01/clojurescript-one-execution-flow/</link>
		<comments>http://blog.darevay.com/2012/01/clojurescript-one-execution-flow/#comments</comments>
		<pubDate>Sat, 28 Jan 2012 13:27:57 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[clojurescript]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=568</guid>
		<description><![CDATA[Last night I puzzled through the execution flow of the ClojureScript One sample app. I even took notes while I was doing it. The results are here. I have to say ClojureScript One is an impressive achievement. Someone should build &#8220;Clojure One&#8221; now :)]]></description>
			<content:encoded><![CDATA[<p>Last night I puzzled through the execution flow of the ClojureScript One sample app. I even took notes while I was doing it.</p>
<p style="text-align: center;"><a href="https://gist.github.com/1692791">The results are here</a>.</p>
<p style="text-align: left;">I have to say ClojureScript One is an impressive achievement. Someone should build &#8220;Clojure One&#8221; now :)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2012/01/clojurescript-one-execution-flow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Seesaw Tutorial</title>
		<link>http://blog.darevay.com/2011/12/a-seesaw-tutorial/</link>
		<comments>http://blog.darevay.com/2011/12/a-seesaw-tutorial/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 03:47:18 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[seesaw]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=557</guid>
		<description><![CDATA[It was recently pointed out to me that Seesaw&#8217;s &#8220;amazing and wonderful&#8221;, but not still not that friendly for developers with no background in Java or Swing. By coincidence, I was scheduled to give a talk on Seesaw at Ann Arbor&#8217;s CraftsmanGuild, so I took the opportunity to try to improve the situation. I&#8217;m usually [...]]]></description>
			<content:encoded><![CDATA[<p>It was recently <a href="https://twitter.com/#!/IORayne/status/143222063950336000">pointed out to me</a> that <a href="https://github.com/daveray/seesaw">Seesaw&#8217;s</a> &#8220;amazing and wonderful&#8221;, but not still not that friendly for developers with no background in Java or Swing. By coincidence, I was scheduled to give a talk on Seesaw at Ann Arbor&#8217;s <a href="http://craftsmanguild.herokuapp.com/">CraftsmanGuild</a>, so I took the opportunity to try to improve the situation.</p>
<p>I&#8217;m usually <a href="http://blog.darevay.com/2011/04/here-comes-clojure-a-clojure-talk-in-clojure/">not a fan of slides</a>, so I did the talk completely in the REPL. The result was, I think, a nice introduction that assumes no Java or Swing knowledge and shows off some of Seesaw&#8217;s cool high-level features.</p>
<p><em>You might want to <a href="https://gist.github.com/1441520">view it in its native habitat</a> since this dumb fixed-width layout will hide some of the text. Also, depending on your fonts, my lovingly crafted screenshots might look like crap. Sorry about that.</em></p>
<p>In any case, here it is. Feedback welcome and encouraged.</p>
<p><script src="https://gist.github.com/1441520.js?file=seesaw-repl-tutorial.clj"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2011/12/a-seesaw-tutorial/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VimClojure – Easy</title>
		<link>http://blog.darevay.com/2011/10/vimclojure-easy/</link>
		<comments>http://blog.darevay.com/2011/10/vimclojure-easy/#comments</comments>
		<pubDate>Thu, 13 Oct 2011 02:56:40 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[vimclojure]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=545</guid>
		<description><![CDATA[I promise this is the last VimClojure post for a while. I get the feeling that my post describing my VimClojure setup is a little too involved, especially for someone that just wants something working quickly. Also, from what I&#8217;ve seen on #clojure, there&#8217;s some frustration out there. With that in mind, I put together [...]]]></description>
			<content:encoded><![CDATA[<p><em>I promise this is the last VimClojure post for a while.</em></p>
<p>I get the feeling that <a href="http://blog.darevay.com/2010/10/how-i-tamed-vimclojure/">my post describing my VimClojure setup</a> is a little too involved, especially for someone that just wants something working quickly. Also, from what I&#8217;ve seen on <a href="http://clojure-log.n01se.net/">#clojure</a>, there&#8217;s some frustration out there. With that in mind, I put together a minimal example configuration with instructions.</p>
<p>So, without further ado, <a href="https://github.com/daveray/vimclojure-easy">vimclojure-easy</a>.</p>
<p>Feedback welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2011/10/vimclojure-easy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Familiar: Clojure plus Ruby</title>
		<link>http://blog.darevay.com/2011/10/familiar-clojure-plus-ruby/</link>
		<comments>http://blog.darevay.com/2011/10/familiar-clojure-plus-ruby/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 02:37:36 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[jruby]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=537</guid>
		<description><![CDATA[Last week I had three separate conversations with people about using Clojure&#8217;s data structures and concurrency primitives from a language that&#8217;s not Clojure. Something about parens or insisting on using a less capable language :) So, this weekend I played around with using Clojure from Ruby, in particular JRuby. I haven&#8217;t done much Ruby in [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I had three separate conversations with people about using Clojure&#8217;s data structures and concurrency primitives from a language that&#8217;s not Clojure. Something about parens or insisting on using a less capable language :)</p>
<p>So, this weekend I played around with using Clojure from Ruby, in particular <a href="http://jruby.org">JRuby</a>. I haven&#8217;t done much Ruby in about a year, so I spent most of my time remembering that. Anyway, I ended up with a proof of concept: <a href="https://github.com/daveray/familiar">Familiar</a>. See the readme there. Here&#8217;s an example:</p>
<p><script src="https://gist.github.com/1274510.js"> </script></p>
<p>That&#8217;s it. This gives full access to the <code>clojure.core</code> API including lazy seqs, STM, and all those goodies. Good idea? Idiotic? I don&#8217;t know, but it was a fun exercise.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2011/10/familiar-clojure-plus-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Taming VimClojure 2.3.0</title>
		<link>http://blog.darevay.com/2011/10/taming-vimclojure-2-3-0/</link>
		<comments>http://blog.darevay.com/2011/10/taming-vimclojure-2-3-0/#comments</comments>
		<pubDate>Sat, 08 Oct 2011 01:28:28 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[vimclojure]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=533</guid>
		<description><![CDATA[In honor of the release of VimClojure 2.3.0, I&#8217;ve updated my post on taming VimClojure. Thankfully, very little has changed in the setup. Cheers.]]></description>
			<content:encoded><![CDATA[<p>In honor of the <a href="https://groups.google.com/group/vimclojure/browse_thread/thread/a1b649b095e26c65">release of VimClojure 2.3.0</a>, I&#8217;ve updated <a href="http://blog.darevay.com/2010/10/how-i-tamed-vimclojure/">my post on taming VimClojure</a>. Thankfully, very little has changed in the setup. Cheers.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2011/10/taming-vimclojure-2-3-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clojure Update Literacy</title>
		<link>http://blog.darevay.com/2011/10/clojure-update-literacy/</link>
		<comments>http://blog.darevay.com/2011/10/clojure-update-literacy/#comments</comments>
		<pubDate>Wed, 05 Oct 2011 03:20:45 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=523</guid>
		<description><![CDATA[I want to write briefly (ok, not that briefly) about my understanding of Clojure&#8216;s various &#8220;update&#8221; functions, in other words, functions that take a function argument, apply it to some value and do something with the result. These include swap!, alter, commute, send, send-off, alter-var-root, update-in, alter-meta!, vary-meta, and probably several more. From here on [...]]]></description>
			<content:encoded><![CDATA[<p>I want to write briefly (ok, not that briefly) about my understanding of <a href="http://clojure.org">Clojure</a>&#8216;s various &#8220;update&#8221; functions, in other words, functions that take a function argument, apply it to some value and do something with the result. These include <code><a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/swap!">swap!</a></code>, <code><a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/alter">alter</a></code>, <code><a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/commute">commute</a></code>, <code><a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/send">send</a></code>, <code><a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/send-off">send-off</a></code>, <code><a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/alter-var-root">alter-var-root</a></code>, <code><a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/update-in">update-in</a></code>, <code><a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/alter-meta!">alter-meta!</a></code>, <code><a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/vary-meta">vary-meta</a></code>, and probably several more. From here on out, I&#8217;ll call a function in this family an <b>update function</b> and the function passed to it a <b>transform function</b>.</p>
<p>Note that update functions are most commonly encountered when using Clojure&#8217;s reference types, i.e. <a href="http://clojure.org/vars">vars</a>, <a href="http://clojure.org/refs">refs</a>, <a href="http://clojure.org/atoms">atoms</a>, and <a href="http://clojure.org/agents">agents</a>. Their signatures are all very consistent:</p>
<pre>
    (&lt;update-fn&gt; old-state &lt;tranform-fn&gt; &amp; args)
    ;=> new-state
</pre>
<p>That they&#8217;re so consistent is a testament to the thought that has gone into the design of Clojure&#8217;s core API.</p>
<p>Nonetheless, a newcomer to Clojure, overwhelmed by all the other new concepts they&#8217;re learning, might find update functions a little confusing. They might find themselves calling <code><a href="">reset!</a></code> a lot because <code>swap!</code> is scary. Of course, if the value passed to <code>reset!</code> (or <code><a href="">ref-set</a></code>) depends on the previous value of the ref, you&#8217;ve thrown away all the nice concurrency guarantees that atoms and refs are supposed to give you. For example, the prototypical id generator:</p>
<pre>
  ; BADBAD Don't do this!
  (defn id-generator []
    (let [id (atom 0)]
     (fn []
       (reset! id (inc @id)))))
</pre>
<p><b><a href="https://gist.github.com/1263503">No No No No No</a></b></p>
<p>Of course, everyone knows to use <code>(swap! id inc)</code> for a canonical example like this, but in the thick of a larger app, feeling like you&#8217;re <a href="http://blog.fogus.me/2011/04/21/never-feel-safe/">in over your head</a>, it&#8217;s easier to make mistakes.</p>
<hr />
<p><b>So</b>, learning to read (and write) an update function can take you a long way along the road toward writing more idiomatic Clojure.  For someone with an OO background, it might be easier if we mentally re-wrote the signature above like this:</p>
<pre>
   state = transform_fn(state, arg0, arg1, ...);
</pre>
<p>that is, we&#8217;re applying some <b>transformation</b> to the current value of <code>state</code> and storing the value back in <code>state</code>. See, <code>swap!</code> and friends are <b>just a function call in disguise</b>. Their signatures are completely consistent with Clojure&#8217;s <a href="http://kotka.de/blog/2010/08/Did_you_know_VII.html">argument order conventions</a>, but they slightly obscure what&#8217;s going on because the state and transform function are switched. This makes sense since <code>state</code> is the <em>important argument</em>, but it took me a little while to realize this and make the mental adjustment.</p>
<p>Once I reached this conclusion, I found writing in a functional style with state transitions much more straightforward. </p>
<p><em>Note that I find this a useful way to *read* update functions. It&#8217;s not a replacement for thinking hard about the semantics of the update function you&#8217;re using</em></p>
<hr />
<p>Here are some general guidlines that I find helpful:</p>
<ul>
<li>Always write <b>pure</b> functions that represent state transitions or transformations. They take in the current state and maybe some additional arguments and calculate a new state. This is obvious, but sometimes I have to keep repeating these things to myself. Kind of like in <a href="http://www.tcl.tk/">Tcl</a>, &#8220;everything&#8217;s a string, everything&#8217;s a string, everything&#8217;s a string, &#8230;&#8221;
<li><code>reset!</code> (and its cousin <code>ref-set</code>) is for exactly what its name says: &#8220;reset this atom back to some initial state dammit!&#8221; Only use it if you&#8217;re resetting your app, or if the new value truly doesn&#8217;t depend on the old value. In the latter case, randomly generated values or user input come to mind.
<li>Always give your transform functions a name. Passing an anonymous function to <code>swap!</code> means you just wrote a function that&#8217;s harder to test or experiment with at the repl. Besides, a descriptive name is more readable than most anonymous functions.
<li>Limit the number of call sites for update functions in your app. A system that&#8217;s sprinkled liberally with <code>(dosync)</code> blocks and calls to <code>swap!</code> will be more difficult to reason about than one where the state transitions are localized.
<li>Don&#8217;t forget the <code>args</code>! I&#8217;ve often fallen into the trap of passing an anonymous function when I needed to pass args to my state transition function.
<li>Don&#8217;t forget that most things are functions and, therefore, candidates for transform functions!
<pre>
    ; A contrived example
    (def signal (atom :red))
    (def transitions { :red :green, :green :yellow, :yellow :red })
    (swap! signal #(get transitions %)) ; &lt;- NO
    (swap! signal transitions) ; &lt;- YES
</pre>
</li>
</ul>
<p>and finally, as a wise man once said <b>&#8220;functions and data!&#8221;</b></p>
<hr />
<p>In conclusion, I hope this sheds some light on an area of Clojure that I&#8217;ve personally found to require a great deal of mental deprogramming. As I&#8217;ve gained confidence with these concepts, my code has been easier to read, easier to test, and easier to reason about. Happy Clojuring.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2011/10/clojure-update-literacy/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>On Clojure, Swig, and the UnsatisfiedLinkError</title>
		<link>http://blog.darevay.com/2011/09/on-clojure-swig-and-the-unsatisfiedlinkerror/</link>
		<comments>http://blog.darevay.com/2011/09/on-clojure-swig-and-the-unsatisfiedlinkerror/#comments</comments>
		<pubDate>Sat, 17 Sep 2011 02:40:21 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[jni]]></category>
		<category><![CDATA[repl]]></category>
		<category><![CDATA[swig]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=517</guid>
		<description><![CDATA[This week I did some Clojure work with a swigged-up C++ library that works great from Java. I was surprised when things didn&#8217;t go as smoothly in Clojure. In particular, loading native libraries in the REPL doesn&#8217;t seem to work. I&#8217;m writing this down in case someone else has the same problem in hopes that [...]]]></description>
			<content:encoded><![CDATA[<p>This week I did some <a href="http://clojure.org">Clojure</a> work with a <a href="http://www.swig.org">swigged-up</a> C++ library that works great from Java. I was surprised when things didn&#8217;t go as smoothly in Clojure. In particular, loading native libraries in the REPL doesn&#8217;t seem to work. I&#8217;m writing this down in case someone else has the same problem in hopes that it&#8217;ll save them an hour or two of head-scratching and library path fiddling.</p>
<p>So, to set the stage, let&#8217;s make a tiny Swig library, compile it and try using it from the REPL. I&#8217;m using Leiningen just to save some time:</p>
<pre>
    $ lein new swiggy
    $ cd swiggy
    $ cat &gt;swiggy.i &lt;&lt;EOF
    &gt; %module Swiggy
    &gt; %{
    &gt;   int get_something() { return 42; }
    &gt; %}
    &gt; int get_something();
    &gt; EOF
    $ swig -java -package swiggy -outdir src/swiggy swiggy.i
    $ gcc -I$JAVA_HOME/include -L$JAVA_HOME/lib -shared swiggy_wrap.c -o libswiggy.dylib
    $ javac src/swiggy/Swiggy*.java
    $ export JAVA_OPTS="-Djava.library.path=. -verbose:jni"
    $ lein repl
    ... snipped tons of irrelevant -verbose:jni output ...
    user=&gt; (System/loadLibrary "swiggy")
    nil
    user=&gt; (swiggy.Swiggy/get_something)
    java.lang.UnsatisfiedLinkError: swiggy.SwiggyJNI.get_something()I (NO_SOURCE_FILE:0)
</pre>
<p>That&#8217;s interesting. <code>libswiggy.dylib</code> loaded happily and yet there&#8217;s an UnsatisfiedLinkError. The thing to note here is that it&#8217;s complaining about linking the <code>get_something()</code> <em>method</em>, not the library itself. That&#8217;s unexpected. Also note that I have the <code>-verbose:jni</code> flag set, but nothing was printed about linking the method to the backing native code in the library. hmmm.</p>
<p>So, I googled. And found a <a href="https://groups.google.com/group/clojure/browse_thread/thread/aa72e43091ec3228?pli=1">couple references</a> to this problem without real solutions. Unfortunately, the usual reaction to this problem is immediately &#8220;Did you set LD_LIBRARY_PATH and java.library.path?&#8221; The exception is the same and that&#8217;s normally the problem in Java. It turns out that putting the <code>loadLibrary()</code> call within a Java class that&#8217;s loaded by Clojure is enough to trick the system into correctly linking the native method. That is, edit Swiggy.java something like this:</p>
<pre>
    ...
    public class Swiggy {
      // Explicitly load the library when this class loads
      static {
        System.loadLibrary("swiggy");
      }
      public static int get_something() {
        return SwiggyJNI.get_something();
      }
    }
    ...
</pre>
<p>then recompile and give it another try:</p>
<pre>
    $ javac src/swiggy/Swiggy*.java
    $ lein repl
    user=&gt; (swiggy.Swiggy/get_something)
    [Dynamic-linking native method swiggy.SwiggyJNI.get_something ... JNI]
    42
</pre>
<p>That&#8217;s better. Does anyone know what causes this? Is it related to the classloader used by the repl or something?</p>
<p>If your in the situation where you can&#8217;t or don&#8217;t want to modify the swig interface file to include this change, all is not lost. Simply create your own dummy Java class with the <code>loadLibrary</code> call and reference it from Clojure. You can even use Leiningen&#8217;s <code>:java-source-path</code> property to get it to auto-compile it for you. I suppose <code>gen-class</code> should also work, but the Java&#8217;s pretty trivial.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2011/09/on-clojure-swig-and-the-unsatisfiedlinkerror/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Briefly, the “arity-reduce” pattern in Clojure</title>
		<link>http://blog.darevay.com/2011/08/briefly-the-arity-reduce-pattern-in-clojure/</link>
		<comments>http://blog.darevay.com/2011/08/briefly-the-arity-reduce-pattern-in-clojure/#comments</comments>
		<pubDate>Sun, 21 Aug 2011 02:43:00 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=500</guid>
		<description><![CDATA[Update: As noted on Reddit, this pattern, as usual, is both old and well-known: induction or structural induction. Duh. :) Update2:: Or catemorphism perhaps (see comments). I think it&#8217;s a good policy to read as much source code as I write. Writing code will make you better, and you&#8217;ll even stumble across some genuinely good [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> As noted on <a href="http://www.reddit.com/r/programming/comments/jpdqg/the_arityreduce_pattern_in_clojure/">Reddit</a>, this pattern, as usual, is both old and well-known: <a href="http://existentialtype.wordpress.com/tag/induction/">induction</a> or <a href="http://en.wikipedia.org/wiki/Structural_induction">structural induction</a>. Duh. :)</p>
<p><strong>Update2:</strong>: Or <a href="http://en.wikipedia.org/wiki/Catamorphism#Catamorphisms_in_functional_programming">catemorphism</a> perhaps (see comments).</p>
<hr/>
<p>I think it&#8217;s a good policy to <strong>read</strong> as much source code as I write. Writing code will make you better, and you&#8217;ll even stumble across some genuinely good approaches to software. However, it&#8217;s hard to compete with closely studying other people&#8217;s code. With that in mind, I&#8217;ve been learning <a href="http://clojure.org">Clojure</a> since late last year and making a point to read the source code for the standard library regularly. It&#8217;s really great code and I guess it&#8217;s idiomatic Clojure pretty much by definition (with the exception of some of the core preamble).</p>
<p>While exploring Clojure, there&#8217;s a pattern that I&#8217;ve encountered which I&#8217;ll call the &#8220;arity-reduce&#8221; pattern. If it has an official name, I&#8217;m not aware of it. In any case, I&#8217;ve seen it many times, but never seen it called out in books or blogs. It came up again today in a <a href="http://www.4clojure.com/problem/69">4clojure problem</a> so I figured I&#8217;d write it down.</p>
<p>To motivate things, let&#8217;s image we&#8217;re implementing <strong><a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/max">clojure.core/max</a></strong>, which takes one or more numeric arguments and returns their maximum value. Here&#8217;s how I might have approached it when I started learning Clojure:</p>
<pre>(defn my-max [x &amp; more]
  (if more
    (let [y (first more)]
      (recur (if (&gt; x y) x y) (next more)))
    x))</pre>
<p>This works, but the meaning of the what we&#8217;re trying to accomplish is lost in the details of the implementation. Here&#8217;s the official implementation:</p>
<pre>(defn max
  "Returns the greatest of the nums."
  {:added "1.0"}
  ([x] x)
  ([x y] (if (&gt; x y) x y))
  ([x y &amp; more]
   (reduce max (max x y) more)))</pre>
<p>Note how nice and declarative this is. It basically describes the solution to the problem:</p>
<ul>
<li>Given a single argument, return it</li>
<li>Given two arguments, return the greater</li>
<li>Given more arguments, take the max of the first two args, rinse and repeat.</li>
</ul>
<p>Maybe I&#8217;m slow, but for me, this was an eye-opener. With Clojure I&#8217;ve had to learn that whenever I write an anonymous function for use in <code>map</code> or <code>reduce</code>, I should ponder whether a built-in function that does what I need already exists (this is especially true with using sets, maps, and keywords as functions). Similarly, I should always ask my self if multiple arities of the same function would be clearer than jamming the whole implementation into one.</p>
<p>Here are some places where this pattern is used in Clojure:</p>
<ul>
<li><a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L809">+</a> (add)</li>
<li><a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L820">*</a> (multiply)</li>
<li><a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L831">/</a> (divide)</li>
<li><a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L842">-</a> (subtract)</li>
<li><a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L912">max</a></li>
<li><a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L920">min</a></li>
<li><a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L3947">max-key</a></li>
<li><a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L3955">min-key</a></li>
<li><a href="https://github.com/clojure/clojure/blob/a07040032714a2f88c23b76c6484c1b8acd462b0/src/clj/clojure/set.clj#L19">union</a></li>
<li><a href="https://github.com/clojure/clojure/blob/a07040032714a2f88c23b76c6484c1b8acd462b0/src/clj/clojure/set.clj#L32">intersection</a></li>
<li><a href="https://github.com/clojure/clojure/blob/a07040032714a2f88c23b76c6484c1b8acd462b0/src/clj/clojure/set.clj#L48">difference</a></li>
</ul>
<p>And a few more that are made clearer by multi-arity definitions although they don&#8217;t explicitly use reduce in the &#8220;more&#8221; case:</p>
<ul>
<li><a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L751">&lt;</a>, <a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L868">&gt;</a>, and the rest of the comparison functions</li>
<li><a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L693">and</a></li>
<li><a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L705">or</a></li>
<li>The <a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L1266">..</a>, <a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L1284">-&gt;</a>, and <a href="https://github.com/clojure/clojure/blob/f86db9cc68773dd3e4a166c1ff7b81e4a98aa602/src/clj/clojure/core.clj#L1296">-&gt;&gt;</a> threading macros</li>
</ul>
<p>Cheers and happy Clojuring!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2011/08/briefly-the-arity-reduce-pattern-in-clojure/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Dorothy: Graphviz from the comfort of Clojure</title>
		<link>http://blog.darevay.com/2011/08/dorothy-graphviz-from-the-comfort-of-clojure/</link>
		<comments>http://blog.darevay.com/2011/08/dorothy-graphviz-from-the-comfort-of-clojure/#comments</comments>
		<pubDate>Sat, 13 Aug 2011 03:42:26 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[graphviz]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=491</guid>
		<description><![CDATA[I&#8217;ve used Graphviz quite a bit in the past. When I did, I was almost always generating dot files from code; I never wrote them by hand other than to experiment with various Graphviz features. Well, string-slinging is a pain. Generating one language from another is a pain. So, inspired by Hiccup, I threw together [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve used <a href="http://www.graphviz.org/">Graphviz</a> quite a bit in the past. When I did, I was almost always generating dot files from code; I never wrote them by hand other than to experiment with various Graphviz features. Well, string-slinging is a pain. Generating one language from another is a pain. So, inspired by <a href="https://github.com/weavejester/hiccup">Hiccup</a>, I threw together <a href="https://github.com/daveray/dorothy">Dorothy</a> over the last couple evenings. It&#8217;s a Clojure DSL for generating DOT graph representations as well as rendering them to images.</p>
<p>For a few hours work, the <a href="https://github.com/daveray/dorothy">documentation is pretty thorough</a>, so I&#8217;ll leave off with one simple example which is translated from the <a href="http://www.graphviz.org/content/ER">Entity Relationship example</a> in the Graphviz gallery. Any feedback or criticism is welcome and encouraged.</p>
<p><script src="https://gist.github.com/1143453.js?file=er.clj"></script></p>
<p>and here it is displayed in Dorothy&#8217;s simple built-in viewer:</p>
<p><a href="http://blog.darevay.com/wp-content/uploads/2011/08/dorothy-er.png"><img src="http://blog.darevay.com/wp-content/uploads/2011/08/dorothy-er.png" alt="Entity Relationship graph" title="dorothy-er" width="624" height="602" class="aligncenter size-full wp-image-493" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2011/08/dorothy-graphviz-from-the-comfort-of-clojure/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>I did a bad thing with ClojureScript</title>
		<link>http://blog.darevay.com/2011/07/i-did-a-bad-thing-with-clojurescript/</link>
		<comments>http://blog.darevay.com/2011/07/i-did-a-bad-thing-with-clojurescript/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 03:45:56 +0000</pubDate>
		<dc:creator>dave</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[clojurescript]]></category>

		<guid isPermaLink="false">http://blog.darevay.com/?p=479</guid>
		<description><![CDATA[UPDATE: Don&#8217;t do this. It&#8217;s a joke and (js*) is an internal detail of ClojureScript. If you need to access native code, access it through the js namespace. Tonight ClojureScript was announced and made available. I&#8217;m interested in how JavaScript interop works for environments like node.js, CouchDB, etc. So far, it seems like (js*) is [...]]]></description>
			<content:encoded><![CDATA[<p><b>UPDATE:</b> Don&#8217;t do this. It&#8217;s a joke and <code>(js*)</code> is an internal detail of ClojureScript. If you need to access native code, access it through the <code>js</code> namespace.</p>
<hr />
Tonight <a href="https://github.com/clojure/clojurescript">ClojureScript</a> was announced and made available. I&#8217;m interested in how JavaScript interop works for environments like node.js, CouchDB, etc. So far, it seems like <strong>(js*)</strong> is the main entry point to JavaScript land and functions like <strong>(aset)</strong> can also be used:</p>
<pre>
$ script/repljs
#'user/jse
"Type: " :cljs/quit " to quit"
ClojureScript:cljs.user> (def something (js* "{x: 100}"))
#&lt;[object Object]>
ClojureScript:cljs.user> (.x something)
100
ClojureScript:cljs.user> (aget something "x")
100
ClojureScript:cljs.user> (aset something "y" 99)
99
ClojureScript:cljs.user> (.y something)
99
</pre>
<p>It happens that <strong>repljs</strong> (the ClojureScript repl) runs in <a href="https://www.mozilla.org/rhino/">Rhino</a> which is ripe for abuse:</p>
<pre>
$ script/repljs
#'user/jse
"Type: " :cljs/quit " to quit"
ClojureScript:cljs.user> (def f
  (js* "new Packages.javax.swing.JFrame(\"ClojureScript!!\")"))
#&lt;javax.swing.JFrame ... >
ClojureScript:cljs.user> (js* "~{f}.setVisible(true)")
#&lt;undefined>
</pre>
<p><a href="http://blog.darevay.com/wp-content/uploads/2011/07/clojurescript.png"><img src="http://blog.darevay.com/wp-content/uploads/2011/07/clojurescript.png" alt="" title="clojurescript" width="281" height="147" class="aligncenter size-full wp-image-480" /></a></p>
<p>Yes. I am a bad person. I will try to do something useful with ClojureScript shortly.</p>
<p>Also note the interpolation syntax, <code>~{f}</code>, for referring to Clojure bindings in JavaScript.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.darevay.com/2011/07/i-did-a-bad-thing-with-clojurescript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

