<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[loganlinn.log]]></title>
  <link href="http://loganlinn.com/atom.xml" rel="self"/>
  <link href="http://loganlinn.com/"/>
  <updated>2016-03-02T09:39:41-08:00</updated>
  <id>http://loganlinn.com/</id>
  <author>
    <name><![CDATA[Logan Linn]]></name>
    <email><![CDATA[logan@loganlinn.com]]></email>
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[The Immutable Frontend in ClojureScript]]></title>
    <link href="http://loganlinn.com/blog/2015/01/05/the-immutable-frontend-in-clojurescript/"/>
    <updated>2015-01-05T15:59:51-08:00</updated>
    <id>http://loganlinn.com/blog/2015/01/05/the-immutable-frontend-in-clojurescript</id>
    <content type="html"><![CDATA[<p><a href="http://www.infoq.com/presentations/immutable-frontend-clojurescript"><img class="right" src="http://loganlinn.com/images/loganlinn-qcon2015.jpg" width="400"></a></p>

<p>The <a href="http://www.infoq.com/presentations/immutable-frontend-clojurescript">video</a> is up for a talk I gave at QCon 2014 in which I spoke about
the benefits of using immutable data to build a frontend and my experience
working on <a href="http://getprismatic.com">Prismatic&rsquo;s</a> ClojureScript web application.</p>

<p><a href="http://www.infoq.com/presentations/immutable-frontend-clojurescript">Watch on InfoQ</a></p>

<p>Brief summary: Mutable state is ubiquitous in UI programming and contributes to
large amounts of additional complexity and bugs.
Functional reactive data-flows can greatly simplify rendering logic and state
management.
ClojureScript&rsquo;s immutable data structures pair nicely with
<a href="https://facebook.github.io/react/">React.js</a> to build UIs that are declarative
and performant.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Advanced Compilation, Externs, and Window]]></title>
    <link href="http://loganlinn.com/blog/2014/06/01/advanced-compilation-and-window/"/>
    <updated>2014-06-01T03:28:30-07:00</updated>
    <id>http://loganlinn.com/blog/2014/06/01/advanced-compilation-and-window</id>
    <content type="html"><![CDATA[<p>Fixing issues that arise from advanced compilation mode with the
<a href="https://developers.google.com/closure/compiler/">Closure</a> compiler
can be tricky to debug.
Referencing external libraries can be troublesome when the compiler
doesn&rsquo;t know about them.
However, even when
<a href="https://developers.google.com/closure/compiler/docs/api-tutorial3">externs</a>
are
<a href="http://swannodette.github.io/2014/03/14/externs-got-you-down/">sucessfully configured</a>,
things can go wrong&hellip; Here&rsquo;s a quick lesson learned from fixing a function that
my coworker identified as broken after advanced compilation was used.</p>

<p>This function uses the
<a href="https://github.com/twitter/twitter-text-js">twitter-text-js</a>
library to count the length of a tweet after link shortening by calling
<code>twttr.txt.getTweetLength()</code>. Our misbehaving ClojureScript function looked
something like:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">tweet-length</span> <span class="p">[</span><span class="nv">text</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">if </span><span class="o">^</span><span class="nb">boolean </span><span class="p">(</span><span class="nf">.-twttr</span> <span class="nv">js/window</span><span class="p">)</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">.. </span><span class="nv">js/window</span> <span class="nv">-twttr</span> <span class="nv">-txt</span> <span class="p">(</span><span class="nf">getTweetLength</span> <span class="nv">text</span><span class="p">))</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">count </span><span class="nv">text</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Pretty straight forward: it checks to see if the external script has been
before calling a function that it provides or falls back counting the number of
characters.</p>

<p>Let&rsquo;s see what the advanced compiled code looks like:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="nb">window</span><span class="p">.</span><span class="nx">de</span> <span class="o">?</span> <span class="nb">window</span><span class="p">.</span><span class="nx">de</span><span class="p">.</span><span class="nx">txt</span><span class="p">.</span><span class="nx">getTweetLength</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span> <span class="o">:</span> <span class="nx">M</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Spot the issue? The optimizer munged <code>window.twttr</code> to <code>window.de</code> even though
the extern was configured.</p>

<p>As you may have guessed by now,
<strong>externs are not recognized when referenced through <code>window</code></strong>.
It makes sense, but may not be immediately obvious, especially when debugging.
After all, this function only broke after advanced compilation was used.</p>

<p>The fixed function:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">tweet-length</span> <span class="p">[</span><span class="nv">text</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nf">exists?</span> <span class="nv">js/twttr</span><span class="p">)</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">.. </span><span class="nv">js/twttr</span> <span class="nv">-txt</span> <span class="p">(</span><span class="nf">getTweetLength</span> <span class="nv">text</span><span class="p">))</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">count </span><span class="nv">text</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note the use of <code>cljs.core/exists?</code>, to test whether a variable exists. Simply
testing <code>js/twttr</code> as boolean when the script hasn&rsquo;t been evaluated would throw
a <code>ReferenceError</code>.</p>

<p>The advanced compiled result looks like:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="kd">function</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="s2">&quot;undefined&quot;</span> <span class="o">!==</span> <span class="nx">twttr</span> <span class="o">?</span> <span class="nx">twttr</span><span class="p">.</span><span class="nx">txt</span><span class="p">.</span><span class="nx">getTweetLength</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span> <span class="o">:</span> <span class="nx">M</span><span class="p">(</span><span class="nx">a</span><span class="p">)</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p><em>Lessons learned</em>: reference externs directly from <code>js/</code> and, more generally,
avoid <code>js/window</code> unless you&rsquo;re accessing one of its actual properties.</p>

<p>Using the advanced compiler can bring tons of little nuances like this. They&rsquo;re
often things you may not consider when writing code and thinking about its
correctness.</p>

<p>In the end, a rather simple lesson learned, but hopefully it&rsquo;s helpful to others.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Push State and ClojureScript]]></title>
    <link href="http://loganlinn.com/blog/2014/05/26/push-state-and-clojurescript/"/>
    <updated>2014-05-26T16:48:07-07:00</updated>
    <id>http://loganlinn.com/blog/2014/05/26/push-state-and-clojurescript</id>
    <content type="html"><![CDATA[<p>A
<a href="http://stackoverflow.com/questions/16731271/history-pushstate-and-scroll-position">common problem</a>
people have when using HTML5 pushState is that previous scroll
positions are not always restored when navigating back. A strategy to address
this is to store the current scroll position in <code>history.state</code> before navigating
forward.</p>

<p>Let&rsquo;s see how you might use the Push State API directly to accomplish this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">declare </span><span class="nv">scroll-top</span><span class="p">)</span> <span class="c1">;; returns current scroll position</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">set-scroll-top!</span> <span class="p">[]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">state</span> <span class="p">(</span><span class="nb">or </span><span class="p">(</span><span class="nf">.-state</span> <span class="nv">js/history</span><span class="p">)</span> <span class="o">#</span><span class="nv">js</span> <span class="p">{})]</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">aset </span><span class="nv">state</span> <span class="s">&quot;scroll-top&quot;</span> <span class="p">(</span><span class="nf">scroll-top</span><span class="p">))</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">.replaceState</span> <span class="nv">js/history</span> <span class="nv">state</span> <span class="p">)))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">get-scroll-top</span> <span class="p">[]</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">when-let </span><span class="p">[</span><span class="nv">state</span> <span class="p">(</span><span class="nf">.-state</span> <span class="nv">js/history</span><span class="p">)]</span>
</span><span class='line'>    <span class="p">(</span><span class="nb">aget </span><span class="nv">state</span> <span class="s">&quot;scroll-top&quot;</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>This works, but what if we could interface with <code>history.state</code> as if it were
Clojure data?
More specifically, <em>stateful</em> Clojure data, like an <code>atom</code>.</p>

<p>It might look something like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">require</span> <span class="ss">&#39;history</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="kd">declare </span><span class="nv">scroll-top</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">set-scroll-top!</span> <span class="p">[]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">swap!</span> <span class="nv">history/state</span> <span class="nb">assoc </span><span class="ss">:scroll-top</span> <span class="p">(</span><span class="nf">scroll-top</span><span class="p">)))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">get-scroll-top</span> <span class="p">[]</span>
</span><span class='line'>  <span class="p">(</span><span class="ss">:scroll-top</span> <span class="err">@</span><span class="nv">history/state</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>And in fact, this can be done by anonymously implementing a few protocols.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">ns </span><span class="nv">history</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">state</span>
</span><span class='line'>  <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">clj-state</span> <span class="o">#</span><span class="p">(</span><span class="nf">js-&gt;clj</span> <span class="p">(</span><span class="nf">.-state</span> <span class="nv">js/history</span><span class="p">)</span> <span class="ss">:keywordize-keys</span> <span class="nv">true</span><span class="p">)]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">reify</span>
</span><span class='line'>      <span class="nv">IDeref</span>
</span><span class='line'>      <span class="p">(</span><span class="nf">-deref</span> <span class="p">[</span><span class="nv">_</span><span class="p">]</span>
</span><span class='line'>        <span class="p">(</span><span class="nf">clj-state</span><span class="p">))</span>
</span><span class='line'>      <span class="nv">IReset</span>
</span><span class='line'>      <span class="p">(</span><span class="nf">-reset!</span> <span class="p">[</span><span class="nv">_</span> <span class="nv">v</span><span class="p">]</span>
</span><span class='line'>        <span class="p">(</span><span class="nf">.replaceState</span> <span class="nv">js/history</span> <span class="p">(</span><span class="nf">clj-&gt;js</span> <span class="nv">v</span><span class="p">)</span> <span class="p">(</span><span class="nf">.-title</span> <span class="nv">js/document</span><span class="p">)))</span>
</span><span class='line'>      <span class="nv">ISwap</span>
</span><span class='line'>      <span class="p">(</span><span class="nf">-swap!</span> <span class="p">[</span><span class="nv">s</span> <span class="nv">f</span><span class="p">]</span>
</span><span class='line'>        <span class="p">(</span><span class="nf">-reset!</span> <span class="nv">s</span> <span class="p">(</span><span class="nf">f</span> <span class="p">(</span><span class="nf">clj-state</span><span class="p">))))</span>
</span><span class='line'>      <span class="p">(</span><span class="nf">-swap!</span> <span class="p">[</span><span class="nv">s</span> <span class="nv">f</span> <span class="nv">x</span><span class="p">]</span>
</span><span class='line'>        <span class="p">(</span><span class="nf">-reset!</span> <span class="nv">s</span> <span class="p">(</span><span class="nf">f</span> <span class="p">(</span><span class="nf">clj-state</span><span class="p">)</span> <span class="nv">x</span><span class="p">)))</span>
</span><span class='line'>      <span class="p">(</span><span class="nf">-swap!</span> <span class="p">[</span><span class="nv">s</span> <span class="nv">f</span> <span class="nv">x</span> <span class="nv">y</span><span class="p">]</span>
</span><span class='line'>        <span class="p">(</span><span class="nf">-reset!</span> <span class="nv">s</span> <span class="p">(</span><span class="nf">f</span> <span class="p">(</span><span class="nf">clj-state</span><span class="p">)</span> <span class="nv">x</span> <span class="nv">y</span><span class="p">)))</span>
</span><span class='line'>      <span class="p">(</span><span class="nf">-swap!</span> <span class="p">[</span><span class="nv">s</span> <span class="nv">f</span> <span class="nv">x</span> <span class="nv">y</span> <span class="nv">more</span><span class="p">]</span>
</span><span class='line'>        <span class="p">(</span><span class="nf">-reset!</span> <span class="nv">s</span> <span class="p">(</span><span class="nb">apply </span><span class="nv">f</span> <span class="p">(</span><span class="nf">clj-state</span><span class="p">)</span> <span class="nv">x</span> <span class="nv">y</span> <span class="nv">more</span><span class="p">))))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can checkout a more complete wrapper for <code>js/history</code> here: <a href="https://gist.github.com/loganlinn/930c043331c52cb73a98">https://gist.github.com/loganlinn/930c043331c52cb73a98</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Clojure/conj 2013]]></title>
    <link href="http://loganlinn.com/blog/2013/11/18/clojureconj-2013/"/>
    <updated>2013-11-18T08:49:00-08:00</updated>
    <id>http://loganlinn.com/blog/2013/11/18/clojureconj-2013</id>
    <content type="html"><![CDATA[<p>I attended <a href="http://clojure-conj.org">Clojure/conj</a> this past weekend and I wanted to recollect my
notes and thoughts on my experience to share with others who weren&rsquo;t able to
make it.</p>

<h2>The Venue</h2>

<p><img class="right" src="http://loganlinn.com/images/clojure_conj_intro.jpg" width="281" height="375"></p>

<p>The event was held at the <a href="http://en.wikipedia.org/wiki/George_Washington_Masonic_National_Memorial">George Washington Masonic Memorial</a>, a landmark
structure in Alexandria, Virginia.</p>

<p>Being a VA native, I have seen this building several times while visiting the
area, but never been inside.  Even if I had, I don&rsquo;t think I&rsquo;d imagine that it
would be a good spot to host a programming conference, but it was.</p>

<p>There was a single stage with theater seating &mdash; the acoustics were great and
it seemed to be exactly the right size.  Not being able to bring food &amp; drinks
(i.e., caffeine) into the theater might have been the only inconvenience, but
totally understandable given the significance of the building.</p>

<h2>The Talks</h2>

<h3>Day 1</h3>

<h4>Stu Halloway &ndash; data.fresssian</h4>

<p><a href="https://twitter.com/stuarthalloway">Stu</a> kicked off the conference by introducing a new data encoding library,
<a href="https://github.com/clojure/data.fressian">data.fressian</a> (pronounced &lsquo;data freshen&rsquo;), that&rsquo;s being used
in <a href="http://www.datomic.com/">Datomic</a> to efficiently transport data across the
wire.
It has similar properties to <a href="https://github.com/edn-format/edn">edn</a>, like
being language agnostic, self-describing, and extensible, but in a binary
format. The library&rsquo;s designers prioritized effectiveness before efficiency to
avoid common idiosyncrasies found in existing (JVM) serialization libraries.</p>

<p>Checkout the <a href="https://github.com/clojure/data.fressian">clojure implementation</a> and the
<a href="https://github.com/Datomic/fressian/wiki">format&rsquo;s wiki</a> for more information.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=JArZqMqsaB0">video</a></p>

<h4>Mike Anderson &ndash; Enter the Matrix</h4>

<p><a href="https://twitter.com/mikera">Mike</a> presented the <a href="https://github.com/mikera/core.matrix">core.matrix</a> API for matrix and vector
operations. At its core, the library is a set of protocols that abstract both
fundamental (required) and composite (optional) matrix operations. This allows
any of the existing matrix libraries for the JVM to be leveraged.
By providing a standard API to build applications and leaving the underlying
matrix engine pluggable, you can optimize for your particular use case.
Since this is all protocol-based, you could conceivably mix &amp; match them.</p>

<p>core.matrix currently ships with protocol extensions for vectorz-clj, Clatrix
and NDArray.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=_h9TLJtjSJo">video</a></p>

<h4>Aria Haghighi &ndash; Prismatic&rsquo;s Schema</h4>

<p>I was looking forward to this talk about <a href="https://github.com/Prismatic/schema">Prismatic&rsquo;s schema library</a>,
as I had seen their <a href="http://blog.getprismatic.com/blog/2013/9/4/schema-for-clojurescript-data-shape-declaration-and-validation">introductory blog post</a> and started playing
with it on my flight to the conference.  Schema, as its name suggests, is a way
to describe and validate the shape of your data.</p>

<p><a href="https://twitter.com/aria42">Aria</a> made the point that while it&rsquo;s certainly possible to infer the
shape of data from the source code or convey with a docstrings, it&rsquo;s time
consuming and imprecise even in the most trivial of situations. Schemas provide
a clear and concise documentation that is non-intrusive like static type systems
can be. He reported noticeable productivity gains at Prismatic from using
schema and simple code organizational patterns.</p>

<p>Aside from documentation and validation, he mentioned that schemas can be used
for other interesting things, like code generation. Prismatic has been using
schema to generate ObjectiveC and Java code for their mobile iOS and Android
applications, which they plan to open source in the future.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=o_jtwIs2Ot8">video</a></p>

<h4>Chris Houser &amp; Jonathan Claggett &ndash; Illuminated Macros</h4>

<p>Clojure has powerful macro facilities, but they can be hard to debug when something
goes wrong. There aren&rsquo;t many ways for a macro&rsquo;s author to provide fine-grained
error messages or suggested fixes. And aside from <code>macroexpand</code>, macros
can be opaque to inspect and understand.</p>

<p><a href="https://twitter.com/chrishouser">Chouser</a> presented properties of macros and ideas that we can
learn from (good parts of) Racket&rsquo;s macros. And <a href="https://twitter.com/metagorm">Jonathan</a> demoed
<a href="https://github.com/jclaggett/seqex">seqex</a>, a library for &ldquo;s-expression regexes.&rdquo;</p>

<p>He gave examples by describing the syntax for the <code>let</code> macro that provides helpful
feedback to users and even generates a colorized, complete syntax specification.
It would be really cool to see some of these patterns become popular in Clojure.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=o75g9ZRoLaw">video</a></p>

<h4>Nada Amin &amp; William Byrd &ndash; From Greek to Clojure!</h4>

<p>A talk that was equally entertaining as it was thought-provoking;
<a href="https://twitter.com/nadamin">Nada</a> and <a href="https://twitter.com/webyrd">Will</a> gave tips on how to approach technical
research papers with some live coding using <a href="https://github.com/clojure/core.logic">core.logic</a>.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=7kPMFkNm2dw">video</a></p>

<h4>Rich Hickey &ndash; Harmonikit</h4>

<p><a href="https://twitter.com/richhickey">Rich</a> took a break from his normal keynote this year to present a
&ldquo;fun&rdquo; project that he&rsquo;s been working on. Harmonikit is a musical synthesis
system built with <a href="https://github.com/clojure/core.async">core.async</a>, <a href="http://overtone.github.io/">overtone</a>,
<a href="http://supercollider.sourceforge.net/">Supercollider</a>, <a href="http://liine.net/en/products/lemur/">Lemur</a> and
a keyboard controller that I didn&rsquo;t catch the name of.</p>

<p>As always, Rich did an exemplary job of dissecting &amp; describing the problem
domain, choosing the distinct problems he wants to address and modeling a system
to do it. This system focused on realistically synthesizing acoustics of harmonic
(e.g., stringed, wind) instruments.</p>

<p>I won&rsquo;t do this project &amp; presentation justice if I continue attempting to
describe it, so I&rsquo;ll spare you (and myself) and let you check out the recording
when it is released.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=bhkdyCPYgLs">video</a></p>

<h3>Day 2</h3>

<h4>Carin Meier &ndash; Learning to Talk to Machines with Speech Acts</h4>

<p><a href="https://twitter.com/carinmeier">Carin</a> is a great presenter and story teller. She told a story
illustrating how computer science can learn from philosophy and vice versa. She
did an awesome demo of a flying drone using <a href="https://github.com/gigasquid/babar">babar</a> and
<a href="https://github.com/gigasquid/clj-drone">clj-drone</a>, and of course, now I want a drone.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=4qFvNMnPX68">video</a></p>

<h4>Timothy Baldridge &ndash; core.async</h4>

<p><a href="https://twitter.com/timbaldridge">Tim</a> skipped talking about the rationale of
<a href="https://github.com/clojure/core.async">core.async</a>, which is well documented <a href="http://clojure.com/blog/2013/06/28/clojure-core-async-channels.html">elsewhere</a>,
and jumped right into the usage and mechanics of the library that has recently
taken the Clojure &amp; ClojureScript communities by storm. I&rsquo;ve been using this
library in a <a href="https://github.com/loganlinn/ji">side-project</a>, yet learned several new things like the
<a href="https://github.com/clojure/core.async/blob/94eed9f2c66de0cad3922b26a48895b5fa12e428/src/main/clojure/clojure/core/async/impl/exec/threadpool.clj#L21">size of the threadpool</a>.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=enwIIGzhahw">video</a></p>

<h4>Michael O&#8217;Keefe &ndash; Predicting Vehicle Usage with Clojure</h4>

<p><a href="https://twitter.com/michaelpokeefe">Michael</a> is a mechanical engineer by trade who has written
several vehicle efficiency simulators
<a href="http://sourceforge.net/projects/adv-vehicle-sim/">in the past</a>, with
his most recent being in Clojure. He had helpful graphics of the simulation model
and totally awesome original illustrations. It seemed that Clojure&rsquo;s
immutable values and ease of building dataflows via functional composition were
significant advantages of this type of system.</p>

<p>Interestingly, he was also the only presenter to use LightTable editor during a
demo (AFAIK), which was very easy to follow (one presenter used vim, and the
rest used emacs)</p>

<p>Update: <a href="http://www.youtube.com/watch?v=joOGH-JtHNY">video</a></p>

<h4>Tim Ewald &ndash; Clojure: Programming with Hand Tools</h4>

<p>This was one of the most memorable talks I&rsquo;ve been to. This wasn&rsquo;t a Clojure or
even programming specific talk. Instead, Tim shed some light on the craft of
woodworking, particularly with hand-tools rather than than power-tools. Here
are a few of the observations he made that I can remember:</p>

<ul>
<li><em>The less you do, the less you know.</em>
Automating a complex process isn&rsquo;t a bad thing in and of itself, but it can
build reliance on machines to do a job; humans begin to lose the skills and
appreciation for the task at hand.</li>
<li><em>Your tools change the way you perceive the world.</em>
This is related to the &ldquo;if all you have is a hammer, everything begins to
look like a nail&rdquo; lesson.  Confirmation bias leads to bad decisions that
don&rsquo;t appear bad on the surface.</li>
<li><em>Simpler tools give you a broader perspective.</em>
You are closer to your trade when your tools allow you (humans) to create
unique items, leaving automation to handle the tasks that never change.
Regardless of how archaic hand-tools may seem, it can be impossible to
match the level of precision that they provide.</li>
</ul>


<p>Looking forward to watching the recording of this talk.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=ShEez0JkOFw">video</a></p>

<h3>Day 3</h3>

<h4>Thomas Kristensen &ndash; Propagators in Clojure</h4>

<p><a href="https://twitter.com/tgkristensen">Thomas</a> presented <a href="https://github.com/tgk/propaganda">propaganda</a>, a library for
declarative logic programming with propagators. It provides similar
functionality as <a href="https://github.com/clojure/core.logic">core.logic</a>, but benefits from a more transparent
processing model and has efficiency benefits for numerical problems.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=JXOOO9MLvhs">video</a></p>

<h4>Fogus &ndash; Zeder: Production Rule Systems in Clojure</h4>

<p>Zeder is a production rule system
(think <a href="http://en.wikipedia.org/wiki/OPS5">OSP5</a>), that <a href="https://twitter.com/fogus">Fogus</a> has been
building.  The syntax was very similar to Datomic&rsquo;s and looks very powerful.
He doesn&rsquo;t appear to have released the source for it yet, but I did find a
<a href="https://gist.github.com/fogus/7118257">gist with an example</a>.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=1E2CoObAaPQ">video</a></p>

<h4>Russ Olsen &ndash; To the Moon!</h4>

<p><a href="https://twitter.com/russolsen">Russ</a> gave a vivid recollection of America&rsquo;s race to
the moon in the 60&rsquo;s and the little and big lessons we can learn from it today.
The talk&rsquo;s theme was centered around one of JFK&rsquo;s quotes, &ldquo;We choose to go to
the moon, not because it&rsquo;s easy, but because it&rsquo;s hard.&rdquo;
As a Millennial, I&rsquo;ve heard and seen this story in popular culture countless
times, but he covered some amazing context &amp; details that most leave out.</p>

<p>Russ is a great speaker and the story was inspirational.
Definitely watch this talk when the video is released.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=4Sso4HtvJsw">video</a></p>

<h3>Last but Not Least</h3>

<p>There were a handful of talks not mentioned here, including
some lightning talks. One of which was from <a href="https://twitter.com/alandipert">Alan Dipert</a> on
<a href="https://github.com/alandipert/gherkin">gherkin</a>, aka bash killer. I also got a kick out of talks by
<a href="https://twitter.com/miner">Steve Miner</a> and <a href="https://twitter.com/amitrathore">Amit Rathore</a>.</p>

<p>Update: <a href="http://www.youtube.com/watch?v=bmHTFo2Rf2w">video</a></p>

<h2>Final Words</h2>

<p>Huge thanks to conference organizers, <a href="https://twitter.com/puredanger">Alex Miller</a> and
<a href="https://twitter.com/lynngrogan">Lynn Grogan</a>, for your hard work and leadership. I am immensely
grateful.</p>

<p>And, of course, thanks to all of the speakers, as well as the sponsors, for
making it happen. For me, this has been a real testament to the authenticity of
the Clojure community.</p>

<p>Lastly, I wanted to give a big thank you to my employer,
<a href="http://www.huddler.com/">Huddler</a>, for sponsoring me to attend this conference
(and letting me work in Clojure).</p>

<p>Until next year!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Dijkstra's Algorithm in Clojure]]></title>
    <link href="http://loganlinn.com/blog/2013/04/22/dijkstras-algorithm-in-clojure/"/>
    <updated>2013-04-22T11:34:00-07:00</updated>
    <id>http://loganlinn.com/blog/2013/04/22/dijkstras-algorithm-in-clojure</id>
    <content type="html"><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Dijkstra's_algorithm">Dijkstra&rsquo;s algorithm</a> is a
simple, yet useful method to find shortest-path to nodes a graph. I found myself
wanting to implement it for a <a href="https://github.com/loganlinn/powergrid">Clojure side project</a>.</p>

<p>Here&rsquo;s a my implementation that I thought was decent enough to share :)</p>

<p>Normally, the algorithm finds the shortest path to all other nodes in graph, but
you can optionally pass a specific destination node. The algorithm will exit early
once destination has been visited.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="o">^</span><span class="ss">:private</span> <span class="nv">inf</span> <span class="nv">Double/POSITIVE_INFINITY</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">update-costs</span>
</span><span class='line'>  <span class="s">&quot;Returns costs updated with any shorter paths found to curr&#39;s unvisisted</span>
</span><span class='line'><span class="s">  neighbors by using curr&#39;s shortest path&quot;</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">g</span> <span class="nv">costs</span> <span class="nv">unvisited</span> <span class="nv">curr</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">curr-cost</span> <span class="p">(</span><span class="nb">get </span><span class="nv">costs</span> <span class="nv">curr</span><span class="p">)]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">reduce-kv</span>
</span><span class='line'>      <span class="p">(</span><span class="k">fn </span><span class="p">[</span><span class="nv">c</span> <span class="nv">nbr</span> <span class="nv">nbr-cost</span><span class="p">]</span>
</span><span class='line'>        <span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nf">unvisited</span> <span class="nv">nbr</span><span class="p">)</span>
</span><span class='line'>          <span class="p">(</span><span class="nf">update-in</span> <span class="nv">c</span> <span class="p">[</span><span class="nv">nbr</span><span class="p">]</span> <span class="nb">min </span><span class="p">(</span><span class="nb">+ </span><span class="nv">curr-cost</span> <span class="nv">nbr-cost</span><span class="p">))</span>
</span><span class='line'>          <span class="nv">c</span><span class="p">))</span>
</span><span class='line'>      <span class="nv">costs</span>
</span><span class='line'>      <span class="p">(</span><span class="nb">get </span><span class="nv">g</span> <span class="nv">curr</span><span class="p">))))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">dijkstra</span>
</span><span class='line'>  <span class="s">&quot;Returns a map of nodes to minimum cost from src using Dijkstra algorithm.</span>
</span><span class='line'><span class="s">  Graph is a map of nodes to map of neighboring nodes and associated cost.</span>
</span><span class='line'><span class="s">  Optionally, specify destination node to return once cost is known&quot;</span>
</span><span class='line'>  <span class="p">([</span><span class="nv">g</span> <span class="nv">src</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">dijkstra</span> <span class="nv">g</span> <span class="nv">src</span> <span class="nv">nil</span><span class="p">))</span>
</span><span class='line'>  <span class="p">([</span><span class="nv">g</span> <span class="nv">src</span> <span class="nv">dst</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="k">loop </span><span class="p">[</span><span class="nv">costs</span> <span class="p">(</span><span class="nb">assoc </span><span class="p">(</span><span class="nb">zipmap </span><span class="p">(</span><span class="nb">keys </span><span class="nv">g</span><span class="p">)</span> <span class="p">(</span><span class="nb">repeat </span><span class="nv">inf</span><span class="p">))</span> <span class="nv">src</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'>           <span class="nv">curr</span> <span class="nv">src</span>
</span><span class='line'>           <span class="nv">unvisited</span> <span class="p">(</span><span class="nb">disj </span><span class="p">(</span><span class="nb">apply hash-set </span><span class="p">(</span><span class="nb">keys </span><span class="nv">g</span><span class="p">))</span> <span class="nv">src</span><span class="p">)]</span>
</span><span class='line'>      <span class="p">(</span><span class="nf">cond</span>
</span><span class='line'>       <span class="p">(</span><span class="nb">= </span><span class="nv">curr</span> <span class="nv">dst</span><span class="p">)</span>
</span><span class='line'>       <span class="p">(</span><span class="nb">select-keys </span><span class="nv">costs</span> <span class="p">[</span><span class="nv">dst</span><span class="p">])</span>
</span><span class='line'>
</span><span class='line'>       <span class="p">(</span><span class="nb">or </span><span class="p">(</span><span class="nf">empty?</span> <span class="nv">unvisited</span><span class="p">)</span> <span class="p">(</span><span class="nb">= </span><span class="nv">inf</span> <span class="p">(</span><span class="nb">get </span><span class="nv">costs</span> <span class="nv">curr</span><span class="p">)))</span>
</span><span class='line'>       <span class="nv">costs</span>
</span><span class='line'>
</span><span class='line'>       <span class="ss">:else</span>
</span><span class='line'>       <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">next-costs</span> <span class="p">(</span><span class="nf">update-costs</span> <span class="nv">g</span> <span class="nv">costs</span> <span class="nv">unvisited</span> <span class="nv">curr</span><span class="p">)</span>
</span><span class='line'>             <span class="nv">next-node</span> <span class="p">(</span><span class="nb">apply min-key </span><span class="nv">next-costs</span> <span class="nv">unvisited</span><span class="p">)]</span>
</span><span class='line'>         <span class="p">(</span><span class="nf">recur</span> <span class="nv">next-costs</span> <span class="nv">next-node</span> <span class="p">(</span><span class="nb">disj </span><span class="nv">unvisited</span> <span class="nv">next-node</span><span class="p">)))))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>It could be used like like so:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="k">def </span><span class="nv">demo-graph</span> <span class="p">{</span><span class="ss">:red</span>    <span class="p">{</span><span class="ss">:green</span> <span class="mi">10</span>, <span class="ss">:blue</span>   <span class="mi">5</span>, <span class="ss">:orange</span> <span class="mi">8</span><span class="p">}</span>,
</span><span class='line'>                 <span class="ss">:green</span>  <span class="p">{</span><span class="ss">:red</span> <span class="mi">10</span>,   <span class="ss">:blue</span>   <span class="mi">3</span><span class="p">}</span>,
</span><span class='line'>                 <span class="ss">:blue</span>   <span class="p">{</span><span class="ss">:green</span> <span class="mi">3</span>,  <span class="ss">:red</span>    <span class="mi">5</span>, <span class="ss">:purple</span> <span class="mi">7</span><span class="p">}</span>,
</span><span class='line'>                 <span class="ss">:purple</span> <span class="p">{</span><span class="ss">:blue</span> <span class="mi">7</span>,   <span class="ss">:orange</span> <span class="mi">2</span><span class="p">}</span>,
</span><span class='line'>                 <span class="ss">:orange</span> <span class="p">{</span><span class="ss">:purple</span> <span class="mi">2</span>, <span class="ss">:red</span>    <span class="mi">2</span><span class="p">}})</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">dijkstra</span> <span class="nv">demo-graph</span> <span class="ss">:red</span><span class="p">)</span>
</span><span class='line'><span class="c1">;; =&gt; {:green 8, :blue 5, :purple 10, :red 0, :orange 8}</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">dijkstra</span> <span class="nv">demo-graph</span> <span class="ss">:red</span> <span class="ss">:purple</span><span class="p">)</span>
</span><span class='line'><span class="c1">;; =&gt; {:purple 10}</span>
</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Other Cache-Control Headers for iOS6 AJAX Caching Bug]]></title>
    <link href="http://loganlinn.com/blog/2012/09/26/another-cache-control-header-for-ios6-ajax-caching-bug/"/>
    <updated>2012-09-26T10:55:00-07:00</updated>
    <id>http://loganlinn.com/blog/2012/09/26/another-cache-control-header-for-ios6-ajax-caching-bug</id>
    <content type="html"><![CDATA[<p>Last week, we discovered that, Safari in iOS 6 will aggressively <a href="http://stackoverflow.com/questions/12506897/is-safari-on-ios-6-caching-ajax-results">cache AJAX POST requests</a>.</p>

<p>There are two popular workarounds that <a href="http://www.devthought.com/2012/09/22/understanding-the-ios6-ajax-bugs/">people</a> have proposed:</p>

<ol>
<li>Make URLs unique by adding query parameter, such as a timestamp,  to the URL.</li>
<li>Set the header, <code>Cache-Control: no-cache</code>, in the response</li>
</ol>


<p>I noticed that some of my POST requests were not getting cached even
though I had neither of these workarounds in place. After some poking
around, <strong>I&rsquo;ve discovered that having <code>must-revalidate</code> or <code>no-store</code>
in the <code>Cache-Control</code> response header also works</strong>.</p>

<p>So, before you go adding <code>no-cache</code> to all of your POST responses, check to
see if <code>must-revalidate</code> or <code>no-store</code> is already getting set.</p>

<p><strong>Update</strong>:</p>

<p>I&rsquo;ve published a simple Sinatra app that helped me test this:
<a href="https://github.com/loganlinn/ajax-post-cache-test">https://github.com/loganlinn/ajax-post-cache-test</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Solving HackerRank Challenges Using Clojure]]></title>
    <link href="http://loganlinn.com/blog/2012/07/30/solving-hackerrank-challenges-with-clojure/"/>
    <updated>2012-07-30T17:43:00-07:00</updated>
    <id>http://loganlinn.com/blog/2012/07/30/solving-hackerrank-challenges-with-clojure</id>
    <content type="html"><![CDATA[<p><em>Disclaimer: This post contains my solution to a HackerRank programming
puzzle. If you wish to solve this problem yourself, please do so before
reading this post! Also, I am still very new to Clojure and functional
programming, so solutions are surely not the most ideal/idiomatic!</em></p>

<p>I recently came across <a href="http://www.hackerrank.com">HackerRank</a>, a site
with programming puzzles, and decided to try one out. The site&rsquo;s still
in a closed beta, so the only challenge that&rsquo;s currently available  is
the &ldquo;SpaceX&rdquo; challenges. The premise is as follows:</p>

<blockquote><p>Emily is an astronaut applying to be a part of the SpaceX project. To
get the job, she must interview with a series of researchers, following
this interview pattern:</p>

<p>She asks a factual question first, which the scientist answers in an
encrypted string. The scientist immediately responds with a different
string that she has to decrypt with the same key as the first. The
encryption algorithm is the same for all questions.</p></blockquote>

<p>Here&rsquo;s the first challenge:</p>

<blockquote><p>Emily: Who is the author of the book Three O&#8217;Clock Dinner?</p>

<p>Scientist: Mrvhsklqh Slqfnqhb</p>

<p>Scientist: I work for &ldquo;hljkw kxqguhg dqg iliwb-wzr&rdquo; hours a year. How
many hours do I work?</p></blockquote>

<p>The first 10,000 challenges follow this pattern. When I first saw this
output, I noticed that the capitalization and non-alphabetic characters
seemed to be preserved. I also noticed that word boundries seemed to be
preserved, too. Could it be a simple <a href="http://en.wikipedia.org/wiki/Substitution_cipher">substitution cipher</a>?</p>

<p>Since I&rsquo;ve been playing with <a href="http://clojure.org/">Clojure</a> recently, I
fired up a REPL to see if I could solve the cipher.</p>

<h3>Solving the Cipher</h3>

<p>Googling the &ldquo;Three O&#8217;Clock Dinner&rdquo; tells me that Josephine Pinckney
is the author. So, I started comparing the difference between the ASCII
values of the coresponding characters&hellip;</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="nv">user=&gt;</span> <span class="p">(</span><span class="nb">- </span><span class="p">(</span><span class="nb">int </span><span class="sc">\J</span><span class="p">)</span> <span class="p">(</span><span class="nb">int </span><span class="sc">\M</span><span class="p">))</span>
</span><span class='line'><span class="mi">-3</span>
</span><span class='line'><span class="nv">user=&gt;</span> <span class="p">(</span><span class="nb">- </span><span class="p">(</span><span class="nb">int </span><span class="sc">\o</span><span class="p">)</span> <span class="p">(</span><span class="nb">int </span><span class="sc">\r</span><span class="p">))</span>
</span><span class='line'><span class="mi">-3</span>
</span><span class='line'><span class="nv">user=&gt;</span> <span class="p">(</span><span class="nb">- </span><span class="p">(</span><span class="nb">int </span><span class="sc">\s</span><span class="p">)</span> <span class="p">(</span><span class="nb">int </span><span class="sc">\v</span><span class="p">))</span>
</span><span class='line'><span class="mi">-3</span>
</span></code></pre></td></tr></table></div></figure>


<p>So everything looks to be shifted down 3 characters. Let&rsquo;s apply this to
the encrypted answer:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="nv">user=&gt;</span> <span class="p">(</span><span class="nb">reduce </span><span class="nv">str</span>
</span><span class='line'>         <span class="p">(</span><span class="nb">map </span><span class="o">#</span><span class="p">(</span><span class="nb">char </span><span class="p">(</span><span class="nb">+ </span><span class="nv">%</span> <span class="mi">-3</span><span class="p">))</span>
</span><span class='line'>              <span class="p">(</span><span class="nb">map int </span><span class="s">&quot;hljkw kxqguhg dqg iliwb-wzr&quot;</span><span class="p">)))</span>
</span><span class='line'><span class="s">&quot;eight^]hundred^]and^]fift_*two&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>We can pick out the words to see that the answer is 852, but clearly,
substracting 3 for all characters isn&rsquo;t going to produce the correct
result. This is because only alphabetic characters are shifted. Also,
the &lsquo;y&rsquo; in &lsquo;fifty&rsquo; seems to be missing. That&rsquo;s becuase the cipher wraps
letters; the encrypted character, &lsquo;b&rsquo;, should be shifted, like &lsquo;a&rsquo; &ndash;>
&lsquo;z&rsquo; &ndash;> &lsquo;x&rsquo; &ndash;> &lsquo;y&rsquo;. I whipped up the following function to shift a single
character:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="nv">user=&gt;</span> <span class="p">(</span><span class="kd">defn </span><span class="nv">alpha-rotate</span> <span class="p">[</span><span class="nv">c</span> <span class="nv">shift</span><span class="p">]</span>
</span><span class='line'>         <span class="s">&quot;Circularly shifts an alphabetic character&quot;</span>
</span><span class='line'>         <span class="p">(</span><span class="k">let </span><span class="p">[</span><span class="nv">between</span> <span class="o">#</span><span class="p">(</span><span class="nb">and </span><span class="p">(</span><span class="nb">&gt;= </span><span class="nv">%1</span> <span class="p">(</span><span class="nb">int </span><span class="nv">%2</span><span class="p">))</span> <span class="p">(</span><span class="nb">&lt;= </span><span class="nv">%1</span> <span class="p">(</span><span class="nb">int </span><span class="nv">%3</span><span class="p">)))</span>
</span><span class='line'>               <span class="nv">alpha-wrap</span> <span class="p">(</span><span class="k">fn </span><span class="p">[</span><span class="nv">c</span> <span class="nv">shift</span> <span class="nv">offset</span><span class="p">]</span>
</span><span class='line'>                            <span class="p">(</span><span class="nb">+ </span><span class="p">(</span><span class="nf">mod</span> <span class="p">(</span><span class="nb">- </span><span class="p">(</span><span class="nb">+ </span><span class="nv">c</span> <span class="nv">shift</span><span class="p">)</span> <span class="nv">offset</span><span class="p">)</span> <span class="mi">26</span><span class="p">)</span> <span class="nv">offset</span><span class="p">))]</span>
</span><span class='line'>           <span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nf">between</span> <span class="nv">c</span> <span class="sc">\A</span> <span class="sc">\Z</span><span class="p">)</span> <span class="p">(</span><span class="nf">alpha-wrap</span> <span class="nv">c</span> <span class="nv">shift</span> <span class="p">(</span><span class="nb">int </span><span class="sc">\A</span><span class="p">))</span>
</span><span class='line'>             <span class="p">(</span><span class="k">if </span><span class="p">(</span><span class="nf">between</span> <span class="nv">c</span> <span class="sc">\a</span> <span class="sc">\z</span><span class="p">)</span> <span class="p">(</span><span class="nf">alpha-wrap</span> <span class="nv">c</span> <span class="nv">shift</span> <span class="p">(</span><span class="nb">int </span><span class="sc">\a</span><span class="p">))</span> <span class="nv">c</span><span class="p">))))</span>
</span><span class='line'><span class="o">#</span><span class="ss">&#39;user/alpha-rotate</span>
</span></code></pre></td></tr></table></div></figure>


<p>Dropping in this function, we get:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="nv">user=&gt;</span> <span class="p">(</span><span class="nb">reduce </span><span class="nv">str</span>
</span><span class='line'>         <span class="p">(</span><span class="nb">map </span><span class="o">#</span><span class="p">(</span><span class="nb">char </span><span class="p">(</span><span class="nf">alpha-rotate</span> <span class="nv">%</span> <span class="mi">-3</span><span class="p">))</span>
</span><span class='line'>              <span class="p">(</span><span class="nb">map int </span><span class="s">&quot;hljkw kxqguhg dqg iliwb-wzr&quot;</span><span class="p">)))</span>
</span><span class='line'><span class="s">&quot;eight hundred and fifty-two&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Much better!</p>

<p>To speed things up, I wrote <a href="https://github.com/loganlinn/hackerrank_challenge/blob/f6937f4e7277ac4b69d7d4096ef52ddf8ec04c6e/src/hackerrank/challenge.clj#L17-22">another function</a>
to take the encrypted question, it&rsquo;s answer, and the encrypted number to figure
out the shift width, and shift the encrypted number.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="nv">user=&gt;</span> <span class="p">(</span><span class="nf">solve-sample</span> <span class="s">&quot;Mrvhsklqh Slqfnqhb&quot;</span> <span class="s">&quot;Josephine Pinckney&quot;</span> <span class="s">&quot;hljkw kxqguhg dqg iliwb-wzr&quot;</span><span class="p">)</span>
</span><span class='line'><span class="s">&quot;eight hundred and fifty-two&quot;</span>
</span><span class='line'><span class="nv">user=&gt;</span> <span class="p">(</span><span class="nf">solve-sample</span> <span class="s">&quot;Fssj Ufwwnxm&quot;</span> <span class="s">&quot;Anne Parrish&quot;</span> <span class="s">&quot;tsj ymtzxfsi fsi ymwjj&quot;</span><span class="p">)</span>
</span><span class='line'><span class="s">&quot;one thousand and three&quot;</span>
</span><span class='line'><span class="nv">user=&gt;</span> <span class="p">(</span><span class="nf">solve-sample</span> <span class="s">&quot;Whuhth Jpaf&quot;</span> <span class="s">&quot;Panama City&quot;</span> <span class="s">&quot;upul aovbzhuk, zlclu obukylk huk mpmaf-vul&quot;</span><span class="p">)</span>
</span><span class='line'><span class="s">&quot;nine thousand, seven hundred and fifty-one&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>I did these three just to verify that things were working.</p>

<p>I quickly realized that obtaining the shift width is the tricky part of these
problems. For the first challenge, it was -3, but turns out to be different
between challenges. But more importantly, I had to make a Google search for
each challenge&hellip; Clearly, not most efficient way to solve this</p>

<p>Let&rsquo;s leverage the fact that all of the answers are really just a number
that&rsquo;s been spelled-out. There&rsquo;s a pretty small set of words that can be
in each answer. If we cosider each word individually, we can reduce the
set of spelled-out numbers by a good amount just by the word&rsquo;s length (eg.
&ldquo;zlclu&rdquo; must be &ldquo;three&rdquo;, &ldquo;seven&rdquo;, &ldquo;eight&rdquo;, &ldquo;fifty&rdquo;, or &ldquo;sixty&rdquo;). Lastly,
we igore words that don&rsquo;t have a consistent shift width our encrypted
word.</p>

<p>If we do this for each encrypted word and find a common shift width, we
can decypher the spelled-out number.</p>

<h2>Extracting the Value</h2>

<p>Since a challenges is answered by providing the numeric value of the
spelled-out number, we need to be able to parse the strings that we have
decrypted.</p>

<p>After a quick search, I didn&rsquo;t find an existing Clojure library for this, so I
took a stab at it myself. It turned out to be a relatively simple set of
functions to solve this.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="nv">hackerrank.numberparse=&gt;</span> <span class="p">(</span><span class="nb">parse </span><span class="s">&quot;eight thousand, four hundred and fifty-three&quot;</span><span class="p">)</span>
</span><span class='line'><span class="mi">8453</span>
</span><span class='line'><span class="nv">hackerrank.numberparse=&gt;</span> <span class="p">(</span><span class="nb">parse </span><span class="s">&quot;four million and two&quot;</span><span class="p">)</span>
</span><span class='line'><span class="mi">4000002</span>
</span></code></pre></td></tr></table></div></figure>


<p>Check out the source <a href="https://github.com/loganlinn/hackerrank_challenge/blob/f6937f4e7277ac4b69d7d4096ef52ddf8ec04c6e/src/hackerrank/numberparse.clj">here</a></p>

<h2>Shipping it</h2>

<p>With the ability to solve these challenges progamatically, the only
thing remaining steps are requesting challenges and submitting answers.</p>

<p>I used <a href="https://github.com/dakrone">dakrone</a>&rsquo;s <a href="https://github.com/dakrone/clj-http/">clj-http</a> and <a href="https://github.com/dakrone/cheshire">cheshire</a> libraries to make HTTP requests and parse JSON.</p>

<p>You can check out the <a href="https://github.com/loganlinn/hackerrank_challenge">full source of my solution on GitHub</a>.</p>

<p>Although this wasn&rsquo;t a particularly difficult programming puzzle to
solve, it was a fun exercise to practice my Clojure skills.</p>

<p>Thanks to the <a href="http://www.twitter.com/hackerrank">@HackerRank</a> guys for
putting up the puzzles!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Now Running Octopress]]></title>
    <link href="http://loganlinn.com/blog/2011/10/15/now-running-octopress/"/>
    <updated>2011-10-15T16:12:00-07:00</updated>
    <id>http://loganlinn.com/blog/2011/10/15/now-running-octopress</id>
    <content type="html"><![CDATA[<p>As my friends are aware, I love <a href="http://ruby-lang.org">Ruby</a> [1]. I was,
and still am, hungry to write more of it.  Shortly after putting up my
<a href="http://loganlinn.heroku.com">wimpy Rails app for blogging</a>, I had
a feeling that it wasn&rsquo;t going to work that well. I realized I mainly
wrote the app to create something in Rails and deploy it on Heroku. Fun,
but not a long term solution. I got discouraged and <s>stopped</s>
failed to start blogging in hopes to build a system I would use.</p>

<p>Still trying to feed my Ruby appetite, I decided to use Sinatra to make
a super simple app that would serve my static
<a href="http://daringfireball.net/projects/markdown/">Markdown</a> files. But
somewhere in between graduating and moving across the country, I lost
track of that project and its source code. I also learned about
<a href="https://github.com/mojombo/jekyll">Jekyll</a>, which did everything I
wanted my app to do and more. I got discouraged again.</p>

<p>Today I learned about <a href="http://octopress.org/">Octopress</a>, a Sinatra
based framework that utilizes Jekyll. Described as an, &ldquo;obsessively
designed framework&rdquo;, I was delighted to find that it&rsquo;s almost exactly
what I wanted to build myself. And since it&rsquo;s a Rack app, it plays nice
with <a href="http://pow.cx/">pow</a>, another tool I&rsquo;ve been looking for a reason
to use. So, shout out to <a href="https://github.com/imathis">Brandon Mathis</a>
and the Octopress contributors for making a great tool!</p>

<p>[1] They&rsquo;ll also tell you that I have attention deficit disorder when it
comes to learning and using new programming languages.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Gitcrunch]]></title>
    <link href="http://loganlinn.com/blog/2011/05/03/gitcrunch/"/>
    <updated>2011-05-03T21:32:00-07:00</updated>
    <id>http://loganlinn.com/blog/2011/05/03/gitcrunch</id>
    <content type="html"><![CDATA[<p>Just finished the last project for my Internet Software Development
course (VT&rsquo;s CS4244). It compares the similarity between repositories
hosted on github. Repositories are scored based on similar commit
patterns. The two dimensions that are currently incorporated are hour
of the day and day of the week.</p>

<p>You can check out the project is at <a href="http://gitcrunch.com">http://gitcrunch.com</a>.</p>

<p><em>Update: This is now offline. I will try to resurrect the project
sometime.</em></p>
]]></content>
  </entry>
  
</feed>
