Posts tagged "Clojure"urn:www-greghendershott-com:Clojure2014-10-27T16:29:15ZApplicable symbolsurn:www-greghendershott-com:-2014-10-applicable-symbols.html2014-10-27T16:29:15Z2014-10-27T16:29:15ZGreg Hendershott
<div>
<article>
<header>
<h1>Applicable symbols</h1>
<p class="date-and-tags">
<time datetime="2014-10-27" pubdate="true">2014-10-27</time> :: <span class="tags"><a href="/tags/Clojure.html">Clojure</a>, <a href="/tags/Hacker-School.html">Hacker School</a></span></p></header>
<p>Here are my notes about being puzzled about some Clojure code and diving into the implementation to figure it out. Although I figured it out the hard way, the exploration turned out to be interesting for me.</p><!-- more-->
<p>As I was reading <a href="http://www.zephyrizing.net/blog/2014/10/14/adventures-with-clojure-macros/">Geoff Shannon’s blog post</a>, I was thinking, “Oh, maybe this will turn out to be a mixup between compile time and run time”. (At least, in my experience writing macros, it can be challenging to keep that straight.)</p>
<p>Instead, what confused me was line 103 in the last example (here I’ve changed the symbol from <code>'+</code> to <code>'x</code>):</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="nb">apply </span><span class="ss">'x</span> <span class="p">[</span><span class="mi">1</span> <span class="mi">2</span><span class="p">])</span> <span class="c1">;=> 2</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Huh? I don’t understand. Why does this return <code>2</code>, instead of raising an error?</p>
<p>First, let’s factor out <code>apply</code> and confirm the same result for a direct application:</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="ss">'x</span> <span class="mi">1</span> <span class="mi">2</span><span class="p">)</span> <span class="c1">;=> 2</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Same result. Same mystery.</p>
<p>Let’s explore some examples:</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4
5
6</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="ss">'x</span><span class="p">)</span> <span class="c1">;=> ArityException Wrong number of args (0) passed to: Symbol clojure.lang.AFn.throwArity (AFn.java:429)</span>
<span class="p">(</span><span class="ss">'x</span> <span class="mi">1</span><span class="p">)</span> <span class="c1">;=> nil ???</span>
<span class="p">(</span><span class="ss">'x</span> <span class="mi">1</span> <span class="mi">2</span><span class="p">)</span> <span class="c1">;=> 2 ???</span>
<span class="p">(</span><span class="ss">'x</span> <span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">)</span> <span class="c1">;=> ArityException Wrong number of args (3) passed to: Symbol clojure.lang.AFn.throwArity (AFn.java:429)</span>
<span class="p">(</span><span class="nf">type</span> <span class="ss">'x</span><span class="p">)</span> <span class="c1">;=> clojure.lang.Symbol</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>There’s another mystery — the arity 1 case of <code>('x 1)</code>.</p>
<p>To summarize so far: It appears that it is not an error to use a symbol in the function position of an application if the arity is 1 or 2.</p>
<p>I’m aware that Clojure collections are applicable. And I’m aware that Clojure tends to <code>nil</code> pun. So the arity 1 case might be less surprising… except that neither <code>'x</code> nor <code>1</code> is a collection, so this seems like it should raise an exception, not evaluate to <code>nil</code>. And I don’t understand the arity 2 case.<sup><a href="#applicable-symbols-footnote-1-definition" name="applicable-symbols-footnote-1-return">1</a></sup></p>
<hr />
<p>What to do? I could ask for help. But let’s take this as an opportunity to go spelunking in some Clojure implementation code I’ve never seen before.</p>
<p>From looking at <a href="https://github.com/clojure/clojure/blob/201a0dd9701e1a0ee3998431241388eb4a854ebf/src/jvm/clojure/lang/AFn.java">the source for clojure.lang.AFn</a>, it seems that all <code>invoke</code> members call <code>throwArity</code>. It’s been some years since I did C++, and I’m not up-to-speed on Java. But if I understand correctly, <code>AFn</code> is a sort of default class that always errors. Applicable things (functions, keywords, collections) probably derive from <code>AFn</code> and override <code>invoke</code> members to do something other than error. Somehow such a class is created for the symbol <code>'x</code>, and I’m guessing that it overrides the arity 1 and 2 variants of <code>invoke</code>.</p>
<p>That seems plausible. So, how to find this in the Clojure source? Instead of searching the code on GitHub, let’s <code>git clone</code> the source locally and use Emacs <code>rgrep</code> to search for <code>invoke</code>. I’m sure there are Java source navigation tools, but probably not worth choosing/installing/learning for something this straightforward. Yes <code>rgrep</code> returns 1200+ matches, but flipping through them quickly there are some obvious patterns — huge swaths that seem OK to ignore, and a few names that pop out.</p>
<p>For example in <code>APersistentMap.java</code> I see arity 1 and 2 definitions of <code>invoke</code>:</p>
<div class="brush: java">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4
5
6
7</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="kd">public</span> <span class="n">Object</span> <span class="nf">invoke</span><span class="o">(</span><span class="n">Object</span> <span class="n">arg1</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">valAt</span><span class="o">(</span><span class="n">arg1</span><span class="o">);</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="n">Object</span> <span class="nf">invoke</span><span class="o">(</span><span class="n">Object</span> <span class="n">arg1</span><span class="o">,</span> <span class="n">Object</span> <span class="n">notFound</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">valAt</span><span class="o">(</span><span class="n">arg1</span><span class="o">,</span> <span class="n">notFound</span><span class="o">);</span>
<span class="o">}</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>That sure seems how <code>({:key val} :key)</code> and <code>({:key val} :key
default)</code> would work. <code>valAt</code> is a core member function that does the lookup of a key and value in the map.</p>
<p>OK. Next let’s try to find a class that might correspond to a symbol like <code>'x</code> and examine its <code>invoke</code> member(s).</p>
<p>And bingo, here’s a file called <code>Symbol.java</code>, also defining arity 1 and 2 variants of <code>invoke</code>:</p>
<div class="brush: java">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4
5
6
7</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="kd">public</span> <span class="n">Object</span> <span class="nf">invoke</span><span class="o">(</span><span class="n">Object</span> <span class="n">obj</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">RT</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">obj</span><span class="o">,</span> <span class="k">this</span><span class="o">);</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="n">Object</span> <span class="nf">invoke</span><span class="o">(</span><span class="n">Object</span> <span class="n">obj</span><span class="o">,</span> <span class="n">Object</span> <span class="n">notFound</span><span class="o">)</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">RT</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">obj</span><span class="o">,</span> <span class="k">this</span><span class="o">,</span> <span class="n">notFound</span><span class="o">);</span>
<span class="o">}</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>So this seems to explain why arities 1 and 2 are special. Next question: What is <code>RT.get</code>? In C++ this would mean a static <code>get</code> member of an <code>RT</code> class. That seems to be the case here, looking in <code>RT.java</code>. <code>get</code> comes in both arity 1 and 2 flavors (not counting the first argument; this is a static member so the implicit <code>this</code> argument is explicit). First here’s arity 1:</p>
<div class="brush: java">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4
5</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="kd">static</span> <span class="kd">public</span> <span class="n">Object</span> <span class="nf">get</span><span class="o">(</span><span class="n">Object</span> <span class="n">coll</span><span class="o">,</span> <span class="n">Object</span> <span class="n">key</span><span class="o">){</span>
<span class="k">if</span><span class="o">(</span><span class="n">coll</span> <span class="k">instanceof</span> <span class="n">ILookup</span><span class="o">)</span>
<span class="k">return</span> <span class="o">((</span><span class="n">ILookup</span><span class="o">)</span> <span class="n">coll</span><span class="o">).</span><span class="na">valAt</span><span class="o">(</span><span class="n">key</span><span class="o">);</span>
<span class="k">return</span> <span class="n">getFrom</span><span class="o">(</span><span class="n">coll</span><span class="o">,</span> <span class="n">key</span><span class="o">);</span>
<span class="o">}</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Which of the two paths here would <code>'x</code> take? I’m going to make a guess that a symbol probably isn’t an instance of <code>ILookup</code>, and what’s happening is the call to <code>getFrom</code>. Which is defined in <code>RT.java</code> as:</p>
<div class="brush: java">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="kd">static</span> <span class="n">Object</span> <span class="nf">getFrom</span><span class="o">(</span><span class="n">Object</span> <span class="n">coll</span><span class="o">,</span> <span class="n">Object</span> <span class="n">key</span><span class="o">){</span>
<span class="k">if</span><span class="o">(</span><span class="n">coll</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span>
<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
<span class="k">else</span> <span class="k">if</span><span class="o">(</span><span class="n">coll</span> <span class="k">instanceof</span> <span class="n">Map</span><span class="o">)</span> <span class="o">{</span>
<span class="n">Map</span> <span class="n">m</span> <span class="o">=</span> <span class="o">(</span><span class="n">Map</span><span class="o">)</span> <span class="n">coll</span><span class="o">;</span>
<span class="k">return</span> <span class="n">m</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">key</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">else</span> <span class="k">if</span><span class="o">(</span><span class="n">coll</span> <span class="k">instanceof</span> <span class="n">IPersistentSet</span><span class="o">)</span> <span class="o">{</span>
<span class="n">IPersistentSet</span> <span class="n">set</span> <span class="o">=</span> <span class="o">(</span><span class="n">IPersistentSet</span><span class="o">)</span> <span class="n">coll</span><span class="o">;</span>
<span class="k">return</span> <span class="n">set</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">key</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">else</span> <span class="k">if</span><span class="o">(</span><span class="n">key</span> <span class="k">instanceof</span> <span class="n">Number</span> <span class="o">&&</span> <span class="o">(</span><span class="n">coll</span> <span class="k">instanceof</span> <span class="n">String</span> <span class="o">||</span> <span class="n">coll</span><span class="o">.</span><span class="na">getClass</span><span class="o">().</span><span class="na">isArray</span><span class="o">()))</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">n</span> <span class="o">=</span> <span class="o">((</span><span class="n">Number</span><span class="o">)</span> <span class="n">key</span><span class="o">).</span><span class="na">intValue</span><span class="o">();</span>
<span class="k">if</span><span class="o">(</span><span class="n">n</span> <span class="o">>=</span> <span class="mi">0</span> <span class="o">&&</span> <span class="n">n</span> <span class="o"><</span> <span class="n">count</span><span class="o">(</span><span class="n">coll</span><span class="o">))</span>
<span class="k">return</span> <span class="n">nth</span><span class="o">(</span><span class="n">coll</span><span class="o">,</span> <span class="n">n</span><span class="o">);</span>
<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>My guess is that all of the conditional branches fail for a symbol like <code>'x</code>, and <code>getFrom</code> returns Java <code>null</code>. Which becomes Clojure <code>nil</code>. First mystery solved.</p>
<p>How about arity 2? This is the case where a “not-found” value is supplied:</p>
<div class="brush: java">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 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</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="kd">static</span> <span class="kd">public</span> <span class="n">Object</span> <span class="nf">get</span><span class="o">(</span><span class="n">Object</span> <span class="n">coll</span><span class="o">,</span> <span class="n">Object</span> <span class="n">key</span><span class="o">,</span> <span class="n">Object</span> <span class="n">notFound</span><span class="o">){</span>
<span class="k">if</span><span class="o">(</span><span class="n">coll</span> <span class="k">instanceof</span> <span class="n">ILookup</span><span class="o">)</span>
<span class="k">return</span> <span class="o">((</span><span class="n">ILookup</span><span class="o">)</span> <span class="n">coll</span><span class="o">).</span><span class="na">valAt</span><span class="o">(</span><span class="n">key</span><span class="o">,</span> <span class="n">notFound</span><span class="o">);</span>
<span class="k">return</span> <span class="n">getFrom</span><span class="o">(</span><span class="n">coll</span><span class="o">,</span> <span class="n">key</span><span class="o">,</span> <span class="n">notFound</span><span class="o">);</span>
<span class="o">}</span>
<span class="kd">static</span> <span class="n">Object</span> <span class="nf">getFrom</span><span class="o">(</span><span class="n">Object</span> <span class="n">coll</span><span class="o">,</span> <span class="n">Object</span> <span class="n">key</span><span class="o">,</span> <span class="n">Object</span> <span class="n">notFound</span><span class="o">){</span>
<span class="k">if</span><span class="o">(</span><span class="n">coll</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span>
<span class="k">return</span> <span class="n">notFound</span><span class="o">;</span>
<span class="k">else</span> <span class="k">if</span><span class="o">(</span><span class="n">coll</span> <span class="k">instanceof</span> <span class="n">Map</span><span class="o">)</span> <span class="o">{</span>
<span class="n">Map</span> <span class="n">m</span> <span class="o">=</span> <span class="o">(</span><span class="n">Map</span><span class="o">)</span> <span class="n">coll</span><span class="o">;</span>
<span class="k">if</span><span class="o">(</span><span class="n">m</span><span class="o">.</span><span class="na">containsKey</span><span class="o">(</span><span class="n">key</span><span class="o">))</span>
<span class="k">return</span> <span class="n">m</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">key</span><span class="o">);</span>
<span class="k">return</span> <span class="n">notFound</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">else</span> <span class="k">if</span><span class="o">(</span><span class="n">coll</span> <span class="k">instanceof</span> <span class="n">IPersistentSet</span><span class="o">)</span> <span class="o">{</span>
<span class="n">IPersistentSet</span> <span class="n">set</span> <span class="o">=</span> <span class="o">(</span><span class="n">IPersistentSet</span><span class="o">)</span> <span class="n">coll</span><span class="o">;</span>
<span class="k">if</span><span class="o">(</span><span class="n">set</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="n">key</span><span class="o">))</span>
<span class="k">return</span> <span class="n">set</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">key</span><span class="o">);</span>
<span class="k">return</span> <span class="n">notFound</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">else</span> <span class="k">if</span><span class="o">(</span><span class="n">key</span> <span class="k">instanceof</span> <span class="n">Number</span> <span class="o">&&</span> <span class="o">(</span><span class="n">coll</span> <span class="k">instanceof</span> <span class="n">String</span> <span class="o">||</span> <span class="n">coll</span><span class="o">.</span><span class="na">getClass</span><span class="o">().</span><span class="na">isArray</span><span class="o">()))</span> <span class="o">{</span>
<span class="kt">int</span> <span class="n">n</span> <span class="o">=</span> <span class="o">((</span><span class="n">Number</span><span class="o">)</span> <span class="n">key</span><span class="o">).</span><span class="na">intValue</span><span class="o">();</span>
<span class="k">return</span> <span class="n">n</span> <span class="o">>=</span> <span class="mi">0</span> <span class="o">&&</span> <span class="n">n</span> <span class="o"><</span> <span class="n">count</span><span class="o">(</span><span class="n">coll</span><span class="o">)</span> <span class="o">?</span> <span class="n">nth</span><span class="o">(</span><span class="n">coll</span><span class="o">,</span> <span class="n">n</span><span class="o">)</span> <span class="o">:</span> <span class="n">notFound</span><span class="o">;</span>
<span class="o">}</span>
<span class="k">return</span> <span class="n">notFound</span><span class="o">;</span>
<span class="o">}</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Aha. <code>('x 1 2)</code> is being treated as, “Look up <code>1</code> in <code>'x</code> and if not found return <code>2</code>”.</p>
<h1 id="editorializing">Editorializing</h1>
<p>It makes sense to support a symbol in the function position of an application to enable doing this:</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="ss">'key</span> <span class="p">{</span><span class="ss">'key</span> <span class="mi">42</span><span class="p">})</span> <span class="c1">;=> 42</span>
<span class="p">(</span><span class="ss">'key</span> <span class="p">{</span><span class="ss">'key</span> <span class="mi">42</span><span class="p">}</span> <span class="mi">42</span><span class="p">)</span> <span class="c1">;=> 42</span>
<span class="p">(</span><span class="ss">'key</span> <span class="p">{}</span> <span class="mi">42</span><span class="p">)</span> <span class="c1">;=> 42</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Of course. That’s awesome.</p>
<p>But when the second element is <em>not</em> a collection? It should error. Looking up a key in an integer doesn’t make any sense. It is the result of a mistake. Clojure should tell you it’s an error. Returning something here is as bad as silent coercions in Javascript.</p>
<h1 id="postscript">Postscript</h1>
<p>For what it’s worth, here’s how this works in <a href="https://github.com/greghendershott/rackjure"><code>rackjure</code></a>:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 1
2
3
4
5
6
7
8
9
10
11</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="kn">#lang </span><span class="nn">rackjure</span>
<span class="p">(</span><span class="o">'</span><span class="ss">key</span> <span class="p">{</span><span class="o">'</span><span class="ss">key</span> <span class="mi">42</span><span class="p">})</span> <span class="c1">;=> 42</span>
<span class="p">(</span><span class="o">'</span><span class="ss">key</span> <span class="p">{</span><span class="o">'</span><span class="ss">key</span> <span class="mi">42</span><span class="p">}</span> <span class="kd">#:else</span> <span class="mi">42</span><span class="p">)</span> <span class="c1">;=> 42</span>
<span class="p">(</span><span class="o">'</span><span class="ss">key</span> <span class="p">{}</span> <span class="kd">#:else</span> <span class="mi">42</span><span class="p">)</span> <span class="c1">;=> 42</span>
<span class="p">(</span><span class="o">'</span><span class="ss">key</span> <span class="mi">1</span><span class="p">)</span>
<span class="c1">; applicable-dict: No dict? supplied</span>
<span class="c1">; in: ('key 1)</span>
<span class="c1">; Context:</span>
<span class="c1">; /tmp/foo.rkt:1:1 [running body]</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>One difference is that arity 3 means <code>assoc</code>. If you want to supply an explicit default, you use an <code>#:else</code> keyword argument.</p>
<p>The main difference is that a nonsense application results in an error.</p>
<div class="footnotes">
<ol>
<li id="applicable-symbols-footnote-1-definition" class="footnote-definition">
<p>Spoiler: By the end of this, I remember there’s an optional not-found argument. <a href="#applicable-symbols-footnote-1-return">↩</a></p></li></ol></div>
<footer></footer></article></div>Why macros?urn:www-greghendershott-com:-2014-10-why-macros.html2014-10-21T17:56:54Z2014-10-21T17:56:54ZGreg Hendershott
<div>
<article>
<header>
<h1>Why macros?</h1>
<p class="date-and-tags">
<time datetime="2014-10-21" pubdate="true">2014-10-21</time> :: <span class="tags"><a href="/tags/Racket.html">Racket</a>, <a href="/tags/Clojure.html">Clojure</a>, <a href="/tags/Hacker-School.html">Hacker School</a></span></p></header>
<p>Yesterday a couple people asked me, “How and why do you use macros in a Lisp like Racket or Clojure?”.</p>
<p>I gave answers like:</p>
<ul>
<li>
<p>The compiler can do a search-and-replace on your code.</p></li>
<li>
<p>You can make DSLs.</p></li>
<li>
<p>They’re an “API for the compiler”.</p></li></ul>
<p>Although all true, I wasn’t sure I was getting the full idea across.</p><!-- more-->
<p>Worse, one time Peter Seibel was within earshot. Although I don’t know if he heard my explanation, I imagined him biting his tongue and politely remembering the “well, actually” rule. :)</p>
<p>Later I remembered Matthias Felleisen boiling down macros into three main categories:</p>
<ol>
<li>
<p><strong>Binding forms</strong>. You can make your own syntax for binding values to identifiers, including function definition forms. You may hear people say, in a Lisp you don’t have to wait for the language designers to add a feature (like <code>lambda</code> for Java?). Using macros you can add it yourself. Binding forms is one example.</p></li>
<li>
<p><strong>Changing order of evaluation</strong>. Something like <code>or</code> or <code>if</code> can’t really be a function, because you want it to “short-circuit” — if the first test evaluates to true, don’t evaluate the other test at all.</p></li>
<li>
<p><strong>Abstractions like domain specific langagues (DSLs)</strong>. You want to provide a special language, which is simpler and/or more task-specific than the full/raw Lisp you’re using. This DSL might be for users of your software, and/or it might be something that you use to help implement parts of your own program.</p></li></ol>
<p>Every macro is doing one of those three things. Only macros can really do the first two, at all<sup><a href="#why-macros-footnote-1-definition" name="why-macros-footnote-1-return">1</a></sup>. Macros let you do the last one more elegantly.</p>
<p>I think the preceding is a better answer. However, maybe it’s still not the best way to get people from zero to sixty on, “Why macros?”.<sup><a href="#why-macros-footnote-2-definition" name="why-macros-footnote-2-return">2</a></sup></p>
<p>Maybe the ideal is a “teachable moment” — facing a problem that macrology would solve.<sup><a href="#why-macros-footnote-3-definition" name="why-macros-footnote-3-return">3</a></sup> That’s also good because you really really <em>really</em> don’t want to use a macro when a normal function would suffice. So the goal isn’t to get people <em>so</em> enthusiastic about macros that they go forth in search of nails to which to apply that new hammer. Macros often aren’t the right approach. But once in a while, they are the bestest approach ever.</p>
<div class="footnotes">
<ol>
<li id="why-macros-footnote-1-definition" class="footnote-definition">
<p>A language like Haskell can choose lazy evaluation, and implement <code>if</code> as a function. I’m saying that only a macro can futz with whatever the default evaluation order is, be it eager or lazy. <a href="#why-macros-footnote-1-return">↩</a></p></li>
<li id="why-macros-footnote-2-definition" class="footnote-definition">
<p>Although I wrote a guide called <a href="http://www.greghendershott.com/fear-of-macros/">Fear of Macros</a>, it’s (a) specific to Racket macros and (b) much more about the “how” than the “why”. <a href="#why-macros-footnote-2-return">↩</a></p></li>
<li id="why-macros-footnote-3-definition" class="footnote-definition">
<p>Certainly that’s my own optimal learning situation, as opposed to getting answers or solutions before I have the questions or problems. <a href="#why-macros-footnote-3-return">↩</a></p></li></ol></div>
<footer></footer></article></div>Hands-on with Clojure day 5urn:www-greghendershott-com:-2014-10-hands-on-with-clojure-day-5.html2014-10-21T17:54:46Z2014-10-21T17:54:46ZGreg Hendershott
<div>
<article>
<header>
<h1>Hands-on with Clojure day 5</h1>
<p class="date-and-tags">
<time datetime="2014-10-21" pubdate="true">2014-10-21</time> :: <span class="tags"><a href="/tags/Clojure.html">Clojure</a>, <a href="/tags/Hacker-School.html">Hacker School</a></span></p></header>
<p>So I’ve fallen behind on the blogging, for a few reasons. Time to catch up.</p>
<p>I’m calling this “day 5” as a useful fiction. It’s a distillation of what is closer to days 5–7, or something like that.</p>
<p>As I mentioned before, this series of blog posts is going more directly from brain to web. Reflection and editing? Not so much.</p><!-- more-->
<h1 id="clojure-port-of-wffi">Clojure port of wffi</h1>
<p>I finished what I think is a reasonable initial port of wffi from Racket to Clojure. Pushed at <a href="https://github.com/greghendershott/clojure-wffi">clojure-wffi</a>.</p>
<p>The simplest possible example is, given a <code>horseebooks.md</code> file like this:</p>
<pre><code># horseebooksipsum.com
Endpoint: http://horseebooksipsum.com
# Get
## Request
````
GET /api/v1/{paragraphs}
````</code></pre>
<p>You can write:</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="nf">defwrappers</span> <span class="s">"horseebooks.md"</span><span class="p">)</span>
<span class="p">(</span><span class="nf">pprint</span> <span class="p">(</span><span class="nb">get </span><span class="p">{</span><span class="ss">:paragraphs</span> <span class="mi">2</span><span class="p">}))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Which prints:</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 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</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">{</span><span class="ss">:orig-content-encoding</span> <span class="s">"gzip"</span>,
<span class="ss">:trace-redirects</span> <span class="p">[</span><span class="s">"http://horseebooksipsum.com/api/v1/2"</span><span class="p">]</span>,
<span class="ss">:request-time</span> <span class="mi">190</span>,
<span class="ss">:status</span> <span class="mi">200</span>,
<span class="ss">:headers</span>
<span class="p">{</span><span class="s">"Content-Type"</span> <span class="s">"text/plain"</span>,
<span class="s">"Transfer-Encoding"</span> <span class="s">"chunked"</span>,
<span class="s">"Connection"</span> <span class="s">"close"</span>,
<span class="s">"Vary"</span> <span class="s">"Accept-Encoding"</span>,
<span class="s">"Cache-Control"</span> <span class="s">"no-cache"</span>,
<span class="s">"Server"</span> <span class="s">"Apache/2.2.22 (Debian)"</span>,
<span class="s">"Date"</span> <span class="s">"Tue, 21 Oct 2014 18:07:16 GMT"</span><span class="p">}</span>,
<span class="ss">:body</span>
<span class="s">"Principle to work to make more money while having more fun. Unlucky people.</span>
<span class="s"> And practical explanations. Process from preparation, through to delivery.</span>
<span class="s"> And practical explanations. Process from preparation, through to delivery.</span>
<span class="s"> And practical explanations. And practical explanations. Process from</span>
<span class="s"> preparation, through to delivery. And practical explanations. And practical</span>
<span class="s"> explanations. And practical explanations.\n\nDon't stubbornly. This is a</span>
<span class="s"> very special technique that I have never seen. Don't stubbornly. This is</span>
<span class="s"> a very special technique that I have never seen. And practical explanations.</span>
<span class="s"> Don't stubbornly. Principle to work to make more money while having more fun.</span>
<span class="s"> Unlucky people. Process from preparation, through to delivery. Don't</span>
<span class="s"> stubbornly. Process from preparation, through to delivery. And practical</span>
<span class="s"> explanations. This is a very special technique that I have never seen.</span>
<span class="s"> And practical explanations. And practical explanations.\n\n"</span><span class="p">}</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Of course this simple example doesn’t show much value-add. But real-world web services often have numerous parameters allocated among URL path segments, query parameters, and headers. With wffi, useful keyword wrapper functions are automatically generated from a markdown file that both documents and specifies the web service.</p>
<p>If I weren’t at Hacker School, I would spend much more time polishing and refining this. However this project is really just a means to the end of learning Clojure. So I’m going to force myself to task-switch to something else, next. I’ll return to this project if/when it seems like the best vehicle to learn more.</p>
<h1 id="split-with-and-lazy-seqs"><code>split-with</code> and lazy seqs</h1>
<p>Previously I posted that <code>split-with</code> seems to have an inefficient implementation.</p>
<p>Needing something like Racket’s <code>splitf-at</code>, I wrote a quick and dirty version in Clojure:</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4
5
6</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="kd">defn </span><span class="nv">split</span>
<span class="s">"FIXME: This is the conceptual, inefficient implementation. Should</span>
<span class="s"> reimplement like Racket's splitf-at."</span>
<span class="p">[</span><span class="nv">pred</span> <span class="nv">coll</span><span class="p">]</span>
<span class="p">[(</span><span class="nb">take-while </span><span class="nv">pred</span> <span class="nv">coll</span><span class="p">)</span>
<span class="p">(</span><span class="nb">drop-while </span><span class="nv">pred</span> <span class="nv">coll</span><span class="p">)])</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>This isn’t great because it traverses the first portion of the collection twice.</p>
<p>Someone pointed out that Clojure already provides this. It’s called <code>split-with</code>. Nice. But when I
<kbd>M-.</kbd>, I see that its definition is my conceptual one, not the efficient one.</p>
<p>Racket defines <code>splitf-at</code> like so:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4
5
6
7</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="k"><a href="http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))" style="color: inherit">define</a></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/list..rkt)._splitf-at))" style="color: inherit">splitf-at</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span> <span class="n"><a href="http://docs.racket-lang.org/ts-reference/Legacy_Forms.html#(form._((lib._typed-racket/base-env/base-types-extra..rkt)._pred))" style="color: inherit">pred</a></span><span class="p">)</span>
<span class="p">(</span><span class="k"><a href="http://docs.racket-lang.org/reference/when_unless.html#(form._((lib._racket/private/letstx-scheme..rkt)._unless))" style="color: inherit">unless</a></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/procedures.html#(def._((quote._~23~25kernel)._procedure~3f))" style="color: inherit">procedure?</a></span> <span class="n"><a href="http://docs.racket-lang.org/ts-reference/Legacy_Forms.html#(form._((lib._typed-racket/base-env/base-types-extra..rkt)._pred))" style="color: inherit">pred</a></span><span class="p">)</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/exns.html#(def._((quote._~23~25kernel)._raise-argument-error))" style="color: inherit">raise-argument-error</a></span> <span class="o">'</span><span class="ss"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/list..rkt)._splitf-at))" style="color: inherit">splitf-at</a></span> <span class="s2">"procedure?"</span> <span class="mi">1</span> <span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span> <span class="n"><a href="http://docs.racket-lang.org/ts-reference/Legacy_Forms.html#(form._((lib._typed-racket/base-env/base-types-extra..rkt)._pred))" style="color: inherit">pred</a></span><span class="p">))</span>
<span class="p">(</span><span class="k"><a href="http://docs.racket-lang.org/reference/let.html#(form._((lib._racket/private/letstx-scheme..rkt)._let))" style="color: inherit">let</a></span> <span class="n">loop</span> <span class="p">([</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span><span class="p">]</span> <span class="p">[</span><span class="n">pfx</span> <span class="o">'</span><span class="p">()])</span>
<span class="p">(</span><span class="k"><a href="http://docs.racket-lang.org/reference/if.html#(form._((quote._~23~25kernel)._if))" style="color: inherit">if</a></span> <span class="p">(</span><span class="k"><a href="http://docs.racket-lang.org/reference/if.html#(form._((lib._racket/private/letstx-scheme..rkt)._and))" style="color: inherit">and</a></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._pair~3f))" style="color: inherit">pair?</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span><span class="p">)</span> <span class="p">(</span><span class="n"><a href="http://docs.racket-lang.org/ts-reference/Legacy_Forms.html#(form._((lib._typed-racket/base-env/base-types-extra..rkt)._pred))" style="color: inherit">pred</a></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._car))" style="color: inherit">car</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span><span class="p">)))</span>
<span class="p">(</span><span class="n">loop</span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._cdr))" style="color: inherit">cdr</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span><span class="p">)</span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._cons))" style="color: inherit">cons</a></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._car))" style="color: inherit">car</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span><span class="p">)</span> <span class="n">pfx</span><span class="p">))</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/values.html#(def._((quote._~23~25kernel)._values))" style="color: inherit">values</a></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/private/list..rkt)._reverse))" style="color: inherit">reverse</a></span> <span class="n">pfx</span><span class="p">)</span> <span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span><span class="p">))))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>I “ported” this to Clojure like so:</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4
5
6</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="kd">defn </span><span class="nv">efficient-split-with</span>
<span class="p">[</span><span class="nv">pred</span> <span class="nv">coll</span><span class="p">]</span>
<span class="p">(</span><span class="k">loop </span><span class="p">[</span><span class="nv">ps</span> <span class="p">[]</span>, <span class="nv">coll</span> <span class="nv">coll</span><span class="p">]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">and </span><span class="p">(</span><span class="nb">seq </span><span class="nv">coll</span><span class="p">)</span> <span class="p">(</span><span class="nf">pred</span> <span class="p">(</span><span class="nb">first </span><span class="nv">coll</span><span class="p">)))</span>
<span class="p">(</span><span class="nf">recur</span> <span class="p">(</span><span class="nb">conj </span><span class="nv">ps</span> <span class="p">(</span><span class="nb">first </span><span class="nv">coll</span><span class="p">))</span> <span class="p">(</span><span class="nb">rest </span><span class="nv">coll</span><span class="p">))</span>
<span class="p">[</span><span class="nv">ps</span> <span class="nv">coll</span><span class="p">])))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>One neat thing is the use of <code>conj</code> with a <code>vector</code> means we don’t have to do the <code>reverse</code> like we do in Racket, which should be even more efficient.</p>
<p>So why does Clojure implement <code>split-with</code> the way it does? David Nolen pointed out that I was forgetting about laziness. Aha.</p>
<p>In connection with this I learned about “chunked sequences” in Clojure from a <a href="http://blog.fogus.me/2010/01/22/de-chunkifying-sequences-in-clojure/">Fogus blog post</a>. Chunked sequences were added as an optimization in v1.1. The force granularity was increased from 1 item to 32.</p>
<p>Someone else pointed out that, had transducers been a thing, maybe lazy seqs wouldn’t be needed. (At least not as a default policy. You could have something like Racket’s <a href="http://docs.racket-lang.org/reference/streams.html"><code>racket/stream</code></a>, with laziness and memoization.)</p>
<p>I already understood, in theory, that side effects expose the difference between eager and lazy evaluation. I learned, hands on, that this includes side effects like ad hoc debugging <code>println</code>s.<sup><a href="#hands-on-with-clojure-day-5-footnote-1-definition" name="hands-on-with-clojure-day-5-footnote-1-return">1</a></sup> For example if you have:</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4
5
6
7</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">coll</span> <span class="p">(</span><span class="nb">map </span><span class="p">(</span><span class="k">fn </span><span class="p">[</span><span class="nv">x</span><span class="p">]</span>
<span class="c1">;; 0: some bug that will throw an exception</span>
<span class="p">)</span>
<span class="nv">coll</span><span class="p">)</span>
<span class="nv">_</span> <span class="p">(</span><span class="nb">println </span><span class="s">"Everything AOK thus far -- or maybe not!"</span><span class="p">)]</span>
<span class="c1">;; 1: use coll in way that forces the lazy seq</span>
<span class="p">)</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>The error won’t occur at 0, it will only occur at 1. The progress <code>println</code> will be misleading. At least it misled me, for awhile, about the actual location of a bug.</p>
<p>With Clojure and Haskell, I’ll need to keep in mind when and where laziness is used as the default policy. If I understand correctly, in Clojure that means lazy sequences, and in Haskell lazy evaluation generally.</p>
<h1 id="load-vs-modules">Load vs. modules</h1>
<p>After about a week hands-on with Clojure one of the things I miss the most from Racket is modules. Not even Racket’s submodules. Just plain modules.</p>
<p>Clojure namespaces handle name collisions. But modules go further:</p>
<ol>
<li>
<p>Forward references are OK.</p></li>
<li>
<p>Redefinitions are flagged as errors.</p></li>
<li>
<p>Deleted definitions actually disappear from the environment on re-eval of the source file.</p></li></ol>
<p>In other words, Clojure seems to be like Racket’s <code>#lang racket/load</code>, which isn’t recommended for general use.</p>
<p>An example scenario: I rename a function from <code>foo</code> to <code>bar</code>. I overlook updating a call site. Clojure doesn’t warn me with an error. Worse, I change <code>bar</code>’s behavior. But old <code>foo</code> still exists — and is being used at the overlooked call site. Hilarity and gnashing of teeth ensues.</p>
<p>This isn’t a hypothetical example. It’s happened to me a couple times in not that many days of hands-on with Clojure. On the one hand, this seems like an insane programming workflow. On the other hand, I have already learned to “measure twice, cut once” when renaming — and to bite the bullet and invest 10 seconds in a cider-restart. So if life had to be this way, I could cope.</p>
<p>But why does it have to be this way? I actually started to draft a <a href="https://github.com/greghendershott/clojure-module"><code>module</code></a> macro for Clojure. As I put in its README:</p>
<blockquote>
<p><strong>DISCLAIMER</strong>: This is a first draft by someone who…</p>
<ul>
<li>
<p>has been hands-on with Clojure for just a week</p></li>
<li>
<p>doesn’t necessarily appreciate how Clojure namespaces work</p></li>
<li>
<p>doesn’t know the complete story behind Racket modules</p></li></ul></blockquote>
<p>I can imagine Inigo Montoya telling me, “I do not think <code>module</code> means what you seem to think it means”. Yeah. That’s probably me, with <a href="https://github.com/greghendershott/clojure-module">this code</a>. At the moment it’s an exercise in starting to think about what might be involved.</p>
<h1 id="conclusions-and-next-steps">Conclusions and next steps</h1>
<p>There is a lot about Clojure that I really, really like and enjoy. At times I do wish it had better tooling and were built on a more-rigorous foundation.</p>
<p>I need to determine what to do next — spend more time with Clojure, or move on to Haskell. Also I need and want to spend significantly more time pairing with people here — which will at least partially entail working with a variety of other languages and platforms.</p>
<p>So it’s likely that I’ll take at least a brief break from Clojure. But I’ll return at some point.</p>
<div class="footnotes">
<ol>
<li id="hands-on-with-clojure-day-5-footnote-1-definition" class="footnote-definition">
<p>Are <code>println</code>s a sophisticated debugging technique? Nope. But some experienced programmers use them as a quick first resort (even when they’re willing and able to fire up a real debugger). <a href="#hands-on-with-clojure-day-5-footnote-1-return">↩</a></p></li></ol></div>
<footer></footer></article></div>Hands-on with Clojure day 4urn:www-greghendershott-com:-2014-10-hands-on-with-clojure-day-4.html2014-10-14T21:52:11Z2014-10-14T21:52:11ZGreg Hendershott
<div>
<article>
<header>
<h1>Hands-on with Clojure day 4</h1>
<p class="date-and-tags">
<time datetime="2014-10-14" pubdate="true">2014-10-14</time> :: <span class="tags"><a href="/tags/Clojure.html">Clojure</a>, <a href="/tags/Hacker-School.html">Hacker School</a></span></p></header>
<p>Overview:</p>
<ul>
<li>Spent time with <a href="https://github.com/clojure-cookbook/clojure-cookbook">Clojure Cookbook</a>.</li>
<li>Started a <a href="https://github.com/greghendershott/racket-clojure-cheat-sheet">cheat sheet</a> for Racket ↔ Clojure.</li>
<li>Looked at <code>split-with</code>.</li>
<li>Started a port of <a href="https://github.com/greghendershott/wffi">wffi</a> from Racket to Clojure.</li>
<li>Confusion: Aggregates and generics.</li></ul><!-- more-->
<h1 id="clojure-cookbook">Clojure Cookbook</h1>
<p>I discovered <a href="https://github.com/clojure-cookbook/clojure-cookbook">Clojure Cookbook</a>, which describes itself as:</p>
<blockquote>
<p><em>Clojure Cookbook</em> doesn’t just teach you Clojure, it also shows you how to use the language and many of its common libraries. The most difficult part of mastering any language is knowing how to apply it, in an idiomatic way, to tasks that real software developers encounter every day. This is especially true of Clojure.</p>
<p>With code recipes that teach you how to use the language in a variety of domains, <em>Clojure Cookbook</em> goes beyond simply teaching Clojure syntax and semantics. It contains annotated example code with detailed analysis and explanation for hundreds of real programming tasks. You can read the book straight through to gain insights about Clojure, or use it as a reference to solve particular problems.</p></blockquote>
<p>You can <code>git clone</code> the <a href="https://github.com/clojure-cookbook/clojure-cookbook">Clojure Cookbook</a> project and work with it in Emacs. This is a nice experience.</p>
<p>You can read each section in an <code>adoc-mode</code> buffer, with cider-repl active. That way you can
<kbd>C-x C-e</kbd> to see the result on the Emacs status bar (or if you prefer, paste or type directly in the the cider-repl buffer).</p>
<p>Plus you can bind a key like
<kbd>M-+</kbd> to an Elisp function that finds and advances to the next section.</p>
<p>I found most sections to be helpful. Not only do they show how to do X, they get into the rationale of when and why should use a certain Clojure approach vs. another. Very good stuff.</p>
<h1 id="racket--clojure-cheat-sheet">Racket ↔ Clojure Cheat Sheet</h1>
<p>As I’ve learned Clojure equivalents for Racket functions, I’ve jotted them down in notes. The notes got long enough that I moved them into org-mode tables — resulting in this <a href="https://github.com/greghendershott/racket-clojure-cheat-sheet">cheat sheet</a>.</p>
<p>The first part covers, given Racket function X, what’s the exact or close equivalent Clojure function Y? The second part is for the other direction, from Clojure to Racket.</p>
<p>Again, these are my rough working notes. Don’t rely on it. I wouldn’t be shocked if some of it is subtly — or blatantly — wrong.</p>
<p>I found that, as is often the case with taking notes, the act of taking them makes it less-likely to need to refer to them.</p>
<h1 id="cheat-sheets-split-with">Cheat sheets, <code>split-with</code></h1>
<p>Speaking of cheat sheets, on <code>#clojure</code> someone reminded me of the <a href="http://clojure.org/cheatsheet">Clojure cheatsheet</a>. This seems like something to keep open in a browser tab. Or, say, print and paste on the inside of my forehead.</p>
<p>I was on IRC asking if Clojure had some equivalent to Racket’s <a href="http://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Flist..rkt%29._splitf-at%29%29"><code>splitf-at</code></a>, with an implementation faster than the conceptual one I was able to come up with:</p>
<div class="brush: clojure">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4
5
6</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="kd">defn </span><span class="nv">split</span>
<span class="s">"FIXME: This is the conceptual, inefficient implementation. Should</span>
<span class="s"> re-implement like Racket's splitf-at."</span>
<span class="p">[</span><span class="nv">pred</span> <span class="nv">coll</span><span class="p">]</span>
<span class="p">[(</span><span class="nb">take-while </span><span class="nv">pred</span> <span class="nv">coll</span><span class="p">)</span>
<span class="p">(</span><span class="nb">drop-while </span><span class="nv">pred</span> <span class="nv">coll</span><span class="p">)])</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Someone pointed out <a href="https://github.com/clojure/clojure/blob/clojure-1.6.0/src/clj/clojure/core.clj#L2698"><code>split-with</code></a>, which I could have found on the cheat sheet.</p>
<p>So that’s cool. However
<kbd>M-.</kbd> on <a href="https://github.com/clojure/clojure/blob/clojure-1.6.0/src/clj/clojure/core.clj#L2698"><code>split-with</code></a> shows an implementation that is… the conceptual one. I guess that could be a micro-project — write an implementation that doesn’t excessively traverse and create intermediate collections.</p>
<h1 id="wffi-for-clojure">wffi for Clojure</h1>
<p>A couple years ago, I was reading so much Amazon Web Services documentation that included what were essentially HTTP request and response templates. Such as the following, albeit much more complicated than this:</p>
<pre><code>GET /users/{user}/?key={val}
Header: {value}
...</code></pre>
<p>Eventually it dawned on me that it would awfully be nice if web service documentation like this could actually drive code generation. Both generate client wrappers to make requests to the service, and, generate API glue for the service’s server. That was the idea behind <a href="https://github.com/greghendershott/webapi-markdown">webapi-markdown</a>. For example, you could take some service’s documentation, run it through a tool, and voila, an “SDK” in your favorite language.</p>
<p>So <a href="https://github.com/greghendershott/webapi-markdown">webapi-markdown</a> is just a spec. The one implementation to-date is in Racket: <a href="https://github.com/greghendershott/wffi">wffi</a>. (The name implying both “web FFI” and “stinky”.)</p>
<p>In searching for a somewhat more challenging project to do in Clojure, I figured this might fit the bill.</p>
<p>So I spent the last day working on it. It’s been slow going, but so far I’ve had a chance to learn:</p>
<ul>
<li>
<p>How to use <a href="https://github.com/yogthos/markdown-clj">markdown-clj</a> for parsing markdown. Although it returns an HTML string, which isn’t ideal, it’s easy enough to convert that using <code>tagsoup</code> into structured data. (I keep thinking of these as “Clojure’s equivalent of x-expressions”. Is this known as “hiccup”? Not sure.)</p></li>
<li>
<p>How to use <a href="https://github.com/Engelberg/instaparse">Instaparse</a>. I only got a couple hours into this, but so far it seems pretty spiffy. It was fun to see the acknowledgment in the README that Danny Yoo’s <a href="https://www.hashcollision.org/ragg/">Ragg</a> was the inspiration. It is definitely nice to use in the same way that Ragg is, and more.</p></li></ul>
<p>And eventually this project will get into areas like:</p>
<ul>
<li>
<p>Code-generation via Clojure macros.</p></li>
<li>
<p>Generating docs (via doc strings, I suppose).</p></li>
<li>
<p>Distributing a project on Clojars (if I take it that far).</p></li></ul>
<p>So I think that’s a pretty good chunk of real-world stuff, which makes it worth pursuing this, even if there’s no value in releasing it per se.</p>
<p>The code is still pretty raw, and my grammar for Instaparse needs work. So I’m making commits, but not confident enough to push stuff to GitHub quite yet.</p>
<h1 id="aggregates-and-generics">Aggregates and generics</h1>
<p>One area I’m still confused by is Clojure’s variety of approaches to aggregate data types and polymorphism.</p>
<p>Coming from Racket, we have structs and generics. <code>struct</code>s have compile-time defined fields. As a result:</p>
<ul>
<li>They’re fast: A field is a direct offset.</li>
<li>They’re strict:
<ul>
<li>You can’t accidentally add or omit a field.</li>
<li>Structs can inherit fields from other structs, strictly.</li></ul></li></ul>
<p>Of course we also have dictionaries (hash tables and association lists) when we want runtime flexibility.</p>
<p>My current impression is that Clojurians would often use a <code>map</code> instead of a <code>struct</code> in many situations. I think?</p>
<p>Clojure does have things that seem to be in the same ballpark as <code>struct</code>s. <code>defrecord</code> sounds like it is about <code>struct</code>s. Is it sugar for maps? <code>deftype</code> is, erm… I don’t know. Clojure Cookbook has a quote from Chas Emerick:</p>
<blockquote>
<p>Is your class modeling a domain value—thus benefiting from hash map-like functionality and semantics? Use <code>defrecord</code>.</p>
<p>Do you need to define mutable fields? Use <code>deftype</code>.</p></blockquote>
<p>Hmm. That’s not what I would have guessed from the name, “type”. Plus, I thought mutability was strongly discouraged in Clojure.</p>
<p>The docs for <code>defrecord</code> and <code>deftype</code> seem a bit terse and opaque to me: The words make sense but aren’t crisply connecting to concepts I already know.</p>
<p>So, clearly I need to learn more in this space.</p>
<h1 id="requires-are-less-often-but-still-tricky">Requires are less often, but still, tricky</h1>
<p>Although it’s much better, I still seem to get confused by requires. Good example is clj-tagsoup. I kept trying to <code>:require</code> <code>pl.danieljanus.tagsoup.core</code>. But it’s just <code>pl.danieljanus.tagsoup</code>. The classpath error message isn’t very helpful for me. If I go in <code>~/.m2</code> and look, I don’t see source files. I need to go up on GitHub and view the source. That’s how I eventually noticed it’s <code>src/pl/danieljanus/tagsoup.clj</code>. Not e.g. <code>src/pl/danieljanus/tagsoup/core.clj</code>.</p>
<p>To be fair, I suppose this is really about one library’s choice. And I’m new to Clojure. But I don’t seem to struggle with this in Racket. Especially when an installed package has Scribble-generated docs, the docs automatically show a <code>(require foo)</code> that, well, just works the first time.</p>
<h1 id="reflection-and-next-steps">Reflection and next steps</h1>
<p>Notwithstanding my whinge in the previous section, I’m hitting tooling and cider speed bumps much less frequently. I felt like yesterday was the first time I could actually focus on the code, without distraction, for solid hours at a time. Although I still don’t feel like I’ve figured out an optimal Clojure workflow, it is easier for me to imagine such a thing being possible someday.</p>
<p>Next up, I plan to keep working on the wffi port, and see how far I can take that this remainder of this week.</p>
<footer></footer></article></div>Hands-on with Clojure day 3urn:www-greghendershott-com:-2014-10-hands-on-with-clojure-day-3.html2014-10-10T15:13:34Z2014-10-10T15:13:34ZGreg Hendershott
<div>
<article>
<header>
<h1>Hands-on with Clojure day 3</h1>
<p class="date-and-tags">
<time datetime="2014-10-10" pubdate="true">2014-10-10</time> :: <span class="tags"><a href="/tags/Clojure.html">Clojure</a>, <a href="/tags/Hacker-School.html">Hacker School</a></span></p></header>
<p>Please see the usual disclaimers from my <a href="http://www.greghendershott.com/2014/10/hands-on-with-clojure.html">previous</a> <a href="http://www.greghendershott.com/2014/10/hands-on-with-clojure-day-2.html">posts</a>.</p>
<p>As I mentioned yesterday, my next toy project is to write wrappers libraries for the new <a href="https://github.com/HackerNews/API">Hacker News API</a>. This seems like a good exercise because the REST API is very simple, and I have experience doing this sort of thing in Racket. In fact, I’ll do the same thing in both Racket and Clojure.</p>
<p>The result is <a href="https://github.com/greghendershott/clacker-news">clacker-news</a> and <a href="https://github.com/greghendershott/racker-news">racker-news</a>. Trademark registration application is in-process.<sup><a href="#hands-on-with-clojure-day-3-footnote-1-definition" name="hands-on-with-clojure-day-3-footnote-1-return">1</a></sup></p><!-- more-->
<p>Although I knew this would be a simple project, it turned out to be simpler than I expected:</p>
<ul>
<li>
<p>The Hacker News API is just a half-dozen items, so that helps.</p></li>
<li>
<p>Both Racket and Clojure have easy ways to parse JSON into the relevant idiomatic representations. (As you might imagine, Racket is more <code>list</code>y and Clojure is more <code>vector</code>y, but they are pretty similar. Because JSON.)</p></li>
<li>
<p>I didn’t try to do anything fancy with error-handling. If there’s an HTTP-related exception, it’s going to bubble up to the user of my library. I think this is probably fine.</p></li>
<li>
<p>I didn’t try to do anything fancy with retries. This is probably less-fine. Given response like say <code>429 Too Many Requests</code>, it would nice if my library would automatically retry some number of times (with an exponential delay back-off). But it doesn’t.</p></li></ul>
<p>However, maybe it was unduly easy — because I overlooked something important in Clojure (or about making REST API requests in general). If so, feel free to hit the comments.</p>
<h1 id="redefinitions-and-the-top-level">Redefinitions and the top-level</h1>
<p>While doing some copy-pasta of the Racket source to Clojure, I accidentally ended up with two <code>defn</code>s of the same function, <code>get-user</code>. The thing is, Clojure did <em>not</em> give me an error message. This really surprised me. In Racket, this would be a redefinition error.</p>
<p>I guess this is related to my observation in yesterday’s post about the need to use <code>declare</code> for forward references. If I understand correctly, this means that Clojure’s evaluation model is closer to a simple <code>load</code>: It is essentially equivalent to typing stuff at a top-level REPL prompt. Things are evaluated one s-expression <code>read</code> at a time.</p>
<p>So for example it’s perfectly fine to redefine something at the top-level in the REPL.</p>
<p>And, if you <code>read</code> things one s-expression at a time — as opposed to an entire file and/or namespace — you can’t know about something that isn’t defined yet. You need a hint like <code>declare</code>: “Chill, I’m going to supply it later.”</p>
<p>What I’m used to from Racket is an actual module system. As I understand it, the first unit of evaluation is a module. And the <code>#lang</code> business is a shorthand for <code>module</code> forms. In other words:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="kn">#lang </span><span class="nn">racket</span>
<span class="p">(</span><span class="k"><a href="http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))" style="color: inherit">define</a></span> <span class="p">(</span><span class="n"><a href="http://docs.racket-lang.org/syntax/Library_Syntax_Classes_and_Literal_Sets.html#(form._((lib._syntax/parse..rkt)._id))" style="color: inherit">id</a></span> <span class="n">x</span><span class="p">)</span>
<span class="n">x</span><span class="p">)</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>is shorthand for:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="k"><a href="http://docs.racket-lang.org/reference/module.html#(form._((quote._~23~25kernel)._module))" style="color: inherit">module</a></span> <span class="n">racket</span>
<span class="p">(</span><span class="k"><a href="http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))" style="color: inherit">define</a></span> <span class="p">(</span><span class="n"><a href="http://docs.racket-lang.org/syntax/Library_Syntax_Classes_and_Literal_Sets.html#(form._((lib._syntax/parse..rkt)._id))" style="color: inherit">id</a></span> <span class="n">x</span><span class="p">)</span>
<span class="n">x</span><span class="p">))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>And in fact you will see older Racket files that use the <code>module</code> form explicitly like that (as well as newer <code>#lang</code> files that use <code>module</code> forms within, i.e. for nested modules).</p>
<p>Maybe I’m misunderstanding, and the business about modules is orthogonal to the business about redefinitions and forward references. Maybe that’s really about <code>read</code>-ing s-expressions one at a time like in a top-level REPL. In any case, I don’t love this aspect of Clojure.<sup><a href="#hands-on-with-clojure-day-3-footnote-2-definition" name="hands-on-with-clojure-day-3-footnote-2-return">2</a></sup></p>
<h1 id="next-steps">Next steps</h1>
<p>I need an idea for a gradually more-complicated project to try, next.</p>
<p>Ideally it would exercise something special/strong about Clojure, such as the persistent immutable data structures, concurrency primitives, or so on.</p>
<p>(As a counter-example, I could explore macros in Clojure, but my expectation is that’s an area that might be disappointing compared to Racket. Instead I’d like to find something where I’m more likely to say, “Dang, I wish Racket did this.”)</p>
<p>If you have suggestions, let me know in the comments, and thanks in advance.</p>
<div class="footnotes">
<ol>
<li id="hands-on-with-clojure-day-3-footnote-1-definition" class="footnote-definition">
<p>Kidding. <a href="#hands-on-with-clojure-day-3-footnote-1-return">↩</a></p></li>
<li id="hands-on-with-clojure-day-3-footnote-2-definition" class="footnote-definition">
<p>Among Racketeers, a famous quote is, <a href="https://gist.github.com/samth/3083053">“the top-level is hopeless”</a>. <a href="#hands-on-with-clojure-day-3-footnote-2-return">↩</a></p></li></ol></div>
<footer></footer></article></div>Hands-on with Clojure day 2urn:www-greghendershott-com:-2014-10-hands-on-with-clojure-day-2.html2014-10-09T14:58:46Z2014-10-09T14:58:46ZGreg Hendershott
<div>
<article>
<header>
<h1>Hands-on with Clojure day 2</h1>
<p class="date-and-tags">
<time datetime="2014-10-09" pubdate="true">2014-10-09</time> :: <span class="tags"><a href="/tags/Clojure.html">Clojure</a>, <a href="/tags/Hacker-School.html">Hacker School</a></span></p></header>
<p>As with <a href="http://www.greghendershott.com/2014/10/hands-on-with-clojure.html">yesterday’s post</a>, important disclaimers:</p>
<ul>
<li>
<p>I’ve used <a href="http://www.racket-lang.org">Racket</a> heavily but not Clojure.</p></li>
<li>
<p>Opinions expressed herein are not those of my employer, were I to have one.</p></li>
<li>
<p>If indignation lasts more than four hours, please seek medical attention.</p></li></ul>
<p>Day 2 with Clojure was much more fun! I didn’t hit speed bumps with tooling and workflow, so frequently. I was able to focus mostly on the code itself, which was wonderful.</p>
<p>For a <em>slightly</em> more realistic task, I decided to make a really simple function that, given a URL, would make a <code>GET</code> request and return the value of the <code>Server</code> response header (if any), and a list of URLs found on the page that are for <em>other</em> servers. The idea being, you could crawl from some starting point and accumulate some data about web server technology.</p><!-- more-->
<p>To do this, I needed to understand how to:</p>
<ol>
<li>
<p>Make HTTP requests.</p></li>
<li>
<p>Parse an HTTP response entity (the HTML document) in a way I could walk it to look for <code><a href="foo"></code> elements.</p></li>
<li>
<p>Miscellaneous other things.</p></li></ol>
<h1 id="making-http-get-requests">Making HTTP <code>GET</code> requests</h1>
<p>I discovered <code>clj-http</code>. Added it to <code>projects.clj</code>, and added <code>(:require [clj-http.client :as client])</code> to my <code>core.clj</code>. I got errors in cider. It worked fine with <code>lein repl</code> at the command line. I did a <code>cider-restart</code> and eventually it started it working.</p>
<p>Take-away: I’m starting to learn how to supplicate the system reliably, with certain incantations. But I don’t yet have a good mental model for the statefulness of adding a lib to a project and using it, at least not with cider.</p>
<p>Using <code>clj-http</code> was a joy. Returns a nice map with key/value pairs I’d expect.</p>
<h1 id="parsing-and-walking-html">Parsing and walking HTML</h1>
<p>Next, how to parse and walk the HTML in the <code>:body</code>?</p>
<p>In Racket we typically use what are called <code>x-expressions</code> to represent XML and (well-formed) HTML:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4
5</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="o">`</span><span class="p">(</span><span class="ss">html</span> <span class="p">()</span>
<span class="p">(</span><span class="ss">head</span> <span class="p">())</span>
<span class="p">(</span><span class="ss">body</span> <span class="p">()</span>
<span class="p">(</span><span class="ss">p</span> <span class="p">([</span><span class="ss">attr</span> <span class="s2">"val"</span><span class="p">][</span><span class="ss">attr2</span> <span class="s2">"val2"</span><span class="p">])</span>
<span class="s2">"Some text "</span> <span class="ss">amp</span> <span class="s2">" some more."</span><span class="p">)))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>In other words this is an s-expression that follows a convention: There is a list. The first element is a symbol for the element tag name. The second element is an association-list of attributes. The zero or more remaining elements are either plain data like strings or numbers or symbols, or, other x-expressions.</p>
<p>I searched around and read about Enliven. That seemed like a heavier tool than I needed. On StackOverflow someone mentioned <code>clj-tagsoup</code>. That looked like exactly what I wanted — or at least was familiar with. To me, these look like x-expressions, but using vectors instead of lists, and using maps instead of association lists.</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4
5</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">[</span><span class="ss">:html</span> <span class="p">{}</span>
<span class="p">[</span><span class="ss">:head</span> <span class="p">{}]</span>
<span class="p">[</span><span class="ss">:body</span> <span class="p">{}</span>
<span class="p">[</span><span class="ss">:p</span> <span class="p">{</span><span class="ss">:attr</span> <span class="s">"val"</span>, <span class="ss">:attr2</span> <span class="s">"val2"</span><span class="p">}</span>
<span class="s">"Some text & some more."</span><span class="p">]]]</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Wonderful.</p>
<p>Adding that to my <code>project.clj</code> and requiring it in my <code>core.clj</code> was again weird. From the GitHub README and from Clojars.org, I’m not always sure when to use an organization-id prefix, and if so, what to use. I must be confusing myself because this seems like it should be simpler. At this stage, I try things until it works. The only catch being, I might need to use <code>cider-restart</code> each time, otherwise I might not realize I actually did get it correct.</p>
<p>Anyway, once added and required properly, <code>clj-tagsoup</code> was also a joy to use.</p>
<h1 id="parsing-urls">Parsing URLs</h1>
<p>Now that things were working, I noticed that the list of URLs included many links to the same server. There’s no point in crawling those — presumably the same web site is using the same web server. Instead I want to filter these to be only URLs for <em>other</em> sites, so we can go crawl those and discover what <em>they</em> return for <code>Server:</code>.</p>
<p>To do this, I want to split the URL into its components, and look at the scheme (a.k.a. protocol like “http”), hostname, and port, but disregard the rest. In Racket I would use <code>string->url</code>, which would return a <code>url</code> struct having fields for each of these. How to do this in Clojure?</p>
<p>I couldn’t find an answer in the Clojure docs. For a minute I thought about using a regular expression. But I’ve seen the regexp that Racket’s <code>string->url</code> uses. A <em>correct</em> regexp is non-trivial. Anyway this seems like a wheel I should not be reinventing.</p>
<p>I asked on #clojure IRC. Justin Smith and David Nolen quickly helped me out, showing me I could use <code>(bean (java.net.URL.
"http://www.google.com"))</code>, which returns a map, members of which include <code>:protocol</code>, <code>:host</code>, <code>:port</code>. Perfect.</p>
<p>Then I looked again at the map:</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 1
2
3
4
5
6
7
8
9
10
11
12</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">{</span><span class="ss">:path</span> <span class="s">""</span>,
<span class="ss">:protocol</span> <span class="s">"http"</span>,
<span class="ss">:ref</span> <span class="nv">nil</span>,
<span class="ss">:content</span> <span class="o">#</span><span class="nv"><HttpInputStream</span> <span class="nv">sun.net.www.protocol.http.HttpURLConnection$HttpInputStream</span><span class="o">@</span><span class="mi">56</span><span class="nv">e01e8a></span>,
<span class="ss">:authority</span> <span class="s">"www.google.com"</span>,
<span class="ss">:file</span> <span class="s">""</span>,
<span class="ss">:port</span> <span class="mi">-1</span>,
<span class="ss">:host</span> <span class="s">"www.google.com"</span>,
<span class="ss">:class</span> <span class="nv">java.net.URL</span>,
<span class="ss">:query</span> <span class="nv">nil</span>,
<span class="ss">:defaultPort</span> <span class="mi">80</span>,
<span class="ss">:userInfo</span> <span class="nv">nil</span><span class="p">}</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Hmm, what’s up with that part, <code>:content #<HttpInputStream
sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@56e01e8a></code>. That sure looks like an open connection. I don’t want to open a connection for all of these — just parse/split the URL string into its components.</p>
<p>It seems that <code>bean</code> calls all of the accessor members, and <a href="http://docs.oracle.com/javase/7/docs/api/java/net/URL.html#getContent%28%29">Java.Net.URL.getContent()</a> has a side-effect:</p>
<div class="brush: java">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="n">openConnection</span><span class="o">().</span><span class="na">getContent</span><span class="o">()</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>So that’s not good. Back on IRC, just as I was typing, “I wonder if I can call the methods directly instead of using <code>bean</code>”, Justin Smith said the same. Turns out he’d already posted a benchmark of <code>bean</code> vs. directly calling select members, which is just what I needed.</p>
<p>Of course on my first attempt I managed to forget the trailing <code>.</code> in <code>(java.net.URL. url)</code> — but then figured that out.</p>
<p>Take-aways:</p>
<ul>
<li>
<p>I needed to learn about Java interop earlier than I expected. The interop <em>per se</em> seems simple enough, and I’ll re-read that part of <em>Joy of Clojure</em> today.</p></li>
<li>
<p>I’m a bit worried I don’t yet know the Java library ecosystem. How will I know what’s available? But I guess library discoverability is a challenge for every language and language-learner. Maybe for Clojure the steps will be:</p>
<ol>
<li>Search Clojure docs.</li>
<li>Search Clojars.</li>
<li>Search Java docs.</li>
<li>Ask on #clojure. Preferably as step 4 not 0. :)</li></ol></li>
<li>
<p>Justin Smith suggested the <a href="https://chrome.google.com/webstore/detail/javadoc-search-frame/mfgkihmpcjjnijmkchojlbccbmhcdfcd?hl=en">javadoc-search-pane</a> browser add-on for Chrome and Firefox as a great way to explore Java docs.</p></li>
<li>
<p>Justin Smith is incredibly helpful! Thanks!</p></li></ul>
<h1 id="forward-declarations">Forward declarations</h1>
<p>One thing I find slightly annoying in Clojure is the need to use <code>declare</code>, which isn’t necessary in Racket. If you like your source code to be “bottom up”, building up ingredients to a dramatic finale at the end of the source file, it’s not an issue. However, if you like to be “top-down”, starting with the main public product, and working down into the supporting cast, it can be awkward.</p>
<h1 id="code">Code</h1>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="kd">ns </span><span class="nv">web-client.core</span>
<span class="p">(</span><span class="ss">:require</span> <span class="p">[</span><span class="nv">clj-http.client</span> <span class="ss">:as</span> <span class="nv">client</span><span class="p">]</span>
<span class="p">[</span><span class="nv">clojure.test</span> <span class="ss">:refer</span> <span class="ss">:all</span><span class="p">])</span>
<span class="p">(</span><span class="ss">:use</span> <span class="nv">pl.danieljanus.tagsoup</span><span class="p">))</span>
<span class="p">(</span><span class="kd">defn- </span><span class="nv">server</span>
<span class="p">[</span><span class="nv">response</span><span class="p">]</span>
<span class="p">(</span><span class="nb">get </span><span class="p">(</span><span class="ss">:headers</span> <span class="nv">response</span><span class="p">)</span> <span class="s">"Server"</span><span class="p">))</span>
<span class="c1">;; Grumble: Needing to use `declare` is annoying compared to Racket.</span>
<span class="p">(</span><span class="kd">declare </span><span class="nv">body-elements</span>
<span class="nv">hrefs</span><span class="p">)</span>
<span class="p">(</span><span class="kd">defn- </span><span class="nv">urls</span>
<span class="p">[</span><span class="nv">response</span> <span class="nv">pred</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">entity</span> <span class="p">(</span><span class="ss">:body</span> <span class="nv">response</span><span class="p">)</span>
<span class="nv">tree</span> <span class="p">(</span><span class="nf">parse-string</span> <span class="nv">entity</span><span class="p">)</span>
<span class="nv">bodies</span> <span class="p">(</span><span class="nf">body-elements</span> <span class="nv">tree</span><span class="p">)]</span>
<span class="p">(</span><span class="nf">hrefs</span> <span class="nv">bodies</span> <span class="nv">pred</span><span class="p">)))</span>
<span class="p">(</span><span class="kd">defn- </span><span class="nv">body-elements</span>
<span class="p">[</span><span class="nv">tree</span><span class="p">]</span>
<span class="p">(</span><span class="nb">some </span><span class="p">(</span><span class="k">fn </span><span class="p">[</span><span class="nv">node</span><span class="p">]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">= </span><span class="ss">:body</span> <span class="p">(</span><span class="nf">tag</span> <span class="nv">node</span><span class="p">))</span>
<span class="p">(</span><span class="nb">children </span><span class="nv">node</span><span class="p">)))</span>
<span class="p">(</span><span class="nb">children </span><span class="nv">tree</span><span class="p">)))</span>
<span class="p">(</span><span class="kd">defn- </span><span class="nv">hrefs</span>
<span class="s">"Given a collection of body elements, and a predicate, return all of</span>
<span class="s"> the link URLs on the page satisfying the predicate. For example the</span>
<span class="s"> predicate might be if the URLs are for another hostname, i.e. a link</span>
<span class="s"> to some other web server. "</span>
<span class="p">[</span><span class="nv">xs</span> <span class="nv">pred</span><span class="p">]</span>
<span class="p">(</span><span class="nb">filter </span><span class="nv">pred</span>
<span class="p">(</span><span class="nf">flatten</span>
<span class="p">(</span><span class="nb">map </span><span class="p">(</span><span class="k">fn </span><span class="p">[</span><span class="nv">x</span><span class="p">]</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nf">coll?</span> <span class="nv">x</span><span class="p">)</span>
<span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nb">= </span><span class="ss">:a</span> <span class="p">(</span><span class="nf">tag</span> <span class="nv">x</span><span class="p">))</span>
<span class="p">(</span><span class="ss">:href</span> <span class="p">(</span><span class="nf">attributes</span> <span class="nv">x</span><span class="p">))</span>
<span class="p">(</span><span class="nf">hrefs</span> <span class="p">(</span><span class="nb">children </span><span class="nv">x</span><span class="p">)</span> <span class="nv">pred</span><span class="p">))))</span>
<span class="nv">xs</span><span class="p">))))</span>
<span class="c1">;; TODO: Move this test elsewhere.</span>
<span class="p">(</span><span class="nf">is</span> <span class="p">(</span><span class="nb">= </span><span class="p">(</span><span class="nf">find-hrefs</span> <span class="p">[[</span><span class="ss">:p</span> <span class="p">{}</span> <span class="s">"par"</span><span class="p">]</span>
<span class="p">[</span><span class="ss">:a</span> <span class="p">{</span><span class="ss">:href</span> <span class="s">"http://hi.com"</span><span class="p">}</span> <span class="s">"hi"</span><span class="p">]</span>
<span class="p">[</span><span class="ss">:p</span> <span class="p">{}</span>
<span class="s">"par"</span>
<span class="p">[</span><span class="ss">:a</span> <span class="p">{</span><span class="ss">:href</span> <span class="s">"http://there.com"</span><span class="p">}</span> <span class="s">"there"</span><span class="p">]</span>
<span class="s">"par"</span><span class="p">]])</span>
<span class="p">[</span><span class="s">"http://hi.com"</span>
<span class="s">"http://there.com"</span><span class="p">]))</span>
<span class="c1">;; Grumble: Needing to use `declare` is annoying compared to Racket.</span>
<span class="p">(</span><span class="kd">declare </span><span class="nv">valid-url?</span> <span class="nv">scheme+host+port</span><span class="p">)</span>
<span class="p">(</span><span class="kd">defn- </span><span class="nv">server-equal?</span>
<span class="s">"Predicate for two URLs having 'the same server', as determined by</span>
<span class="s"> having the same scheme, host, and port.</span>
<span class="s"> If either URL is malformed (according to java.net.URL) then this</span>
<span class="s"> returns t. That's because the intended use of this is by</span>
<span class="s"> `get-server-and-links'. Of course that means the name</span>
<span class="s"> `server-equal?` isn't quite right, and probably all this should be</span>
<span class="s"> refactored. TO-DO."</span>
<span class="p">[</span><span class="nv">a</span> <span class="nv">b</span><span class="p">]</span>
<span class="p">(</span><span class="nb">or </span><span class="p">(</span><span class="nb">not </span><span class="p">(</span><span class="nf">valid-url?</span> <span class="nv">a</span><span class="p">))</span>
<span class="p">(</span><span class="nb">not </span><span class="p">(</span><span class="nf">valid-url?</span> <span class="nv">b</span><span class="p">))</span>
<span class="p">(</span><span class="nb">= </span><span class="p">(</span><span class="nf">scheme+host+port</span> <span class="nv">a</span><span class="p">)</span>
<span class="p">(</span><span class="nf">scheme+host+port</span> <span class="nv">b</span><span class="p">))))</span>
<span class="p">(</span><span class="kd">defn- </span><span class="nv">valid-url?</span>
<span class="p">[</span><span class="nv">url</span><span class="p">]</span>
<span class="c1">;; Note: java.net.URL can throw an exception if the string is not a</span>
<span class="c1">;; well-formed URL. In that case, return some default map.</span>
<span class="p">(</span><span class="nf">try</span>
<span class="p">(</span><span class="nf">java.net.URL.</span> <span class="nv">url</span><span class="p">)</span>
<span class="p">(</span><span class="nf">catch</span> <span class="nv">java.net.MalformedURLException</span> <span class="nv">e</span> <span class="nv">nil</span><span class="p">)))</span>
<span class="p">(</span><span class="nf">is</span> <span class="p">(</span><span class="nf">valid-url?</span> <span class="s">"http://www.google.com"</span><span class="p">))</span>
<span class="p">(</span><span class="nf">is</span> <span class="p">(</span><span class="nb">not </span><span class="p">(</span><span class="nf">valid-url?</span> <span class="s">"#anchor"</span><span class="p">)))</span>
<span class="p">(</span><span class="kd">defn- </span><span class="nv">scheme+host+port</span>
<span class="s">"Given a URL, return a map of its scheme, host, and port."</span>
<span class="p">[</span><span class="nv">url</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">loc</span> <span class="p">(</span><span class="nf">java.net.URL.</span> <span class="nv">url</span><span class="p">)]</span>
<span class="p">{</span><span class="ss">:scheme</span> <span class="p">(</span><span class="nf">.getProtocol</span> <span class="nv">loc</span><span class="p">)</span>
<span class="ss">:host</span> <span class="p">(</span><span class="nf">.getHost</span> <span class="nv">loc</span><span class="p">)</span>
<span class="ss">:port</span> <span class="p">(</span><span class="nf">.getPort</span> <span class="nv">loc</span><span class="p">)}))</span>
<span class="c1">;; TODO: Move this test elsewhere</span>
<span class="p">(</span><span class="nf">is</span> <span class="p">(</span><span class="nb">= </span><span class="p">(</span><span class="nf">scheme+host+port</span> <span class="s">"http://www.google.com"</span><span class="p">)</span>
<span class="p">(</span><span class="nf">scheme+host+port</span> <span class="s">"http://www.google.com/path/to/foo?q=0;p=1"</span><span class="p">)))</span>
<span class="p">(</span><span class="nf">is</span> <span class="p">(</span><span class="nb">not </span><span class="p">(</span><span class="nb">= </span><span class="p">(</span><span class="nf">scheme+host+port</span> <span class="s">"http://www.google.com"</span><span class="p">)</span>
<span class="p">(</span><span class="nf">scheme+host+port</span> <span class="s">"http://www.giggle.com"</span><span class="p">))))</span>
<span class="p">(</span><span class="nf">is</span> <span class="p">(</span><span class="nb">not </span><span class="p">(</span><span class="nb">= </span><span class="p">(</span><span class="nf">scheme+host+port</span> <span class="s">"http://www.google.com"</span><span class="p">)</span>
<span class="p">(</span><span class="nf">scheme+host+port</span> <span class="s">"https://www.goggle.com"</span><span class="p">))))</span>
<span class="p">(</span><span class="nf">is</span> <span class="p">(</span><span class="nb">not </span><span class="p">(</span><span class="nb">= </span><span class="p">(</span><span class="nf">scheme+host+port</span> <span class="s">"http://www.google.com:80"</span><span class="p">)</span>
<span class="p">(</span><span class="nf">scheme+host+port</span> <span class="s">"http://www.goggle.com:8080"</span><span class="p">))))</span>
<span class="c1">;; TODO: Move this test elsewhere</span>
<span class="p">(</span><span class="nf">is</span> <span class="p">(</span><span class="nb">= </span><span class="p">(</span><span class="nf">scheme+host+port</span> <span class="s">"http://www.google.com"</span><span class="p">)</span>
<span class="p">{</span><span class="ss">:scheme</span> <span class="s">"http"</span>, <span class="ss">:host</span> <span class="s">"www.google.com"</span>, <span class="ss">:port</span> <span class="mi">-1</span><span class="p">}))</span>
<span class="p">(</span><span class="nf">is</span> <span class="p">(</span><span class="nb">= </span><span class="p">(</span><span class="nf">scheme+host+port</span> <span class="s">"http://www.google.com/path/to/foo?q=0;p=1"</span><span class="p">)</span>
<span class="p">{</span><span class="ss">:scheme</span> <span class="s">"http"</span>, <span class="ss">:host</span> <span class="s">"www.google.com"</span>, <span class="ss">:port</span> <span class="mi">-1</span><span class="p">}))</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">get-server-and-links</span>
<span class="s">"Given a URL, visit it and return a map with the value of the Server</span>
<span class="s"> response header (if any), and a collection of URLs for other hosts</span>
<span class="s"> to which it links (if any).</span>
<span class="s"> In other words, the intended use is that you'd crawl and accumulate</span>
<span class="s"> data about web server technology for some subset of teh interwebs."</span>
<span class="p">[</span><span class="nv">url</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">response</span> <span class="p">(</span><span class="nf">client/get</span> <span class="nv">url</span><span class="p">)]</span>
<span class="p">{</span><span class="ss">:server</span> <span class="p">(</span><span class="nf">server</span> <span class="nv">response</span><span class="p">)</span>
<span class="ss">:urls</span> <span class="p">(</span><span class="nf">urls</span> <span class="nv">response</span> <span class="p">(</span><span class="nb">complement </span><span class="p">(</span><span class="nb">partial </span><span class="nv">server-equal?</span> <span class="nv">url</span><span class="p">)))}))</span>
<span class="p">(</span><span class="nf">comment</span>
<span class="p">(</span><span class="nb">println </span><span class="p">(</span><span class="nb">map </span><span class="nv">get-server-and-links</span> <span class="o">'</span><span class="p">(</span><span class="s">"http://www.google.com"</span>
<span class="s">"http://clojure.org"</span>
<span class="s">"http://www.racket-lang.org"</span><span class="p">))))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<h1 id="next-steps">Next Steps</h1>
<p>UPDATE: Oops, I posted without this part.</p>
<p>What next? Given that:</p>
<ul>
<li>I’m a roll with HTTP stuff in Clojure.</li>
<li>I’ve done wrappers for web services like <a href="https://github.com/greghendershott/aws">AWS</a> and <a href="https://github.com/greghendershott/gapi">GAPI</a> in Racket.</li>
<li><a href="https://news.ycombinator.com/news">Hacker News</a> has a new, simple <a href="https://github.com/HackerNews/API">REST API</a>.</li></ul>
<p>I think I might try writing wrappers libraries for Hacker News in both Clojure and Racket. As a plus, I can name them <code>clacker-news</code> and <code>racker-news</code>. I mean, maybe that’s a plus.</p>
<p>Thursdays are alumni days at <a href="https://www.hackerschool.com">Hacker School</a>, and if I understand correctly there are lightning talks (maybe this afternoon?). So maybe I’ll have less time to work on stuff today. But I can carry it over to tomorrow, as well.</p>
<footer></footer></article></div>Hands-on with Clojureurn:www-greghendershott-com:-2014-10-hands-on-with-clojure.html2014-10-08T14:01:59Z2014-10-08T14:01:59ZGreg Hendershott
<div>
<article>
<header>
<h1>Hands-on with Clojure</h1>
<p class="date-and-tags">
<time datetime="2014-10-08" pubdate="true">2014-10-08</time> :: <span class="tags"><a href="/tags/Clojure.html">Clojure</a>, <a href="/tags/Hacker-School.html">Hacker School</a></span></p></header>
<p>Until now, Clojure has been an “armchair” language for me. I’ve read a lot <em>about</em> it, and I’ve read a fair amount of code written <em>in</em> it. But aside from typing a few things into an online REPL like <a href="http://tryclj.com/">Try Clojure</a>, I’ve not really used it hands-on.</p>
<p>A couple days ago I started my 12-week batch at <a href="https://www.hackerschool.com/">Hacker School</a>. First on my list is to get some real experience with Clojure.</p>
<p>And I’m going to blog about the experience.</p><!-- more-->
<hr />
<p>A couple caveats:</p>
<ol>
<li>
<p>Hacker School encourages you to blog early, blog often. This post won’t necessarily be super well thought-out. Expect me to write some really dumb Clojure code, and state some silly opinions about Clojure.</p></li>
<li>
<p>I’m coming from a few years of using <a href="http://www.racket-lang.org">Racket</a> — and especially during the last year feeling very comfortable and reasonably proficient. Obviously Clojure is not Racket, in ways that are probably some mix of better, worse, and just different. But I may get cranky about a few things. I expect some of that will turn out to be “early days” and I’ll feel better later.</p></li></ol>
<p>In other words: I welcome comments that help me understand how to do something better or more idiomatically. However please cut me some slack on my subjective reactions, because I’m not trying to flame, here.</p>
<hr />
<h1 id="jvm">JVM</h1>
<p>Back when there was that security issue with the JVM on OS X I removed the JVM from my MacBook. The fact that Clojure uses the JVM is a huge plus for some people. For me, it’s meh. Anyway, no choice. First order of business is to install it.</p>
<p>I decide to download JDK 8. Later I hear that wasn’t necessarily the best choice:</p>
<div class="embed-tweet">
<blockquote align="center" class="twitter-tweet">
<p dir="ltr" lang="en"><a href="https://twitter.com/greghendershott?ref_src=twsrc%5Etfw">@greghendershott</a> you sure you didn't mean to download OpenJDK?</p>— technomancy fled to the fediverse as should you (@technomancy) <a href="https://twitter.com/technomancy/status/519328722324049920?ref_src=twsrc%5Etfw">October 7, 2014</a></blockquote>
<script async="async" charset="utf-8" src="https://platform.twitter.com/widgets.js"></script>
</div>
<p>Amusingly I mistakenly choose to download to <code>~/Dropbox</code> not <code>~/Downloads</code>. Although I think oracle.com was slow, it probably didn’t help matters that I was re-uploading the 200 MB back to Dropbox. Derp.</p>
<p>While waiting for the download to complete, I spend time re-working through the early chapters of <a href="http://learnyouahaskell.com/">Learn You a Haskell for Great Good</a>, since I plan to dive into Haskell and/or Idris in later weeks.</p>
<p>After I installed the JDK I tried:</p>
<div class="brush: sh">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span>$ java -version
java version <span class="s2">"1.8.0_20"</span>
Java<span class="o">(</span>TM<span class="o">)</span> SE Runtime Environment <span class="o">(</span>build <span class="m">1</span>.8.0_20-b26<span class="o">)</span>
Java HotSpot<span class="o">(</span>TM<span class="o">)</span> <span class="m">64</span>-Bit Server VM <span class="o">(</span>build <span class="m">25</span>.20-b23, mixed mode<span class="o">)</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Erm. What. Version 1.8 doesn’t sound like 8. Also <code>which</code> and <code>ll</code> show file dates that don’t seem new enough.</p>
<p>So apparently I already had JRE on here, despite removing it (I thought) back around the time of the OS X security bug. Or, is JDK 8 the “8” in JRE 1.8? Huh??</p>
<p>I spend some time trying to figure this out, with no luck.</p>
<p>Eventually I need to find where JDK 8 actually was installed, and make sure it’s in my PATH.</p>
<p>But meanwhile I decide to try ignoring it, and forge ahead and install <code>lein</code> and Clojure.</p>
<p>I followed part of <a href="http://www.unexpected-vortices.com/clojure/brief-beginners-guide/development-env.html">A Brief Beginner’s Guide to Clojure</a>. I went through its examples using <code>lein</code> to create a new project, start a REPL, and so on. Worked fine.</p>
<h1 id="clojure-and-emacs">Clojure and Emacs</h1>
<p>Next, how to get this working in Emacs?</p>
<p>I found <a href="http://clojure-doc.org/articles/tutorials/emacs.html">Emacs and Clojure</a>, a guide for getting set up with Clojure on Emacs. Following that worked out pretty well. Just a couple problems of my own creation.</p>
<p>Initially, <code>cider-jack-in</code> would complain that <code>pretty-regexp-alist</code> wasn’t defined. Some searching turned up <a href="https://github.com/clojure-emacs/cider/issues/833">issue 833</a>. Yep, I had an old clojure-mode from doinking around with just editing clojure code in the past. So I updated clojure-mode.</p>
<p>Now,
<kbd>C-c M-j</kbd> cider-jack-in is working. I find it weird that I must both 1.
<kbd>C-c C-k</kbd> to compile the buffer<sup><a href="#hands-on-with-clojure-footnote-1-definition" name="hands-on-with-clojure-footnote-1-return">1</a></sup>, then 2.
<kbd>C-c M-n</kbd> to enter the namespace… and only then can I use the REPL. This seems awkward compared to just
<kbd>F5</kbd> in racket-mode. Is there a good reason for separating the two, compiling vs. entering the ns? Probably. Anyway, I suppose I could bind
<kbd>F5</kbd> in clojure-mode to the composition of compile and enter. For now, I just learn to type both when needed.</p>
<p>So things are mostly working, except two things:</p>
<ul>
<li>
<p>I try to use
<kbd>C-c C-d d</kbd> (to see documentation) but it errors: <code>Wrong type argument: stringp, nil</code>. Huh.</p></li>
<li>
<p>I try to use
<kbd>M-.</kbd>. to visit source, but it says, <code>No source
location</code>. Huh.</p></li></ul>
<p>Well, maybe I should have paid attention to those warnings about mismatched versions. I had ended up with <code>cider 0.8.0-SNAPSHOT</code> from MELPA, but <code>cider-nrepl 0.7.0</code> from copy-pasta. Eventually I figured out how to create a <code>~/lein/profiles.clj</code> and set it to</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">{</span><span class="ss">:user</span> <span class="p">{</span><span class="ss">:plugins</span> <span class="p">[[</span><span class="nv">cider/cider-nrepl</span> <span class="s">"0.8.0-SNAPSHOT"</span><span class="p">]]}}</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Now all seems well. Overall <a href="http://clojure-doc.org/articles/tutorials/emacs.html">Emacs and Clojure</a> really helped me understand the pieces. Big thanks to the author, Gareth Jones, and the other people who contributed it.</p>
<h1 id="my-first-project-and-require">My First Project, and <code>require</code></h1>
<p>From my armchair Clojure I suspected I’d have some “uncanny valley” moments, where Clojure and Racket seem the same but are subtly different. I wasn’t disappointed. Right off the bat I struggled with <code>require</code>. I was trying to write <code>penultimate</code>. At first I thought, oh in Racket I’d use <code>match</code> for this. So I tried to require <code>core.match</code>. What I didn’t realize is that Clojure’s <code>:require</code> imports things under a name with a namespace prefix, by default.</p>
<p>I added it to <code>:dependencies</code> in my <code>project.clj</code>:</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span> <span class="ss">:dependencies</span> <span class="p">[[</span><span class="nv">org.clojure/clojure</span> <span class="s">"1.6.0"</span><span class="p">]</span>
<span class="p">[</span><span class="nv">org.clojure/core.match</span> <span class="s">"0.2.1"</span><span class="p">]]</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>That’s fine. But trying to require it in my <code>core.clj</code>:</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="kd">ns </span><span class="nv">my-lib.core</span>
<span class="p">(</span><span class="ss">:require</span> <span class="p">[</span><span class="nv">clojure.core.match</span> <span class="ss">:only</span> <span class="p">(</span><span class="nf">match</span><span class="p">)]))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Gives an error:</p>
<pre><code>=> #<CompilerException java.io.FileNotFoundException: Could not locate clojure/core/match__init.class or clojure/core/match.clj on classpath: , compiling:(my_lib/core.clj:1:1)></code></pre>
<p>And I have no idea really what the hell this means or what to do about it.</p>
<p>I gave up on core.match, realizing I could do what I want with Clojure’s destructuring <code>let</code>. Which is awesome sauce. But then, I wanted to make some unit tests. And I had the same problem trying to use <code>clojure.test</code>.</p>
<p>Eventually I realized that if I wanted to refer to <code>deftest</code> and <code>is</code> imported from the <code>clojure.test</code> namespace, I had to do it either of two ways:</p>
<ol>
<li>
<p>Use <code>(:require [clojure.test :refer :all])</code> so I could refer to <code>deftest</code> and <code>is</code> without any namespace prefix.</p></li>
<li>
<p>Use <code>(:require clojure.test)</code> and prefix e.g. <code>clojure.test/deftest</code> and <code>clojure.test/is</code>.</p></li></ol>
<p>In other words, by default, Clojure’s <code>require</code> is backwards from Racket’s with respect to name prefixes. I’m not sure I really like Clojure’s choice here. In my not-insubstantial experience with Racket, name collisions are infrequent. They seem like the special case that should require (no pun) special work — not the other way around. (When name collisions do occur in Racket, it’s easy to use Racket’s <code>only-in</code>, and/or use <code>prefix-in</code> to create some prefix. You could even do <code>(prefix-in foo/)</code> to make prefixes in a superficial Clojure style).</p>
<h1 id="tests">Tests</h1>
<p>After I managed to get a <code>deftest</code> in my <code>core.clj</code> file, I noticed the <code>../test/my_lib/core_test.clj</code> file created by Leinengen. Ah. The tests are supposed to go there. And if I put them there, <code>lein test</code> and <code>cider-run-all-tests</code> will work.</p>
<p>So that’s good, but I don’t love is that I <em>must</em> put tests in a separate file. I’ve really grown to like Racket’s submodules, which enable things like putting things in a submodule named <code>test</code>, in the same file as what’s being tested.<sup><a href="#hands-on-with-clojure-footnote-2-definition" name="hands-on-with-clojure-footnote-2-return">2</a></sup></p>
<h1 id="code">Code</h1>
<p>Here’s what I ended up with right before it was time to head out for dinner:</p>
<div class="brush: clj">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="kd">ns </span><span class="nv">my-lib.core</span>
<span class="p">(</span><span class="ss">:require</span> <span class="p">[</span><span class="nv">clojure.test</span> <span class="ss">:refer</span> <span class="ss">:all</span><span class="p">]))</span>
<span class="c1">;; A (probably dubious) implementation of penultimate.</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">penultimate-dubious</span> <span class="p">[</span><span class="nv">xs</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[[</span><span class="nv">x</span> <span class="o">&</span> <span class="nv">xs</span><span class="p">]</span> <span class="nv">xs</span><span class="p">]</span>
<span class="p">(</span><span class="k">if </span><span class="nv">xs</span>
<span class="p">(</span><span class="k">let </span><span class="p">[[</span><span class="nv">y</span> <span class="o">&</span> <span class="nv">xs</span><span class="p">]</span> <span class="nv">xs</span><span class="p">]</span>
<span class="p">(</span><span class="nb">if-not </span><span class="nv">xs</span>
<span class="c1">;; y is last, therefore x must be next-to-last</span>
<span class="nv">x</span>
<span class="c1">;; keep trying</span>
<span class="p">(</span><span class="nf">recur</span> <span class="p">(</span><span class="nb">cons </span><span class="nv">y</span> <span class="nv">xs</span><span class="p">))))</span>
<span class="nv">nil</span><span class="p">)))</span>
<span class="p">(</span><span class="kd">defn </span><span class="nv">penultimate</span> <span class="p">[</span><span class="nv">xs</span><span class="p">]</span>
<span class="p">(</span><span class="k">let </span><span class="p">[[</span><span class="nv">x</span> <span class="o">&</span> <span class="p">[</span><span class="nv">y</span> <span class="o">&</span> <span class="nv">ys</span><span class="p">]]</span> <span class="nv">xs</span><span class="p">]</span>
<span class="p">(</span><span class="k">if </span><span class="nv">ys</span>
<span class="p">(</span><span class="nf">recur</span> <span class="p">(</span><span class="nb">cons </span><span class="nv">y</span> <span class="nv">ys</span><span class="p">))</span>
<span class="p">(</span><span class="nb">and </span><span class="nv">y</span> <span class="nv">x</span><span class="p">))))</span>
<span class="c1">;; See ../test/my_lib/core_test.clj for tests. Unfortunately nothing</span>
<span class="c1">;; like Racket's test submodules.</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Although <code>penultimate</code> seems OK to me, I’m curious about other ways to express that in Clojure.</p>
<h1 id="conclusions-and-next-steps">Conclusions and next steps</h1>
<p>What conclusions can we draw from all this? Nothing too important. This is just a brain dump of my experience and opinions. It’s probably fair to say that the “onboarding” for Racket is simpler, in terms of getting a working development environment, writing and evaluating code. Beyond that, I have some knee-jerk reactions that probably don’t mean much.</p>
<p>The 4clojure.com problems are great, and I will work on more today. However I’m already feeling the urge to pick some small “real” project, and work on that instead of (or at least in addition to) problem sets. The trick will be to pick something that’s not too big or small to tackle.</p>
<div class="footnotes">
<ol>
<li id="hands-on-with-clojure-footnote-1-definition" class="footnote-definition">
<p>Also,
<kbd>C-c C-x</kbd> “refreshes” REPL with code from current buffer. At the moment this feels like a distinction without a difference. <a href="#hands-on-with-clojure-footnote-1-return">↩</a></p></li>
<li id="hands-on-with-clojure-footnote-2-definition" class="footnote-definition">
<p>To clarify, the neat thing about a <code>test</code> <em>submodule</em> in Racket is that it exists at “test time”. In other words, the submodule can live in the same source file, and access its surrounding parent module. But it is not loaded or evaluated at run time — just at “test time”. Likewise you can use submodules for “documentation time” or other concepts. <a href="#hands-on-with-clojure-footnote-2-return">↩</a></p></li></ol></div>
<footer></footer></article></div>The ~> Threading Macrourn:www-greghendershott-com:-2013-05-threading-macro.html2013-05-24T14:02:55Z2013-05-24T14:02:55ZGreg Hendershott
<div>
<article>
<header>
<h1>The ~> Threading Macro</h1>
<p class="date-and-tags">
<time datetime="2013-05-24" pubdate="true">2013-05-24</time> :: <span class="tags"><a href="/tags/Racket.html">Racket</a>, <a href="/tags/Clojure.html">Clojure</a>, <a href="/tags/macros.html">macros</a></span></p></header>
<p>Although I prefer Racket, there are a few idioms from Clojure I like. I’m particularly infatuated with the threading macros, <code>-></code> and <code>->></code>.</p>
<p>I was surprised how little documentation I could find for these. So although I’m writing this for Racketeers, it’s possible a few Clojure folks might find it interesting, too.</p><!-- more-->
<blockquote>
<p>Note: You can use them in Racket with Asumu Takikawa’s <a href="https://github.com/takikawa/racket-clojure">#lang clojure</a> or my own <a href="https://github.com/greghendershott/rackjure">#lang rackjure</a>. In the latter, the threading macros are named <code>~></code> and <code>~>></code> (using a <code>~</code> instead of a <code>-</code>) so as not to conflict with Racket’s use of <code>-></code> for contracts. I’ll use the <code>~></code> names in this blog post.</p></blockquote>
<p>First, the threading macros have nothing to do with concurrency. Instead, the macros “thread” values through a chain of function calls.</p>
<p>Here’s a simple example. You might write code like this:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/Writing.html#(def._((lib._racket/private/misc..rkt)._displayln))" style="color: inherit">displayln</a></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/bytestrings.html#(def._((quote._~23~25kernel)._bytes-append))" style="color: inherit">bytes-append</a></span> <span class="p">(</span><span class="n">bytes->hex-string</span> <span class="p">(</span><span class="n">sha1</span> <span class="n">in</span><span class="p">))</span> <span class="s2">#"."</span><span class="p">))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>The nesting makes it difficult to see the data flow. You need to read not just right-to-left, but also inside-out. And although we Racketeers don’t exactly hate parentheses, there are an awful lot of them.</p>
<p>Here’s that with the <code>~></code> “thread first” macro:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="n">~></span> <span class="p">(</span><span class="n">sha1</span> <span class="n">in</span><span class="p">)</span>
<span class="p">(</span><span class="n">bytes->hex-string</span><span class="p">)</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/bytestrings.html#(def._((quote._~23~25kernel)._bytes-append))" style="color: inherit">bytes-append</a></span> <span class="s2">#"."</span><span class="p">)</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/Writing.html#(def._((lib._racket/private/misc..rkt)._displayln))" style="color: inherit">displayln</a></span><span class="p">))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>It expands into <em>exactly</em> the same code as above, but is easier to read as a series of transformations. The <code>~></code> macro takes the result of each function, and “threads” it in as the first argument to the next function.</p>
<p>In Clojure, <code>,</code> is whitespace so you can use it as a visual reminder where the argument is being inserted:</p>
<div class="brush: clojure">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="nb">-> </span><span class="p">(</span><span class="nf">sha1</span> <span class="nv">in</span><span class="p">)</span>
<span class="p">(</span><span class="nf">bytes->hex-string</span> ,<span class="p">)</span>
<span class="p">(</span><span class="nf">bytes-append</span> , <span class="o">#</span><span class="s">"."</span><span class="p">)</span>
<span class="p">(</span><span class="nf">displayln</span> ,<span class="p">))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>In Racket, I suppose you could use comments like so:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="n">~></span> <span class="p">(</span><span class="n">sha1</span> <span class="n">in</span><span class="p">)</span>
<span class="p">(</span><span class="n">bytes->hex-string</span> <span class="cm">#||#</span><span class="p">)</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/bytestrings.html#(def._((quote._~23~25kernel)._bytes-append))" style="color: inherit">bytes-append</a></span> <span class="cm">#||#</span> <span class="s2">#"."</span><span class="p">)</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/Writing.html#(def._((lib._racket/private/misc..rkt)._displayln))" style="color: inherit">displayln</a></span> <span class="cm">#||#</span><span class="p">))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>But that’s a bit noisy. I’ve found that with a little experience you don’t really need that. It’s fine to say:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="n">~></span> <span class="p">(</span><span class="n">sha1</span> <span class="n">in</span><span class="p">)</span>
<span class="p">(</span><span class="n">bytes->hex-string</span><span class="p">)</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/bytestrings.html#(def._((quote._~23~25kernel)._bytes-append))" style="color: inherit">bytes-append</a></span> <span class="s2">#"."</span><span class="p">)</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/Writing.html#(def._((lib._racket/private/misc..rkt)._displayln))" style="color: inherit">displayln</a></span><span class="p">))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Plus, when any function takes just one argument, you can omit the parentheses:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="n">~></span> <span class="p">(</span><span class="n">sha1</span> <span class="n">in</span><span class="p">)</span>
<span class="n">bytes->hex-string</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/bytestrings.html#(def._((quote._~23~25kernel)._bytes-append))" style="color: inherit">bytes-append</a></span> <span class="s2">#"."</span><span class="p">)</span>
<span class="nb"><a href="http://docs.racket-lang.org/reference/Writing.html#(def._((lib._racket/private/misc..rkt)._displayln))" style="color: inherit">displayln</a></span><span class="p">)</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>That’s really clean.</p>
<p>For certain programs it’s really helpful to use this style, which emphasizes data flow. If you’re nesting functions with <code>-></code> in the name, like <code>port->string</code> or <code>string->bytes/utf-8</code> — or functions that <em>could</em> be named that way — that’s often a good indicator that <code>~></code> might be natural.</p>
<p>As I searched for more information about the threading macro, I came across the idea of <a href="https://en.wikipedia.org/wiki/Concatenative_programming_language">concatenative programming</a>. Think of Unix command line pipes. Think of postfix HP calculators. Think of postfix languages like Forth, including newer ones like Cat.</p>
<p>If we Lispers can accept prefix notation, why not postfix? Like extremists on opposite sides of an issue, we actually have more in common with each other, than with the infix majority — because we care more than they do. (I am mostly joking, but not entirely.)</p>
<p>However you view it, the emphasis is on a chain of transformations. Maybe it’s my background in music and audio processing, but I find that a very clear and natural way to think about many problems.</p>
<h2 id="caveat-dont-forget-its-a-macro">Caveat: Don’t forget it’s a <em>macro</em></h2>
<p>Occasionally you’ll have some function that <em>doesn’t</em> take the interesting bit as the first argument:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="n">~></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span> <span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/private/map..rkt)._map))" style="color: inherit">map</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._add1))" style="color: inherit">add1</a></span><span class="p">)</span> <span class="c1">;; won't work: map takes the list as the last argument</span>
<span class="nb"><a href="http://docs.racket-lang.org/reference/Writing.html#(def._((lib._racket/private/misc..rkt)._displayln))" style="color: inherit">displayln</a></span><span class="p">)</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>You might think, whatevs, I’ll just supply an anonymous function that takes a list as the first argument. I’ll use <code>(lambda (xs) (map add1
xs))</code> or more elegantly <code>(curry map add1)</code>:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="n">~></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span> <span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">(</span><span class="k"><a href="http://docs.racket-lang.org/reference/lambda.html#(form._((lib._racket/private/base..rkt)._lambda))" style="color: inherit">lambda</a></span> <span class="p">(</span><span class="n">xs</span><span class="p">)</span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/private/map..rkt)._map))" style="color: inherit">map</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._add1))" style="color: inherit">add1</a></span> <span class="n">xs</span><span class="p">))</span> <span class="c1">;; won't work!</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/procedures.html#(def._((lib._racket/function..rkt)._curry))" style="color: inherit">curry</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/private/map..rkt)._map))" style="color: inherit">map</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._add1))" style="color: inherit">add1</a></span><span class="p">)</span> <span class="c1">;; won't work!</span>
<span class="nb"><a href="http://docs.racket-lang.org/reference/Writing.html#(def._((lib._racket/private/misc..rkt)._displayln))" style="color: inherit">displayln</a></span><span class="p">)</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Why won’t this work? Because <code>~></code> is a <em>macro</em> and it expands to:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span> <span class="p">(</span><span class="k"><a href="http://docs.racket-lang.org/reference/lambda.html#(form._((lib._racket/private/base..rkt)._lambda))" style="color: inherit">lambda</a></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span> <span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">)</span> <span class="p">(</span><span class="n">xs</span><span class="p">)</span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/private/map..rkt)._map))" style="color: inherit">map</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._add1))" style="color: inherit">add1</a></span> <span class="n">xs</span><span class="p">))</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/procedures.html#(def._((lib._racket/function..rkt)._curry))" style="color: inherit">curry</a></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span> <span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">)</span> <span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/private/map..rkt)._map))" style="color: inherit">map</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._add1))" style="color: inherit">add1</a></span><span class="p">)</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Unfortunately in such a situation you’ll have to define the function outside the <code>~></code> macro: <sup><a href="#threading-macro-footnote-1-definition" name="threading-macro-footnote-1-return">1</a></sup></p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2
3
4</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="k"><a href="http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))" style="color: inherit">define</a></span> <span class="n">map-add1</span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/procedures.html#(def._((lib._racket/function..rkt)._curry))" style="color: inherit">curry</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/private/map..rkt)._map))" style="color: inherit">map</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/generic-numbers.html#(def._((quote._~23~25kernel)._add1))" style="color: inherit">add1</a></span><span class="p">))</span>
<span class="p">(</span><span class="n">~></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((quote._~23~25kernel)._list))" style="color: inherit">list</a></span> <span class="mi">1</span> <span class="mi">2</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">map-add1</span>
<span class="nb"><a href="http://docs.racket-lang.org/reference/Writing.html#(def._((lib._racket/private/misc..rkt)._displayln))" style="color: inherit">displayln</a></span><span class="p">)</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>The main take-away is that <code>~></code> is a syntax transform, not a function application. Often you won’t need to care, but sometimes it will matter.</p>
<h2 id="thrush-combinator">Thrush Combinator</h2>
<p>Sometimes the threading macros are referred to as the thrush combinator. They’re not, really, because the threading macros are <em>macros</em>, as we just saw. What they do have in common is specifying the functions in order of execution: in data-flow order. In contrast <code>compose</code> specifies the function in math notation order, the same order in which we’d write them as nested expressions.</p>
<p>In other words, one implementation of thrush is simply:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="k"><a href="http://docs.racket-lang.org/reference/define.html#(form._((lib._racket/private/base..rkt)._define))" style="color: inherit">define</a></span> <span class="p">(</span><span class="n">thrush</span> <span class="o">.</span> <span class="n">fs</span><span class="p">)</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/procedures.html#(def._((lib._racket/private/base..rkt)._apply))" style="color: inherit">apply</a></span> <span class="nb"><a href="http://docs.racket-lang.org/reference/procedures.html#(def._((lib._racket/private/list..rkt)._compose))" style="color: inherit">compose</a></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/pairs.html#(def._((lib._racket/private/list..rkt)._reverse))" style="color: inherit">reverse</a></span> <span class="n">fs</span><span class="p">)))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Here are the permutations:</p>
<pre><code>Argument Order Function Macro
-------------- -------- -----
Math Notation compose
Data Flow thrush ~></code></pre>
<h2 id="combined-with-applicable-dicts">Combined with applicable dicts</h2>
<p>Sometimes I need to deal with JSON. Often this has nested object literals, <em>a.k.a.</em> hash tables <em>a.k.a.</em> dictionaries.</p>
<p>This can be a bit painful in Racket:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/dicts.html#(def._((lib._racket/dict..rkt)._dict-ref))" style="color: inherit">dict-ref</a></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/dicts.html#(def._((lib._racket/dict..rkt)._dict-ref))" style="color: inherit">dict-ref</a></span> <span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/dicts.html#(def._((lib._racket/dict..rkt)._dict-ref))" style="color: inherit">dict-ref</a></span> <span class="n">some-dict</span> <span class="o">'</span><span class="ss">a</span><span class="p">)</span> <span class="o">'</span><span class="ss">b</span><span class="p">)</span> <span class="o">'</span><span class="ss">c</span><span class="p">)</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>But <a href="https://github.com/greghendershott/rackjure">#lang rackjure</a> has applicable dicts. When an application form has two elements, and the second one is a <code>dict?</code>, then this expands to a <code>dict-ref</code> using the first element as the key:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1
2</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="o">'</span><span class="ss">a</span> <span class="n">d</span><span class="p">)</span> <span class="c1">;; when (dict? d) is #t, expands to...</span>
<span class="p">(</span><span class="nb"><a href="http://docs.racket-lang.org/reference/dicts.html#(def._((lib._racket/dict..rkt)._dict-ref))" style="color: inherit">dict-ref</a></span> <span class="n">d</span> <span class="o">'</span><span class="ss">a</span><span class="p">)</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>As a result, that nested <code>dict-ref</code> example can be written as:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="o">'</span><span class="ss">c</span> <span class="p">(</span><span class="o">'</span><span class="ss">b</span> <span class="p">(</span><span class="o">'</span><span class="ss">a</span> <span class="n">some-dict</span><span class="p">)))</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>That’s a bit shorter, but of course this feels “backwards and inside-out”. So, <code>~></code> to the rescue:</p>
<div class="brush: racket">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="p">(</span><span class="n">~></span> <span class="n">some-dict</span> <span class="o">'</span><span class="ss">a</span> <span class="o">'</span><span class="ss">b</span> <span class="o">'</span><span class="ss">c</span><span class="p">)</span>
</pre></div>
</td></tr></tbody></table>
</div>
<p>Which is a very natural way to do a nested dict reference, and very similar to JavaScript’s:</p>
<div class="brush: javascript">
<table class="sourcetable">
<tbody>
<tr>
<td class="linenos">
<div class="linenodiv">
<pre>1</pre></div></td>
<td class="code">
<div class="source">
<pre><span></span><span class="nx">some_dict</span><span class="p">.</span><span class="nx">a</span><span class="p">.</span><span class="nx">b</span><span class="p">.</span><span class="nx">c</span><span class="p">;</span>
</pre></div>
</td></tr></tbody></table>
</div>
<h1 id="welcome-your-feedback">Welcome your feedback</h1>
<p>Those are my reflections on the threading macros. If you’d like to add something, or correct some horrible misunderstanding, feel free to leave a comment.</p>
<div class="footnotes">
<ol>
<li id="threading-macro-footnote-1-definition" class="footnote-definition">
<p>Update: Although it’s a bit ugly, you could also wrap any function-creating expressions with an extra set of parens. For example, <code>((lambda (xs) (map add1 xs)))</code> and <code>((curry map add1))</code>. <a href="#threading-macro-footnote-1-return">↩</a></p></li></ol></div>
<footer></footer></article></div>