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

  <title><![CDATA[50Ply Blog]]></title>
  <link href="http://www.50ply.com/atom.xml" rel="self"/>
  <link href="http://www.50ply.com/"/>
  <updated>2015-04-06T04:53:53-04:00</updated>
  <id>http://www.50ply.com/</id>
  <author>
    <name><![CDATA[Brian Taylor]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[HTML5 Life of a Photon]]></title>
    <link href="http://www.50ply.com/blog/2013/11/18/html5-life-of-a-photon/"/>
    <updated>2013-11-18T18:09:00-05:00</updated>
    <id>http://www.50ply.com/blog/2013/11/18/html5-life-of-a-photon</id>
    <content type="html"><![CDATA[<p><a href="http://www.50ply.com/loap_web">Play my game!</a></p>

<p><img src="http://www.50ply.com/images/loap.png"></p>

<p><a href="https://github.com/netguy204/giggle">Giggle</a> (the game engine I&rsquo;ve
been building over the past year) has made the leap to HTML5 thanks to
the excellent <a href="https://github.com/kripken/emscripten">Emscripten</a>
compiler. This means that all of the games I&rsquo;ve written for Giggle so
far can now be &ldquo;ported to the web&rdquo; so that they can run inside any
reasonably modern desktop browser. Today, &ldquo;reasonably modern desktop
browser&rdquo; translates into an updated-in-the-last year Chrome or Firefox
or (without sound) Safari 7. The big new feature we need to make this
work is WebGL.</p>

<p>The first game I&rsquo;ve ported is my Ludum Dare #27 entry
<a href="http://www.ludumdare.com/compo/ludum-dare-27/?action=preview&amp;uid=5044">&ldquo;Life of a Photon&rdquo;</a>. You
can <a href="http://www.50ply.com/loap_web">play it in your modern browser</a> or you can
<a href="http://bit.ly/14vQ58Y">download the desktop (Mac/Windows/Linux) version</a>.</p>

<p>Or, you could just watch me play it! I haven&rsquo;t mastered the art of
screencasting yet so time feels a little lurchy in this recording.</p>

<iframe width="420" height="315" src="//www.youtube.com/embed/cNTh1ZCwJ7o" frameborder="0" allowfullscreen></iframe>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Improve the News!]]></title>
    <link href="http://www.50ply.com/blog/2013/11/09/improve-the-news/"/>
    <updated>2013-11-09T15:43:00-05:00</updated>
    <id>http://www.50ply.com/blog/2013/11/09/improve-the-news</id>
    <content type="html"><![CDATA[<p>Is the bleakness of the news getting you down? Kick the news up a notch with this bookmarklet (<a href="http://www.50ply.com/downloads/improve-news.js">source code here</a>):</p>

<p><a href="javascript: (function () { var jsCode = document.createElement('script'); jsCode.setAttribute('src', '//50ply.com/downloads/improve-news.js'); document.body.appendChild(jsCode); }());">Improved News</a></p>

<p>This was inspired by <a href="http://xkcd.com/1288/">XKCD #1288</a></p>

<p><img src="http://imgs.xkcd.com/comics/substitutions.png"></p>

<p>Try improving this <a href="news:">news:</a> <a href="http://online.wsj.com/news/articles/SB10001424127887324128504578346913994914472">WSJ: Green Cars Have a Dirty Little Secret</a>.</p>

<p>Who knew that the green cats were up to so much trouble?</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Photoshop iOS Auto Resizer]]></title>
    <link href="http://www.50ply.com/blog/2013/11/06/photoshop-ios-auto-resizer/"/>
    <updated>2013-11-06T07:44:00-05:00</updated>
    <id>http://www.50ply.com/blog/2013/11/06/photoshop-ios-auto-resizer</id>
    <content type="html"><![CDATA[<p>Are you as annoyed as I am when you have to create the 7 (assuming you use Spotlight and Settings) different icon sizes that Apple requires iOS apps to provide? This post should be a real time-saver if you created your original icon in Photoshop.</p>

<p>One neat thing about implementing this auto-resize functionality as a Photoshop script (instead of a bash script using imagemagick and friends) is that the resize can take advantage of vector information (shapes, paths, layer effects) to provide a higher quality result than simple pixel downsampling can provide.</p>

<ol>
<li><p><a href="http://www.50ply.com/downloads/save-for-ios.jsx">Download and save my save-for-ios Photoshop script.</a></p></li>
<li><p>Open your original high-res icon. If you created your icon primarily out of shapes, paths, and layer effects then this technique will really shine!</p></li>
<li><p>From the Photoshop File menu select Scripts and then Browse.</p></li>
<li><p>Select my save-for-ios.jsx script that you downloaded in step 1.</p></li>
<li><p>Select the output directory for your resized results.</p></li>
<li><p>Select the prefix for your resized results. The final resized files will be named prefix29.png, prefix29@2x.png, etc.</p></li>
</ol>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[C++: Eliminate Parallel Lists with Higher Order Macros]]></title>
    <link href="http://www.50ply.com/blog/2013/04/19/eliminate-parallel-lists-with-higher-order-macros/"/>
    <updated>2013-04-19T18:29:00-04:00</updated>
    <id>http://www.50ply.com/blog/2013/04/19/eliminate-parallel-lists-with-higher-order-macros</id>
    <content type="html"><![CDATA[<p>Here&rsquo;s a handy tool in my tool-belt that I thought would be worth sharing. We&rsquo;ve all dealt with situations in development where we have to create multiple lists of the same logical things. Here&rsquo;s a classic example:</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='c'><span class='line'><span class="k">enum</span> <span class="n">Animals</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">DOG</span><span class="p">,</span>
</span><span class='line'>  <span class="n">CAT</span><span class="p">,</span>
</span><span class='line'>  <span class="n">MOUSE</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">AnimalNames</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>  <span class="s">&quot;DOG&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s">&quot;CAT&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s">&quot;MOUSE&quot;</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here we&rsquo;ve defined an enumeration and their stringified names so that we can pretty-print them. But, this means we have two lists that must have the same number and order of elements or there is a bug. These are parallel lists and they can be tricky to maintain.</p>

<p>Luckily, the C preprocessor gives us an elegant (but not well known) solution. We can put the list into a macro and then <em>generate</em> the parallel lists in the preprocessor such that they will always be correct. Here&rsquo;s how it works:</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>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cp">#define ANIMAL_LIST(m)        \</span>
</span><span class='line'><span class="cp">  m(DOG),                     \</span>
</span><span class='line'><span class="cp">  m(CAT),                     \</span>
</span><span class='line'><span class="cp">  m(MOUSE)</span>
</span><span class='line'>
</span><span class='line'><span class="cp">#define GENERATE_ENUM(m) m</span>
</span><span class='line'>
</span><span class='line'><span class="cp">#define GENERATE_STRING(m) #m</span>
</span><span class='line'>
</span><span class='line'><span class="k">enum</span> <span class="n">Animals</span> <span class="p">{</span>
</span><span class='line'>   <span class="n">ANIMAL_LIST</span><span class="p">(</span><span class="n">GENERATE_ENUM</span><span class="p">)</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'>
</span><span class='line'><span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">AnimalNames</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span>
</span><span class='line'>   <span class="n">ANIMAL_LIST</span><span class="p">(</span><span class="n">GENERATE_STRING</span><span class="p">)</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>Slick, huh? We&rsquo;ve only listed our items once but we&rsquo;ve generated the enum and the stringified names from that single list. Here&rsquo;s what happened:</p>

<p>We defined a macro named &ldquo;ANIMAL_LIST&rdquo; that takes another macro as an argument. The macro then calls that macro with each of the elements of the list. Thus, ANIMAL_LIST is a &ldquo;higher order macro&rdquo; because its behavior is partially determined by the macro it accepts as an argument. We then define our enum and within the curly braces we expand the ANIMAL_LIST higher order macro using a macro that just emits each of the elements of the list directly. We then define our stringification array and within those curly braces we expand ANIMAL_LIST using a macro that emits the stringified version of each list element.</p>

<p>For a far more ambitious use of this higher-order macro concept, check out <a href="https://github.com/netguy204/brianscheme/blob/master/vm.c#L37">the BrianScheme Virtual Machine</a>. Here, the opcode_table is a higher order macro that is expanded 7 times to produce opcode to string mappings, symbols for each of the opcodes, pointer to opcode mappings, and more. That&rsquo;s a lot of parallel lists that must be kept consistent!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Mosaic for Mac]]></title>
    <link href="http://www.50ply.com/blog/2013/01/26/mosaic-for-mac/"/>
    <updated>2013-01-26T23:14:00-05:00</updated>
    <id>http://www.50ply.com/blog/2013/01/26/mosaic-for-mac</id>
    <content type="html"><![CDATA[<h1>Update!</h1>

<p>The original mac release had a packaging bug that meant it would only
work on Mountain Lion. This release should work on any version of OSX
version 10.5 or newer. Please leave a comment if you have
trouble. Thanks!</p>

<hr />

<p>We&rsquo;ve released a demo of Mosaic for the Mac! Mosaic puzzle game
created by
<a href="https://plus.google.com/109004388816388921985">Stephen Mitchell</a> and
me for <a href="http://killscreendaily.com/create/">game making contest</a>
promoting the upcoming <a href="http://ouya.tv">Ouya</a> game
console. <a href="http://www.50ply.com/downloads/Mosaic-0.1.2.dmg">Grab the mac demo</a> at let me know
what you think. If you like what you see then remember to thumbs up
our <a href="http://www.youtube.com/watch?v=34QjrRvyPic">contest video</a> so the
OUYA Create judges can count your vote.</p>

<p><img src="http://www.50ply.com/images/mosaic_mac.png"></p>

<p>If you&rsquo;re one of the few folks lucky enough to own an Ouya, you can
<a href="http://www.50ply.com/downloads/Mosaic.apk">download the Ouya version too!</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Game Jam Complete!]]></title>
    <link href="http://www.50ply.com/blog/2013/01/24/game-jam-complete/"/>
    <updated>2013-01-24T20:58:00-05:00</updated>
    <id>http://www.50ply.com/blog/2013/01/24/game-jam-complete</id>
    <content type="html"><![CDATA[<p>I finished my second Game Jam and my very first team game project!
We&rsquo;re calling this game &ldquo;Mosaic.&rdquo; It is designed for the upcoming
<a href="http://www.ouya.tv">Ouya game console</a>.</p>

<iframe width="560" height="315" src="http://www.youtube.com/embed/34QjrRvyPic" frameborder="0" allowfullscreen></iframe>


<p>If you like what you see and can spare some social love then
<a href="https://www.youtube.com/watch?v=34QjrRvyPic">thumbs up our video</a> and
<a href="https://plus.google.com/113030461949209212750/">+1 our page</a>. You
see, this jam is also a bit of a popularity contest.</p>

<p><a href="https://plus.google.com/109004388816388921985">Stephen Mitchell</a> took
on most of the art and design responsibilities for this project. The
simple and clean aesthetic he brings really made Mosaic shine.</p>

<p>If you&rsquo;re one of the few lucky people in the world who own an Ouya
(I&rsquo;m loving mine) then you can
<a href="http://www.50ply.com/downloads/Mosaic.apk">download the APK</a>. We would love to hear what
you think.</p>

<p>I really get a lot out of <a href="http://onegameamonth.com/netguy204">game</a>
<a href="http://www.ludumdare.com/compo/author/netguy204/">jams</a>. Sometimes
the deadline they impose is essential for forcing me to focus on the
things that matter most in my
game. <a href="https://twitter.com/McFunkypants">@McFunkypants</a> really helped
me understand how to harness the power of a deadline in his book
<a href="http://www.amazon.com/gp/product/1849692505/ref=as_li_tf_tl?ie=UTF8&amp;camp=1789&amp;creative=9325&amp;creativeASIN=1849692505&amp;linkCode=as2&amp;tag=50ply-20">The Game Jam Survival Guide</a>. I
highly recommend this book for anyone considering participating in a
game jam or who has participated but would like to do better next
time.</p>

<p>Making games is hard!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Loading Compressed Android Assets with File Pointer]]></title>
    <link href="http://www.50ply.com/blog/2013/01/19/loading-compressed-android-assets-with-file-pointer/"/>
    <updated>2013-01-19T07:12:00-05:00</updated>
    <id>http://www.50ply.com/blog/2013/01/19/loading-compressed-android-assets-with-file-pointer</id>
    <content type="html"><![CDATA[<p>My game engine now runs on the <a href="http://www.ouya.tv">Ouya</a> (a new
Android-based device using a Tegra 3 ARM processor!)</p>

<p>One of the big challenges that I encountered during the port was asset
loading. Android packages, which are delivered as .apk files,
typically load assets at run-time by reaching back into the .apk
file. The Android provided API for accomplishing asset loading in Java
is appropriate but the Android NDK C API has some shortcomings. Why
would I say that when the Java API and the C API are almost identical?
C applications that handle streams of data from files are typically
designed around the file pointer abstraction (<code>FILE*</code>) and the Android
API isn&rsquo;t compatible. This means, typically, that you would need to
alter the code of any third-party libraries that you are using (if
they deal with files) in order to port them the Android asset
API&hellip; until now!</p>

<p>My engine uses quite a few third-party libraries, specifically:</p>

<ol>
<li><p>Box2D for physics</p></li>
<li><p>lobogg / tremor for sound and music</p></li>
<li><p>Lua for scripting</p></li>
<li><p><a href="http://nothings.org/stb_image.c">stb_image</a> for image loading</p></li>
</ol>


<p>All of these libraries, except Box2D, provide features that already
exist behind some other Android API. The reason I explicitly provide
these features again through my own libraries is for portability. I do
my quick-turnaround build testing on my Mac but I want the same code
to work exactly the same way on the Ouya. The simplest way to achieve
this is to provide a lot of my own low-level support. The more &ldquo;pure&rdquo;
way to achieve this would be to create an abstraction layer that used
the Android APIs and the OSX APIs under the hood.</p>

<p>libogg, Lua, and stb_image all use the <code>FILE*</code> API to load files from
disk. They call <code>fopen</code> when they want to open a new file and <code>fread</code>
to extract data from it. They also occasionally use <code>fseek</code> to jump
around.</p>

<p>One amazing and well-hidden fact about the <code>FILE*</code> API is that it is
actually polymorphic and extensible! (A surprise from C, right?) We
can create a <code>FILE*</code> that uses code we wrote to satisfy fopen, fread,
fseek, and <code>fwrite</code> requests. We can use this little-known feature to
wrap the Android asset API in a <code>FILE*</code> API instead.</p>

<p>Here&rsquo;s how:</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='c++'><span class='line'><span class="kt">FILE</span><span class="o">*</span> <span class="nf">android_fopen</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">fname</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">mode</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">if</span><span class="p">(</span><span class="n">mode</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="sc">&#39;w&#39;</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">AAsset</span><span class="o">*</span> <span class="n">asset</span> <span class="o">=</span> <span class="n">AAssetManager_open</span><span class="p">(</span><span class="n">android_asset_manager</span><span class="p">,</span> <span class="n">fname</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
</span><span class='line'>  <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">asset</span><span class="p">)</span> <span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">return</span> <span class="n">funopen</span><span class="p">(</span><span class="n">asset</span><span class="p">,</span> <span class="n">android_read</span><span class="p">,</span> <span class="n">android_write</span><span class="p">,</span> <span class="n">android_seek</span><span class="p">,</span> <span class="n">android_close</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The funopen function creates a new file pointer that will delegate
control to the functions that you specify when the user uses fread and
friends on that file pointer. The first parameter to funopen is the
&ldquo;cookie&rdquo; to associate with the <code>FILE*</code>. This is arbitrary data that will
be given to your delegate functions whenever they are called. OOP
programmers should think of it as the self (or <code>this</code>) pointer.</p>

<p>Let&rsquo;s write those delegate functions:</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>
</pre></td><td class='code'><pre><code class='c++'><span class='line'><span class="k">static</span> <span class="kt">int</span> <span class="nf">android_read</span><span class="p">(</span><span class="kt">void</span><span class="o">*</span> <span class="n">cookie</span><span class="p">,</span> <span class="kt">char</span><span class="o">*</span> <span class="n">buf</span><span class="p">,</span> <span class="kt">int</span> <span class="n">size</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="n">AAsset_read</span><span class="p">((</span><span class="n">AAsset</span><span class="o">*</span><span class="p">)</span><span class="n">cookie</span><span class="p">,</span> <span class="n">buf</span><span class="p">,</span> <span class="n">size</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">static</span> <span class="kt">int</span> <span class="nf">android_write</span><span class="p">(</span><span class="kt">void</span><span class="o">*</span> <span class="n">cookie</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">buf</span><span class="p">,</span> <span class="kt">int</span> <span class="n">size</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="n">EACCES</span><span class="p">;</span> <span class="c1">// can&#39;t provide write access to the apk</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">static</span> <span class="kt">fpos_t</span> <span class="nf">android_seek</span><span class="p">(</span><span class="kt">void</span><span class="o">*</span> <span class="n">cookie</span><span class="p">,</span> <span class="kt">fpos_t</span> <span class="n">offset</span><span class="p">,</span> <span class="kt">int</span> <span class="n">whence</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">return</span> <span class="n">AAsset_seek</span><span class="p">((</span><span class="n">AAsset</span><span class="o">*</span><span class="p">)</span><span class="n">cookie</span><span class="p">,</span> <span class="n">offset</span><span class="p">,</span> <span class="n">whence</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">static</span> <span class="kt">int</span> <span class="nf">android_close</span><span class="p">(</span><span class="kt">void</span><span class="o">*</span> <span class="n">cookie</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">AAsset_close</span><span class="p">((</span><span class="n">AAsset</span><span class="o">*</span><span class="p">)</span><span class="n">cookie</span><span class="p">);</span>
</span><span class='line'>  <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we can use the <code>FILE*</code> API to access our assets!</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='c++'><span class='line'><span class="kt">FILE</span><span class="o">*</span> <span class="n">android_file</span> <span class="o">=</span> <span class="n">android_fopen</span><span class="p">(</span><span class="s">&quot;something.txt&quot;</span><span class="p">);</span>
</span><span class='line'><span class="n">fread</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">android_file</span><span class="p">);</span>
</span><span class='line'><span class="n">fclose</span><span class="p">(</span><span class="n">android_file</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is great but we still have to call android_fopen to create our
special <code>FILE*</code>. If we stopped hacking here then we would still need to
open up all those third party libraries to change the fopen calls to
android fopen instead. We can do better.</p>

<p>If we&rsquo;re willing to say that ALL file access should go through the
asset API then we can achieve that quickly. We&rsquo;ll just create a header
for our new android fopen library and redefine fopen using a macro:</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='c++'><span class='line'><span class="cp">#ifndef ANDROID_FOPEN_H</span>
</span><span class='line'><span class="cp">#define ANDROID_FOPEN_H</span>
</span><span class='line'>
</span><span class='line'><span class="kt">FILE</span><span class="o">*</span> <span class="nf">android_fopen</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">fname</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span><span class="o">*</span> <span class="n">mode</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="cp">#define fopen(name, mode) android_fopen(name, mode)</span>
</span><span class='line'>
</span><span class='line'><span class="cp">#endif</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we just include this header in our code and we can use <code>fopen</code> as
normal! But, again, we&rsquo;re still adding code to those third party
libraries. Thankfully, <code>gcc</code> has the 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>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>gcc -o foo foo.c -include <span class="s2">&quot;android_fopen.h&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>-include</code> argument causes <code>gcc</code> to treat your code as-if you had
included the provided file at the very top of your code. Now, to port
Lua and libogg and any other big-fancy library that uses <code>FILE*</code> to
Android, all we need to do is tweak the build system to include our
magic header.</p>

<p>The full source is
<a href="https://github.com/netguy204/gambit-game-lib/blob/dk94/android_fopen.c">here</a>
and
<a href="https://github.com/netguy204/gambit-game-lib/blob/dk94/android_fopen.h">here</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Enabling Civ4 Python Console in OSX]]></title>
    <link href="http://www.50ply.com/blog/2012/11/25/enabling-civ4-python-console-in-osx/"/>
    <updated>2012-11-25T21:32:00-05:00</updated>
    <id>http://www.50ply.com/blog/2012/11/25/enabling-civ4-python-console-in-osx</id>
    <content type="html"><![CDATA[<p>I am now the proud owner of Civilization 4 (for Mac) and I&rsquo;m really
happy to see all of the modding potential that this game has.</p>

<p>For those of you just getting started (like me) the secret to enabling
the in-game Python console on the Mac is as follows:</p>

<p>Open your Documents folder and find the &ldquo;Civilization IV&rdquo;
directory.  Then, open the CivilizationIV.ini in the text editor of
your choice (TextEdit is fine.) Find the line that looks like</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>CheatCode = 0</span></code></pre></td></tr></table></div></figure>


<p>and change it to</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>CheatCode = chipotle</span></code></pre></td></tr></table></div></figure>


<p>Now, start a new game of Civilization IV (or load an existing one) and
press Shift + ~ in game to bring up the Python Console.</p>

<p>Try typing these commands to see how the Python console works:</p>

<figure class='code'><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=''><span class='line'>&gt; print "hello world"
</span><span class='line'>
</span><span class='line'>&gt; print dir(gc)</span></code></pre></td></tr></table></div></figure>


<p>There you have it!</p>

<p>If you have the Beyond the Sword expansion then follow the same
instructions given above but find the CivilizationIV.ini file in your
&ldquo;~/Documents/Civilization IV Beyond the Sword/&rdquo; directory instead.</p>

<p>Happy modding!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Mass Convert PSD to PNG]]></title>
    <link href="http://www.50ply.com/blog/2012/09/16/mass-convert-psd-to-png/"/>
    <updated>2012-09-16T18:03:00-04:00</updated>
    <id>http://www.50ply.com/blog/2012/09/16/mass-convert-psd-to-png</id>
    <content type="html"><![CDATA[<p>Mass convert a folder of Photoshop Files (psd) into png files!</p>

<p>Copy this script into a file named convert2png.jsx and run it from Photoshop (File | Scripts | Browse&hellip;). Select the files you want to convert and click open. PNG files with the same name will be created and placed in the same directory as the source files.</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='javascript'><span class='line'><span class="kd">var</span> <span class="nx">files</span> <span class="o">=</span> <span class="nx">openDialog</span><span class="p">();</span>
</span><span class='line'>
</span><span class='line'><span class="k">for</span><span class="p">(</span><span class="kd">var</span> <span class="nx">key</span> <span class="k">in</span> <span class="nx">files</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">inFile</span> <span class="o">=</span> <span class="nx">files</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
</span><span class='line'>    <span class="nx">open</span><span class="p">(</span><span class="nx">inFile</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>    <span class="kd">var</span> <span class="nx">pngOptions</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">PNGSaveOptions</span><span class="p">();</span>
</span><span class='line'>    <span class="nx">pngOptions</span><span class="p">.</span><span class="nx">interlaced</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
</span><span class='line'>    <span class="nx">app</span><span class="p">.</span><span class="nx">activeDocument</span><span class="p">.</span><span class="nx">saveAs</span><span class="p">(</span><span class="nx">inFile</span><span class="p">,</span> <span class="nx">pngOptions</span><span class="p">,</span> <span class="kc">true</span><span class="p">,</span> <span class="nx">Extension</span><span class="p">.</span><span class="nx">LOWERCASE</span><span class="p">);</span>
</span><span class='line'>    <span class="nx">app</span><span class="p">.</span><span class="nx">activeDocument</span><span class="p">.</span><span class="nx">close</span><span class="p">();</span>
</span><span class='line'> <span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Spriter SCML Parser in Scheme]]></title>
    <link href="http://www.50ply.com/blog/2012/08/29/spriter-scml-parser-in-scheme/"/>
    <updated>2012-08-29T21:18:00-04:00</updated>
    <id>http://www.50ply.com/blog/2012/08/29/spriter-scml-parser-in-scheme</id>
    <content type="html"><![CDATA[<p><a href="http://www.brashmonkey.com/spriter.htm">Spriter</a> is an exciting tool for building game animations being developed by <a href="http://www.brashmonkey.com">BrashMonkey</a>. Like many other interesting little-guy innovations of our time, Spriter started its life on <a href="http://www.kickstarter.com/projects/539087245/spriter?ref=card">KickStarter</a>. Spriter produces a simple format called SCML that&rsquo;s designed to be consumed by third-party game engines.</p>

<p>Since I&rsquo;m a proud sponsor and future user of Spriter I wrote a SCML parser / playback tool as part of a Scheme game engine I&rsquo;m working on. The SCML code is nicely abstracted from the rest of my engine so it should be very easy to port to other LISPy languages if anyone is interested.</p>

<p>In action:</p>

<iframe width="480" height="360" src="http://www.youtube.com/embed/QH6uS8z6zrg" frameborder="0" allowfullscreen></iframe>


<p>All of the SCML related code is in the <a href="https://github.com/netguy204/gambit-game-lib/blob/master/spriter.scm">spriter.scm file</a>. The only dependency on the rest my code has to do with turning raw XML into an equally raw (but far nicer) tree of s-expressions.</p>

<p>This implements the <a href="http://www.brashmonkeygames.com/spriter/8-17-2012/GettingStartedWithSCMLGuide%28a2%29.pdf">final (post beta) SCML spec</a>. Interestingly, this may actually be the FIRST implementation of the post-beta spec. I haven&rsquo;t found any others yet.</p>

<p>To load an animation:</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>
</pre></td><td class='code'><pre><code class='scheme'><span class='line'><span class="p">(</span><span class="k">define </span><span class="nv">+scml+</span> <span class="p">(</span><span class="nf">scml-load</span> <span class="s">&quot;monster/Example.SCML&quot;</span><span class="p">))</span>
</span><span class='line'><span class="p">(</span><span class="k">define </span><span class="nv">+idle+</span> <span class="p">(</span><span class="nf">animation</span> <span class="p">(</span><span class="nf">entity</span> <span class="nv">+scml+</span> <span class="s">&quot;0&quot;</span><span class="p">)</span> <span class="s">&quot;Idle&quot;</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>The length of the animation (in seconds) is:</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>
</pre></td><td class='code'><pre><code class='scheme'><span class='line'><span class="p">(</span><span class="nf">animation-length</span> <span class="nv">+idle+</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>To interpolate the animation to any time between 0 and its animation-length:</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>
</pre></td><td class='code'><pre><code class='scheme'><span class='line'><span class="p">(</span><span class="nf">interp-anim</span> <span class="nv">anim</span> <span class="nv">time</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This returns a list of tkey objects which contain the interpolated rotations, positions, and centers for all of the sprite fragments presented in the order they should be drawn.</p>

<p><a href="https://github.com/netguy204/gambit-game-lib/blob/46c8723ebf7663fe0ba04fc0853d1337515787e7/scmlib.scm#L76">Here</a> is an example of those tkey objects being rendered using my engine.</p>

<p>Hopefully this saves someone some time!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Cljs-bench Facelift]]></title>
    <link href="http://www.50ply.com/blog/2012/08/14/cljs-bench-facelift/"/>
    <updated>2012-08-14T20:55:00-04:00</updated>
    <id>http://www.50ply.com/blog/2012/08/14/cljs-bench-facelift</id>
    <content type="html"><![CDATA[<p>The <a href="http://50ply.com/cljs-bench">Clojurescript benchmark report</a> that <a href="https://github.com/netguy204/cljs-bench">cljs-bench</a> generates had been looking a little old fashioned. Today I gave it a facelift.</p>

<p>I&rsquo;m now using Enlive to create the HTML for the report. Enlive offers very strong separation between business code and HTML. Since the Enlive template is just a normal HTML file, I can edit it using <a href="https://github.com/netguy204/imp.el">impatient-mode</a> and see the impact of my work immediately.</p>

<p>Here&rsquo;s a video of me using impatient-mode to sequentially enable chunks of CSS while seeing the browser update instantly.</p>

<iframe width="640" height="360" src="http://www.youtube.com/embed/Mcc0w8WNFlw" frameborder="0" allowfullscreen></iframe>



]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Introducing Impatient-mode]]></title>
    <link href="http://www.50ply.com/blog/2012/08/13/introducing-impatient-mode/"/>
    <updated>2012-08-13T18:01:00-04:00</updated>
    <id>http://www.50ply.com/blog/2012/08/13/introducing-impatient-mode</id>
    <content type="html"><![CDATA[<h2>Updates!</h2>

<p>I&rsquo;ve made significant performance improvements to <em>impatient-mode</em> since I recorded this initial demo. <a href="http://www.50ply.com/blog/2012/08/14/cljs-bench-facelift/">Here</a> is an example of refreshing the CSS on the Clojurescript benchmark site. <a href="http://www.youtube.com/watch?v=nQr6LNocMjw">And this</a> demonstrates live editing with 3 browsers at the same time.</p>

<p><em>impatient-mode</em> can be installed from MELPA.</p>

<h2>impatient-mode</h2>

<p><a href="https://github.com/netguy204/imp.el">See your HTML rendered as you type it!</a></p>

<p>My frequent co-conspirator, <a href="http://nullprogram.com/">Chris Wellons</a>, created a HTTP server that runs entirely within Emacs. He made some updates recently that inspired me to create <em>impatient-mode</em>.</p>

<p><em>impatient-mode</em> uses Chris&rsquo;s <a href="https://github.com/skeeto/emacs-http-server">HTTP Server</a> to serve the Emacs buffer of your choice. <em>impatient-mode</em> serves up this buffer along with a thin shim page that makes the browser automatically refresh itself from your buffer as you type.</p>

<p>Here&rsquo;s a video of it in action!</p>

<iframe width="640" height="360" src="http://www.youtube.com/embed/QV6XVyXjBO8" frameborder="0" allowfullscreen></iframe>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[PPfAI Chapter 2 (in Clojure Core.Logic)]]></title>
    <link href="http://www.50ply.com/blog/2012/08/01/pfai-to-core-logic-chapter-2/"/>
    <updated>2012-08-01T21:11:00-04:00</updated>
    <id>http://www.50ply.com/blog/2012/08/01/pfai-to-core-logic-chapter-2</id>
    <content type="html"><![CDATA[<p>The adventures continue with Core.Logic!</p>

<p>Since the last post I&rsquo;ve had some more time to dig into <a href="https://github.com/clojure/core.logic/blob/master/src/main/clojure/clojure/core/logic.clj">the implementation of Core.Logic</a>. I&rsquo;ve also been spending more time with Ivan Bratko&rsquo;s excellent <a href="http://www.amazon.com/gp/product/0321417461/ref=as_li_ss_tl?ie=UTF8&amp;tag=50ply-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321417461">Prolog Programming For Artificial Intelligence</a>. Finally, since Core.Logic is an implementation of miniKanren, I&rsquo;ve discovered that <a href="https://scholarworks.iu.edu/dspace/bitstream/handle/2022/8777/Byrd_indiana_0093A_10344.pdf?sequence=1">William Byrd&rsquo;s miniKanren Dissertation</a> is a very helpful resource for understanding Core.Logic.</p>

<p>Chapter 2 of Prolog Programming For Artificial Intelligence introduces a classic AI problem involving a monkey and a banana.</p>

<p>A monkey is in a room that contains a banana and a box. The banana is in the middle of the room and is out of the monkey&rsquo;s reach if the monkey is standing on the floor. But, if the monkey positions the box properly and stands on it then the monkey will be able to reach the banana. The monkey is allowed to walk around the room, push the box, climb the box, and reach for the banana. They question is, given a particular starting room configuration, can the monkey reach the banana?</p>

<p>The state of monkey-world can be expressed as a 4-tuple. The terms that make it up are:</p>

<ol>
<li>Where the monkey is in the room</li>
<li>If the monkey is on the box</li>
<li>Where the box is in the room</li>
<li>If the monkey has the banana</li>
</ol>


<p>In Prolog, we express this using a named structure with 4 parameters. For example:</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>
</pre></td><td class='code'><pre><code class='prolog'><span class='line'><span class="nf">state</span><span class="p">(</span> <span class="s-Atom">middle</span><span class="p">,</span> <span class="s-Atom">onbox</span><span class="p">,</span> <span class="s-Atom">middle</span><span class="p">,</span> <span class="s-Atom">hasnot</span> <span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>In this configuration, the monkey is in the middle of the room and is standing on the box (also in the middle of the room) and it does not have the banana.</p>

<p>In Core.Logic, we don&rsquo;t really have a concept that maps directly to Prolog&rsquo;s named structures but we do have full access to Clojure&rsquo;s rich data-types. A simple array will do what we need. We could tag the array with a name but that information isn&rsquo;t needed for this example. We&rsquo;ll just express the state of the world as an unnamed 4-vector of symbols:</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">[</span> <span class="ss">:middle</span> <span class="ss">:onbox</span> <span class="ss">:middle</span> <span class="ss">:hasnot</span> <span class="p">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>The monkey can interact with the world by:</p>

<ol>
<li>Grabbing the banana</li>
<li>Climbing the box</li>
<li>Pushing the box</li>
<li>Walking around the room</li>
</ol>


<p>Not all actions are always possible. The available actions depend on the state of the world and applying an action to a world-state produces a new world-state.</p>

<p>In Prolog, we can encode the &ldquo;grabbing the banana&rdquo; action as a relation between the initial world-state, the action, and the resulting world-state:</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='prolog'><span class='line'><span class="nf">move</span><span class="p">(</span>
</span><span class='line'> <span class="nf">state</span><span class="p">(</span> <span class="s-Atom">middle</span><span class="p">,</span> <span class="s-Atom">onbox</span><span class="p">,</span> <span class="s-Atom">middle</span><span class="p">,</span> <span class="s-Atom">hasnot</span> <span class="p">),</span>
</span><span class='line'> <span class="s-Atom">grasp</span><span class="p">,</span>
</span><span class='line'> <span class="nf">state</span><span class="p">(</span> <span class="s-Atom">middle</span><span class="p">,</span> <span class="s-Atom">onbox</span><span class="p">,</span> <span class="s-Atom">middle</span><span class="p">,</span> <span class="s-Atom">has</span> <span class="p">)).</span>
</span></code></pre></td></tr></table></div></figure>


<p>In English: If the monkey is in the middle of the room and on the box (also in the middle of the room) and it doesn&rsquo;t have the banana and it uses the grasp action then it will have the banana.</p>

<p>Now, let&rsquo;s teach the monkey to climb:</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='prolog'><span class='line'><span class="nf">move</span><span class="p">(</span>
</span><span class='line'> <span class="nf">state</span><span class="p">(</span> <span class="nv">P</span><span class="p">,</span> <span class="s-Atom">onfloor</span><span class="p">,</span> <span class="nv">P</span><span class="p">,</span> <span class="nv">H</span><span class="p">),</span>
</span><span class='line'> <span class="s-Atom">climb</span><span class="p">,</span>
</span><span class='line'> <span class="nf">state</span><span class="p">(</span> <span class="nv">P</span><span class="p">,</span> <span class="s-Atom">onbox</span><span class="p">,</span> <span class="nv">P</span><span class="p">,</span> <span class="nv">H</span> <span class="p">)).</span>
</span></code></pre></td></tr></table></div></figure>


<p>This relation introduces the idea of unbound positions in a relation. In Prolog, if a slot in a relation is filled in with a symbol that begins with capital letter then that slot is unbound. Prolog can often apply rules to figure out what the unbound slots in a relationship should be bound to. If the same capitalized symbol fills multiple slots in a relation then those slots should be bound to the same value once Prolog discovers what the value is.</p>

<p>Now box pushing:</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='prolog'><span class='line'><span class="nf">move</span><span class="p">(</span>
</span><span class='line'> <span class="nf">state</span><span class="p">(</span> <span class="nv">P1</span><span class="p">,</span> <span class="s-Atom">onfloor</span><span class="p">,</span> <span class="nv">P1</span><span class="p">,</span> <span class="nv">H</span><span class="p">),</span>
</span><span class='line'> <span class="nf">push</span><span class="p">(</span> <span class="nv">P1</span><span class="p">,</span> <span class="nv">P2</span> <span class="p">),</span>
</span><span class='line'> <span class="nf">state</span><span class="p">(</span> <span class="nv">P2</span><span class="p">,</span> <span class="s-Atom">onfloor</span> <span class="nv">P2</span><span class="p">,</span> <span class="nv">H</span> <span class="p">)).</span>
</span></code></pre></td></tr></table></div></figure>


<p>The monkey can push the box from one position to another if the monkey and the box are in the same place. Once the monkey is done pushing then the monkey and the box will be in the new place.</p>

<p>Now walking:</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='prolog'><span class='line'><span class="nf">move</span><span class="p">(</span>
</span><span class='line'> <span class="nf">state</span><span class="p">(</span> <span class="nv">P1</span><span class="p">,</span> <span class="s-Atom">onfloor</span><span class="p">,</span> <span class="nv">B</span><span class="p">,</span> <span class="nv">H</span><span class="p">),</span>
</span><span class='line'> <span class="nf">walk</span><span class="p">(</span> <span class="nv">P1</span><span class="p">,</span> <span class="nv">P2</span> <span class="p">),</span>
</span><span class='line'> <span class="nf">state</span><span class="p">(</span> <span class="nv">P2</span><span class="p">,</span> <span class="s-Atom">onfloor</span><span class="p">,</span> <span class="nv">B</span><span class="p">,</span> <span class="nv">H</span> <span class="p">)).</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is like box pushing but only the monkey&rsquo;s position changes in the new world-state.</p>

<p>That&rsquo;s quite enough Prolog for the moment. It&rsquo;s time to introduce a new Core.Logic macro: <em>The Mighty &ldquo;defne&rdquo;</em> (which I like to pronounce &ldquo;Daphne&rdquo;.)</p>

<h1>A <em>defne</em> Interlude</h1>

<p><em>defne</em> combines <em>conde</em> style rules with some powerful sugar that saves us from having to explicitly introduce some of our variables with <em>fresh</em>. It&rsquo;s difficult to grasp until you&rsquo;ve used it so let&rsquo;s try getting there through a few examples.</p>

<p>In the my previous post I defined a relation called parent and added facts to that relation. Here&rsquo;s that again but with <em>defne</em>:</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">defne</span> <span class="nv">parent</span> <span class="p">[</span><span class="nv">elder</span> <span class="nv">child</span><span class="p">]</span>
</span><span class='line'>  <span class="p">([</span><span class="ss">:pam</span> <span class="ss">:bob</span><span class="p">])</span>
</span><span class='line'>  <span class="p">([</span><span class="ss">:tom</span> <span class="ss">:bob</span><span class="p">])</span>
</span><span class='line'>  <span class="p">([</span><span class="ss">:tom</span> <span class="ss">:liz</span><span class="p">])</span>
</span><span class='line'>  <span class="p">([</span><span class="ss">:bob</span> <span class="ss">:ann</span><span class="p">])</span>
</span><span class='line'>  <span class="p">([</span><span class="ss">:bob</span> <span class="ss">:pat</span><span class="p">])</span>
</span><span class='line'>  <span class="p">([</span><span class="ss">:pat</span> <span class="ss">:jim</span><span class="p">]))</span>
</span></code></pre></td></tr></table></div></figure>


<p>The macro-expansion sheds some light on what&rsquo;s going on here:</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nb">macroexpand </span><span class="o">&#39;</span><span class="p">(</span><span class="nf">defne</span> <span class="nv">parent</span> <span class="nv">....</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; approximately expands to</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">parent</span> <span class="p">[</span><span class="nv">elder</span> <span class="nv">child</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">conde</span>
</span><span class='line'>    <span class="p">((</span><span class="nb">== </span><span class="ss">:pam</span> <span class="nv">elder</span><span class="p">)</span>
</span><span class='line'>     <span class="p">(</span><span class="nb">== </span><span class="ss">:bob</span> <span class="nv">child</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">((</span><span class="nb">== </span><span class="ss">:tom</span> <span class="nv">elder</span><span class="p">)</span>
</span><span class='line'>     <span class="p">(</span><span class="nb">== </span><span class="ss">:bob</span> <span class="nv">child</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">;; and so on...</span>
</span><span class='line'>    <span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>We&rsquo;re defining a new function of two variables. This new function uses <em>conde</em> to provide a series of possible relations. Remember, <em>conde</em> is somewhat like a logical OR. Within each option, <em>defne</em> has bound (actually, <a href="http://bit.ly/Qvf8AN">unified</a>) each slot of the vector with the corresponding slot in the arguments to the function.</p>

<p><em>defne</em> isn&rsquo;t very exciting when you put only constants in the binding slots. Let&rsquo;s introduce some variables also:</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="nf">defne</span> <span class="nv">appendo</span> <span class="p">[</span><span class="nv">x</span> <span class="nv">y</span> <span class="nv">z</span><span class="p">]</span>
</span><span class='line'>  <span class="p">([</span> <span class="p">()</span> <span class="nv">_</span> <span class="nv">y</span> <span class="p">])</span>
</span><span class='line'>  <span class="p">([</span> <span class="p">[</span> <span class="nv">a</span> <span class="k">. </span><span class="nv">d</span> <span class="p">]</span> <span class="nv">_</span> <span class="p">[</span> <span class="nv">a</span> <span class="k">. </span><span class="nv">r</span><span class="p">]</span> <span class="p">]</span>
</span><span class='line'>   <span class="p">(</span><span class="nf">appendo</span> <span class="nv">d</span> <span class="nv">y</span> <span class="nv">r</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is quite a leap forward in complexity. Let&rsquo;s take it apart by looking at the macro-expansion:</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="kd">defn </span><span class="nv">appendo</span> <span class="p">[</span><span class="nv">x</span> <span class="nv">y</span> <span class="nv">z</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">conde</span>
</span><span class='line'>    <span class="p">((</span><span class="nb">== </span><span class="p">()</span> <span class="nv">x</span><span class="p">)</span>
</span><span class='line'>     <span class="p">(</span><span class="nb">== </span><span class="nv">y</span> <span class="nv">z</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">((</span><span class="nf">fresh</span> <span class="p">[</span><span class="nv">a</span> <span class="nv">d</span> <span class="nv">r</span><span class="p">]</span>
</span><span class='line'>       <span class="p">(</span><span class="nb">== </span><span class="p">(</span><span class="nf">llist</span> <span class="nv">a</span> <span class="nv">d</span><span class="p">)</span> <span class="nv">x</span><span class="p">)</span>
</span><span class='line'>       <span class="p">(</span><span class="nb">== </span><span class="p">(</span><span class="nf">llist</span> <span class="nv">a</span> <span class="nv">r</span><span class="p">)</span> <span class="nv">z</span><span class="p">)</span>
</span><span class='line'>       <span class="p">(</span><span class="nf">appendo</span> <span class="nv">d</span> <span class="nv">y</span> <span class="nv">r</span><span class="p">)))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>This has expanded to a <em>conde</em> with two cases. The first case applies when <em>X</em> is the empty list. This case also unifies its result variable <em>Z</em> with <em>Y</em>. In English, the result of appending the empty list <em>X</em> onto <em>Y</em> is the same as just <em>Y</em>.</p>

<p>The next case has introduced some new variables using <em>fresh</em>. These correspond to the variables we used in the first part of the second case. Here we&rsquo;re unpacking <em>X</em> into its head <em>A</em> and its tail <em>D</em>. We&rsquo;re also packing something new into our result variable <em>Z</em> and we&rsquo;re recursively calling ourselves. In English, the result of appending the non-empty list <em>X</em> onto <em>Y</em> is the result of appending the tail of <em>X</em> onto <em>Y</em> and then sticking the head of X in front of that.</p>

<p><em>appendo</em> is tricky. The big idea to grasp for this post is that <em>defne</em> pattern matches against its input variables and is useful for unpacking things like lists and doing interesting things with their contents.</p>

<h1>Solution to Monkey and Banana</h1>

<p>Using <em>defne</em> we can encode our monkey movement rules in even less space than Prolog required:</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">defne</span> <span class="nv">moveo</span> <span class="p">[</span><span class="nv">state</span> <span class="nv">move</span> <span class="nv">new-state</span><span class="p">]</span>
</span><span class='line'>  <span class="p">([[</span><span class="ss">:middle</span> <span class="ss">:onbox</span> <span class="ss">:middle</span> <span class="ss">:hasnot</span><span class="p">]</span>
</span><span class='line'>    <span class="ss">:grasp</span>
</span><span class='line'>    <span class="p">[</span><span class="ss">:middle</span> <span class="ss">:onbox</span> <span class="ss">:middle</span> <span class="ss">:has</span><span class="p">]])</span>
</span><span class='line'>
</span><span class='line'>  <span class="p">([[</span><span class="nv">p</span> <span class="ss">:onfloor</span> <span class="nv">p</span> <span class="nv">h</span><span class="p">]</span>
</span><span class='line'>    <span class="ss">:climb</span>
</span><span class='line'>    <span class="p">[</span><span class="nv">p</span> <span class="ss">:onbox</span> <span class="nv">p</span> <span class="nv">h</span><span class="p">]])</span>
</span><span class='line'>
</span><span class='line'>  <span class="p">([[</span><span class="nv">p1</span> <span class="ss">:onfloor</span> <span class="nv">p1</span> <span class="nv">h</span><span class="p">]</span>
</span><span class='line'>    <span class="p">[</span><span class="ss">:push</span> <span class="nv">p1</span> <span class="nv">p2</span><span class="p">]</span>
</span><span class='line'>    <span class="p">[</span><span class="nv">p2</span> <span class="ss">:onfloor</span> <span class="nv">p2</span> <span class="nv">h</span><span class="p">]])</span>
</span><span class='line'>
</span><span class='line'>  <span class="p">([[</span><span class="nv">p1</span> <span class="ss">:onfloor</span> <span class="nv">b</span> <span class="nv">h</span><span class="p">]</span>
</span><span class='line'>    <span class="p">[</span><span class="ss">:walk</span> <span class="nv">p1</span> <span class="nv">p2</span><span class="p">]</span>
</span><span class='line'>    <span class="p">[</span><span class="nv">p2</span> <span class="ss">:onfloor</span> <span class="nv">b</span> <span class="nv">h</span><span class="p">]]))</span>
</span></code></pre></td></tr></table></div></figure>


<p>We can test our move engine by asking for all the moves possible when the monkey and the box are in the middle of the room:</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">run*</span> <span class="p">[</span><span class="nv">q</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">fresh</span> <span class="p">[</span><span class="nv">m</span> <span class="nv">s2</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">moveo</span> <span class="p">[</span><span class="ss">:middle</span> <span class="ss">:onfloor</span> <span class="ss">:middle</span> <span class="ss">:hasnot</span><span class="p">]</span> <span class="nv">m</span> <span class="nv">s2</span><span class="p">)</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">conso</span> <span class="nv">m</span> <span class="p">[</span><span class="nv">s2</span><span class="p">]</span> <span class="nv">q</span><span class="p">)))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; result</span>
</span><span class='line'><span class="p">((</span><span class="ss">:climb</span> <span class="p">[</span><span class="ss">:middle</span> <span class="ss">:onbox</span> <span class="ss">:middle</span> <span class="ss">:hasnot</span><span class="p">])</span>
</span><span class='line'> <span class="p">([</span><span class="ss">:push</span> <span class="ss">:middle</span> <span class="nv">_.0</span><span class="p">]</span> <span class="p">[</span><span class="nv">_.0</span> <span class="ss">:onfloor</span> <span class="nv">_.0</span> <span class="ss">:hasnot</span><span class="p">])</span>
</span><span class='line'> <span class="p">([</span><span class="ss">:walk</span> <span class="ss">:middle</span> <span class="nv">_.0</span><span class="p">]</span> <span class="p">[</span><span class="nv">_.0</span> <span class="ss">:onfloor</span> <span class="ss">:middle</span> <span class="ss">:hasnot</span><span class="p">]))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Core.Logic answers that the monkey can climb, push the box from the middle to somewhere, or walk from the middle to somewhere. Grasping the banana isn&rsquo;t possible from this position.</p>

<p>Now that we&rsquo;ve encoded how moves affect the world-state we just need to find out if there is a sequence of moves that will let the monkey reach its goal. Can the monkey get the banana?</p>

<p>In Prolog:</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>
</pre></td><td class='code'><pre><code class='prolog'><span class='line'><span class="nf">canget</span><span class="p">(</span> <span class="nf">state</span><span class="p">(</span> <span class="k">_</span><span class="p">,</span> <span class="k">_</span><span class="p">,</span> <span class="k">_</span><span class="p">,</span> <span class="s-Atom">has</span> <span class="p">)</span> <span class="p">).</span>
</span><span class='line'>
</span><span class='line'><span class="nf">canget</span><span class="p">(</span> <span class="nv">State1</span> <span class="p">)</span> <span class="p">:-</span>
</span><span class='line'> <span class="nf">move</span><span class="p">(</span> <span class="nv">State1</span><span class="p">,</span> <span class="nv">Move</span><span class="p">,</span> <span class="nv">State2</span> <span class="p">),</span>
</span><span class='line'> <span class="nf">canget</span><span class="p">(</span> <span class="nv">State2</span> <span class="p">).</span>
</span></code></pre></td></tr></table></div></figure>


<p>Or, the monkey can get the banana if it already has the banana or if it makes a move and then can get the banana.</p>

<p>In Core.Logic:</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="p">(</span><span class="nf">defne</span> <span class="nv">cangeto</span> <span class="p">[</span><span class="nv">state</span> <span class="nv">out</span><span class="p">]</span>
</span><span class='line'>  <span class="p">([[</span><span class="nv">_</span> <span class="nv">_</span> <span class="nv">_</span> <span class="ss">:has</span><span class="p">]</span> <span class="nv">true</span><span class="p">])</span>
</span><span class='line'>  <span class="p">([</span><span class="nv">_</span> <span class="nv">_</span><span class="p">]</span> <span class="p">(</span><span class="nf">fresh</span>
</span><span class='line'>          <span class="p">[</span><span class="nv">move</span> <span class="nv">state2</span><span class="p">]</span>
</span><span class='line'>          <span class="p">(</span><span class="nf">moveo</span> <span class="nv">state</span> <span class="nv">move</span> <span class="nv">state2</span><span class="p">)</span>
</span><span class='line'>          <span class="p">(</span><span class="nf">cangeto</span> <span class="nv">state2</span> <span class="nv">out</span><span class="p">))))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now, finally, we can ask if our monkey can get to its banana:</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="nf">run</span> <span class="mi">1</span> <span class="p">[</span><span class="nv">q</span><span class="p">]</span> <span class="p">(</span><span class="nf">cangeto</span> <span class="p">[</span><span class="ss">:atdoor</span> <span class="ss">:onfloor</span> <span class="ss">:atwindow</span> <span class="ss">:hasnot</span><span class="p">]</span> <span class="nv">q</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; result</span>
</span><span class='line'><span class="p">(</span><span class="nf">true</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>A happy ending.</p>

<h1>Postlude</h1>

<p>Ivan Bratko makes the point that the Prolog version of this program is sensitive to the order of its rules. If you re-arrange the final two expressions in <em>canget</em> then the resulting program will never find a solution:</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='prolog'><span class='line'><span class="nf">canget</span><span class="p">(</span> <span class="nv">State1</span> <span class="p">)</span> <span class="p">:-</span>
</span><span class='line'> <span class="nf">canget</span><span class="p">(</span> <span class="nv">State2</span> <span class="p">),</span>
</span><span class='line'> <span class="nf">move</span><span class="p">(</span> <span class="nv">State1</span><span class="p">,</span> <span class="nv">Move</span><span class="p">,</span> <span class="nv">State2</span> <span class="p">).</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is because Prolog does a depth-first search of its solution space. The Prolog version of this program gets stuck recursing into the next call to <em>canget</em> before it ever constrains State2 by applying a new Move.</p>

<p>Happily, Core.Logic isn&rsquo;t sensitive in this way and will execute its similarly altered program to the correct conclusion:</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="nf">defne</span> <span class="nv">cangeto</span> <span class="p">[</span><span class="nv">state</span> <span class="nv">out</span><span class="p">]</span>
</span><span class='line'>  <span class="p">([[</span><span class="nv">_</span> <span class="nv">_</span> <span class="nv">_</span> <span class="ss">:has</span><span class="p">]</span> <span class="nv">true</span><span class="p">])</span>
</span><span class='line'>  <span class="p">([</span><span class="nv">_</span> <span class="nv">_</span><span class="p">]</span> <span class="p">(</span><span class="nf">fresh</span>
</span><span class='line'>          <span class="p">[</span><span class="nv">move</span> <span class="nv">state2</span><span class="p">]</span>
</span><span class='line'>          <span class="p">(</span><span class="nf">cangeto</span> <span class="nv">state2</span> <span class="nv">out</span><span class="p">)))</span>
</span><span class='line'>          <span class="p">(</span><span class="nf">moveo</span> <span class="nv">state</span> <span class="nv">move</span> <span class="nv">state2</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">run</span> <span class="mi">1</span> <span class="p">[</span><span class="nv">q</span><span class="p">]</span> <span class="p">(</span><span class="nf">cangeto</span> <span class="p">[</span><span class="ss">:atdoor</span> <span class="ss">:onfloor</span> <span class="ss">:atwindow</span> <span class="ss">:hasnot</span><span class="p">]</span> <span class="nv">q</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; result</span>
</span><span class='line'><span class="p">(</span><span class="nf">true</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Credit:</p>

<p>I got stuck in my first attempt to port the monkey and the banana. Happily, David Nolen had already <a href="https://github.com/clojure/core.logic/#examples">written an example solution to this problem</a> and I was able to study his example. He has implemented <a href="https://github.com/clojure/core.logic/blob/master/src/main/clojure/clojure/core/logic/bench.clj">other interesting examples</a> as benchmarks for Core.Logic.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[PPfAI Chapter 1 in Clojure Core.Logic]]></title>
    <link href="http://www.50ply.com/blog/2012/07/29/pfai-to-core-dot-logic/"/>
    <updated>2012-07-29T09:18:00-04:00</updated>
    <id>http://www.50ply.com/blog/2012/07/29/pfai-to-core-dot-logic</id>
    <content type="html"><![CDATA[<p>David Nolen mentioned Ivan Bratko&rsquo;s <a href="http://www.amazon.com/gp/product/0321417461/ref=as_li_ss_tl?ie=UTF8&amp;tag=50ply-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321417461">Prolog Programming For Artificial Intelligence</a> at a <a href="http://vimeo.com/46163091">talk he gave recently demoing cKanren extensions in Core.Logic</a>. I eagerly picked up an old edition of the book for $5 and got to work.</p>

<p>My other exposure to logic programming has been through the prolog compiler that Peter Norvig presents in <a href="http://www.amazon.com/gp/product/1558601910/ref=as_li_ss_tl?ie=UTF8&amp;tag=50ply-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1558601910">Paradigms of Artificial Intelligence Programming</a> and studying mini-Kanren via Friedman&rsquo;s <a href="http://www.amazon.com/gp/product/0262562146/ref=as_li_ss_tl?ie=UTF8&amp;tag=50ply-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0262562146">The Reasoned Schemer</a>. Dan Friedman and William Byrd also <a href="http://blip.tv/clojure/dan-friedman-and-william-byrd-minikanren-5936333">gave a presentation</a> at a Clojure conj.</p>

<p>Chapter 1 of Ivan Bratko&rsquo;s book introduces an example logic program that reasons about relationships in a family.</p>

<p>The program starts out by defining some facts:</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>
</pre></td><td class='code'><pre><code class='prolog'><span class='line'><span class="cm">/* pam is bob&#39;s parent, and so on... */</span>
</span><span class='line'><span class="nf">parent</span><span class="p">(</span><span class="s-Atom">pam</span><span class="p">,</span> <span class="s-Atom">bob</span><span class="p">)</span>
</span><span class='line'><span class="nf">parent</span><span class="p">(</span><span class="s-Atom">tom</span><span class="p">,</span> <span class="s-Atom">bob</span><span class="p">)</span>
</span><span class='line'><span class="nf">parent</span><span class="p">(</span><span class="s-Atom">tom</span><span class="p">,</span> <span class="s-Atom">liz</span><span class="p">)</span>
</span><span class='line'><span class="nf">parent</span><span class="p">(</span><span class="s-Atom">bob</span><span class="p">,</span> <span class="s-Atom">ann</span><span class="p">)</span>
</span><span class='line'><span class="nf">parent</span><span class="p">(</span><span class="s-Atom">bob</span><span class="p">,</span> <span class="s-Atom">pat</span><span class="p">)</span>
</span><span class='line'><span class="nf">parent</span><span class="p">(</span><span class="s-Atom">pat</span><span class="p">,</span> <span class="s-Atom">jim</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="cm">/* pam is female... */</span>
</span><span class='line'><span class="nf">female</span><span class="p">(</span><span class="s-Atom">pam</span><span class="p">)</span>
</span><span class='line'><span class="nf">male</span><span class="p">(</span><span class="s-Atom">tom</span><span class="p">)</span>
</span><span class='line'><span class="nf">male</span><span class="p">(</span><span class="s-Atom">bob</span><span class="p">)</span>
</span><span class='line'><span class="nf">female</span><span class="p">(</span><span class="s-Atom">liz</span><span class="p">)</span>
</span><span class='line'><span class="nf">female</span><span class="p">(</span><span class="s-Atom">ann</span><span class="p">)</span>
</span><span class='line'><span class="nf">female</span><span class="p">(</span><span class="s-Atom">pat</span><span class="p">)</span>
</span><span class='line'><span class="nf">male</span><span class="p">(</span><span class="s-Atom">jim</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>We can translate this into Clojure Core.Logic fairly directly:</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="c1">;; define the relations and their arity</span>
</span><span class='line'><span class="p">(</span><span class="nf">defrel</span> <span class="nv">parent</span> <span class="nv">p</span> <span class="nv">c</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">defrel</span> <span class="nv">male</span> <span class="nv">p</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">defrel</span> <span class="nv">female</span> <span class="nv">p</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; add facts to the relations. Again, :pam is :bob&#39;s parent</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">parent</span> <span class="ss">:pam</span> <span class="ss">:bob</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">parent</span> <span class="ss">:tom</span> <span class="ss">:bob</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">parent</span> <span class="ss">:tom</span> <span class="ss">:liz</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">parent</span> <span class="ss">:bob</span> <span class="ss">:ann</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">parent</span> <span class="ss">:bob</span> <span class="ss">:pat</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">parent</span> <span class="ss">:pat</span> <span class="ss">:jim</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; and :pam is female</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">female</span> <span class="ss">:pam</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">male</span> <span class="ss">:tom</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">male</span> <span class="ss">:bob</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">female</span> <span class="ss">:liz</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">female</span> <span class="ss">:ann</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">female</span> <span class="ss">:pat</span><span class="p">)</span>
</span><span class='line'><span class="p">(</span><span class="nf">fact</span> <span class="nv">male</span> <span class="ss">:jim</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>At this point we can ask the program to recite the facts back to us. For example, who are Bob&rsquo;s parents?</p>

<p>In prolog:</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>
</pre></td><td class='code'><pre><code class='prolog'><span class='line'><span class="s-Atom">?-</span> <span class="nf">parent</span><span class="p">(</span><span class="nv">X</span><span class="p">,</span> <span class="s-Atom">bob</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>and prolog answers:</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>
</pre></td><td class='code'><pre><code class='prolog'><span class='line'><span class="nv">X</span> <span class="o">=</span> <span class="s-Atom">tom</span>
</span><span class='line'><span class="nv">X</span> <span class="o">=</span> <span class="s-Atom">pam</span>
</span></code></pre></td></tr></table></div></figure>


<p>In Clojure&rsquo;s Core.Logic:</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">run*</span> <span class="p">[</span><span class="nv">q</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">parent</span> <span class="nv">q</span> <span class="ss">:bob</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>


<p>and Core.logic answers:</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="ss">:tom</span> <span class="ss">:pam</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Things get more interesting when we start adding rules to the program so that the system can start deriving new facts. For example, we can add a rule describing what a mother is:</p>

<p>In prolog:</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='prolog'><span class='line'><span class="nf">mother</span><span class="p">(</span><span class="nv">X</span><span class="p">,</span> <span class="nv">Y</span><span class="p">)</span> <span class="p">:-</span>
</span><span class='line'>  <span class="nf">parent</span><span class="p">(</span><span class="nv">X</span><span class="p">,</span> <span class="nv">Y</span><span class="p">),</span>
</span><span class='line'>  <span class="nf">female</span><span class="p">(</span><span class="nv">X</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>This reads: &ldquo;X is the mother of Y if X is the parent of Y and X is female.&rdquo;</p>

<p>In Core.logic:</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">mother</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">fresh</span> <span class="p">[]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">parent</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">female</span> <span class="nv">x</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we can ask who Bob&rsquo;s mother is:</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">run*</span> <span class="p">[</span><span class="nv">q</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">mother</span> <span class="nv">q</span> <span class="ss">:bob</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; answer</span>
</span><span class='line'><span class="p">(</span><span class="ss">:pam</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Or, we can move the variable and ask we has Pam as a mother:</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">run*</span> <span class="p">[</span><span class="nv">q</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">mother</span> <span class="ss">:pam</span> <span class="nv">q</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; answer</span>
</span><span class='line'><span class="p">(</span><span class="ss">:bob</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>I&rsquo;ll provide the rest of the Core.logic version of the family program without much explanation. The important things you need to know to understand this code are:</p>

<ol>
<li><em>run*</em> executes a logic program for all of its solutions</li>
<li><em>fresh</em> creates new lexically scoped, unbound variables. All clauses inside <em>fresh</em> are logically AND&rsquo;d together</li>
<li><em>conde</em> similar to an OR. Each <em>conde</em> clause is asserted independently of the others.</li>
</ol>


<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>
<span class='line-number'>37</span>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="c1">;; C is the offspring of P if P is the parent of C</span>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">offspring</span> <span class="p">[</span><span class="nv">c</span> <span class="nv">p</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">parent</span> <span class="nv">p</span> <span class="nv">c</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; X is the grandparent of Z if, for some Y, X is the parent</span>
</span><span class='line'><span class="c1">;; of Y and Y is the parent of Z</span>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">grandparent</span> <span class="p">[</span><span class="nv">x</span> <span class="nv">z</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">fresh</span> <span class="p">[</span><span class="nv">y</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">parent</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">parent</span> <span class="nv">y</span> <span class="nv">z</span><span class="p">)))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; X is the grandmother of Y if X is the grandparent of Y and</span>
</span><span class='line'><span class="c1">;; X is a female</span>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">grandmother</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">fresh</span> <span class="p">[]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">grandparent</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">female</span> <span class="nv">x</span><span class="p">)))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; X is the sister of Y if, for some Z, Z is the parent of X</span>
</span><span class='line'><span class="c1">;; and that same Z is the parent of Y and X is female and</span>
</span><span class='line'><span class="c1">;; Y and X are not the same person.</span>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">sister</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">fresh</span> <span class="p">[</span><span class="nv">z</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">parent</span> <span class="nv">z</span> <span class="nv">x</span><span class="p">)</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">parent</span> <span class="nv">z</span> <span class="nv">y</span><span class="p">)</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">female</span> <span class="nv">x</span><span class="p">)</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">!=</span> <span class="nv">x</span> <span class="nv">y</span><span class="p">)))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; X is a predecessor of Z if X is the parent of Z or if</span>
</span><span class='line'><span class="c1">;; X is the parent of some Y and that Y is a predecessor</span>
</span><span class='line'><span class="c1">;; of Z</span>
</span><span class='line'><span class="p">(</span><span class="kd">defn </span><span class="nv">predecessor</span> <span class="p">[</span><span class="nv">x</span> <span class="nv">z</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">conde</span>
</span><span class='line'>    <span class="p">[(</span><span class="nf">parent</span> <span class="nv">x</span> <span class="nv">z</span><span class="p">)]</span>
</span><span class='line'>    <span class="p">[(</span><span class="nf">fresh</span> <span class="p">[</span><span class="nv">y</span><span class="p">]</span>
</span><span class='line'>       <span class="p">(</span><span class="nf">parent</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">predecessor</span> <span class="nv">y</span> <span class="nv">z</span><span class="p">))]))</span>
</span></code></pre></td></tr></table></div></figure>


<p>The final rule is especially interesting because it introduces logical recursion. Notice that predecessor is defined in terms of itself.</p>

<p>Now some examples of what this program can derive:</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">run*</span> <span class="p">[</span><span class="nv">q</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">predecessor</span> <span class="nv">q</span> <span class="ss">:jim</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; answer</span>
</span><span class='line'><span class="p">(</span><span class="ss">:pat</span> <span class="ss">:bob</span> <span class="ss">:tom</span> <span class="ss">:pam</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">run*</span> <span class="p">[</span><span class="nv">q</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">grandparent</span> <span class="nv">q</span> <span class="ss">:ann</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; answer</span>
</span><span class='line'><span class="p">(</span><span class="ss">:tom</span> <span class="ss">:pam</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="nf">run*</span> <span class="p">[</span><span class="nv">q</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">grandmother</span> <span class="nv">q</span> <span class="ss">:ann</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'><span class="c1">;; answer</span>
</span><span class='line'><span class="p">(</span><span class="ss">:pam</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now, go forth and build your family tree! Or, better yet, go solve <a href="http://www.reddit.com/r/dailyprogrammer/comments/v3afh/6152012_challenge_65_intermediate/">Daily Programmer #65</a> without building a graph!</p>

<p><em>EDIT</em>: There was a typo in my first example. Thank you <em>zerokarmaleft</em> for pointing it out.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Cljs-Bench]]></title>
    <link href="http://www.50ply.com/blog/2012/07/28/building-cljs-bench/"/>
    <updated>2012-07-28T21:53:00-04:00</updated>
    <id>http://www.50ply.com/blog/2012/07/28/building-cljs-bench</id>
    <content type="html"><![CDATA[<p>Those who have worked with me know that I can become a bit obsessed with optimizing software. I like to think that as I&rsquo;ve matured as a developer I&rsquo;ve become more strategic and disciplined in my optimizations. Regardless, when programming, I&rsquo;m often at my happiest when I&rsquo;m making code go faster.</p>

<p>About two months ago I started a project to track how the performance of Clojurescript evolved with each commit to the repository. The project produces plots that look like this:</p>

<p><img class="center" src="http://www.50ply.com/cljs-bench/30.png"></p>

<p>Each line of the plot represents a different Clojurescript runtime environment. On this plot we see Spidermonkey (the javascript implementation found in Firefox), JavaScriptCore (Webkit), and V8 (Chrome.) Also, included for comparison, is the same test running in Clojure on the JVM. I&rsquo;ve carefully selected one of the cases where Clojurescript actually perfoms better than Clojure.</p>

<p>You can see all of the plots <a href="http://50ply.com/cljs-bench">here</a>.</p>

<p>Grab the source <a href="http://github.com/netguy204/cljs-bench/">here</a>.</p>

<p>One of my favorite engineering professors in undergrad told me that &ldquo;measurement precedes control.&rdquo; This is just as true when optimizing software as it is when building actuator control systems.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Introducing Fast, Persistent Sets in Ruby]]></title>
    <link href="http://www.50ply.com/blog/2012/07/21/introducing-fast/"/>
    <updated>2012-07-21T08:07:00-04:00</updated>
    <id>http://www.50ply.com/blog/2012/07/21/introducing-fast</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://www.50ply.com/images/rfpsets/nsets1.png" width="360" height="246" title="Time (ms) vs number of elements. 1 set intersection" ></p>

<ul>
<li><p><em>Fast</em> - FPSet can compute an intersection in <em>O( min(n) * m )</em> time. In space, the algorithm is <em>O(m)</em>. <em>m</em> is the number of sets. <em>n</em> is the number of elements in each set.</p></li>
<li><p><em>Persistent</em> - Each set can be larger than available memory because the intersection happens while data is streamed from the disk. FPSet does not need to have the sets entirely in memory to perform the intersection.</p></li>
</ul>


<p><a href="http://github.com/netguy204/rfpset">Get the source</a></p>

<p><img class="right" src="http://www.50ply.com/images/rfpsets/nsets2.png" width="360" height="246" title="Time (ms) vs number of elements. 2 set intersection" ></p>

<p><strong>Plots</strong></p>

<p>These plots show the runtime of FPSet (blue) compared with the runtime of Ruby&rsquo;s Array intersect (red) as a function of the size of the set being intersected. Check the alt-text of the image for more details.</p>

<p><strong>How it works</strong></p>

<p>FPSet takes any Ruby enumerable, serializes each member, sorts it, de-duplicates it, compresses it, and then writes it to a file. Creating a new set isn&rsquo;t quick. FPSet is intended for applications that need very fast intersections of large sets but don&rsquo;t expect to change those sets very often.</p>

<p><img class="right" src="http://www.50ply.com/images/rfpsets/nsets3.png" width="360" height="246" title="Time (ms) vs number of elements. 3 set intersection" ></p>

<p>When you ask FPSet to compute the set intersection of several files it opens all of those files and decompresses just enough of the first file to find the first element in that set. It doesn&rsquo;t bother to deserialize that element. Instead, at the byte level, FPSet compares that element with the first element of the next set (again, decompressing just-enough.) If the element in the second set is less than the element being searched for then that lower element cannot be in the intersection. It is discarded and the next element is read. This continues until FPSet finds a match or an element that is greater than the element being searched for. If FPSet finds a match then it compares with the third set and so on. If FPSet instead finds a greater element then the element that was being searched for cannot be in the intersection. However, the greater element may be the next member of the intersection so FPSet makes that the new searched-for element and continues streaming elements from the first set looking for a value that is equal to or greater than this new searched-for element.</p>

<p>If we exhaust all of the elements in one set at any point during the algorithm we know that we&rsquo;ve found the complete intersection. This is where the <em>min(n)</em> comes from in our time complexity.</p>

<p><img class="right" src="http://www.50ply.com/images/rfpsets/nsets5.png" width="360" height="246" title="Time (ms) vs number of elements. 5 set intersection" ></p>

<p><strong>Why C?</strong></p>

<p>The core algorithm of FPSet is actually implemented in <strike>ANSI</strike> C. There are two good reasons for doing this. First, the byte level comparisons that are behind the sort and intersect steps of the algorithm can be very clearly and efficiently represented in C. Second, and most important for performance, the intersection algorithm in C is expressed in a way that doesn&rsquo;t allocate or free memory (no GC churn!) I&rsquo;m not sure how/if one can write a Ruby equivalent of this algorithm that doesn&rsquo;t keep the garbage collector busy.</p>

<p><img class="right" src="http://www.50ply.com/images/rfpsets/nsets6.png" width="360" height="246" title="Time (ms) vs number of elements. 6 set intersection" ></p>

<p><strong>Compression</strong></p>

<p>Using compression when reading and writing was an interesting <em>optimization</em>. By far, the most expensive part of this algorithm is reading data from the disk. FPSet is completely IO bound. Modern processors are incredibly fast relative to modern disks (even SSDs.) My first implementation of FPSet wrote exactly the bytes that the algorithm would later read and compare. I discovered that the intersection was significantly faster if I added a decompression step and thus have fewer bytes to read. In my benchmarks, the runtime of FPSet decreased by 30-40% when I zlib (level 2) compressed the data.</p>

<p><img class="right" src="http://www.50ply.com/images/rfpsets/nsets7.png" width="360" height="246" title="Time (ms) vs number of elements. 7 set intersection" ></p>

<p><strong>Set Contents</strong></p>

<p>You can store anything in an FPSet that Ruby knows how to Marshal.dump. I don&rsquo;t know if there&rsquo;s any value-like thing that Ruby doesn&rsquo;t know how to marshal so I think this is a pretty loose restriction. The byte representation produced by Marshal.dump tends to be sparse but the compression we use tightens it up and makes it efficient on the disk.</p>

<p><strong>RubyGems</strong></p>

<p>FPSet is available through RubyGems under the name rfpset. You get it in the usual way:</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>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>gem install rfpset
</span></code></pre></td></tr></table></div></figure>


<p>You can also build the gem from source:</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>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>gem build rfpset.gemspec
</span><span class='line'>gem install rfpset-0.0.1.gem
</span></code></pre></td></tr></table></div></figure>


<p><em>Edit</em>: My C is ANSI in the same way my shoes are ANSI&hellip; not at all. But, just use gcc like a normal person and you&rsquo;ll be happy.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Automating Debian Installs With Preseed and Puppet]]></title>
    <link href="http://www.50ply.com/blog/2012/07/16/automating-debian-installs-with-preseed-and-puppet/"/>
    <updated>2012-07-16T20:27:00-04:00</updated>
    <id>http://www.50ply.com/blog/2012/07/16/automating-debian-installs-with-preseed-and-puppet</id>
    <content type="html"><![CDATA[<p>Setting up a batch of new servers can be tedious. Updating and maintaining those systems over their lifetimes is also tedious. Here&rsquo;s some DevOps magic to relieve some of that tedium.</p>

<p>The end state we&rsquo;re going for is a new installation of Debian Wheezy with Puppet installed and ready to maintain and update the configuration as our needs evolve.</p>

<p>To follow this recipe to-the-letter you will need an already existing Debian system to build the custom installation media. You will also need a web server to host some post-install scripts. I also have a Debian apt cache on my network to speed up the installation of multiple machines.</p>

<h1>Step 1: Automate the Installation</h1>

<p>There are a number of ways to do this in Debian and the wonderful <a href="http://debian-handbook.info">Debian Administrator&rsquo;s Handbook</a> is where you can read about all of them. The technique I chose is called preseeding. The standard Debian installer is designed so that all of the questions it asks you can be answered in advance in a special file called the preseed file. Here&rsquo;s a preseed file I put together.</p>

<figure class='code'><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>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>d-i debian-installer/locale string en_US
</span><span class='line'>d-i console-keymaps-at/keymap select us
</span><span class='line'>d-i netcfg/choose_interface select auto
</span><span class='line'>
</span><span class='line'># Use a local mirror instead of hitting the internet.
</span><span class='line'># You should change this to an appropriate mirror for your
</span><span class='line'># setup
</span><span class='line'>d-i mirror/country string manual
</span><span class='line'>d-i mirror/http/hostname string my-debian-mirror.mydomain.com
</span><span class='line'>d-i mirror/http/directory string /debian
</span><span class='line'>d-i mirror/http/proxy string
</span><span class='line'>
</span><span class='line'># Set up a user for me. This password is encrypted using the following
</span><span class='line'># command line:
</span><span class='line'>#
</span><span class='line'># printf "r00tme" | mkpasswd -s -m md5
</span><span class='line'>#
</span><span class='line'># Since I've disabled root login, my user will be set up automatically
</span><span class='line'># with the ability to sudo.
</span><span class='line'>d-i passwd/root-login boolean false
</span><span class='line'>d-i passwd/user-fullname string Brian Taylor
</span><span class='line'>d-i passwd/username string btaylor
</span><span class='line'>d-i passwd/user-password-crypted password $1$/Gd5fGd7$QNvq.odwNPXLo/HRuzdkw.
</span><span class='line'>
</span><span class='line'>d-i clock-setup/utc boolean true
</span><span class='line'>d-i time/zone string US/Eastern
</span><span class='line'>
</span><span class='line'># I also run NTP on my network so I'd like the installer to sync with
</span><span class='line'># my NTP server instead of going to the internet
</span><span class='line'>d-i clock-setup/ntp boolean true
</span><span class='line'>d-i clock-setup/ntp-server string my-time-server.mydomain.com
</span><span class='line'>
</span><span class='line'># Make all of the default decisions for partitioning my one drive. You
</span><span class='line'># can configure any setup that the installer can produce here but it
</span><span class='line'># can get complicated in a hurry
</span><span class='line'>d-i partman-auto/disk string /dev/sda
</span><span class='line'>d-i partman-auto/method string regular
</span><span class='line'>
</span><span class='line'># Shut-up any warnings if there are existing LVM or RAID devices
</span><span class='line'>d-i partman-lvm/device_remove_lvm boolean true
</span><span class='line'>d-i partman-md/device_remove_md boolean true
</span><span class='line'>
</span><span class='line'># Pick the "everything in one partition" layout
</span><span class='line'>d-i partman-auto/choose_recipe select atomic
</span><span class='line'>
</span><span class='line'># Say yes to all the standard "are you sure you want to delete your
</span><span class='line'># disk" warnings.
</span><span class='line'>d-i partman-partitioning/confirm_write_new_label boolean true
</span><span class='line'>d-i partman/choose_partition select finish
</span><span class='line'>d-i partman/confirm boolean true
</span><span class='line'>d-i partman/confirm_nooverwrite boolean true
</span><span class='line'>
</span><span class='line'># Make sure my local mirror will land in /etc/apt/sources.list when
</span><span class='line'># we're all done here.
</span><span class='line'>d-i apt-setup/local0/repository string http://my-debian-mirror.mydomain.com/debian/ squeeze main restricted universe multiverse
</span><span class='line'>
</span><span class='line'># Just a basic installation. But make sure I have SSH, puppet, and curl.
</span><span class='line'>tasksel tasksel/first multiselect standard
</span><span class='line'>d-i pkgsel/include string openssh-server puppet curl
</span><span class='line'>d-i pkgsel/upgrade select safe-upgrade
</span><span class='line'>
</span><span class='line'># Put GRUB on that disk as well. Install anyway even if there's already
</span><span class='line'># an MBR on the diskd-i grub-installer/only_debian boolean true
</span><span class='line'>d-i grub-installer/with_other_os boolean true
</span><span class='line'>
</span><span class='line'># I like to opt out of this
</span><span class='line'>d-i popularity-contest/participate boolean false
</span><span class='line'>
</span><span class='line'># Once the installation is done we'll set the system up for some firstboot
</span><span class='line'># magic.
</span><span class='line'>d-i preseed/late_command string chroot /target sh -c "/usr/bin/curl -o /tmp/postinstall http://my-web-server.mydomain.com/postinstall && /bin/sh -x /tmp/postinstall"</span></code></pre></td></tr></table></div></figure>


<h1>Step 2. Automate First-Boot Tasks</h1>

<p>The final line of our preseed file runs a script at the end of the installation process. There&rsquo;s not much room here so we just tell the installer to grab another script from our web-server and run that chrooted in our newly installed system. But, we&rsquo;re still running under the installer and the installer&rsquo;s post-install environment is pretty limiting. So, from our second script, we set up a service that will run a third script when the computer boots into its new installation for the first time. I&rsquo;ll do all my real work in the third script.</p>

<p>Here&rsquo;s the second postinstall script (the one that the script in the installer grabs and runs.) I host it at <a href="http://my-web-server.mydomain.com/postinstall:">http://my-web-server.mydomain.com/postinstall:</a></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>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c">#!/bin/sh</span>
</span><span class='line'>
</span><span class='line'><span class="c"># grab our firstboot script</span>
</span><span class='line'>/usr/bin/curl -o /root/firstboot http://my-web-server.mydomain.com/firstboot
</span><span class='line'>chmod +x /root/firstboot
</span><span class='line'>
</span><span class='line'><span class="c"># create a service that will run our firstboot script</span>
</span><span class='line'>cat &gt; /etc/init.d/firstboot <span class="s">&lt;&lt;EOF</span>
</span><span class='line'><span class="s">### BEGIN INIT INFO</span>
</span><span class='line'><span class="s"># Provides:        firstboot</span>
</span><span class='line'><span class="s"># Required-Start:  $networking</span>
</span><span class='line'><span class="s"># Required-Stop:   $networking</span>
</span><span class='line'><span class="s"># Default-Start:   2 3 4 5</span>
</span><span class='line'><span class="s"># Default-Stop:    0 1 6</span>
</span><span class='line'><span class="s"># Short-Description: A script that runs once</span>
</span><span class='line'><span class="s"># Description: A script that runs once</span>
</span><span class='line'><span class="s">### END INIT INFO</span>
</span><span class='line'>
</span><span class='line'><span class="s">cd /root ; /usr/bin/nohup sh -x /root/firstboot &amp;</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="s">EOF</span>
</span><span class='line'>
</span><span class='line'><span class="c"># install the firstboot service</span>
</span><span class='line'>chmod +x /etc/init.d/firstboot
</span><span class='line'>update-rc.d firstboot defaults
</span><span class='line'>
</span><span class='line'><span class="nb">echo</span> <span class="s2">&quot;finished postinst&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<h1>Step 3. Upgrade to Wheezy</h1>

<p>What should we do with our newly installed Debian Squeeze system? Upgrade it to Wheezy, of course! In my firstboot script, I use the preseed mechanism again to answer the questions that dist-upgrade is going to ask me. I swap out my sources.list with one that points to Wheezy and we&rsquo;re off.</p>

<p>I wait until after I&rsquo;ve upgraded to Wheezy to install my Puppet configuration. There are some changes to the Debian maintainer supplied version of the configs when we go from Squeeze to Wheezy and I want to make sure that my changes win.</p>

<p>This is the third post install script. It&rsquo;s run the first time our newly installed Debian system boots. I host this script at <a href="http://my-web-server.mydomain.com/firstboot">http://my-web-server.mydomain.com/firstboot</a></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>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c">#!/bin/sh</span>
</span><span class='line'>
</span><span class='line'>
</span><span class='line'><span class="c"># This script will run the first time the system boots. Even</span>
</span><span class='line'><span class="c"># though we&#39;ve told it to run after networking is enabled,</span>
</span><span class='line'><span class="c"># I&#39;ve observed inconsistent behavior if we start hitting the</span>
</span><span class='line'><span class="c"># net immediately.</span>
</span><span class='line'><span class="c">#</span>
</span><span class='line'><span class="c"># Introducing a brief sleep makes things work right all the</span>
</span><span class='line'><span class="c"># time.</span>
</span><span class='line'>sleep 30
</span><span class='line'>
</span><span class='line'><span class="c"># install our new sources</span>
</span><span class='line'>cat &gt; /etc/apt/sources.list <span class="s">&lt;&lt;EOF</span>
</span><span class='line'><span class="s">deb http://my-debian-mirror.mydomain.com/debian wheezy main</span>
</span><span class='line'><span class="s">EOF</span>
</span><span class='line'>
</span><span class='line'><span class="c"># update apt</span>
</span><span class='line'>/usr/bin/apt-get update
</span><span class='line'>
</span><span class='line'><span class="c"># install our preseed so libc doesn&#39;t whine</span>
</span><span class='line'>cat &gt; /tmp/wheezy.preseed <span class="s">&lt;&lt;EOF</span>
</span><span class='line'><span class="s">libc6 glibc/upgrade boolean true</span>
</span><span class='line'><span class="s">libc6 glibc/restart-services string</span>
</span><span class='line'><span class="s">libc6 libraries/restart-without-asking boolean true</span>
</span><span class='line'><span class="s">EOF</span>
</span><span class='line'>
</span><span class='line'>/usr/bin/debconf-set-selections /tmp/wheezy.preseed
</span><span class='line'>
</span><span class='line'><span class="c"># do the dist-upgrade</span>
</span><span class='line'>/usr/bin/apt-get -y dist-upgrade
</span><span class='line'>
</span><span class='line'><span class="c"># configure puppet to look for the puppetmaster at a specific</span>
</span><span class='line'><span class="c"># machine. I really don&#39;t like the default of always naming</span>
</span><span class='line'><span class="c"># the puppet master &quot;puppet&quot;. This gets around that.</span>
</span><span class='line'>
</span><span class='line'>cat &gt; /etc/default/puppet <span class="s">&lt;&lt;EOF</span>
</span><span class='line'><span class="s"># Defaults for puppet - sourced by /etc/init.d/puppet</span>
</span><span class='line'>
</span><span class='line'><span class="s"># Start puppet on boot?</span>
</span><span class='line'><span class="s">START=yes</span>
</span><span class='line'>
</span><span class='line'><span class="s"># Startup options</span>
</span><span class='line'><span class="s">DAEMON_OPTS=&quot;&quot;</span>
</span><span class='line'><span class="s">EOF</span>
</span><span class='line'>
</span><span class='line'>cat &gt; /etc/puppet/puppet.conf <span class="s">&lt;&lt;EOF</span>
</span><span class='line'><span class="s">[main]</span>
</span><span class='line'><span class="s">logdir=/var/log/puppet</span>
</span><span class='line'><span class="s">vardir=/var/lib/puppet</span>
</span><span class='line'><span class="s">ssldir=/var/lib/puppet/ssl</span>
</span><span class='line'><span class="s">rundir=/var/run/puppet</span>
</span><span class='line'><span class="s">factpath=$vardir/lib/facter</span>
</span><span class='line'><span class="s">templatedir=$confdir/templates</span>
</span><span class='line'><span class="s">server=my-puppet-master.mydomain.com</span>
</span><span class='line'><span class="s">EOF</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Remove our firstboot service so that it won&#39;t run again</span>
</span><span class='line'>update-rc.d firstboot remove
</span><span class='line'>
</span><span class='line'><span class="c"># Reboot into the new kernel</span>
</span><span class='line'>/sbin/reboot
</span></code></pre></td></tr></table></div></figure>


<p>Now when the system comes up for the second time it will connect to the appropriate puppetmaster and we can manage it from there using Puppet manifests.</p>

<h1>Creating the Installation Media</h1>

<p>To start a new machine down our path-to-glory we&rsquo;ll need some kind of installation media. There are instructions online for starting a preseed installation using network booting. I opted to build a CD image instead. Thankfully, the debian tool simple-cdd makes this process quite painless.</p>

<p>Here&rsquo;s my simple-cdd config:</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>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="c"># simple-cdd.conf minimal configuration file</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Note: this is only an example, it is recommended to only add configuration</span>
</span><span class='line'><span class="c"># values as you need them.</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Profiles to include on the CD</span>
</span><span class='line'><span class="nv">profiles</span><span class="o">=</span><span class="s2">&quot;default&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="c"># set default locale</span>
</span><span class='line'><span class="nv">locale</span><span class="o">=</span><span class="s2">&quot;en_US&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="c"># Mirror for security updates</span>
</span><span class='line'><span class="c">#   Expects security updates to be in dists/DEBIAN_DIST/updates</span>
</span><span class='line'><span class="nv">security_mirror</span><span class="o">=</span><span class="s2">&quot;http://security.debian.org/&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here are the steps:</p>

<p><em>Install simple-cdd</em></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>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>sudo apt-get install simple-cdd
</span></code></pre></td></tr></table></div></figure>


<p><em>Place the preseed file where simple-cdd can find it</em></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='bash'><span class='line'><span class="nb">cd</span> ~
</span><span class='line'>mkdir profiles
</span><span class='line'>cp preseed profiles/default.preseed
</span></code></pre></td></tr></table></div></figure>


<p><em>Run simple-cdd with the provided configuration</em></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>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>simple-cdd --conf ./simple-cdd.conf
</span></code></pre></td></tr></table></div></figure>


<p>If all goes well you will have a freshly baked ISO waiting for you in your images directory. <del>This is a standard awesome Debian ISO that can be burned to a disk or dd&rsquo;d over to a USB flash drive.</del> This ISO is sadly not the awesome standard Debian ISO that can be burned or dd&rsquo;d to a USB disk. At the moment, the image produced by simple-cdd can only be burned to a CDROM.</p>

<p><em>Edit</em>: s/Sid/Wheezy/ per Chris&rsquo;s comment.</p>

<p><em>Edit</em>: ISO can&rsquo;t be dd&rsquo;d to USB disk.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[User Interfaces Are Hard]]></title>
    <link href="http://www.50ply.com/blog/2012/07/13/user-interfaces-are-hard/"/>
    <updated>2012-07-13T07:10:00-04:00</updated>
    <id>http://www.50ply.com/blog/2012/07/13/user-interfaces-are-hard</id>
    <content type="html"><![CDATA[<p>I&rsquo;m struggling with what seems like a simple problem: keeping my views and my models synchronized with each other.</p>

<p>Objectives:</p>

<ol>
<li>Make sure that data committed by a user using a view is correctly reflected into the model.</li>
<li>Ensure that updates to the model from a variety of sources are correctly reflected back into any views that would be impacted by those changes.</li>
<li>Make sure that models know nothing about the views presenting their data.</li>
<li>Minimize or eliminate any knowledge that views have of the models they are mirroring.</li>
</ol>


<p><strong>Make sure that data committed by a user using a view is correctly reflected into the model.</strong></p>

<p><em>Rationale</em>: Accurately capturing the user&rsquo;s intent is the whole purpose of any application.</p>

<p><em>Complications</em>: Bare data captured by the view must be validated and transformed into the richer data-space of the model. Suppose we&rsquo;re editing an email address in our contact list. When the captured data (the new email address) is returned from the view we&rsquo;ll need additional knowledge (perhaps the ID of the user we were editing) before we can update the model with the new data. Where should we remember what user ID we&rsquo;re editing? What if we allow the user to edit multiple email addresses simultaneously?</p>

<p><em>Solutions</em>: The need for a place to store this binding information (user X is being edited by view Y) is one of the reasons we have &ldquo;C&rdquo; in &ldquo;MVC.&rdquo; We need some third entity to mediate the relationship between the view and the model so that neither become overly specialized. In my <a href="http://github.com/netguy204/MOVE.git">MOVE</a> library I&rsquo;m calling this mediating entity the &ldquo;interactor&rdquo; instead of a controller because I&rsquo;m trying to take a slightly different approach. I am trying to embody application use-cases (like &ldquo;user adds contact&rdquo; or &ldquo;user edits contact&rdquo;) in my interactor entities. An nteractor can trigger other interactors and each is responsible for presenting an appropriate sequence of views. Some of my previous posts were written because I&rsquo;ve been trying to figure out how to make flow of steps used to accomplish an interactor&rsquo;s task as explicit in the code as possible. In contrast, a controller typically mediates the relationship between one kind of view and one kind of data in the model (the &ldquo;contact&rdquo; controller.) I stole the word &ldquo;interactor&rdquo; and the definition I&rsquo;m using from <a href="http://www.confreaks.com/videos/759-rubymidwest2011-keynote-architecture-the-lost-years">Uncle Bob</a>.</p>

<p><strong>Ensure that updates to the model from a variety of sources are correctly reflected back into any views that would be impacted by those changes.</strong></p>

<p><em>Rationale</em>: The data the user is seeing in the view should be correct. The model is the canonical source of truth. Again, this is really the whole point.</p>

<p><em>Complications</em>: We need to make good decisions about what kind changes we communicate to the view. Data-binding frameworks like SproutCore really shine in 90% of the scenarios we face here but can make the final 10% a real challenge. Aggregate views like the count of the number of items in a list are a typical corner case where data binding struggles.</p>

<p><em>Solutions</em>: The aggregate view corner case is another opportunity to think about the coupling between view and model. Where should model derived state (like the count) be kept? It would be tempting and fairly reasonable to keep track of the count in the view. It would also be tempting but ill-advised to have the view query the model for the count directly. In a task list example I’m putting together in MOVE, I receive the list-add and list-remove events in the interactor and then query the model for the new count. Then the interactor pushes the count into the view directly.</p>

<p><strong>Make sure that models know nothing about the views presenting their data.</strong></p>

<p><em>Rationale</em>: If the application is actually doing something interesting then there should be quite a lot of code that can be developed and tested independently of the views. Strong coupling between the model and the view could limit the usefulness of the model code in circumstances that don&rsquo;t require a view (like testing, report generation, experimenting at the REPL, etc.)</p>

<p><em>Complications</em>: Since the model is the canonical state of the world it often feels natural to push changes in the model directly out to the places we know that its needed.</p>

<p><em>Solutions</em>: This is actually pretty easy to solve. We embrace the idea that the model should tell the world when it changes but we reject the idea that the model has any knowledge of what specifically is in the world. To accomplish this we use buzzwords like &ldquo;events&rdquo; or &ldquo;publish-subscribe.&rdquo; In my experience this works pretty well. I typically use events to communicate the novelty that the changes added (like what was added, edited, or subtracted.) I don’t use events for communicating the total state of the world (the new list of all of contacts, etc.) Since events only communicate novelty, we must avoid the mistake of having entities only observe the model through events. In my experience, such a decision forces those entities into maintaining their own internal &ldquo;little-models.&rdquo;</p>

<p><strong>Minimize or eliminate any knowledge that views have of the models they are mirroring.</strong></p>

<p><em>Rationale</em>: If the view is unaware of the application’s model then it is more reusable, more testable, and less sensitive to the implementation of the model.</p>

<p><em>Complications</em>: We don&rsquo;t want the view to query the model. But, we also want to avoid creating a re-implementation of the model within the view (see “little-model” commentary in the previous objective.)</p>

<p><em>Solutions</em>: Again we need to lean on a mediating object to help reduce the coupling between the view and the model. The strategy that is working well for me in MOVE is having the interactor register for any view-relevant change events when it creates the view. When those events fire, it is the interactor that pushes updated information to the view (perhaps after consulting the model) when it is needed. Obviously this helps make the views and models easier to test. Less intuitively, it also simplified the testing of the interactors. One approach I take is mocking the view and then firing data change events at the interactor to make sure the view is getting appropriate updates in response.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Extending Closure Classes From Clojurescript]]></title>
    <link href="http://www.50ply.com/blog/2012/07/08/extending-closure-from-clojurescript/"/>
    <updated>2012-07-08T20:16:00-04:00</updated>
    <id>http://www.50ply.com/blog/2012/07/08/extending-closure-from-clojurescript</id>
    <content type="html"><![CDATA[<p>Here&rsquo;s a quick macro to make extending google closure style classes
from Clojurescript a bit easier:</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">ns </span><span class="nv">move.macros</span>
</span><span class='line'>  <span class="p">(</span><span class="ss">:require</span> <span class="p">[</span><span class="nv">cljs.compiler</span> <span class="ss">:as</span> <span class="nv">compiler</span><span class="p">]</span>
</span><span class='line'>            <span class="p">[</span><span class="nv">cljs.core</span> <span class="ss">:as</span> <span class="nv">cljs</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">to-property</span> <span class="p">[</span><span class="nv">sym</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nb">symbol </span><span class="p">(</span><span class="nb">str </span><span class="s">&quot;-&quot;</span> <span class="nv">sym</span><span class="p">)))</span>
</span><span class='line'>
</span><span class='line'><span class="p">(</span><span class="kd">defmacro </span><span class="nv">goog-extend</span> <span class="p">[</span><span class="nv">type</span> <span class="nv">base-type</span> <span class="nv">ctor</span> <span class="o">&amp;</span> <span class="nv">methods</span><span class="p">]</span>
</span><span class='line'>  <span class="o">`</span><span class="p">(</span><span class="nf">do</span>
</span><span class='line'>     <span class="p">(</span><span class="kd">defn </span><span class="o">~</span><span class="nv">type</span> <span class="o">~@</span><span class="nv">ctor</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>     <span class="p">(</span><span class="nf">goog/inherits</span> <span class="o">~</span><span class="nv">type</span> <span class="o">~</span><span class="nv">base-type</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'>     <span class="o">~@</span><span class="p">(</span><span class="nf">map</span>
</span><span class='line'>        <span class="p">(</span><span class="k">fn </span><span class="p">[</span><span class="nv">method</span><span class="p">]</span>
</span><span class='line'>          <span class="o">`</span><span class="p">(</span><span class="nf">set!</span> <span class="p">(</span><span class="nb">.. </span><span class="o">~</span><span class="nv">type</span> <span class="nv">-prototype</span> <span class="o">~</span><span class="p">(</span><span class="nf">to-property</span> <span class="p">(</span><span class="nb">first </span><span class="nv">method</span><span class="p">)))</span>
</span><span class='line'>                 <span class="p">(</span><span class="k">fn </span><span class="o">~@</span><span class="p">(</span><span class="nb">rest </span><span class="nv">method</span><span class="p">))))</span>
</span><span class='line'>        <span class="nv">methods</span><span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>And, here&rsquo;s how you use it. In this example, I&rsquo;m extending
goog.ui.tree.TreeControl so that I can override handleKeyEvent and
provide a bit richer interaction with my tree.</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">ns </span><span class="nv">move.views</span>
</span><span class='line'>  <span class="p">(</span><span class="ss">:require</span> <span class="p">[</span><span class="nv">goog.ui.tree.TreeControl</span> <span class="ss">:as</span> <span class="nv">TreeControl</span><span class="p">])</span>
</span><span class='line'>  <span class="p">(</span><span class="ss">:use-macros</span> <span class="p">[</span><span class="nv">move.macros</span> <span class="ss">:only</span> <span class="p">[</span><span class="nv">goog-extend</span><span class="p">]]</span>
</span><span class='line'>               <span class="p">[</span><span class="nv">cljs.core</span> <span class="ss">:only</span> <span class="p">[</span><span class="nv">this-as</span><span class="p">]]))</span>
</span><span class='line'><span class="p">(</span><span class="nf">goog-extend</span>
</span><span class='line'> <span class="nv">MyTree</span> <span class="nv">goog/ui.tree.TreeControl</span>
</span><span class='line'> <span class="p">([</span><span class="nb">name </span><span class="nv">config</span><span class="p">]</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">this-as</span> <span class="nv">this</span>
</span><span class='line'>      <span class="p">(</span><span class="nf">goog/base</span> <span class="nv">this</span> <span class="nb">name </span><span class="nv">config</span><span class="p">)))</span>
</span><span class='line'>
</span><span class='line'> <span class="p">(</span><span class="nf">handleKeyEvent</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">e</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">this-as</span> <span class="nv">this</span>
</span><span class='line'>    <span class="p">(</span><span class="nf">goog/base</span> <span class="nv">this</span> <span class="s">&quot;handleKeyEvent&quot;</span> <span class="nv">e</span><span class="p">)</span>
</span><span class='line'>    <span class="c1">;; my special code to handle the key event</span>
</span><span class='line'>    <span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>When this is compiled, it expands to this javascript:</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>
</pre></td><td class='code'><pre><code class='javascript'><span class='line'><span class="nx">move</span><span class="p">.</span><span class="nx">views</span><span class="p">.</span><span class="nx">MyTree</span> <span class="o">=</span> <span class="kd">function</span> <span class="nx">MyTree</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">config</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">this__44428</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
</span><span class='line'>  <span class="k">return</span> <span class="nx">goog</span><span class="p">.</span><span class="nx">base</span><span class="p">(</span><span class="nx">this__44428</span><span class="p">,</span> <span class="nx">name</span><span class="p">,</span> <span class="nx">config</span><span class="p">)</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'><span class="nx">goog</span><span class="p">.</span><span class="nx">inherits</span><span class="p">(</span><span class="nx">move</span><span class="p">.</span><span class="nx">views</span><span class="p">.</span><span class="nx">MyTree</span><span class="p">,</span> <span class="nx">goog</span><span class="p">.</span><span class="nx">ui</span><span class="p">.</span><span class="nx">tree</span><span class="p">.</span><span class="nx">TreeControl</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="nx">move</span><span class="p">.</span><span class="nx">views</span><span class="p">.</span><span class="nx">MyTree</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">handleKeyEvent</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>  <span class="kd">var</span> <span class="nx">this__44429</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
</span><span class='line'>  <span class="nx">goog</span><span class="p">.</span><span class="nx">base</span><span class="p">(</span><span class="nx">this__44429</span><span class="p">,</span> <span class="s2">&quot;handleKeyEvent&quot;</span><span class="p">,</span> <span class="nx">e</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1">// my special code to handle the key event</span>
</span><span class='line'><span class="p">};</span>
</span></code></pre></td></tr></table></div></figure>


<p>Cool! That&rsquo;s pretty close to the code I need to write by hand when
extending google closure classes in my javascript code.</p>

<p>This code will work great if we compile with standard optimizations
but we get into trouble in advanced mode. The Closure compiler does
more error checking in advanced mode and wrongly flags our call to
goog.base for not having <em>this</em> as its first argument. That&rsquo;s
unfortunate but not a big deal. We just have to write some less
idiomatic code to fix it:</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="kd">ns </span><span class="nv">move.views</span>
</span><span class='line'>  <span class="p">(</span><span class="ss">:require</span> <span class="p">[</span><span class="nv">goog.ui.tree.TreeControl</span> <span class="ss">:as</span> <span class="nv">TreeControl</span><span class="p">])</span>
</span><span class='line'>  <span class="p">(</span><span class="ss">:use-macros</span> <span class="p">[</span><span class="nv">move.macros</span> <span class="ss">:only</span> <span class="p">[</span><span class="nv">goog-extend</span><span class="p">]]</span>
</span><span class='line'>               <span class="p">[</span><span class="nv">cljs.core</span> <span class="ss">:only</span> <span class="p">[</span><span class="nv">this-as</span><span class="p">]]))</span>
</span><span class='line'><span class="p">(</span><span class="nf">goog-extend</span>
</span><span class='line'> <span class="nv">MyTree</span> <span class="nv">goog/ui.tree.TreeControl</span>
</span><span class='line'> <span class="p">([</span><span class="nb">name </span><span class="nv">config</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">goog/base</span> <span class="p">(</span><span class="nf">js*</span> <span class="s">&quot;this&quot;</span><span class="p">)</span> <span class="nb">name </span><span class="nv">config</span><span class="p">))</span>
</span><span class='line'>
</span><span class='line'> <span class="p">(</span><span class="nf">handleKeyEvent</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">e</span><span class="p">]</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">goog/base</span> <span class="p">(</span><span class="nf">js*</span> <span class="s">&quot;this&quot;</span><span class="p">)</span> <span class="s">&quot;handleKeyEvent&quot;</span> <span class="nv">e</span><span class="p">)</span>
</span><span class='line'>    <span class="c1">;; my special code to handle the key event</span>
</span><span class='line'>    <span class="p">)))</span>
</span></code></pre></td></tr></table></div></figure>


<p>This will generate the code that the advanced compiler is looking for.</p>

<p>This code is part of my <a href="https://github.com/netguy204/MOVE.git">Move</a>
library.</p>

<p><em>EDIT</em>: Corrected errors wrt. advanced mode.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[defasync]]></title>
    <link href="http://www.50ply.com/blog/2012/07/07/defasync/"/>
    <updated>2012-07-07T08:10:00-04:00</updated>
    <id>http://www.50ply.com/blog/2012/07/07/defasync</id>
    <content type="html"><![CDATA[<p>Christopher&rsquo;s comments to
<a href="http://www.50ply.com/blog/2012/07/06/asynchronous-sequential-code-shape/">yesterday&rsquo;s post</a>
forced me to think more about the return value of the doasync form and
whether it would make sense to give it a body.</p>

<p>doasync doesn&rsquo;t return anything useful. How could it? doasync is
designed for callback driven code that&rsquo;s probably ultimately pumped by
the event loop. So, when it is time to return from the form we
probably haven&rsquo;t actually done anything yet.</p>

<p>But, as I was using doasync to write
<a href="https://github.com/netguy204/MOVE/blob/e5888267a84ee6e22298fe41dc60066d7c6e17e9/src-cljs/move/interactors.cljs">interactors</a>,
I noticed that all of my doasync forms really did have a &ldquo;return&rdquo;
value that they were passing on to their own callback (nextfn in the
linked code.) This sounds like a pattern to capture in a new macro.</p>

<p><a href="https://github.com/netguy204/MOVE/blob/master/src/move/macros.clj#L77">defasync</a>
works like a defn but for defining asynchronous functions. I define an
&ldquo;asynchronous function&rdquo; to be a function that takes a callback as its
last argument and &ldquo;returns&rdquo; its ultimate value by passing it to the
callback exactly once. Since my definition includes a &ldquo;return&rdquo; value
we finally have a reasonable place to put whatever the body computes.</p>

<p>Here&rsquo;s how my asynchronous function definitions looked before:</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="p">(</span><span class="kd">defn </span><span class="nv">get-current-list</span> <span class="p">[</span><span class="nv">state</span> <span class="nv">nextfn</span><span class="p">]</span>
</span><span class='line'>  <span class="s">&quot;[async] get the current list from the server and update the model&quot;</span>
</span><span class='line'>  <span class="p">(</span><span class="nf">doasync</span>
</span><span class='line'>   <span class="p">[</span><span class="nv">current-list</span> <span class="p">[</span><span class="nv">get-json</span> <span class="s">&quot;/resources/current-list.json&quot;</span><span class="p">]</span>
</span><span class='line'>    <span class="nv">_</span> <span class="p">(</span><span class="nf">models/set-current-list</span> <span class="nv">state</span> <span class="nv">current-list</span><span class="p">)</span>
</span><span class='line'>    <span class="nv">_</span> <span class="p">(</span><span class="nf">nextfn</span> <span class="nv">current-list</span><span class="p">)]))</span>
</span></code></pre></td></tr></table></div></figure>


<p>And here&rsquo;s how they look now:</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="p">(</span><span class="nf">defasync</span> <span class="nv">get-current-list</span> <span class="p">[</span><span class="nv">state</span><span class="p">]</span>
</span><span class='line'>  <span class="s">&quot;[async] get the current list from the server and update the model&quot;</span>
</span><span class='line'>  <span class="p">[</span><span class="nv">current-list</span> <span class="p">[</span><span class="nv">get-json</span> <span class="s">&quot;/resources/current-list.json&quot;</span><span class="p">]]</span>
</span><span class='line'>
</span><span class='line'>  <span class="p">(</span><span class="nf">models/set-current-list</span> <span class="nv">state</span> <span class="nv">current-list</span><span class="p">)</span>
</span><span class='line'>  <span class="nv">current-list</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>I&rsquo;m trying to mimic defn as much as possible with defasync. Like defn,
defasync has a name and a set of arguments. After an optional docstring,
defasync accepts a vector containing the same asynchronous sequential
binding style as doasync. This is the only part of the code where the
asynchronous invocation form [func arg1 arg2] is allowed.</p>

<p>Next comes the body. The body is executed once all of the bindings are
established. The return value of the body is passed to our now
implicit nextfn. Did you notice that nextfn is missing? It&rsquo;s inserted
into the last slot in the argument list by defasync and only invoked
once with the result of executing the body.</p>

<p>Here&rsquo;s another example (again from
<a href="https://github.com/netguy204/MOVE/blob/master/src-cljs/move/interactors.cljs">interactors</a>.)
Here we create a dialog asking the user for input and then collect the
result when it becomes available. In the body we update our model with
the user&rsquo;s new data.</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>
</pre></td><td class='code'><pre><code class='clojure'><span class='line'><span class="p">(</span><span class="nf">defasync</span> <span class="nv">create-new-todo</span> <span class="p">[</span><span class="nv">state</span> <span class="nv">view</span><span class="p">]</span>
</span><span class='line'>  <span class="s">&quot;[async] create a new item in the current list&quot;</span>
</span><span class='line'>  <span class="p">[</span><span class="nb">list </span><span class="p">(</span><span class="nf">models/current-list</span> <span class="nv">state</span><span class="p">)</span>
</span><span class='line'>   <span class="nv">input-dialog</span> <span class="p">(</span><span class="nf">views/make-input-dialog</span> <span class="s">&quot;What do you want to do?&quot;</span><span class="p">)</span>
</span><span class='line'>   <span class="nv">input</span> <span class="p">[</span><span class="nv">events/register-once</span> <span class="p">[</span><span class="ss">:ok-clicked</span> <span class="nv">input-dialog</span><span class="p">]]]</span>
</span><span class='line'>
</span><span class='line'>  <span class="p">(</span><span class="nf">models/add-todo</span> <span class="nv">state</span> <span class="nb">list </span><span class="nv">input</span><span class="p">))</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
</feed>
