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

<channel>
	<title>searching for signal &#187; Chouser</title>
	<atom:link href="http://blog.n01se.net/?feed=rss2&#038;author=3" rel="self" type="application/rss+xml" />
	<link>http://blog.n01se.net</link>
	<description>theoretically unambiguous</description>
	<lastBuildDate>Sun, 29 Aug 2010 20:21:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using binding to mock out even &#8220;direct linked&#8221; functions in Clojure</title>
		<link>http://blog.n01se.net/?p=134</link>
		<comments>http://blog.n01se.net/?p=134#comments</comments>
		<pubDate>Sat, 17 Apr 2010 19:00:44 +0000</pubDate>
		<dc:creator>Chouser</dc:creator>
				<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://blog.n01se.net/?p=134</guid>
		<description><![CDATA[The Clojure macro binding is frequently handy for mocking out functionality during testing, but this sometimes does not behave as desired in multi-threaded context. Fortunately, there's a solution... For example, at work we have unit tests on functions that may attempt to talk to network services, which we'd rather they not do: (defn send-request [request [...]]]></description>
			<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>The Clojure macro <tt class="docutils literal"><span class="pre">binding</span></tt> is frequently handy for mocking out
functionality during testing, but this sometimes does not behave as
desired in multi-threaded context.  Fortunately, there's a solution...</p>
<p>For example, at work we have unit tests on functions that may attempt
to talk to network services, which we'd rather they not do:</p>
<pre class="literal-block">
(defn send-request [request server]
  '(...do real RPC stuff here...))

(defn average-timestamp [time-servers]
  (/ (apply + (map #(send-request :get-timestamp %)
                   time-servers))
     (count time-servers)))
</pre>
<p>In order to test <tt class="docutils literal"><span class="pre">average-timestamp</span></tt>, we need a &quot;mock&quot; function that
will stand in for <tt class="docutils literal"><span class="pre">send-request</span></tt>.  This must be a function that
takes a request and a server and returns a timestamp, just like the
real send-request.  For this example, it can take the timestamp itself
as the &quot;server&quot; and simply return that timestamp.  For bonus points we
can make sure the request parameter is what we expect:</p>
<pre class="literal-block">
(defn mock-send-request [request server]
  (assert (= request :get-timestamp))
  server)  ; assume &quot;server&quot; is actually the timestamp

(mock-send-request :get-timestamp 5)
;=&gt; 5
</pre>
<p>Using <tt class="docutils literal"><span class="pre">binding</span></tt> we can temporarily replace <tt class="docutils literal"><span class="pre">send-request</span></tt> with
<tt class="docutils literal"><span class="pre">mock-send-request</span></tt>:</p>
<pre class="literal-block">
(binding [send-request mock-send-request]
  (send-request :get-timestamp 5))
;=&gt; 5

(binding [send-request mock-send-request]
  (average-timestamp [5 15]))
;=&gt; 10
</pre>
<p>So there's the background: a pretty normal way to mock out stuff in
Clojure.  What this doesn't address is when some clever co-worker (hi,
Nathan!) realizes that <tt class="docutils literal"><span class="pre">average-timestamp</span></tt> would work better if it
talked to multiple time-servers in parallel, and that this could be
easily accomplished by replacing the use of <tt class="docutils literal"><span class="pre">map</span></tt> with <tt class="docutils literal"><span class="pre">pmap</span></tt>:</p>
<pre class="literal-block">
(defn average-timestamp [time-servers]
  (/ (apply + (pmap #(send-request :get-timestamp %) time-servers))
     (count time-servers)))
</pre>
<p>This is an easy single-letter change that indeed works quite well with
the real <tt class="docutils literal"><span class="pre">send-request</span></tt>.  But when we try to use <tt class="docutils literal"><span class="pre">binding</span></tt> to mock it
out, we run into problems:</p>
<pre class="literal-block">
(binding [send-request mock-send-request]
  (average-timestamp [5 15]))
; java.lang.ClassCastException:
;   clojure.lang.PersistentList cannot be cast to java.lang.Number
</pre>
<p>It's not obvious from the error message, but what's happening is our
original un-mocked <tt class="docutils literal"><span class="pre">send-request</span></tt> is getting called.  This is because
<tt class="docutils literal"><span class="pre">binding</span></tt> only has an effect on the current thread, but <tt class="docutils literal"><span class="pre">pmap</span></tt> causes
<tt class="docutils literal"><span class="pre">send-request</span></tt> to be called in other threads.</p>
<p>The solution is simple enough in Clojure 1.0 -- you simply mock out
<tt class="docutils literal"><span class="pre">pmap</span></tt> as well, and since the API is identical to <tt class="docutils literal"><span class="pre">map</span></tt>, it's quite
easy to do.  But as you can see, this does us no good at all in recent
versions of Clojure:</p>
<pre class="literal-block">
(binding [send-request mock-send-request
          pmap map]
  (average-timestamp [5 15]))
; java.lang.ClassCastException:
;   clojure.lang.PersistentList cannot be cast to java.lang.Number
</pre>
<p>The reason is that starting with Clojure 1.1, most clojure.core Vars
are linked directly into code that uses them.  This means that our
definition of <tt class="docutils literal"><span class="pre">average-timestamp</span></tt> above links directly to the actual
definition of <tt class="docutils literal"><span class="pre">clojure.core/pmap</span></tt>, not just the Var that points to it,
thus attempts to rebind the Var with <tt class="docutils literal"><span class="pre">binding</span></tt> are futile.</p>
<p>But do not despair, there is a solution even for this.  All you need
to do is tell Clojure not to directly link <tt class="docutils literal"><span class="pre">pmap</span></tt> when it compiles
<tt class="docutils literal"><span class="pre">average-timestamp</span></tt>.  This is done by adjusting <tt class="docutils literal"><span class="pre">pmap</span></tt>'s metadata
before <tt class="docutils literal"><span class="pre">average-timestamp</span></tt> is compiled, like so:</p>
<pre class="literal-block">
; Set pmap to be dynamically linked
(alter-meta! #'pmap assoc :dynamic true)

; Must now re-define the function that uses pmap
(defn average-timestamp [time-servers]
  (/ (apply + (pmap #(send-request :get-timestamp %)
                    time-servers))
     (count time-servers)))

; Finally, our mocking out of pmap works
(binding [send-request mock-send-request
          pmap map]
  (average-timestamp [5 15]))
;=&gt; 10
</pre>
<p>The best place to put the <tt class="docutils literal"><span class="pre">alter-meta!</span></tt> depends on your particular use
case.  You might want to figure out how to do the <tt class="docutils literal"><span class="pre">alter-meta!</span></tt> only
when in testing and not in production.  Remember that the metadata on
<tt class="docutils literal"><span class="pre">pmap</span></tt> is global -- you're changing the only metadata that
<tt class="docutils literal"><span class="pre">clojure.core/pmap</span></tt> has, so the namespace you're in when you do it
makes no difference at all.</p>
<p>In our case at work however, the performance impact of leaving pmap
dynamic all the time was not enough to worry about.  Dynamic linking
is still pretty fast and is fine even for our production uses of
<tt class="docutils literal"><span class="pre">pmap</span></tt> in this particular code base.  So we simply put the
<tt class="docutils literal"><span class="pre">alter-meta!</span></tt> at the top of the file that used <tt class="docutils literal"><span class="pre">pmap</span></tt> and got on with
our unit testing.</p>
<a class="reference image-reference" href="http://joyofclojure.com/"><img align="left" alt="http://blog.n01se.net/wp-content/uploads/2010/01/joy.png" class="align-left" src="http://blog.n01se.net/wp-content/uploads/2010/01/joy.png" /></a>
<dl class="docutils">
<dt><strong>The Joy of Clojure</strong></dt>
<dd>Thinking the Clojure Way</dd>
</dl>
<p><em>by Michael Fogus and Chris Houser</em></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.n01se.net/?feed=rss2&amp;p=134</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Clojure: Controlling run-away trains, onions, and exercise bikes.</title>
		<link>http://blog.n01se.net/?p=85</link>
		<comments>http://blog.n01se.net/?p=85#comments</comments>
		<pubDate>Wed, 03 Feb 2010 13:35:20 +0000</pubDate>
		<dc:creator>Chouser</dc:creator>
				<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://blog.n01se.net/?p=85</guid>
		<description><![CDATA[A normal Clojure REPL (or prompt) in a terminal window is by default a bit touchy about infinite seqs, deeply nested structures, and long-running operations. Any of these can cause your REPL to wander off into the weeds, busy spinning, perhaps printing pages of useless data, with the only apparent remedy being to press CTRL-C, [...]]]></description>
			<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>A normal Clojure REPL (or prompt) in a terminal window is by default a bit
touchy about infinite seqs, deeply nested structures, and long-running
operations.  Any of these can cause your REPL to wander off into the weeds, busy
spinning, perhaps printing pages of useless data, with the only apparent remedy
being to press CTRL-C, which generally kills the entire JVM, Clojure and all,
and dumps you back at your operating system prompt.</p>
<p>It's unfortunate that this is the default, but Clojure's youth shows
particularly when it come to tools and settings like this.  Happily it's
sufficiently mature to provide several solutions that are not difficult to
apply.</p>
<div class="section" id="run-away-trains">
<h3>Run-away trains</h3>
<p>The most common of the problems listed above is the infinite seq.  Such a seq
is easy to create, easy to print, and can result in an run-away REPL.  The
solution: setting your <tt class="docutils literal"><span class="pre">*print-length*</span></tt> to something short of infinity.
I recommend 103:</p>
<pre class="literal-block">
(set! *print-length* 103)
</pre>
<p>Now it's safe to print infinite sequences:</p>
<pre class="literal-block">
(iterate inc 0)
;=&gt; (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
    24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 ...)
</pre>
<p>Note the <tt class="docutils literal"><span class="pre">...</span></tt> at the end of the list which indicates there was more to print, but
Clojure is respecting our requested limit and giving up after 103 items.  I've
been mocked upon occasion for choosing 103, as if 102 is insufficient and 104
dangerous.  Of course it's not so important exactly what you pick, so I'm happy
to keep my rationale to myself.</p>
</div>
<div class="section" id="run-away-onions">
<h3>Run-away onions</h3>
<p>But <tt class="docutils literal"><span class="pre">*print-length*</span></tt> won't help you in the case of infinitely recursive
structures, data nested inside data like layers of an onion.  While less common,
such nesting can be deep enough that it can still be a problem.  To limit
printing of recursive structures, use <tt class="docutils literal"><span class="pre">*print-level*</span></tt>.  Usually 15 is about
right for me:</p>
<pre class="literal-block">
(set! *print-level* 15)
</pre>
<p>Now it's safe to print infinitely recursive structures:</p>
<pre class="literal-block">
(let [x (atom 0)] (reset! x {:deeper x}))
;=&gt; {:deeper #&lt;Atom&#64;4cb533b8: {:deeper #&lt;Atom&#64;4cb533b8:
    {:deeper #&lt;Atom&#64;4cb533b8: {:deeper #&lt;Atom&#64;4cb533b8:
    {:deeper #&lt;Atom&#64;4cb533b8: {:deeper #&lt;Atom&#64;4cb533b8:
    {:deeper #&lt;Atom&#64;4cb533b8: {:deeper #}&gt;}&gt;}&gt;}&gt;}&gt;}&gt;}&gt;}
</pre>
<p>Again you get a little textual indicator that print is giving up, in this case
it's just the <tt class="docutils literal"><span class="pre">#</span></tt> at the deepest level.</p>
</div>
<div class="section" id="run-away-exercise-bikes">
<h3>Run-away exercise bikes</h3>
<p>But these settings only help take control of run-away printing.  Sometimes the
REPL gets hung up doing something other than printing, sitting there spinning
away like a stationary bike, and neither of these settings will help you.  My
final advice is to use <tt class="docutils literal"><span class="pre">repl-utils</span></tt> to register your REPL thread as killable
by CTRL-C:</p>
<pre class="literal-block">
(require 'clojure.contrib.repl-utils)
(clojure.contrib.repl-utils/add-break-thread!)
</pre>
<p>Now when you press CTRL-C, instead of shutting down the whole JVM, an exception
will be thrown within the REPL thread, which is usually exactly what you need to
halt run-away computation.  Note that <tt class="docutils literal"><span class="pre">repl-utils</span></tt> does this using
a deprecated Java API that is described as <a class="reference" href="http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#stop()">unsafe</a>.  And yet
it remains useful for just such circumstances.  For example:</p>
<pre class="literal-block">
(deref (promise))
</pre>
<p>Look at that -- instant deadlock!  Now press CTRL-C, and you get:</p>
<pre class="literal-block">
java.lang.Exception: SIGINT (NO_SOURCE_FILE:0)
</pre>
<p>...and you're back to REPL prompt.  But now you've violated Java, so I'd
recommend saving what you need to save and then restart your JVM anyway.  If you
don't, weird things can happen.  For example if you run the above deref-promise
example again immediately, it may fail with a slightly different exception, even
before you press CTRL-C.  Similar strangeness can occur if you press CTRL-C at
the REPL when no operation is running, though in that case it usually just kills
your next expression, whatever it is.</p>
<p>Hopefully by setting your <tt class="docutils literal"><span class="pre">*print-length*</span></tt>, <tt class="docutils literal"><span class="pre">*print-level*</span></tt>, and
break-thread, you can feel more comfortable at the REPL with less fear of
getting stuck.</p>
<a class="reference image-reference" href="http://joyofclojure.com/"><img align="left" alt="http://blog.n01se.net/wp-content/uploads/2010/01/joy.png" class="align-left" src="http://blog.n01se.net/wp-content/uploads/2010/01/joy.png" /></a>
<dl class="docutils">
<dt><strong>The Joy of Clojure</strong></dt>
<dd>Thinking the Clojure Way</dd>
</dl>
<p><em>by Michael Fogus and Chris Houser</em></p>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.n01se.net/?feed=rss2&amp;p=85</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The Joy of Clojure, my perspective.</title>
		<link>http://blog.n01se.net/?p=80</link>
		<comments>http://blog.n01se.net/?p=80#comments</comments>
		<pubDate>Sun, 31 Jan 2010 15:40:04 +0000</pubDate>
		<dc:creator>Chouser</dc:creator>
				<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://blog.n01se.net/?p=80</guid>
		<description><![CDATA[There are a few books about Clojure in the works now. But despite the variety in authorship and publisher, they all seem to be primarily in the vein of a language tutorial, starting from a basis of &#34;common knowledge&#34; among most programmers and taking the reader to a level of basic familiarity with Clojure. Michael [...]]]></description>
			<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>There are a few books about <a class="reference" href="http://clojure.org/">Clojure</a> in the works now.
But despite the variety in authorship and publisher, they all seem to be
primarily in the vein of a language tutorial, starting from a basis of &quot;common
knowledge&quot; among most programmers and taking the reader to a level of basic
familiarity with Clojure.</p>
<p>Michael Fogus and I think there is more to Clojure that has yet to be addressed
by any of the books currently being written.  So we've started writing <a class="reference" href="http://joyofclojure.com/">&quot;The Joy
of Clojure&quot;</a>.  Our vision for this book is to impart
not just what Clojure <em>is</em>, but <em>why</em> it's the way it is; not just <em>how</em> to use it,
but how to <em>choose well</em> from among the wide variety of options Clojure provides.
And along the way we hope you'll experience the wonder, amazement, and, yes <em>joy</em> that
Clojure can bring to programming tasks that often feel like nothing more than
chores in other environments.</p>
<p>I also wanted to take this opportunity to brag a little on <a class="reference" href="http://blog.fogus.me/about/">Fogus</a>.  He's been a fantastic partner throughout this
process -- I wouldn't even be involved if it weren't for him.  He has put into
our book many references to papers and books that I've never heard of, but that
illuminate much of Clojure's design -- clearly he has read a good deal more than
I have.  Once we're done writing, I intend to use the Bibliography as a reading
list -- I'm sure it will be enlightening.  He is dedicated to producing the best
book we can, and he's the only reason we're meeting any of our deadlines.  I'm
looking forward to the next few months as we finish this book together.</p>
<a class="reference image-reference" href="http://joyofclojure.com/"><img align="left" alt="http://blog.n01se.net/wp-content/uploads/2010/01/joy.png" class="align-left" src="http://blog.n01se.net/wp-content/uploads/2010/01/joy.png" /></a>
<dl class="docutils">
<dt><strong>The Joy of Clojure</strong></dt>
<dd>Thinking the Clojure Way</dd>
</dl>
<p><em>by Michael Fogus and Chris Houser</em></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.n01se.net/?feed=rss2&amp;p=80</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>What is Clojure-in-Clojure?</title>
		<link>http://blog.n01se.net/?p=41</link>
		<comments>http://blog.n01se.net/?p=41#comments</comments>
		<pubDate>Sat, 11 Jul 2009 04:10:49 +0000</pubDate>
		<dc:creator>Chouser</dc:creator>
				<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://blog.n01se.net/?p=41</guid>
		<description><![CDATA[The code making up Clojure's current implementation can be divided into 3 main groups: 1. The compiler -- used at the REPL and for AOT compilation, the compiler translates Clojure expressions into JVM bytecode. For this discussion, the reader lives in this group. 2. The data structures -- persistent maps, sets, and vectors, sorted and [...]]]></description>
			<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>The code making up Clojure's current implementation can be divided
into 3 main groups:</p>
<p>1. The compiler -- used at the REPL and for AOT compilation, the
compiler translates Clojure expressions into JVM bytecode.  For
this discussion, the reader lives in this group.</p>
<p>2. The data structures -- persistent maps, sets, and vectors, sorted
and unsorted.  For this discussion we'll also include here the
reference types (ref, agent, var, atom) and the STM implementation.</p>
<p>3. clojure.core -- Destructuring, most of syntax quote, the Clojure
functions for manipulating seqs (filter, map, for, doseq, etc.) and
all the other programmer-facing functions we know and love.</p>
<p>Today, Clojure is written mostly in Java.  Of the groups above, only
the core library is written almost entirely in Clojure.  This is no
small thing, mind you -- the language that is defined by just the
first two is an awkward, tiny little language that is rather painful
to use.  (You can see this demonstrated near the top of
clojure/core.clj.)
However, there is still quite a bit of code in the
compiler and data structures, and they're almost entirely implemented
in .java files.</p>
<p>Clojure-in-Clojure is the effort to re-write the compiler and built-in
data structures in Clojure.  Note that the primary compiler
implementation would still run on the JVM and still produce JVM
bytecode.  This is not about getting rid of the JVM or implementing
any new runtime or virtual machine.</p>
<div class="section" id="but-first-a-better-new">
<h3>But first a better 'new'</h3>
<p>There's a bit of work that has to be done first.  Specifically,
although you could use gen-class or proxy to implement the Clojure
data structures today, their performance would not match the current
Java implementations.  Both gen-class and proxy use an extra
dereference on each method call which give them dynamic redefinition
features.  For application-level code this is often desirable, but for
these low level data structures it is not.  Rich Hickey's plan for
solving this is a more featureful 'new' operator cleverly nicknamed
&quot;new-new&quot; [update: this has since been renamed reify and has been augmented with the related constructs defprotocol and deftype].</p>
<p>Clojure is already sufficient for implementing the compiler (the
reader being the low-hanging fruit there, I would think), and once
new-new is in place the data structures can be ported to Clojure as
well.</p>
</div>
<div class="section" id="the-benefits-of-clojure-in-clojure">
<h3>The benefits of Clojure-in-Clojure</h3>
<p>So what's the point of all this?  I'm not sure what Rich's primary
motivation is here, but it will certainly be nice that writing bug
fixes and features for Clojure itself will involve more time in
Clojure and less in Java.</p>
<p>But a more fascinating benefit is that porting Clojure to non-JVM
targets will be much easier.  The majority of the effort so far put
into ClojureCLR and ClojureScript has been rewriting the data structures for the target platform.  This
has required a lot of hand-written C# and JavaScript (respectively)
all of which can become quickly obsolete as changes are made to the
primary Java versions.</p>
<p>Once Clojure-in-Clojure is complete, there will be no need to
hand-port the data structures.  In fact, most of the compiler won't
have to be ported either.  Imagine you want to be able to run Clojure
code on the Parrot VM.  All you'd need to do is describe how each of
Clojure's dozen or so special forms (including new-new) are to be
compiled to Parrot bytecode.  This code would be written in Clojure,
and would initially run on the JVM to AOT-compile all the rest of
Clojure (the rest of the compiler, the data structures, clojure.core)
to Parrot bytecode.  And <em>poof</em> you'd have ParrotClojure -- start it
up in the Parrot VM and you'd have a ParrotClojure REPL.</p>
<p>Clojure is already great at allowing you to use the JVM without
dealing with Java.  Clojure-in-Clojure would be another step in the
same direction, with the added benefit of making it easier to target
non-JVM platforms.</p>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.n01se.net/?feed=rss2&amp;p=41</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Experimental branches of Clojure</title>
		<link>http://blog.n01se.net/?p=39</link>
		<comments>http://blog.n01se.net/?p=39#comments</comments>
		<pubDate>Sat, 14 Feb 2009 20:03:10 +0000</pubDate>
		<dc:creator>Chouser</dc:creator>
				<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://blog.n01se.net/?p=39</guid>
		<description><![CDATA[The author of Clojure, Rich Hickey, has been contemplating changing the behavior of one of Clojure's core features, the seq abstraction. He's been working on a few different alternatives for a couple months now. First, a quick review of how things stand now. Currently a lazy seq can be produced by using lazy-cons: (defn my-range [...]]]></description>
			<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>The author of Clojure, Rich Hickey, has been contemplating changing
the behavior of one of Clojure's core features, the <strong>seq abstraction</strong>.
He's been working on a few different alternatives for a couple months
now.</p>
<p>First, a quick review of how things stand now.  Currently a lazy seq
can be produced by using lazy-cons:</p>
<pre class="literal-block">
(defn my-range [from to]
  (when (&lt; from to)
    (lazy-cons from (my-range (inc from) to))))
</pre>
<p>Calling my-range returns a lazy seq of the integers between <em>from</em> and
<em>to</em>.  This seq can be used by any of the functions that consume
seqs:</p>
<pre class="literal-block">
user=&gt; (reduce + (my-range 5 10))
35
</pre>
<p>Reduce takes the first two numbers in the seq (5 and 6) and passes
them as parameters to the addition function.  This result and the next
value from the seq (11 and 7) are passed to the addition function
again.  This continues until the end of the seq is reached, and the
result (35) is returned.</p>
<p>The first alternative I heard about is the Left Fold Enumerator (LFE).
LFEs are about inversion of control, where the function passed to
reduce would be in charge of the looping and could choose to stop
before the end of the seq and signal to the seq to free any resources
it was holding.</p>
<p>This was was explored in <a class="reference" href="http://groups.google.com/group/clojure/browse_thread/thread/3cb6bcd1d19b5fdf/1c7f57977b1498f9">much more detail by Christophe Grand</a>.</p>
<p>However after some consideration, Rich appears to have abandoned the
idea of replacing Clojure's current seq abstraction with LFEs.</p>
<p>The next alternative is something called &quot;streams&quot;.  These are
stateful iterators, carefully protected to help prevent the problems
of state that Clojure sets out to solve.  Their main benefits over the
existing seq model are better performance and more complete laziness.
Clojure's existing seq abstraction could be built on top of streams
such that both the extra laziness and most of the performance
would be available to user-level code that remains almost entirely unchanged</p>
<p>One user-visible change would be the loss of a feature called <strong>nil
punning</strong>.  Currently, something that returns a seq actually
promises to return either an instance of ISeq (which is guaranteed to
have at least one item in it), or <tt class="docutils literal"><span class="pre">nil</span></tt>.  Since <tt class="docutils literal"><span class="pre">nil</span></tt> acts like <tt class="docutils literal"><span class="pre">false</span></tt>, you
can write code like:</p>
<pre class="literal-block">
(if (filter odd? coll)          ; nil pun
  (println &quot;coll is a bit odd&quot;)
  (println &quot;no odds in coll&quot;))
</pre>
<p>If there are no odd numbers in coll, filter promises to return nil,
otherwise it will return a lazy seq containing all the odd numbers.</p>
<p>With streams, though, the behavior changes: filter could return a
non-nil object representing an empty stream.  This provides the extra
laziness that's desirable, but the example above would no longer be
correct.  If you want to find out if the stream is empty, you must
force it to do some traversal, the idiom for which is to get a seq of
the stream:</p>
<pre class="literal-block">
(if (seq (filter odd? coll))
  (println &quot;coll is a bit odd&quot;)
  (println &quot;no odds in coll&quot;))
</pre>
<p>The only way to save nil punning would be to provide a whole separate
set of seq-library-like functions for dealing with streams:
filter-stream, map-stream, concat-stream, etc.  These would not allow
nil punning, but the regular seq functions would continue to behave as
they always had.</p>
<p>Despite the potential loss of nil punning, Rich thought this approach
had enough promise that he forked a <a class="reference" href="http://code.google.com/p/clojure/source/browse/branches/streams">streams branch of Clojure</a>
and wrote <a class="reference" href="http://clojure.org/streams">some docs</a>:</p>
<pre class="literal-block">
(defn my-range [from to]
  (let [a (atom (dec from))]
    (stream
      (fn [eos]
        (swap! a inc)
        (if (&lt; &#64;a to)
          &#64;a
          eos)))))
</pre>
<p>But the stateful and imperative nature of the streams code were
apparently unpleasant enough that he began looking at another
alternative.  This new <a class="reference" href="http://code.google.com/p/clojure/source/browse/branches/lazy">lazy branch</a> does
not have quite the performance benefits of streams, but does keep the
immutable nature of Clojure and adds the extra laziness demonstrated by
streams.</p>
<p>The lazy-cons macro is gone, and instead there is a lazy-seq macro:</p>
<pre class="literal-block">
(defn my-range [from to]
  (lazy-seq
    (when (&lt; from to)
      (cons from (my-range (inc from) to)))))
</pre>
<p>This has the same loss of nil punning as streams, but perhaps the
<a class="reference" href="http://clojure.org/lazier">extra laziness</a> is worth it.</p>
<p>But what happened to the resource management touted by LFEs?  It
appears likely that a new macro (currently named 'scope' in the
streams branch) will be introduced to help with that problem,
regardless of whether the lazy or streams branch is finally adopted.</p>
<p>So there you have it -- two forks representing possible futures for
Clojure.  Which will be the chosen path?  Well, I can't say for sure,
but the only drawback of the lazy branch compared to today's Clojure
is the loss of nil punning, and Rich seemed pleased by how little code
in clojure.core was actually making that assumption.  There's even a
compile-time flag that can be turned on to help users track down their nil
puns, which I'll blog about later.</p>
<p>Besides, I think Rich pushed streams as far as he could toward
simplicity and protection against mutation, and still didn't like the
results.  I doubt the streams branch has much future, but Rich has
been asking for feedback so now's the time to explore the options and
make your opinion know.</p>
<p>In my next blog post I'll show my setup for easily switching between
Clojure branches.</p>
<p><strong>Update:</strong> My next blog post will <em>not</em> be about switching Clojure
branches.  As of Tue Feb 17 2009, the lazy branch was merged into
trunk.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.n01se.net/?feed=rss2&amp;p=39</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>They really tried, but&#8230;.</title>
		<link>http://blog.n01se.net/?p=38</link>
		<comments>http://blog.n01se.net/?p=38#comments</comments>
		<pubDate>Mon, 02 Feb 2009 22:28:03 +0000</pubDate>
		<dc:creator>Chouser</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascsript]]></category>

		<guid isPermaLink="false">http://blog.n01se.net/?p=38</guid>
		<description><![CDATA[I'm sure they really tried, but they just plain screwed it up. for( var i = 0; i &#60; foo.length; ++i ) { ... } That pattern is used very often in JavaScript to do something with each item in the array foo, setting aside for the moment that it's often not quite correct (if [...]]]></description>
			<content:encoded><![CDATA[<p>I'm sure they really tried, but they just plain screwed it up.</p>
<blockquote><p>
for( var i = 0; i &lt; foo.length; ++i ) { ... }</p></blockquote>
<p>That pattern is used very often in JavaScript to do something with each item in the array <code>foo</code>, setting aside for the moment that it's often not quite correct (if the var <code>i</code> is declared anywhere else in the same function).</p>
<p>You'd think they would have provided a more succinct way to walk<br />
through an array.  And actually they tried:</p>
<blockquote><p>
for( var i in foo ) { ... }</p></blockquote>
<p>but they screwed it up, because in this case <code>i</code> is set not to each<br />
value in <code>foo</code>, but to each <em><strong>index</strong></em>, 0 through whatever. <em><strong>...as a string.</strong></em></p>
<p>So the first time through, <code>i</code> is the string <code>"0"</code>.  Fortunately (I suppose) this still works as a parameter to your array, because you can say <code>foo["0"]</code> to get the initial item in the array.</p>
<p>But the knowledge of the amount of work being done to convert the integer range 0 through length into a series of strings, back to ints, and then look them up again in the array just pains me too much.  It's not that the performance has actually ever hurt me in such cases, it's just that I can't stand to do it.</p>
<p>So I type <code>for( var i = 0; i &lt; foo.length; ++i )</code> one more time, and glance around to make sure <code>i</code> isn't being used for anything else nearby...</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.n01se.net/?feed=rss2&amp;p=38</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>My path to Clojure</title>
		<link>http://blog.n01se.net/?p=37</link>
		<comments>http://blog.n01se.net/?p=37#comments</comments>
		<pubDate>Tue, 20 Jan 2009 04:22:57 +0000</pubDate>
		<dc:creator>Chouser</dc:creator>
				<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://blog.n01se.net/?p=37</guid>
		<description><![CDATA[Someone asked me about the path that lead me to Clojure. On the off chance that he's not the only one curious, I thought I'd post the answer here. Sometime after college I started hunting for the &#34;best language&#34;, not with any formal definition but as a personal quest for what I liked the best. [...]]]></description>
			<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p>Someone asked me about the path that lead me to Clojure.  On the off
chance that he's not the only one curious, I thought I'd post the
answer here.</p>
<p>Sometime after college I started hunting for the &quot;best language&quot;, not
with any formal definition but as a personal quest for what I liked
the best.  A primary criteria was something I called &quot;expressiveness&quot;.
I don't know if I can define that accurately, but it has something to
do with being able to provide APIs that concisely model whatever
specific domain you're dealing with.  A common example is how much
easier it is to work with text in perl (because of strongly integrated
regex support) than it is in C.</p>
<p>My &quot;best language&quot; before Clojure was Ruby, because of its clean
object system and iterators, but I hadn't experienced any functional
or immutable languages yet.</p>
<p>I had heard a lot about how amazing LISP was, but I didn't get it.  I
couldn't figure out why anyone liked LISP's annoying syntax, or why
anyone (who wasn't deeply into Artificial Intellegence) would want to
write programs that write programs.  So one big turning point was
reading &quot;On Lisp&quot; by Paul Graham (<a class="reference" href="http://www.paulgraham.com/onlisp.html">free download</a>).  By the time I was done
with that book, I was convinced that any &quot;best language&quot; had to
support macros.</p>
<p>So I tried to use Common Lisp, but just couldn't make it &quot;stick&quot;.  I
had learned plenty of other languages, and usually enjoyed the process,
but Common Lisp's lack of libraries really drove me nuts.  I could do
network, file or text stuff in 4 or 5 lines of ruby that took a page
of code in Common Lisp.  So I shelved Common Lisp for a while.</p>
<p>I do a lot of C++ and JavaScript at work, and was learning to really
like JavaScript -- clean syntax, closures, direct support for
hash-maps and regex, a builtin &quot;gui&quot; system.  So it was easy for
<a class="reference" href="http://steve-yegge.blogspot.com/">Yegge</a> to lure me into trying
<a class="reference" href="http://www.mozilla.org/rhino/">JavaScript on the JVM</a>, especially
with the promise of optional static typing, which I was pretty sure I
wanted.</p>
<p>Around the same time I learned about <a class="reference" href="http://projecteuler.net/">Project Euler</a>, and thought it would be a great way to
get used to using JavaScript for general purpose programming rather
than just browser stuff.  I did the first five PE problems in
JavaScript before giving up.  It was terrible.  Not only was there no
static typing yet, but things that had been minor annoyances in the
browser now seemed to be giant obstacles to solving the problem at
hand.  Re-reading some of Yegge's cheerleading brought me to the
conclusion that he's actually endorsing some private language based
roughly on JavaScript.  None of the other languages that Google lets
him use are as malleable, and Rhino lets him code up whatever new
features he wants in Java and morph the language to his will.  This is
great for him, I'm sure, but was lousy for me.</p>
<p>I hadn't used Java in any capacity for years, but now thanks to Yegge
I had it installed and ready to go.  This made me more willing to try
Scala.  I don't remember how I heard about it, but I picked it up to
work on the rest of the PE problems, and this is when I started to
learn about immutability.  Scala encourages the use of immutable
locals, and has a few immutable collections.  I expended a lot of
energy bending my problem-solving thought processes to this new
context.  Scala also has lazy streams, which was another big hurdle.
Both of these were challenging to learn, but once I got it, they were
very rewarding tools to have at my fingertips.  Project Euler was my
primary teaching tool, forcing me to learn new concepts a little at a
time.  The folks on the IRC channel helped me when I got stuck, though
in exchange, I frequently had to endure condescension from some of
them.</p>
<p>So this was going along okay, until I thought I had enough Scala under
my belt to take a crack at a tricky little API I'd had on my mind.
I'd been wanting a way of picking data out of HTML documents,
something with the expressive power of XPath, but that embedded
cleanly in the host language.  I wanted to be able to provide new
functions, filters, etc. (have you tried to work with dates in XPath
and XSLT??), and for the return values to be host language data
structures, not more XML documents.  I'd actually built a beastly
little project using xlstproc and ruby, but that's another story
altogether.</p>
<p>Anyway, I began sculpting this API in Scala.  I spent a day or two
experimenting with syntax, the right combinations of traits,
implicits, etc. to create the kind of XPath-like expressions I wanted.
I finally got the beginnings of something that I thought would work,
and compiled a minimal test.  Well, tried to compile it, but the types
weren't quite right.  I then spent the another day or two trying to
get the type declarations correct enough to even compile, and finally
gave up in frustration.  I'm sure it would have been possible to make
it work, but I wanted to spend my time solving actual problems, not
solving problems the compiler was making up for me.  Maybe I didn't
want static typing after all.</p>
<p>This is when I started learning <a class="reference" href="http://clojure.org/">Clojure</a>.
Paul Graham had convinced me I wanted a LISP.  Yegge's hype about
JavaScript had prepared me for the JVM.  Scala had introduced me to
immutability and lazy streams.  Clojure let me use the best properties
of each of these, so it was a very natural fit right from the
beginning.  I've since learned more -- how to use more immutable
collections, software transaction memory, etc., and the discussion
group and IRC channel have always been friendly and encouraging.</p>
<p>That's how I got to where I am.  I don't think I'd recommend the
JavaScript or Scala tangents to anyone else, but &quot;On Lisp&quot; may still
be worth reading, at least until <a class="reference" href="http://www.pragprog.com/titles/shcloj/programming-clojure">Programming Clojure</a> comes
out.  Since bailing on Scala, I've done another 25 or so Project Euler
problems in Clojure, and had so much fun I even went back and did the
first 14 all over again.  I also took another stab at XPath-inspired
document querying, resulting in a zip-filter lib that I'll write about
some other time.</p>
<p>Clojure's definitely closer to the &quot;best language&quot; than any other I've
tried.  If you haven't tried it yet, you should!</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.n01se.net/?feed=rss2&amp;p=37</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Merry Christmas</title>
		<link>http://blog.n01se.net/?p=35</link>
		<comments>http://blog.n01se.net/?p=35#comments</comments>
		<pubDate>Fri, 26 Dec 2008 05:45:07 +0000</pubDate>
		<dc:creator>Chouser</dc:creator>
				<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://blog.n01se.net/?p=35</guid>
		<description><![CDATA[In the beginning was the Word, and the Word was with God, and the Word was fully God. The Word was with God in the beginning. All things were created by him, and apart from him not one thing was created that has been created. In him was life, and the life was the light [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://blog.n01se.net/wp-content/uploads/2008/12/tree.png'><img src="http://blog.n01se.net/wp-content/uploads/2008/12/tree.png" alt="Fractal Tree" title="Merry Christmas" width="300" height="350" class="alignright size-full wp-image-36" /></a></p>
<p>In the beginning was the Word, and the Word was with God, and the Word was fully God. The Word was with God in the beginning. All things were created by him, and apart from him not one thing was created that has been created. In him was life, and the life was the light of mankind.  And the light shines on in the darkness, but the darkness has not mastered it.</p>
<p>A man came, sent from God, whose name was John. He came as a witness to testify about the light, so that everyone might believe through him. He himself was not the light, but he came to testify about the light. The true light, who gives light to everyone, was coming into the world. He was in the world, and the world was created by him, but the world did not recognize him. He came to what was his own, but his own people did not receive him. But to all who have received him – those who believe in his name – he has given the right to become God’s children</p>
<p><i>-- <a href="http://www.bible.org/netbible/joh1.htm">John 1:1-12a, NET</a></i></p>
<p><i>The program to draw the tree is <a href="http://gist.github.com/40012">30 lines of Clojure</a></i></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.n01se.net/?feed=rss2&amp;p=35</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Agents and watchers in Clojure</title>
		<link>http://blog.n01se.net/?p=34</link>
		<comments>http://blog.n01se.net/?p=34#comments</comments>
		<pubDate>Wed, 17 Dec 2008 19:10:55 +0000</pubDate>
		<dc:creator>Chouser</dc:creator>
				<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://blog.n01se.net/?p=34</guid>
		<description><![CDATA[Below is a walk-though of solving a problem using side-effect free functions, then agents, and finally agents and watchers. It's a very slightly edited copy of a post I made to the Clojure Google Group a week or two ago. I'm including it here because the audience for this blog is a bit different from [...]]]></description>
			<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<p><em>Below is a walk-though of solving a problem using side-effect free
functions, then agents, and finally agents and watchers.  It's a very
slightly edited copy of a post I made to the</em>
<a class="reference" href="http://groups.google.com/group/clojure">Clojure Google Group</a> <em>a week or two ago.  I'm
including it here because the audience for this blog is a bit
different from the Google group</em></p>
<p>Spoiler warning -- this is about a <a class="reference" href="http://projecteuler.net/">Project Euler</a>
problem.  If you don't follow the links, I think you'll be
able to understand what I'm talking about without learning
anything specific enough to ruin any particular puzzle.  If
you want to know which specific puzzle I'm discussing (to
see if you've already done it, for example) you can go to the
<a class="reference" href="http://tinyurl.com/6b528n">problem description</a>.
The problem number isn't mentioned in the
<a class="reference" href="http://gist.github.com/32494">full solution code</a>.</p>
<p>For this puzzle, I had a grid of cells, each of which had a
value that depends on the values of its neighbors in a way
that guaranteed a stable solution.  The value of one cell
was given.</p>
<p>My initial solution [single-threaded.clj] represented the
grid as a vector of vectors of Integers, and maintained a
PersistentQueue of cells that needed to be updated, with a
single loop to work through the queue.  For each iteration
of the loop, a cell would be popped off the queue, and a new
value for that cell computed.  If the new value was
different from the old value, the 'recur' then updated the
cell in the vector and pushed the neighboring cells onto the
work queue.  When the queue was empty, a stable state had
been reached and the answer value could be read.</p>
<p>This ran fairly fast, used no mutable state, and the
implementation seemed relatively clean to me.  I was quite
pleased with myself.</p>
<p>But my friend Aaron Brooks who had already solved the
problem was encouraging me to created a multi-threaded
solution.  Note the solution I had was already thread-safe,
but only used one of my two processor cores.</p>
<p>My second solution [using-agents.clj] represented the grid
as a vector of vectors of agents.  The action function
computed a new value for a given agent, and then used 'send'
to queue up the same action for neighboring agents.  It also
maintained other shared state to keep track of how many cell
agents were running so that it could detect when a stable
state for the whole grid had been reached.</p>
<p>Despite the complexity one might expect from all that, the
agent solution was only 3 lines longer than the
single-threaded solution.  It also ran about 30% faster.</p>
<p>But it had a bug -- often returning the right answer, but
sometimes returning an incorrect number.  It also seemed
more imperative than the first solution, because of the
'send' calls, updating shared counters, etc.  When I
mentioned this on IRC, it was recommended I try watchers.</p>
<p>So I added a watcher to every agent before kicking off the
computation process.  The watcher did no computation as
such, but it was the perfect place to 'send' to neighboring
agents, update the running count, etc.  Moving this code to
the watcher also meant the action function was now pure,
with no side-effects.</p>
<p>It was during the process of separating the stateless and
state-management code that I discovered my bug -- the code
to manage global state had obscured an error in the
computation logic.  With them completely separate, it was
easier to think about the specific responsibilities of each.</p>
<p>It was also now easy to see that the pure computation
function was almost exactly the same whether I was using
agents or just a simple loop.  I finished factoring out this
duplication and ended up with code that could use either
mechanism to solve the same problem.  [with-watchers.clj]</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.n01se.net/?feed=rss2&amp;p=34</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing a macro: for vs. doseq</title>
		<link>http://blog.n01se.net/?p=33</link>
		<comments>http://blog.n01se.net/?p=33#comments</comments>
		<pubDate>Mon, 10 Nov 2008 17:29:22 +0000</pubDate>
		<dc:creator>Chouser</dc:creator>
				<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://blog.n01se.net/?p=33</guid>
		<description><![CDATA[Looping: for vs. doseq Newcomers to Clojure are often surprised to find that for is not a flow-control mechanism, but instead a lazy-generator. This may trip you up if you want side-effects: (defn count-and-print-words [text] (let [words (re-seq #&#34;\S+&#34; text)] (for [word words] ; not what I want (println word)) (count words))) user=&#62; (count-and-print-words &#34;What's [...]]]></description>
			<content:encoded><![CDATA[
<div class="document">


<!-- -*- mode: rst -*- -->
<div class="section" id="looping-for-vs-doseq">
<h3>Looping: <tt class="docutils literal"><span class="pre">for</span></tt> vs. <tt class="docutils literal"><span class="pre">doseq</span></tt></h3>
<p>Newcomers to Clojure are often surprised to find that <tt class="docutils literal"><span class="pre">for</span></tt> is not a
flow-control mechanism, but instead a lazy-generator.  This may trip
you up if you want side-effects:</p>
<pre class="literal-block">
(defn count-and-print-words [text]
  (let [words (re-seq #&quot;\S+&quot; text)]
    (for [word words]               ; not what I want
      (println word))
    (count words)))

user=&gt; (count-and-print-words &quot;What's that blue thing?&quot;)
4
</pre>
<p>No words are printed, because nothing uses the return value of <tt class="docutils literal"><span class="pre">for</span></tt>
and so, being lazy, it does nothing at all.  What you want is <tt class="docutils literal"><span class="pre">doseq</span></tt>:</p>
<pre class="literal-block">
(defn count-and-print-words [text]
  (let [words (re-seq #&quot;\S+&quot; text)]
    (doseq [word words]
      (println word))
    (count words)))

user=&gt; (count-and-print-words &quot;What's that blue thing?&quot;)
What's
that
blue
thing?
4
</pre>
<p>But <tt class="docutils literal"><span class="pre">for</span></tt> has a lot of power that's been missing from <tt class="docutils literal"><span class="pre">doseq</span></tt>,
specifically nesting loops, <tt class="docutils literal"><span class="pre">:while</span></tt>, and <tt class="docutils literal"><span class="pre">:when</span></tt>.  In case you're
not familiar with these, here's a quick example that just
(inefficiently) lists the factors of the Fibonacci numbers under 100:</p>
<pre class="literal-block">
(def fibs (lazy-cat [0 1] (map + fibs (rest fibs))))

(def some-factors
  (for [fib fibs :while (&lt; fib 100)
        fac (range 2 fib) :when (zero? (rem fib fac))]
    [fib fac]))

user=&gt; some-factors
([8 2] [8 4] [21 3] [21 7] [34 2] [34 17] [55 5] [55 11])
</pre>
<p>Isn't that the most beautiful definition of fib?  Alas, it's not my
invention.</p>
<p>Anyway, if I want to be able to have nesting, <tt class="docutils literal"><span class="pre">:while</span></tt>, and
<tt class="docutils literal"><span class="pre">:when</span></tt> in an imperative, non-lazy loop, I'll have to write a macro.
A good way to start is with an example of calling your macro.  Start
with the simplest useful example:</p>
<pre class="literal-block">
(my-doseq [i (range 10)]
  (prn i))
</pre>
</div>
<div class="section" id="the-first-rule-of-writing-macros">
<h3>The first rule of writing macros</h3>
<p>I heard somewhere that the first rule of writing macros is: don't
write a macro.  This is a cute way of reminding you to only write a
macro when it's necessary.  In this case it's necessary because I want
<tt class="docutils literal"><span class="pre">my-doseq</span></tt> to be able to define new locals (<tt class="docutils literal"><span class="pre">i</span></tt> in the above
example).  If <tt class="docutils literal"><span class="pre">my-doseq</span></tt> were a function and <tt class="docutils literal"><span class="pre">i</span></tt> wasn't defined
yet, the example wouldn't even compile.</p>
<p>So here are some ideas that I've found useful when writing macros:</p>
<blockquote>
<ul class="simple">
<li>Don't write a macro (perhaps a function would do or the macro already exists).</li>
<li>Write an example usage (start with a simple one).</li>
<li>Expand your example usage by hand.</li>
<li>Use <tt class="docutils literal"><span class="pre">macroexpand</span></tt> when your macro doesn't work.</li>
<li>Experiment at the REPL</li>
<li>Break up complicated macros into smaller functions.</li>
</ul>
</blockquote>
</div>
<div class="section" id="our-own-looping-macro">
<h3>Our own looping macro</h3>
<p>I'll demonstrate all these as we go along.  Write out by hand the
code the macro should produce for this example:</p>
<pre class="literal-block">
(loop [i-seq (seq (range 10))]
  (when i-seq
    (let [i (first i-seq)]
      (prn i)
      (recur (rest i-seq)))))
</pre>
<p>Try that out to see that it prints 0 through 9, just like it should.
Note that without the call to <tt class="docutils literal"><span class="pre">seq</span></tt>, passing in an empty collection
(instead of <tt class="docutils literal"><span class="pre">range</span></tt>) would cause <tt class="docutils literal"><span class="pre">(when</span> <span class="pre">i-seq</span> <span class="pre">...)</span></tt> to succeed
when it shouldn't.  It's an important detail to remember -- use
<tt class="docutils literal"><span class="pre">seq</span></tt> to get a false value for an empty collection.</p>
<p>If you don't understand the above loop, take a moment to walk through
it -- it's the foundation all the following examples are built on.</p>
<p>A macro for this simple case is pretty straightforward:</p>
<pre class="literal-block">
(defmacro my-doseq [[sym init] &amp; body]
  `(loop [sq# (seq ~init)]
     (when sq#
       (let [~sym (first sq#)]
         ~&#64;body
         (recur (rest sq#))))))
</pre>
<p>The hand-coded loop is in this macro almost verbatim.  The backquote
is what makes this work, and enables the placeholder magic: <tt class="docutils literal"><span class="pre">sq#</span></tt>
for a temporary local name, <tt class="docutils literal"><span class="pre">~</span></tt> to insert a parameter, <tt class="docutils literal"><span class="pre">~&#64;</span></tt> to
splice in a parameter that's a seq.</p>
<p>This isn't terribly impressive, though -- it's almost a straight copy
of how doseq is already defined.  Next up: nested loops.</p>
</div>
<div class="section" id="nested-loops">
<h3>Nested loops</h3>
<p>First, the example usage:</p>
<pre class="literal-block">
(my-doseq [i (range 3) j (range 3)] (prn i j))
</pre>
<p>Expanded by hand:</p>
<pre class="literal-block">
(loop [i-seq (seq (range 3))]
  (when i-seq
    (let [i (first i-seq)]
      (loop [j-seq (seq (range 3))]
        (when j-seq
          (let [j (first j-seq)]
            (prn i j)
            (recur (rest j-seq)))))
      (recur (rest i-seq)))))
</pre>
<p>I'm actually not sure if this one easier to expand by hand or to just
write the macro to do it for you.  The pattern to notice, though, is
that where our earlier expansion had the body, this one has another
loop.  What we want is a function that produces the loop part, but
which can be called again if there's a nested loop:</p>
<pre class="literal-block">
(defmacro my-doseq [seq-exprs &amp; body]
  (let [emit (fn emit [[sym init &amp; rest-exprs]]
               `(loop [sq# (seq ~init)] ; same loop as before
                  (when sq#
                    (let [~sym (first sq#)]
                      ~(if rest-exprs   ; more rest-exprs?
                         (emit rest-exprs)
                         `(do ~&#64;body))
                      (recur (rest sq#))))))]
    (emit seq-exprs)))
</pre>
<p>The main <tt class="docutils literal"><span class="pre">loop</span></tt> is recognizable from the previous version, but now
it's in its own local function so it can be called again.  Where it
used to simply splice in <tt class="docutils literal"><span class="pre">~&#64;body</span></tt>, it now has an <tt class="docutils literal"><span class="pre">if</span></tt> to either
recurse (if there's another nested loop) or in the terminal case
splices in <tt class="docutils literal"><span class="pre">~&#64;body</span></tt> like it did before.</p>
<p>The use of <tt class="docutils literal"><span class="pre">`(do</span> <span class="pre">~&#64;body)</span></tt> instead of just <tt class="docutils literal"><span class="pre">body</span></tt> may be a bit
surprising, so let's take a moment to look at why.  It shows up in a
<tt class="docutils literal"><span class="pre">~</span></tt> expression inside a <tt class="docutils literal"><span class="pre">let</span></tt> body, so let's strip it down to just
that and see what goes wrong with a plain <tt class="docutils literal"><span class="pre">body</span></tt>:</p>
<pre class="literal-block">
user=&gt; (defmacro tmp [&amp; body] `(let [] ~(when true body)))
nil
user=&gt; (macroexpand '(tmp (prn 1) (prn 2)))
(let* [] ((prn 1) (prn 2)))
</pre>
<p>Can you spot what's wrong with that expansion?  What if you run it:</p>
<pre class="literal-block">
user=&gt; (tmp (prn 1) (prn 2))
1
2
java.lang.NullPointerException (NO_SOURCE_FILE:0)
</pre>
<p>Both the <tt class="docutils literal"><span class="pre">prn</span></tt>'s get run, but then the first one's return value is at
the head of a list, so Clojure tries to invoke that return value.  The
<tt class="docutils literal"><span class="pre">prn</span></tt> function always returns nil, and the attempt to invoke <tt class="docutils literal"><span class="pre">nil</span></tt>
causes the NPE.</p>
<p>There a couple ways to solve this, one of which is to splice the whole
body into a <tt class="docutils literal"><span class="pre">do</span></tt>:</p>
<pre class="literal-block">
user=&gt; (defmacro tmp [&amp; body] `(let [] ~(when true `(do ~&#64;body))))
nil
user=&gt; (macroexpand '(tmp (prn 1) (prn 2)))
(let* [] (do (prn 1) (prn 2)))
</pre>
<p>By putting the word <tt class="docutils literal"><span class="pre">do</span></tt> at the head of the list, we get the
behavior we want.</p>
</div>
<div class="section" id="implementing-the-when-filter">
<h3>Implementing the <tt class="docutils literal"><span class="pre">:when</span></tt> filter</h3>
<p>So now that nested loops are working, next up is support for <tt class="docutils literal"><span class="pre">:when</span></tt>
and <tt class="docutils literal"><span class="pre">:while</span></tt>.  The main difficulty here is that the number of items
our <tt class="docutils literal"><span class="pre">emit</span></tt> function has to consume each iteration depends on how
many of these options follow the init value:</p>
<pre class="literal-block">
(my-doseq [prod (range 13)
           fac (range 2 prod) :when (zero? (rem prod fac))]
  (prn prod fac))
</pre>
<p>One approach to this kind of problem is to assume, for the moment, that
the input to <tt class="docutils literal"><span class="pre">my-doseq</span></tt> was open to change.  What format would make
it easiest to write the <tt class="docutils literal"><span class="pre">emit</span></tt> function?  Perhaps:</p>
<pre class="literal-block">
[{:name prod
  :init (range 13)}
 {:name fac
  :init (range 2 prod)
  :when (zero? (rem prod fac))}]
</pre>
<p>By grouping the name, initial expression, and all options together for
each nesting level of the loop, the macro could be written:</p>
<pre class="literal-block">
(defmacro my-doseq [seq-exprs &amp; body]
  (let [emit (fn emit [[bind-map &amp; rest-binds]]
               `(loop [sq# (seq ~(:init bind-map))] ; changed to use :init
                  (when sq#
                    (let [~(:name bind-map) (first sq#)] ; changed to use :name
                      ~(if rest-binds
                         (emit rest-binds)
                         `(do ~&#64;body))
                      (recur (rest sq#))))))]
    (emit seq-exprs)))
</pre>
<p>This works, but is very awkward to call... but that's a problem we can
solve later.  For now let's make <tt class="docutils literal"><span class="pre">:when</span></tt> work, by inserting an
<tt class="docutils literal"><span class="pre">if</span></tt> clause that defaults to <tt class="docutils literal"><span class="pre">true</span></tt> if no <tt class="docutils literal"><span class="pre">:when</span></tt> is given:</p>
<pre class="literal-block">
(defmacro my-doseq [seq-exprs &amp; body]
  (let [emit (fn emit [[bind-map &amp; rest-binds]]
               `(loop [sq# (seq ~(:init bind-map))]
                  (when sq#
                    (let [~(:name bind-map) (first sq#)]
                      (if ~(or (:when bind-map) true) ; new &quot;if&quot; clause
                        ~(if rest-binds
                           (emit rest-binds)
                           `(do ~&#64;body)))
                      (recur (rest sq#))))))]
    (emit seq-exprs)))
</pre>
<p>If no <tt class="docutils literal"><span class="pre">:when</span></tt> clause is given, such as with the <tt class="docutils literal"><span class="pre">prod</span></tt> loop shown
earlier, that part of the macro will expand to:</p>
<pre class="literal-block">
(if true
  (loop...))
</pre>
<p>This is slightly inefficient, but completely removing the <tt class="docutils literal"><span class="pre">if</span></tt> would
make the macro significantly more complex.  Instead we'll leave it in
and rely on the JVM to optimize it away at runtime.</p>
</div>
<div class="section" id="implementing-the-while-limiter">
<h3>Implementing the <tt class="docutils literal"><span class="pre">:while</span></tt> limiter</h3>
<p>A minimal example using <tt class="docutils literal"><span class="pre">:while</span></tt>:</p>
<pre class="literal-block">
(my-doseq [{:name fib :init fibs :while (&lt; fib 40)}] (prn fib))
</pre>
<p>That's really a terribly verbose syntax.  I promise we'll fix it soon.</p>
<p>To get the <tt class="docutils literal"><span class="pre">:while</span></tt> part working, a <tt class="docutils literal"><span class="pre">when</span></tt> clause is inserted to
skip both the body and the <tt class="docutils literal"><span class="pre">recur</span></tt> when the test expression is false:</p>
<pre class="literal-block">
(defmacro my-doseq [seq-exprs &amp; body]
  (let [emit (fn emit [[bind-map &amp; rest-binds]]
               `(loop [sq# (seq ~(:init bind-map))]
                  (when sq#
                    (let [~(:name bind-map) (first sq#)]
                      (when ~(or (:while bind-map) true) ; new &quot;when&quot; clause
                        (if ~(or (:when bind-map) true)
                          ~(if rest-binds
                             (emit rest-binds)
                             `(do ~&#64;body)))
                        (recur (rest sq#)))))))]
    (emit seq-exprs)))
</pre>
<p>Try that out to see that it works.</p>
</div>
<div class="section" id="fixing-the-input-syntax">
<h3>Fixing the input syntax</h3>
<p>Now that the <tt class="docutils literal"><span class="pre">emit</span></tt> function has all the required features, we can
fix up the input syntax our macro expects.  The task is to produce the
following kind of format:</p>
<pre class="literal-block">
[{:name prod
  :init (range 13)}
 {:name fac
  :init (range 2 prod)
  :when (zero? (rem prod fac))}]
</pre>
<p>...from input that looks like:</p>
<pre class="literal-block">
[prod (range 13) fac (range 2 prod) :when (zero? (rem prod fac))]
</pre>
<p>So we need to write a function -- let's call it fix-vector:</p>
<pre class="literal-block">
(defn fix-vector [seq-exprs] ...)
</pre>
<p>At the highest level, we'll be consuming a seq of expressions and
producing a new vector (of hash-maps).  This probably means <tt class="docutils literal"><span class="pre">reduce</span></tt> will be a good
fit.  Here the new vector is called <tt class="docutils literal"><span class="pre">out-vec</span></tt>:</p>
<pre class="literal-block">
(defn fix-vector [seq-exprs]
  (reduce (fn [out-vec sym]
            ...)
          [] seq-exprs))
</pre>
<p>The type of expression at the front of each pair is what determines if
that pair is a new name/init pair or a <tt class="docutils literal"><span class="pre">:when</span></tt> or <tt class="docutils literal"><span class="pre">:while</span></tt> that
modifies an existing name/init pair.  Talking about pairs from a seq
suggests <tt class="docutils literal"><span class="pre">partition</span></tt>:</p>
<pre class="literal-block">
user=&gt; (def test-vector
       '[prod (range 13) fac (range 2 prod) :when (zero? (rem prod fac))])
#=(var user/test-vector)

user=&gt; (partition 2 test-vector)
((prod (range 13)) (fac (range 2 prod)) (:when (zero? (rem prod fac))))
</pre>
<p>...and since each of those will be passed as the second argument to
our <tt class="docutils literal"><span class="pre">reduce</span></tt> function, we can pull the pair apart using
destructuring:</p>
<pre class="literal-block">
(defn fix-vector [seq-exprs]
  (reduce (fn [out-vec [sym value]]
            ...)
          [] (partition 2 seq-exprs)))
</pre>
<p>The <tt class="docutils literal"><span class="pre">sym</span></tt> argument needs to be examined so this pair can be treated
differently when its a keyword vs. when it's just a symbol.  This
function's getting complicated enough that I'd like to test it to make
sure we're on the right track, so instead of <tt class="docutils literal"><span class="pre">...</span></tt> I'm going to put
in some stub code:</p>
<pre class="literal-block">
(defn fix-vector [seq-exprs]
  (reduce (fn [out-vec [sym value]]
            (if (keyword? sym)
              (conj out-vec [:found-keyword sym value]) ; stub for testing
              (conj out-vec {:name sym :init value})))
          [] (partition 2 seq-exprs)))

user=&gt; (fix-vector test-vector)
[{:name prod,
  :init (range 13)}
 {:name fac,
  :init (range 2 prod)}
 [:found-keyword :when (zero? (rem prod fac))]]
</pre>
<p>Clojure doesn't have pretty-printing yet (<a class="reference" href="http://richhickey.backpackit.com/pub/1597914">someone want to get on
that?</a>) so I formatted
the above output by hand for clarity.</p>
<p>Now for that stub -- we need to replace it with something that updates
the final hash of <tt class="docutils literal"><span class="pre">out-vec</span></tt>.  I thought by now I was pretty
comfortable with Clojure's immutable collections, but I still found
this a bit tricky to think through.</p>
<p>Here's what I ended up with: we're going to have a modified hash, so
we'll want to drop the final hash of the vector to allow the modified
one to be appended in its place.  That suggests:</p>
<pre class="literal-block">
(conj (pop out-vec) ...)
</pre>
<p>To &quot;modify&quot; that hash we have to get it out of the vector and assoc
the new key/value into it:</p>
<pre class="literal-block">
(assoc (peek out-vec) sym value)
</pre>
<p>Put them together in place of our earlier stub, and we get:</p>
<pre class="literal-block">
(defn fix-vector [seq-exprs]
  (reduce (fn [out-vec [sym value]]
            (if (keyword? sym)
              (conj (pop out-vec) (assoc (peek out-vec) sym value))
              (conj out-vec {:name sym :init value})))
          [] (partition 2 seq-exprs)))
</pre>
<p>Are we even close?</p>
<div class="system-message">
<p class="system-message-title">System Message: ERROR/3 (<tt class="docutils">&lt;stdin&gt;</tt>, line 390)</p>
Unexpected indentation.</div>
<pre class="literal-block">
user=&gt; (fix-vector test-vector)
[{:name prod,
  :init (range 13)}
 {:when (zero? (rem prod fac)),
  :name fac,
  :init (range 2 prod)}]
</pre>
<p>Perfect!  Now <tt class="docutils literal"><span class="pre">fix-vector</span></tt> just needs to be put into the macro:</p>
<pre class="literal-block">
(defmacro my-doseq [seq-exprs &amp; body]
  (let [maps (reduce (fn [out-vec [sym value]]
                       (if (keyword? sym)
                         (conj (pop out-vec) (assoc (peek out-vec) sym value))
                         (conj out-vec {:name sym :init value})))
                     [] (partition 2 seq-exprs))
        emit (fn emit [[bind-map &amp; rest-binds]]
               `(loop [sq# (seq ~(:init bind-map))]
                  (when sq#
                    (let [~(:name bind-map) (first sq#)]
                      (when ~(or (:while bind-map) true) ; new &quot;when&quot; clause
                        (if ~(or (:when bind-map) true)
                          ~(if rest-binds
                             (emit rest-binds)
                             `(do ~&#64;body)))
                        (recur (rest sq#)))))))]
    (emit maps)))
</pre>
<p>And that's it.  Now the <tt class="docutils literal"><span class="pre">for</span></tt> example at top of this article can be
re-worked to use our new macro:</p>
<pre class="literal-block">
(my-doseq [fib fibs :while (&lt; fib 100)
           fac (range 2 fib) :when (zero? (rem fib fac))]
  (prn fib fac))
</pre>
<p>This macro is actually available as <tt class="docutils literal"><span class="pre">doseq</span></tt> in Clojure as of
revision 1090.  Please note: that revision has several features
(including the new <tt class="docutils literal"><span class="pre">doseq</span></tt> syntax) that may require you change your
Clojure code and how you launch Clojure.  These changes are not all
well-document or fully tested yet, so until they are, you may choose
to use <tt class="docutils literal"><span class="pre">my-doseq</span></tt> as shown above.</p>
<p>One final note: if you actually open up <tt class="docutils literal"><span class="pre">core.clj</span></tt> (the file
formerly known as <tt class="docutils literal"><span class="pre">boot.clj</span></tt>) and look at the definition of <tt class="docutils literal"><span class="pre">doseq</span></tt>
you will see a couple minor differences from <tt class="docutils literal"><span class="pre">my-doseq</span></tt>.  This is
because destructuring is not yet available at the point in
<tt class="docutils literal"><span class="pre">core.clj</span></tt> where <tt class="docutils literal"><span class="pre">doseq</span></tt> is required, so I had to do a bit more
<tt class="docutils literal"><span class="pre">first</span></tt>-ing, <tt class="docutils literal"><span class="pre">rest</span></tt>-ing, and <tt class="docutils literal"><span class="pre">apply</span></tt>-ing.</p>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.n01se.net/?feed=rss2&amp;p=33</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 4.158 seconds -->
<!-- Cached page served by WP-Cache -->
