<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Fredrik Appelberg's blog</title>
 <link href="http://mulli.nu/atom.xml" rel="self"/>
 <link href="http://mulli.nu/"/>
 <updated>2012-02-21T11:36:41+00:00</updated>
 <id>http://mulli.nu/</id>
 <author>
   <name>Fredrik Appelberg</name>
   <email>fredrik.appelberg@gmail.com</email>
 </author>

 
 <entry>
   <title>JFokus 2012</title>
   <link href="http://mulli.nu/2012/02/20/jfokus-.html"/>
   <updated>2012-02-20T00:00:00+00:00</updated>
   <id>http://mulli.nu/2012/02/20/jfokus-</id>
   <content type="html"><p>Last week was <a href='http://jfokus.se'>JFokus</a>, and a great time <a href='https://twitter.com/#!/search/%23jfokus'>was had by all</a>. This was the third time for me, and I&#8217;m really glad they moved the location to Stockholm Waterfront. Last time I went, we were crammed into a cinema complex with far too little floorspace, no wifi and very limited toilet facilities. This year though, the location was great and the surprisingly good coffee kept flowing all throughout the three days.</p>

<p>There seemed to be a good deal of interest in <a href='http://www.scala-lang.org/'>Scala</a> at the event, but I&#8217;m not sure this actually reflects some kind of actual change in the Java ecosphere. Mostly, it were small, hip, consultancies doing the promotion, and I have a gnawing suspicion that they don&#8217;t have much Scala work lined up per se, but are instead using Scala to get the attention of potential recruits. Time will tell.</p>

<p>I&#8217;m a pretty introverted guy, and don&#8217;t really feel that comfortable around a lot of people. Most of the time during the big breaks I found myself wishing for a laptop so that I could get hacking instead of having to wander round aimlessly pretending to feel at home in the crowd. Fate, however, seated me beside <a href='http://www.chrisrichardson.net/aboutus.html'>Chris Richardson</a> (of <a href='http://cloudfoundry.org'>Cloud Foundry</a> fame) during lunch the first day, and we had a pretty good discussion, even though my english was still getting up to speed. For some reason I also managed to corner <a href='http://gregluck.com/blog/'>Greg Luck</a> during the tuesday bar and talk to him about <a href='http://ehcache.org'>Ehcache</a>. Both were very nice guys, and I&#8217;d like to thank them both for helping me come out of my shell a bit.</p></content>
 </entry>
 
 <entry>
   <title>The Ballad of Emacs-Eclim</title>
   <link href="http://mulli.nu/2012/02/02/the-ballad-of-emacs-eclim.html"/>
   <updated>2012-02-02T00:00:00+00:00</updated>
   <id>http://mulli.nu/2012/02/02/the-ballad-of-emacs-eclim</id>
   <content type="html"><p>I&#8217;ve been using <a href='http://github.com/senny/emacs-eclim'>emacs-eclim</a> for java development fulltime for well over two years now, and I&#8217;ve been a co-maintainer for a couple of months, but I&#8217;ve never really written anything about it. I guess part of it is because I&#8217;m way too lazy to do a big write-up explaining all the moving parts and how it relates to <a href='http://eclim.org/'>eclim</a> and <a href='http://eclipse.org'>eclipse</a>. You know, a <em>proper blog post</em>.</p>

<p>Like most java programmers, I was a long-time <a href='http://eclipse.org'>eclipse</a> victim. If you&#8217;re not from the java world yourself, it might be hard to grasp the love/hate relationship that all java programmers have with this IDE. Or hate/hate relationship, as it may be. Sure, some people turn to <a href='http://netbeans.org/'>NetBeans</a> or <a href='http://www.jetbrains.com/idea/'>IntelliJ</a>, but you can&#8217;t get away from the fact that eclipse has in essence steamrolled all over the java IDE space. Eclipse is <em>everywhere</em>.</p>

<p>Incidentally, &#8216;steamrolled&#8217; is an appropriate word here, as it suggests huge mass and inertia bordering on inevitability, both of which are also properties of eclipse itself. It takes forever to start up, and if you actually manage to kick off a complex build operation, it&#8217;s impossible to interrupt it.</p>

<p><a href='http://www.gnu.org/software/emacs/'>Emacs</a> was my escape. But the story is kind of complicated. You see, there was this bunch of enterprising <a href='http://www.vim.org/'>vim</a> hackers that realized that even though their editor was the greatest thing since sliced bread, it wasn&#8217;t really that good at handling large java codebases, something which eclipse is atually pretty competent at. So they wrote a plugin for eclipse that turned it into a kind of server, and a vim extension that acted as a client, and then they could edit the code in vim, while leveraging the strengths of eclipse, such as command completion, compilation, and refactoring. And they called it <a href='http://eclim.org/'>eclim</a>.</p>

<p><img class='article_image' src='/img/obscure-setup.jpg' alt='My setup is obscure' /></p>

<p>Then another <a href='http://github.com/senny'>enterprising fellow</a> ported the client libraries to <a href='http://www.gnu.org/software/emacs/'>emacs</a>, and the result was <a href='http://github.com/senny/emacs-eclim'>emacs-eclim</a>. It was a bit rough at first, but it is improving steadily. In the past year, we&#8217;ve gotten <a href='http://cx4a.org/software/auto-complete/'>auto-complete-mode</a> support, inline error highlighting, improved handling of import statements and numerous bug fixes. Right now I&#8217;m working on making calls to the <code>eclimd</code> backend asynchronous, to make editing more responsive. It kind of works, but right now it&#8217;s ugly.</p>

<p>I actually have no idea how many users we have. There are <a href='https://github.com/senny/emacs-eclim/watchers'>116 people watching</a> the project on Github right now, and bug reports are being filed every now and then, so I&#8217;d like to think that at least <em>someone</em> finds it worthwhile.</p></content>
 </entry>
 
 <entry>
   <title>Nine Months of Working Remote</title>
   <link href="http://mulli.nu/2012/01/26/nine-months-of-working-remote.html"/>
   <updated>2012-01-26T00:00:00+00:00</updated>
   <id>http://mulli.nu/2012/01/26/nine-months-of-working-remote</id>
   <content type="html"><p>After a routine software update yesterday, my work laptop suddenly refused to boot. That was a bit scary, and left med stressed out. You see, I&#8217;m working remote, and the rest of our IT department is located about 1000 km away. Which pretty much meant <em>&#8220;You&#8217;re on your own, kid. If you can&#8217;t solve this yourself, then you won&#8217;t have a workstation for a few days.&#8221;</em></p>

<p>This is the first time, however, that I actually had to reflect on my working conditions as a remote worker. I have a home office set up, complete with a stand-up desk, a <a href='http://www.fatboy.com/shop/product/the-original-lime-green'>Fatboy bean bag</a>, and multi-function printer. I do the lion part of my work on my laptop, hooked up to the compay VPN. Inter-team communication is done using <a href='http://campfirenow.com'>Campfire</a> and daily telephone meetings. In many ways, this is just like working at the office, except a) I can blast whatever I want on the stereo, and b) I don&#8217;t get dragged into meetings as often as I used to.</p>

<p>Working alone does mean that I&#8217;m not just a developer anymore; I&#8217;m also responsible for first-line support for my own equipment. If the wifi goes down, no-one will fix it except me. If the laptop bombs out, I&#8217;ll either have to solve it myself, or find some way of setting up a temporary workspace on one of the ancient machines I have lying about until the company can send me a replacement.</p>

<p>Apart from that, I have no complaints about working alone. When I tell people about my situation, they usually go <em>&#8220;Aaah, but you&#8217;ve got to have such self dicipline to be working from home.&#8221;</em> The assumption being, that as soon as you don&#8217;t have someone looking over your shoulder, you will immediately revert to spending your working time goofing off. I don&#8217;t get that. When I&#8217;m working, I&#8217;m working. Same as if I were at the office. And there&#8217;s nothing special about me. In fact, I&#8217;m a pretty lazy guy.</p>

<p>I got the laptop working again, after an agonizing hour of recovery mode reboots, random fiddling with the BIOS and finally reinstalling the X server and proprietary NVIDIA drivers.</p></content>
 </entry>
 
 <entry>
   <title>Generating java code with maven, QDox and StringTemplate</title>
   <link href="http://mulli.nu/2012/01/16/generating-java.html"/>
   <updated>2012-01-16T00:00:00+00:00</updated>
   <id>http://mulli.nu/2012/01/16/generating-java</id>
   <content type="html"><h2 id='introduction_why_would_you_ever_want_to_generate_java_code'>Introduction: why would you ever want to generate Java code?</h2>

<p>In my current project at work we have quite a few value objects with giant constructor methods; these behemoths take 20 to 30 arguments each and it&#8217;s crucial that you get all of them in the right order, which is a pain in those cases when you&#8217;re only really interested in setting one or two of them. Just as an example, let&#8217;s just say that these classes looks something like this:</p>
<div class='highlight'><pre><code class='java'><span class='lineno'> 1</span> <span class='kd'>public</span> <span class='kd'>class</span> <span class='nc'>ValueObj</span> <span class='o'>{</span>
<span class='lineno'> 2</span>    <span class='kd'>private</span> <span class='kt'>int</span> <span class='n'>value1</span><span class='o'>;</span>
<span class='lineno'> 3</span>    <span class='kd'>private</span> <span class='kt'>int</span> <span class='n'>value2</span><span class='o'>;</span>
<span class='lineno'> 4</span>    <span class='kd'>private</span> <span class='kt'>int</span> <span class='n'>value3</span><span class='o'>;</span>
<span class='lineno'> 5</span>    <span class='o'>...</span>
<span class='lineno'> 6</span>    <span class='kd'>private</span> <span class='n'>Object</span> <span class='n'>obj1</span><span class='o'>;</span>
<span class='lineno'> 7</span>    <span class='kd'>private</span> <span class='n'>Object</span> <span class='n'>obj1</span><span class='o'>;</span>
<span class='lineno'> 8</span>    <span class='o'>...</span>
<span class='lineno'> 9</span>    <span class='kd'>public</span> <span class='nf'>ValueObj</span><span class='o'>(</span><span class='kt'>int</span> <span class='n'>value1</span><span class='o'>,</span> <span class='kt'>int</span> <span class='n'>value2</span><span class='o'>,</span> <span class='kt'>int</span> <span class='n'>value2</span><span class='o'>,</span> <span class='o'>...</span> <span class='n'>Object</span>
<span class='lineno'>10</span>        <span class='n'>obj1</span><span class='o'>,</span> <span class='n'>Object</span> <span class='n'>obj2</span><span class='o'>,</span> <span class='o'>...)</span> <span class='o'>{</span>
<span class='lineno'>11</span>       <span class='k'>this</span><span class='o'>.</span><span class='na'>value1</span> <span class='o'>=</span> <span class='n'>value1</span><span class='o'>;</span>
<span class='lineno'>12</span>       <span class='k'>this</span><span class='o'>.</span><span class='na'>value2</span> <span class='o'>=</span> <span class='n'>value2</span><span class='o'>;</span>
<span class='lineno'>13</span>       <span class='k'>this</span><span class='o'>.</span><span class='na'>value2</span> <span class='o'>=</span> <span class='n'>value3</span><span class='o'>;</span>
<span class='lineno'>14</span>       <span class='o'>...</span>
<span class='lineno'>15</span>       <span class='k'>this</span><span class='o'>.</span><span class='na'>obj1</span> <span class='o'>=</span> <span class='n'>obj1</span><span class='o'>;</span>
<span class='lineno'>16</span>       <span class='k'>this</span><span class='o'>.</span><span class='na'>obj2</span> <span class='o'>=</span> <span class='n'>obj2</span><span class='o'>;</span>
<span class='lineno'>17</span>       <span class='o'>...</span>
<span class='lineno'>18</span>    <span class='o'>}</span>
<span class='lineno'>19</span> 
<span class='lineno'>20</span>    <span class='kd'>public</span> <span class='kt'>int</span> <span class='nf'>getValue1</span><span class='o'>()</span> <span class='o'>{</span>
<span class='lineno'>21</span>       <span class='k'>return</span> <span class='k'>this</span><span class='o'>.</span><span class='na'>value1</span><span class='o'>;</span>
<span class='lineno'>22</span>    <span class='o'>}</span>
<span class='lineno'>23</span>    <span class='o'>...</span> <span class='o'>(</span><span class='n'>other</span> <span class='n'>getter</span> <span class='n'>methods</span> <span class='n'>follow</span><span class='o'>)</span>
<span class='lineno'>24</span> <span class='o'>}</span>
</code></pre>
</div>
<p>About a year ago or so we started using the <a href='http://c2.org'>Builder pattern</a>, creating a separate <em>Builder class</em> for each value object class. The builder has one setter method for each parameter, and one <code>create()</code> method that instantiates the new object.</p>
<div class='highlight'><pre><code class='java'><span class='lineno'> 1</span> <span class='kd'>public</span> <span class='kd'>class</span> <span class='nc'>ValueObjBuilder</span> <span class='o'>{</span>
<span class='lineno'> 2</span>    <span class='kd'>private</span> <span class='kt'>int</span> <span class='n'>value1</span><span class='o'>;</span>
<span class='lineno'> 3</span>    <span class='kd'>private</span> <span class='kt'>int</span> <span class='n'>value2</span><span class='o'>;</span>
<span class='lineno'> 4</span>    <span class='kd'>private</span> <span class='kt'>int</span> <span class='n'>value3</span><span class='o'>;</span>
<span class='lineno'> 5</span>    <span class='o'>...</span>
<span class='lineno'> 6</span>    <span class='kd'>private</span> <span class='n'>Object</span> <span class='n'>obj1</span><span class='o'>;</span>
<span class='lineno'> 7</span>    <span class='kd'>private</span> <span class='n'>Object</span> <span class='n'>obj1</span><span class='o'>;</span>
<span class='lineno'> 8</span>    <span class='o'>...</span>
<span class='lineno'> 9</span> 
<span class='lineno'>10</span>    <span class='kd'>public</span> <span class='n'>ValueObjBuilder</span> <span class='nf'>value1</span><span class='o'>(</span><span class='kt'>int</span> <span class='n'>value1</span><span class='o'>)</span> <span class='o'>{</span>
<span class='lineno'>11</span>        <span class='k'>this</span><span class='o'>.</span><span class='na'>value1</span> <span class='o'>=</span> <span class='n'>value1</span><span class='o'>;</span>
<span class='lineno'>12</span>        <span class='k'>return</span> <span class='k'>this</span><span class='o'>;</span>
<span class='lineno'>13</span>    <span class='o'>}</span>
<span class='lineno'>14</span>    <span class='kd'>public</span> <span class='n'>ValueObjBuilder</span> <span class='nf'>value2</span><span class='o'>(</span><span class='kt'>int</span> <span class='n'>value2</span><span class='o'>)</span> <span class='o'>{</span>
<span class='lineno'>15</span>        <span class='k'>this</span><span class='o'>.</span><span class='na'>value2</span> <span class='o'>=</span> <span class='n'>value2</span><span class='o'>;</span>
<span class='lineno'>16</span>        <span class='k'>return</span> <span class='k'>this</span><span class='o'>;</span>
<span class='lineno'>17</span>    <span class='o'>}</span>
<span class='lineno'>18</span>    <span class='o'>...</span>
<span class='lineno'>19</span> 
<span class='lineno'>20</span>    <span class='kd'>public</span> <span class='n'>ValueObj</span> <span class='nf'>create</span><span class='o'>()</span> <span class='o'>{</span>
<span class='lineno'>21</span>        <span class='k'>return</span> <span class='k'>new</span> <span class='nf'>ValueObj</span><span class='o'>(</span><span class='n'>value1</span><span class='o'>,</span> <span class='n'>value2</span><span class='o'>,</span> <span class='o'>...);</span>
<span class='lineno'>22</span>    <span class='o'>}</span>
<span class='lineno'>23</span> <span class='o'>}</span>
</code></pre>
</div>
<p>Since the setter methods return a reference to the builder itself it is possible to chain invocations:</p>
<div class='highlight'><pre><code class='java'><span class='n'>ValueObj</span> <span class='n'>obj</span> <span class='o'>=</span> <span class='k'>new</span> <span class='n'>ValueObjBuilder</span><span class='o'>().</span><span class='na'>value1</span><span class='o'>(</span><span class='mi'>23</span><span class='o'>).</span><span class='na'>value2</span><span class='o'>(</span><span class='mi'>47</span><span class='o'>).</span><span class='na'>create</span><span class='o'>();</span>
</code></pre>
</div>
<p>However convenient these builder classes are to use, I very quickly tired of maintaining them. Every time you add or remove a property to a value object, you also need to make the corresponding change to the builder. Keeping two classes in sync doesn&#8217;t seem like too much work, but it is unnecessary, and it occurred to me that I could probably write something to generate the builders automatically.</p>

<h2 id='writing_a_maven_plugin'>Writing a Maven plugin</h2>

<p>We use <a href='http://maven.apache.org/'>Maven</a> for our builds. Ok, ok, I know. Maven is horrible, maven downloads the whole universe, maven is braindead. Yes, I know all these things. But here&#8217;s the ting: <em>Maven is like democracy: It&#8217;s not perfect, but it&#8217;s the best we have</em>.</p>

<p>One of the nice things about maven is that it has a well-defined standard build lifecycle. The build process is divided into steps, and during one of the steps, <code>generate-sources</code>, a maven plugin has the opportunity to generate source that will then be compiled during the <code>compile</code> step.</p>

<p>When invoked, we&#8217;d like the plugin to:</p>

<ol>
<li>
<p>Loop over our java classes, looking for constructors marked with our custom <code>@builder</code> javadoc comment.</p>
</li>

<li>
<p>For each matching constructor, generate a new source file under <code>target/generated-sources/builderbuilder/</code> containing the Builder class.</p>
</li>
</ol>

<p>The plugin is invoked <em>before</em> the compilation step, meaning that all the generated code will be included in the final output.</p>

<p>As an example, this code (in <code>src/main/java/example/ValueObj.java</code>):</p>
<div class='highlight'><pre><code class='java'><span class='lineno'> 1</span> <span class='kn'>package</span> <span class='n'>example</span><span class='o'>;</span>
<span class='lineno'> 2</span> 
<span class='lineno'> 3</span> <span class='kd'>public</span> <span class='kd'>class</span> <span class='nc'>Cat</span> <span class='o'>{</span>
<span class='lineno'> 4</span>    <span class='kd'>private</span> <span class='n'>String</span> <span class='n'>name</span><span class='o'>;</span>
<span class='lineno'> 5</span>    <span class='kd'>private</span> <span class='kt'>int</span> <span class='n'>age</span><span class='o'>;</span>
<span class='lineno'> 6</span> 
<span class='lineno'> 7</span>    <span class='cm'>/**</span>
<span class='lineno'> 8</span> <span class='cm'>    * @builder</span>
<span class='lineno'> 9</span> <span class='cm'>    */</span>
<span class='lineno'>10</span>    <span class='kd'>public</span> <span class='nf'>Cat</span><span class='o'>(</span><span class='n'>String</span> <span class='n'>name</span><span class='o'>,</span> <span class='kt'>int</span> <span class='n'>age</span><span class='o'>)</span> <span class='o'>{</span>
<span class='lineno'>11</span>       <span class='k'>this</span><span class='o'>.</span><span class='na'>name</span> <span class='o'>=</span> <span class='n'>name</span><span class='o'>;</span>
<span class='lineno'>12</span>       <span class='k'>this</span><span class='o'>.</span><span class='na'>age</span> <span class='o'>=</span> <span class='n'>age</span><span class='o'>;</span>
<span class='lineno'>13</span>    <span class='o'>}</span>
<span class='lineno'>14</span> <span class='o'>}</span>
</code></pre>
</div>
<p>Will generate the following (in <code>target/generated-sources/builderbuilder/example/CatBuilder.java)</code> when run through our plugin:</p>
<div class='highlight'><pre><code class='java'><span class='lineno'> 1</span> <span class='kn'>package</span> <span class='n'>example</span><span class='o'>;</span>
<span class='lineno'> 2</span> 
<span class='lineno'> 3</span> <span class='kd'>public</span> <span class='kd'>class</span> <span class='nc'>CatBuilder</span> <span class='o'>{</span>
<span class='lineno'> 4</span>    <span class='kd'>private</span> <span class='n'>String</span> <span class='n'>name</span><span class='o'>;</span>
<span class='lineno'> 5</span>    <span class='kd'>private</span> <span class='kt'>int</span> <span class='n'>age</span><span class='o'>;</span>
<span class='lineno'> 6</span> 
<span class='lineno'> 7</span>    <span class='kd'>public</span> <span class='n'>CatBuilder</span> <span class='nf'>name</span><span class='o'>(</span><span class='n'>String</span> <span class='n'>name</span><span class='o'>)</span> <span class='o'>{</span>
<span class='lineno'> 8</span>       <span class='k'>this</span><span class='o'>.</span><span class='na'>name</span> <span class='o'>=</span> <span class='n'>name</span><span class='o'>;</span>
<span class='lineno'> 9</span>       <span class='k'>return</span> <span class='k'>this</span><span class='o'>;</span>
<span class='lineno'>10</span>    <span class='o'>}</span>
<span class='lineno'>11</span> 
<span class='lineno'>12</span>    <span class='kd'>public</span> <span class='n'>CatBuilder</span> <span class='nf'>age</span><span class='o'>(</span><span class='kt'>int</span> <span class='n'>age</span><span class='o'>)</span> <span class='o'>{</span>
<span class='lineno'>13</span>       <span class='k'>this</span><span class='o'>.</span><span class='na'>age</span> <span class='o'>=</span> <span class='n'>age</span><span class='o'>;</span>
<span class='lineno'>14</span>       <span class='k'>return</span> <span class='k'>this</span><span class='o'>;</span>
<span class='lineno'>15</span>    <span class='o'>}</span>
<span class='lineno'>16</span> 
<span class='lineno'>17</span>    <span class='kd'>public</span> <span class='n'>Cat</span> <span class='nf'>create</span><span class='o'>()</span> <span class='o'>{</span>
<span class='lineno'>18</span>       <span class='k'>return</span> <span class='k'>new</span> <span class='nf'>Cat</span><span class='o'>(</span><span class='n'>name</span><span class='o'>,</span> <span class='n'>age</span><span class='o'>);</span>
<span class='lineno'>19</span>    <span class='o'>}</span>
<span class='lineno'>20</span> <span class='o'>}</span>
</code></pre>
</div>
<p>For those of you who like to read along, the code for the whole plugin is <a href='http://github.com/fred-o/BuilderBuilder'>on github</a>. The finished version has some extra bells and whistles, like the ability to generate abstract builder classes and explicitly the name of the <code>create()</code> method.</p>

<h2 id='parsing_java_code'>Parsing Java code</h2>

<p>Okay, so let&#8217;s say we want to pick apart the java class files and for each constructor found we want to generate a helper class as discussed above. Now, normally we&#8217;d use the introspection facilities already built into java, but now we have one problem: our code-generating code will run during the <code>generate-sources</code> step, <em>before</em> any compilation has actually taken places.</p>

<p>This is a problem. What we need is a java parser that can pull apart the relevant bits of the source files and give us enough information that so we can generate the new classes. Preferably the level of detail should be on par with with that of the <code>java.util.reflect.*</code> functions, and it should also be fast. I actually started writing <a href='http://github.com/fred-o/timjan'>something like that</a>, but that&#8217;s another story. Instead, let&#8217;s check out <a href='http://qdox.codehaus.org'>QDox</a>, an amazing little library used internally by maven.</p>

<p>Using it is pretty simple. First, create a new <code>JavaDocBuilder</code> object and tell it where the source code you want to parse lives:</p>
<div class='highlight'><pre><code class='java'><span class='lineno'>1</span> <span class='k'>this</span><span class='o'>.</span><span class='na'>docBuilder</span> <span class='o'>=</span> <span class='k'>new</span> <span class='n'>JavaDocBuilder</span><span class='o'>();</span>
<span class='lineno'>2</span> <span class='k'>for</span> <span class='o'>(</span><span class='n'>String</span> <span class='n'>r</span> <span class='o'>:</span> <span class='n'>sources</span><span class='o'>)</span> <span class='o'>{</span>
<span class='lineno'>3</span>     <span class='n'>docBuilder</span><span class='o'>.</span><span class='na'>addSourceTree</span><span class='o'>(</span><span class='k'>new</span> <span class='n'>File</span><span class='o'>(</span><span class='n'>r</span><span class='o'>));</span>
<span class='lineno'>4</span> <span class='o'>}</span>
</code></pre>
</div>
<p>Then loop over all known classes and generate the builder:</p>
<div class='highlight'><pre><code class='java'><span class='lineno'>1</span> <span class='k'>for</span> <span class='o'>(</span><span class='n'>JavaClass</span> <span class='n'>jc</span> <span class='o'>:</span> <span class='n'>docBuilder</span><span class='o'>.</span><span class='na'>getClasses</span><span class='o'>())</span> <span class='o'>{</span>
<span class='lineno'>2</span>     <span class='n'>generateBuilderFor</span><span class='o'>(</span><span class='n'>jc</span><span class='o'>);</span>
<span class='lineno'>3</span> <span class='o'>}</span>
</code></pre>
</div>
<h2 id='generating_the_output'>Generating the output</h2>

<p>This is the <code>generateBuilderFor()</code> method:</p>
<div class='highlight'><pre><code class='java'><span class='lineno'> 1</span> <span class='kd'>public</span> <span class='kt'>void</span> <span class='nf'>generateBuilderFor</span><span class='o'>(</span><span class='n'>JavaClass</span> <span class='n'>jc</span><span class='o'>)</span> <span class='kd'>throws</span> <span class='n'>IOException</span> <span class='o'>{</span>
<span class='lineno'> 2</span>     <span class='k'>for</span><span class='o'>(</span><span class='n'>JavaMethod</span> <span class='nl'>m:</span> <span class='n'>jc</span><span class='o'>.</span><span class='na'>getMethods</span><span class='o'>())</span> <span class='o'>{</span>
<span class='lineno'> 3</span>         <span class='k'>if</span> <span class='o'>(</span><span class='n'>m</span><span class='o'>.</span><span class='na'>isConstructor</span><span class='o'>())</span> <span class='o'>{</span>
<span class='lineno'> 4</span>             <span class='n'>DocletTag</span> <span class='n'>dc</span> <span class='o'>=</span> <span class='n'>m</span><span class='o'>.</span><span class='na'>getTagByName</span><span class='o'>(</span><span class='s'>&quot;builder&quot;</span><span class='o'>);</span>
<span class='lineno'> 5</span>             <span class='k'>if</span> <span class='o'>(</span><span class='n'>dc</span> <span class='o'>!=</span> <span class='kc'>null</span><span class='o'>)</span> <span class='o'>{</span>
<span class='lineno'> 6</span>                 <span class='n'>String</span> <span class='n'>builderName</span> <span class='o'>=</span> <span class='n'>dc</span><span class='o'>.</span><span class='na'>getNamedParameter</span><span class='o'>(</span><span class='s'>&quot;name&quot;</span><span class='o'>);</span>
<span class='lineno'> 7</span>                 <span class='k'>if</span> <span class='o'>(</span><span class='n'>builderName</span> <span class='o'>==</span> <span class='kc'>null</span><span class='o'>)</span>
<span class='lineno'> 8</span>                     <span class='n'>builderName</span> <span class='o'>=</span> <span class='n'>jc</span><span class='o'>.</span><span class='na'>getName</span><span class='o'>()</span> <span class='o'>+</span> <span class='s'>&quot;Builder&quot;</span><span class='o'>;</span>
<span class='lineno'> 9</span> 
<span class='lineno'>10</span>                 <span class='n'>String</span> <span class='n'>packageName</span> <span class='o'>=</span> <span class='n'>dc</span><span class='o'>.</span><span class='na'>getNamedParameter</span><span class='o'>(</span><span class='s'>&quot;package&quot;</span><span class='o'>);</span>
<span class='lineno'>11</span>                 <span class='k'>if</span> <span class='o'>(</span><span class='n'>packageName</span> <span class='o'>==</span> <span class='kc'>null</span><span class='o'>)</span>
<span class='lineno'>12</span>                     <span class='n'>packageName</span> <span class='o'>=</span> <span class='n'>jc</span><span class='o'>.</span><span class='na'>getPackageName</span><span class='o'>();</span>
<span class='lineno'>13</span> 
<span class='lineno'>14</span>                 <span class='n'>StringTemplate</span> <span class='n'>st</span> <span class='o'>=</span> <span class='n'>templates</span><span class='o'>.</span><span class='na'>getInstanceOf</span><span class='o'>(</span><span class='s'>&quot;builder&quot;</span><span class='o'>);</span>
<span class='lineno'>15</span>                 <span class='n'>st</span><span class='o'>.</span><span class='na'>setAttribute</span><span class='o'>(</span><span class='s'>&quot;packageName&quot;</span><span class='o'>,</span> <span class='n'>packageName</span><span class='o'>);</span>
<span class='lineno'>16</span>                 <span class='n'>st</span><span class='o'>.</span><span class='na'>setAttribute</span><span class='o'>(</span><span class='s'>&quot;builderName&quot;</span><span class='o'>,</span> <span class='n'>builderName</span><span class='o'>);</span>
<span class='lineno'>17</span>                 <span class='n'>st</span><span class='o'>.</span><span class='na'>setAttribute</span><span class='o'>(</span><span class='s'>&quot;resultClass&quot;</span><span class='o'>,</span> <span class='n'>jc</span><span class='o'>.</span><span class='na'>asType</span><span class='o'>().</span><span class='na'>toString</span><span class='o'>());</span>
<span class='lineno'>18</span> 
<span class='lineno'>19</span>                 <span class='n'>List</span><span class='o'>&lt;</span><span class='n'>Param</span><span class='o'>&gt;</span> <span class='n'>ps</span> <span class='o'>=</span> <span class='k'>new</span> <span class='n'>LinkedList</span><span class='o'>&lt;</span><span class='n'>Param</span><span class='o'>&gt;();</span>
<span class='lineno'>20</span>                 <span class='k'>for</span><span class='o'>(</span><span class='n'>JavaParameter</span> <span class='nl'>p:</span> <span class='n'>m</span><span class='o'>.</span><span class='na'>getParameters</span><span class='o'>())</span> <span class='o'>{</span>
<span class='lineno'>21</span>                        <span class='n'>ps</span><span class='o'>.</span><span class='na'>add</span><span class='o'>(</span><span class='k'>new</span> <span class='n'>Param</span><span class='o'>(</span><span class='n'>p</span><span class='o'>.</span><span class='na'>getType</span><span class='o'>().</span><span class='na'>toGenericString</span><span class='o'>(),</span> <span class='n'>p</span><span class='o'>.</span><span class='na'>getName</span><span class='o'>()));</span>
<span class='lineno'>22</span>                 <span class='o'>}</span>
<span class='lineno'>23</span>                 <span class='n'>st</span><span class='o'>.</span><span class='na'>setAttribute</span><span class='o'>(</span><span class='s'>&quot;parameters&quot;</span><span class='o'>,</span> <span class='n'>ps</span><span class='o'>);</span>
<span class='lineno'>24</span> 
<span class='lineno'>25</span>                 <span class='n'>File</span> <span class='n'>pd</span> <span class='o'>=</span> <span class='k'>new</span> <span class='n'>File</span><span class='o'>(</span><span class='n'>outputDirectory</span><span class='o'>,</span> <span class='n'>packageName</span><span class='o'>.</span><span class='na'>replaceAll</span><span class='o'>(</span><span class='s'>&quot;\\.&quot;</span><span class='o'>,</span> <span class='s'>&quot;/&quot;</span><span class='o'>));</span>
<span class='lineno'>26</span>                 <span class='n'>pd</span><span class='o'>.</span><span class='na'>mkdirs</span><span class='o'>();</span>
<span class='lineno'>27</span> 
<span class='lineno'>28</span>                 <span class='n'>FileWriter</span> <span class='n'>out</span> <span class='o'>=</span> <span class='k'>new</span> <span class='n'>FileWriter</span><span class='o'>(</span><span class='k'>new</span> <span class='n'>File</span><span class='o'>(</span><span class='n'>pd</span><span class='o'>,</span> <span class='n'>builderName</span> <span class='o'>+</span> <span class='s'>&quot;.java&quot;</span><span class='o'>));</span>
<span class='lineno'>29</span>                 <span class='k'>try</span> <span class='o'>{</span>
<span class='lineno'>30</span>                     <span class='n'>out</span><span class='o'>.</span><span class='na'>append</span><span class='o'>(</span><span class='n'>st</span><span class='o'>.</span><span class='na'>toString</span><span class='o'>());</span>
<span class='lineno'>31</span>                 <span class='o'>}</span> <span class='k'>finally</span> <span class='o'>{</span>
<span class='lineno'>32</span>                     <span class='n'>out</span><span class='o'>.</span><span class='na'>flush</span><span class='o'>();</span>
<span class='lineno'>33</span>                     <span class='n'>out</span><span class='o'>.</span><span class='na'>close</span><span class='o'>();</span>
<span class='lineno'>34</span>                 <span class='o'>}</span>
<span class='lineno'>35</span>             <span class='o'>}</span>
<span class='lineno'>36</span>         <span class='o'>}</span>
<span class='lineno'>37</span>     <span class='o'>}</span>
<span class='lineno'>38</span> <span class='o'>}</span>
<span class='lineno'>39</span> 
<span class='lineno'>40</span> <span class='kd'>public</span> <span class='kd'>static</span> <span class='kd'>class</span> <span class='nc'>Param</span> <span class='o'>{</span>
<span class='lineno'>41</span>     <span class='kd'>public</span> <span class='kd'>final</span> <span class='n'>String</span> <span class='n'>type</span><span class='o'>;</span>
<span class='lineno'>42</span>     <span class='kd'>public</span> <span class='kd'>final</span> <span class='n'>String</span> <span class='n'>name</span><span class='o'>;</span>
<span class='lineno'>43</span>         <span class='kd'>public</span> <span class='nf'>Param</span><span class='o'>(</span><span class='n'>String</span> <span class='n'>type</span><span class='o'>,</span> <span class='n'>String</span> <span class='n'>name</span><span class='o'>)</span> <span class='o'>{</span>
<span class='lineno'>44</span>         <span class='k'>this</span><span class='o'>.</span><span class='na'>type</span> <span class='o'>=</span> <span class='n'>type</span><span class='o'>;</span>
<span class='lineno'>45</span>         <span class='k'>this</span><span class='o'>.</span><span class='na'>name</span> <span class='o'>=</span> <span class='n'>name</span><span class='o'>;</span>
<span class='lineno'>46</span>     <span class='o'>}</span>
<span class='lineno'>47</span> <span class='o'>}</span>
</code></pre>
</div>
<p>This is all pretty straightforward; we loop through the methods for the class, looking for a constructor that has the <code>@builder</code> javadoc tag.</p>

<p>Lines 6-12 checks if the tag has any values specified for <code>name</code> and <code>package</code>, reverting to default values if not.</p>

<p>Lines 14-23 instantiates a new <code>StringTemplate</code> object and supplies it with values for the <code>packageName</code>, <code>builderName</code> and <code>resultClass</code> attributes (the latter being the class that we want the builder to actually build). Lines 19-23 loops through the constructor arguments and creates value object holding the name &amp; type of each.</p>

<p>Finally, a new file is created (lines 25-26) and written (lines 28-34).</p>

<h2 id='templating'>Templating</h2>

<p>There are an abundance of templating frameworks out there. I choose to go with <a href='http://stringtemplate.org'>StringTemplate</a> for this project, since it makes it easy to use subtamplates in a functional way, and generally fits my way of thinking. I could just as easily have used something like <a href='http://freemarker.sourceforge.net/'>FreeMarker</a> or <a href='http://velocity.apache.org/'>Velocity</a>, though.</p>

<p>The templates are not that interesting, so I won&#8217;t go into it here. You can check them out <a href='https://github.com/fred-o/BuilderBuilder/blob/master/src/main/resources/builderbuilder.stg'>on github</a> if you are curious.</p>

<h2 id='conclusion'>Conclusion</h2>

<p>Automatic code generation in java projects is not only feasible, but also quite convenient with maven. We&#8217;ve been using <a href='http://github.com/fred-o/BuilderBuilder'>BuilderBuilder</a> internally in production for a few months now, and it greatly cuts down on code maintenance.</p>

<p>It would be nice, though, if there was a more general approach to code generation. Just for <a href='http://github.com/fred-o/BuilderBuilder'>BuilderBuilder</a>, I had to dive into <a href='http://maven.apache.org/'>Maven</a> internals (which are surprisingly poorly documented), learn <a href='http://qdox.codehaus.org'>QDox</a> (ditto) and write all the java code that solved this particulat problem. Imagine instead if there was some kind of transformation language that operated on java sources, kind of like what <a href='http://www.w3.org/Style/XSL/'>XSLT</a> does for XML? Now, that would be cool.</p></content>
 </entry>
 
 <entry>
   <title>The Night Circus</title>
   <link href="http://mulli.nu/2011/09/16/the-night-circus.html"/>
   <updated>2011-09-16T00:00:00+00:00</updated>
   <id>http://mulli.nu/2011/09/16/the-night-circus</id>
   <content type="html"><p>I&#8217;ve been playing <a href='http://www.nightcircus.co.uk/'>The Night Circus</a> a lot lately. Well, &#8216;playing&#8217; is a strong word; I&#8217;ve been mostly clicking on random cards and enjoying the athmosphere of the game. I&#8217;m not really making any informed choices that influence the story, and I have little control over the game flow. This isn&#8217;t really the type of game I usually bother with, but it does quite a few things right:</p>

<ul>
<li>You don&#8217;t have to create an account to play; just sign in with you twitter or facebook account.</li>

<li>Every move is represented by a card that you play. You get to keep six cards in your and you&#8217;re issued a new card randomly(?) every four minutes. This, in conjunction with that there is very little state to keep track of, means that it&#8217;s very easy to return every now and then, play a few cards and then do something else. In short, a perfect game to play while <a href='http://xkcd.com/303/'>you&#8217;re code&#8217;s compiling</a>.</li>

<li>There&#8217;s a constant stream of small rewards. Almost every card played gains you a memento of some sort, and most of the mementoes can be used to unlock new paths in the game.</li>

<li>Also, the setting is very athmospheric in a kind of fantasy victorian, <a href='http://en.wikipedia.org/wiki/Jonathan_Strange_%26_Mr_Norrell'>Jonathan Strange &amp; Mr Norrell</a> kind of way. The writing is well done, and you get the feeling that there&#8217;s a lot to be discovered. I don&#8217;t know if there&#8217;s a conclusion or if the game ever really &#8216;ends&#8217;. New story segments are added weekly however, so there&#8217;s hope for some kind of coherent story.</li>
</ul>

<p>The game builds Erin Morgenstern&#8217;s upcoming <a href='http://www.amazon.co.uk/The-Night-Circus-ebook/dp/B005AKFXZW/ref=sr_1_2?s=books&amp;ie=UTF8&amp;qid=1315234149&amp;sr=1-2'>novel with the same name</a>, and as a promotion piece it works pretty well. At the very least, it got me engaged and actually considering getting the book at some point.</p></content>
 </entry>
 
 <entry>
   <title>Warning: pictures of cats ahead!</title>
   <link href="http://mulli.nu/2011/08/17/warning-pictures-of-cats-ahead-.html"/>
   <updated>2011-08-17T00:00:00+00:00</updated>
   <id>http://mulli.nu/2011/08/17/warning-pictures-of-cats-ahead-</id>
   <content type="html"><p>I used to draw a lot as a kid, but at some point I stopped doing it. Maybe I didn&#8217;t have the time. Maybe I wasn&#8217;t good enough, or never felt like my technique improved. And from time to time I felt really bad about quitting.</p>

<p>Well, a couple of days ago I dusted off my <a href='http://www.wacom.com/en/Products/BambooTablets.aspx'>Wacom Bamboo</a> tablet, downloaded the latest version of <a href='http://www.artrage.com/artrage2.html'>ArtRage</a>, and started sketching again. For some reason, everything turned out cats:</p>
<a href='https://picasaweb.google.com/lh/photo/ElnGMFdjVle1qxLwkb6S5Q?feat=embedwebsite' class='article_image'><img src='https://lh3.googleusercontent.com/-b0whFcik-Oo/TkkFf8lOdBI/AAAAAAAAAEY/CKfS6YpsBQU/s400/kossan%252520i%252520gr%2525C3%2525A4set.JPG' height='400' width='339' /></a><a href='https://picasaweb.google.com/lh/photo/kvCNAfbQS9JxmHOyk5cAXw?feat=embedwebsite' class='article_image'><img src='https://lh6.googleusercontent.com/-xeKxwRE2la0/TkpFxa7K_kI/AAAAAAAAAEs/4-eA4o0YYu4/s400/kossan%252520vilar.JPG' height='300' width='400' /></a>
<p>The first two were made using photo references. This is our oldest cat in various poses.</p>
<a href='https://picasaweb.google.com/lh/photo/291ZeH_nqcXWsyj_EZZbfw?feat=embedwebsite' class='article_image'><img class='right' src='https://lh6.googleusercontent.com/--K0FxN6qrOA/TkpFxS9qU7I/AAAAAAAAAEw/KncPLNdLQkw/s400/florence.JPG' height='384' width='400' /></a>
<p>This one was actually drawn from memory. This is our smallest cat, and yes, her ears really are that large.</p></content>
 </entry>
 
 <entry>
   <title>The Amazing Arkham Horror Contents Calculator</title>
   <link href="http://mulli.nu/2011/08/07/arkham-horror-contents-calculator.html"/>
   <updated>2011-08-07T00:00:00+00:00</updated>
   <id>http://mulli.nu/2011/08/07/arkham-horror-contents-calculator</id>
   <content type="html"><p>It starts out innocently enough. You purchase the <a href='http://boardgamegeek.com/boardgame/15987/arkham-horror'>Arkham Horror</a> base set and play a few games with friends. You have loads of solid, insanity-inducing fun, but after a while it gets kinda repetitive playing with the same Investigators and Old Ones over and over again. So you start looking at expansions, picking up say, <a href='http://boardgamegeek.com/boardgameexpansion/40776/arkham-horror-innsmouth-horror-expansion'>Innsmouth Horror</a> and Bam! you&#8217;re in Happytown again! Killing monsters! Reading ancient tomes! Learning all things Man Was Not Meant To Know.</p>

<p>Soon you have collected all the expansions, and now your problem is that you&#8217;re running out of space. The base set alone contains over 700 moving parts. Each expansion adds more game boards, cards, tokens, markers and rule books. You start asking yourself exactly what ridiculous amount of game components you&#8217;ve accumulated on your quest to boardgame nirvana.</p>

<p>At least, that&#8217;s what I asked myself. And so I whipped together a web-based calculator using <a href='http://code.google.com/webtoolkit/'>GWT</a> and some free time. Now you too can use it to calculate the extent of <em>your</em> mania.</p>

<p><em>Note: The columns are sortable; try clicking on the column headings. You can filter the selection by (un)checking the boxes for different expansion sets and component types.</em></p>
<div id='ahccRoot' style='margin-left: -10%;'>&nbsp;</div><script language='javascript' src='/media/ahcc/ahcc.nocache.js' type='text/javascript'>&nbsp;</script>
<p>3222 components! Whew, that&#8217;s a <em>lot</em>!</p></content>
 </entry>
 
 <entry>
   <title>inform-htmlizer.el</title>
   <link href="http://mulli.nu/2011/05/14/inform-htmlizer-el.html"/>
   <updated>2011-05-14T00:00:00+00:00</updated>
   <id>http://mulli.nu/2011/05/14/inform-htmlizer-el</id>
   <content type="html"><p>While writing the previous two posts I found the need to display <a href='http://inform7.com'>Inform7</a> source code in a blog-friendly way. Unfortunatly, the <a href='http://pygments.org/'>pygments</a> source highlighter doesn&#8217;t support it, and I couldn&#8217;t find any plugins for it that would. The closest I came was this <a href='http://blog.worldmaker.net/2009/feb/17/code-snippet-moment-inform-7-lexer-pygments/'>blog post by Max Battcher</a>, but as he mentions, pygments is probably a bad fit for this kind of code. Inform7 source is best displayed with a non-fixed-width font and uses regular line wrapping, but depends on significant whitespace for control flow. As far as I could tell, pygments doesn&#8217;t really support that kind of output, and anyway I couldn&#8217;t figure out how to use Python&#8217;s <code>setuptools</code> to hook up my own plugins to begin with.</p>

<p>So I did what any self-respecting hacker would do: I rolled my own solution using the tools I&#8217;m used to. In my case that means <a href='http://en.wikipedia.org/wiki/Emacs_Lisp'>elisp</a>, which is a good fit for med since Emacs is the tool <a href='/2010/04/23/my-setup.html'>I&#8217;m using</a> to write this blog. I&#8217;ve put the code on <a href='https://github.com/fred-o/inform-htmlizer'>GitHub</a> it case it proves useful for the very small subset of Inform7 users who are also a) Emacs fanatics, and b) desperately in need of a HTML syntax highlighter.</p></content>
 </entry>
 
 <entry>
   <title>Working Title: Satisfaction</title>
   <link href="http://mulli.nu/2011/04/29/working-title-satisfaction.html"/>
   <updated>2011-04-29T00:00:00+00:00</updated>
   <id>http://mulli.nu/2011/04/29/working-title-satisfaction</id>
   <content type="html"><p>I&#8217;m starting to settle into my new home office. There are boxes and cables everywhere, but at least I have a desk to work from. Time to get back to actual work again.</p>

<p>For the past month, I have made virtually zero progress on my Interactive Fiction game. I could blame this on the stress of moving across the country while simultaneously negotiating building plans for our summer house, and then having relatives visiting the new house over easter, but I won&#8217;t. Let&#8217;s just say I need a little ignition to get going again, and I&#8217;m hoping blogging about it a bit will help.</p>

<p>When I first concieved the idea for the game I thought I&#8217;d be done in a day, or a weekend at most. That was back in January, and I think I might be halfway to an alpha version now. Obviously, it took a whole lot longer than I expected, mostly because I was learning <a href='http://inform7.com/'>Inform 7</a> and designing the game world at the same time as coding. One thing I did do right though, was actually writing a simple design document before I started. It had a short synopsis describing the game flow and central mechanics, and a section for each important person in the game. Nailing down names, titles, and backgrounds of the characters really helped things later on.</p>

<p>The games is divided into short chapters, with the viewpoint alternating between two playable characters. I want the player to feel like she&#8217;s participating in a story rather than trying to beat a game, so there are no obvious puzzles to solve. In fact, right now most of the game can be played just by moving between rooms, and waiting at the correct places. I&#8217;m considering using the subtitle <em>A Short Story About Inevitability</em>, but we&#8217;ll see.</p>

<p>Much of the plot revolves around discovering things about the player characters, either by exploring or by having conversations with NPCs. Even though the player is largely swept along for the ride, his actions do have consequences for the ending of the game. There are several different outcomes, even though they may not be obvious.</p>

<p>There are two things I&#8217;m doing to invite re-play of the game. First, I&#8217;m trying to pace it so that the player will never be able to get the whole picture in the first play-through. The challenge here is making the player feel intrigued rather than annoyed, and there&#8217;s a cetain risk this will backfire. Second, this will be a short game that can probably be finished within the hour.</p>

<p>As I mentioned earlier, I want this to feel like a proper story. This means that the player should be able to behave like she would in real life, and expect a reasonable outcome. It also means that I have to deal with certain IF staples in creative ways. For example, realism dictates that there should be certain props (like a sideboard with a tea kettle and some breakfast on a sideboard in the Dining Romm) in the game. I want the player to be able to pick up and interact with these objects, but not carry them around the house (which would spoil the overall tone of the story). Thus, I created a mechanism for handling unimportant items:</p>
<div class='i7-sample'>
  <div class='i7-heading'>Chapter - Non-essential props</div>
  <br />
  <div>Things can be unimportant.</div>
  <br />
  <div>A room has an object called the resting place. </div>
  <br />
  <div>Carry out going when the player has an unimportant thing:</div>
  <div class='i7-indent-1'>let the resting place be the resting place of the location;</div>
  <div class='i7-indent-1'>let the inessentials be the list of unimportant things carried by the player;</div>
  <div class='i7-indent-1'>repeat with the inessential running through the inessentials:</div>
  <div class='i7-indent-2'>if the resting place is a supporter:</div>
  <div class='i7-indent-3'>try silently putting the inessential on the resting place;</div>
  <div class='i7-indent-2'>otherwise if the resting place is a container and the resting place is open:</div>
  <div class='i7-indent-3'>try silently inserting the inessential into the resting place;</div>
  <div class='i7-indent-2'>otherwise:</div>
  <div class='i7-indent-3'>try silently dropping the inessential;</div>
  <div class='i7-indent-1'>if the resting place is a supporter:</div>
  <div class='i7-indent-2'>say <div class='i7-quote'>"You put the <div class='i7-bracket'>[the inessentials]</div> back on <div class='i7-bracket'>[the resting place]</div>."</div>;</div>
  <div class='i7-indent-1'>otherwise if the resting place is a container:</div>
  <div class='i7-indent-2'>say <div class='i7-quote'>"You put the <div class='i7-bracket'>[the inessentials]</div> back in <div class='i7-bracket'>[the resting place]</div>."</div>;</div>
  <div class='i7-indent-1'>otherwise:</div>
  <div class='i7-indent-2'>say <div class='i7-quote'>"You leave the <div class='i7-bracket'>[the inessentials]</div> in <div class='i7-bracket'>[the printed name of the location in lower case]</div>."</div></div>
</div>
<p>This rule ensures that an item that is declared <code>unimportant</code> the player will drop it before leaving the room. To make it a bit more realistic, you can name a container or supporter a <code>resting place</code> (for want of a better name) for each room. Unimportant objects will then be put back on the supporter or back into the container.</p>

<p>This is the (somewhat abridged) description of my Dining Room:</p>
<div class='i7-sample'>
  <div class='i7-heading'>Section - Dining Room</div>
  <br />
  <div>The sideboard is a fixed in place supporter in Dining Room. <div class='i7-quote'>"Along one of the walls is a grand old sideboard crafted out of solid oak. Conveniently placed on it is <div class='i7-bracket'>[a list of things on the sideboard]</div>."</div> The resting place of Dining Room is the sideboard.</div>
  <br />
  <div>The silver kettle is on the sideboard. </div>
  <br />
  <div>The kettle contains the tea. The description of the tea is <div class='i7-quote'>"Smells like Earl Grey."</div> The indefinite article of the tea is <div class='i7-quote'>"some"</div>.</div>
  <br />
  <div>Some breakfast is on the sideboard. <div class='i7-quote'>"It seems Watkins has prepared some cucumber sandwiches for you. Just as good, you sincerely doubt you'd be able to eat a full breakfast right now."</div> The breakfast is edible.</div>
  <br />
  <div>The kettle and breakfast are unimportant.</div>

</div>
<p>And this is how it plays:</p>

<pre><code>Dining Room
Along one of the walls is a grand old sideboard crafted out of solid oak. 
Conveniently placed on it is a silver kettle, some breakfast, some china and a 
crystal decanter.

&gt;take kettle
Taken.

&gt;take breakfast
Taken.

&gt;i
You are carrying:
  some breakfast
  a silver kettle
    some tea
  a candle (providing light)
  some clothes (being worn)

&gt;w
You put the breakfast and silver kettle back on the sideboard.

[New room description snipped]</code></pre>

<p>Allright, I should stop now. I just realized I spent an hour on this post instead of actually working on the darn game. See ya next time!</p></content>
 </entry>
 
 <entry>
   <title>Going Remote</title>
   <link href="http://mulli.nu/2011/04/06/going-remote.html"/>
   <updated>2011-04-06T00:00:00+00:00</updated>
   <id>http://mulli.nu/2011/04/06/going-remote</id>
   <content type="html"><p>A week from now the movers will come and stuff all our belongings into a big ol&#8217; truck. We will get in the car, wave goodbye to friends and family and drive north for two days. There, in <a href='http://sv.wikipedia.org/wiki/%C3%96jebyn'>a small town you&#8217;ve probably never heard of</a>, lies our new home.</p>

<p>Naturally, this is quite a scary proposition for a city boy. I honestly don&#8217;t know if I&#8217;ll make it up there. But I was given a great opportunity, and I had to at least try. Besides, life in the city was getting a bit <em>too</em> convenient, if you know what I mean? It was time for a change.</p>

<p>Fortunately, my employer was keen to keep me on, which means that starting two weeks from now I&#8217;ll be working 100% remotely. This is a big change, and I hope it will work out for everyone involved. Our team has started using <a href='http://www.pivotaltracker.com'>PivotalTracker</a> and <a href='http://campirenow.com'>Campfire</a>, and the overall impression is good so far. However, I will be working from my home office, and I just hope the cabin fever doesn&#8217;t get unbearable.</p>

<p>Another big change is that I&#8217;ll just be working 80%. Living in the northern parts of Sweden is pretty cheap, and after doing the math we decided that it wasn&#8217;t really necessary for me to work full time anymore. Instead, I&#8217;ll try to spend the extra time on fun personal projects, without having to worry about them earning me any money (although it would be nice if they did).</p>

<p>I&#8217;ve been longing to give game development a shot for a long time now, and this is kind of my big chance. For a guy with my background the obvious choice seems to be mobile game development with Android, and I already have some ideas to work on. I&#8217;m also really intrigued by what can be done with <a href='http://lispgamesdev.blogspot.com/'>Common Lisp</a>, which I believe could make for a great interactive game environment. However, my first goal is to finish my <a href='/2011/02/11/fun-with-inform7.html'>Inform 7 game</a>, hopefully being able to enter it into <a href='http://www.ifcomp.org/'>IFComp 2011</a>. Watch this space.</p>

<p>You know, I have a good feeling about this.</p></content>
 </entry>
 
 <entry>
   <title>Fun with Inform7</title>
   <link href="http://mulli.nu/2011/02/11/fun-with-inform7.html"/>
   <updated>2011-02-11T00:00:00+00:00</updated>
   <id>http://mulli.nu/2011/02/11/fun-with-inform7</id>
   <content type="html"><p>For the past month I&#8217;ve been amusing myself with a wonderful language: <a href='http://inform7.com'>Inform7</a>. It is a <em>very</em> domain-specific language in that its main application is authoring text adventures (oh, sorry, &#8217;<a href='http://en.wikipedia.org/wiki/Interactive_fiction'>Interactive Fiction</a>&#8217;), and produces the <a href='http://en.wikipedia.org/wiki/Z-machine'>Z-code</a> or <a href='http://www.eblong.com/zarf/glulx/'>Gluglx</a> bytecode when compiled.</p>

<p>There are several reasons I really shouldn&#8217;t enjoy this language so much:</p>

<ul>
<li>It uses natural language, which I initially thought was a terrible choice for language design. I&#8217;ve always treasured brevity and conciseness in code, but I may have to reconsider. True, Inform7 code is pretty verbose and sometimes a bit repetitive, but at the same time it flows like easy to read prose, kind of like Knuth&#8217;s <a href='http://en.wikipedia.org/wiki/Literate_programming'>literate programming</a>.</li>

<li>It comes with an IDE, which you are strongly encouraged to use. Normally I&#8217;m a die-hard emacs guy who only uses Eclipse as a last resort, but in this case it works pretty well. There is integrated documentation, a system for structured code, and testing facilities especially tailored for helping you run through different branches of you interactive stories.</li>
</ul>

<p>Coding in Inform7 is an interesting experience. The code reads a lot like regular english, but of course it isn&#8217;t <em>really</em> natural language. There is still a basic level of syntax you have to adhere to, even if the parser does some fairly intelligent disambiguation of terms and lets you describe you world in a lot of alterative ways.</p>

<p>Yes, &#8216;describe your world&#8217;. That pretty much sums it up; a program is mostly a description of you model game world, with rooms, objects and behaviours. This is a trivial treasure collecting game in five lines of code (including the title):</p>
<div class='i7-sample'>
  <div class='i7-quote'>"Treasure Hunt"</div> by Fredrik Appelberg    <br />
  <br />
  <div>Treasure Hill is a room. The description is <div class='i7-quote'>"A green and abundant hill, with a view of the surrounding fields. It makes an excellent starting point for your treasure hunt."</div></div>
  <br />
  <div>The chest is an closed openable container in Treasure Hill. It contains a gold coin.</div>
  <br />
  <div>The player is carrying a brass lantern. The player is wearing a cloak.</div>
  <br />
  <div>Every turn when the player is carrying the coin: end the story saying <div class='i7-quote'>"You won!"</div>  </div>
</div>
<p>Describing an object is the same as creating it. Here I create a room (with a descriptive text), a chest with a gold coin inside, a lantern and a cloak held by the player. The cloak is interesting: since I state that the player is wearing it, Inform7 is able to infer that it is a piece of clothing (an object subclass that has its own set of properties) without me having to say it. Also, I&#8217;ve explicitly stated that the chest is a container (another subclass), but in this example that&#8217;s not really necessary, since the compiler can infer that from the fact that there&#8217;s a coin inside the chest. However, I&#8217;ll leave it like this since it makes the text a bit easier to read.</p>

<p>For me, this is a very novel way of coding, and reminds me of the <a href='http://en.wikipedia.org/wiki/Prolog'>Prolog</a> hacking we did at the university: programming is mostly just stating facts about the world, and trusting the compiler to handle the rest.</p>

<p>Compiling and running is as simple as pushing a button. This is what a transcript of the game looks like:</p>

<pre><code>Treasure Hunt
An Interactive Fiction by Fredrik Appelberg
Release 1 / Serial number 110211 / Inform 7 build 6G60 (I6/v6.32 lib 6/12N) SD

Treasure Hill
A green and abundant hill, with a view of the surrounding fields. It makes an excellent 
starting point for your treasure hunt.

You can see a chest (closed) here.

&gt;i
You are carrying:
  a brass lantern
  a cloak (being worn)

&gt;x chest
You see nothing special about the chest.

&gt;open it
You open the chest, revealing a gold coin.

&gt;take the coin
Taken.

    *** You won! ***

In that game you scored 0 out of a possible 0, in 4 turns.

Would you like to RESTART, RESTORE a saved game, QUIT or UNDO the last command?</code></pre>

<p>But that&#8217;s just my simple play-through. If you like, you can try the game yourself, <a href='/media/games/treasurehunt/play.html'>without leaving your browser</a>. That&#8217;s right: Inform7 also generates a simple website for your game, complete with a z-code interpreter written in javascript.</p>

<p><img class='right article_image' src='http://3.bp.blogspot.com/_j-AiRnNgy6s/TEt5OuqTwiI/AAAAAAAAASM/OA99j2FiNpw/s320/cifwi7cover-small.jpg' alt='Creating Interactive Fiction With Inform 7' /></p>

<p>If you don&#8217;t hear from me for a while, it&#8217;s probably because I&#8217;m busy hacking my first serious game. It&#8217;s taken me longer than anticipated, but that doesn&#8217;t bother me. Writing with Inform7 is <em>such joy</em> that I can&#8217;t help myself.</p>

<p>By the way, I thought I&#8217;d take the time to plug Aaron Reed&#8217;s excellent <a href='http://www.amazon.com/Creating-Interactive-Fiction-Inform-7/dp/1435455061'>Creating Interactive Fiction With Inform 7</a>. It&#8217;s one of the most inspiring programming books I&#8217;ve ever read. And I&#8217;ve been through a few.</p></content>
 </entry>
 
 <entry>
   <title>Introducing Woodchipper</title>
   <link href="http://mulli.nu/2010/07/20/introducing-woodchipper.html"/>
   <updated>2010-07-20T00:00:00+00:00</updated>
   <id>http://mulli.nu/2010/07/20/introducing-woodchipper</id>
   <content type="html"><p>Well, my <a href='/2010/06/22/nolog.html'>last post</a> may have been a bit of a rant, but it did spur my creativity. The more I thought about automatically replacing logging statements with <code>System.out.println()</code>, the more the idea appealed to me. A couple of days later I had a day off with beautiful weather and nothing to do, and I spent it out on the balcony, hacking frantically. In the end I had a rough proof of concept that I&#8217;ve now refined to the point that I&#8217;m not embarrased to show it to people.</p>

<p>Ladies and gentlemen, I give you <a href='http://github.com/fred-o/woodchipper'>WoodChipper</a>. It is a command line utility that will automatically remove all <a href='http://logging.apache.org/log4j/1.2/'>Log4j</a> and <a href='http://commons.apache.org/logging/'>Commons Logging</a> statements from a given .jar file or a classpath directory. This happens through bytecode manipulation, so the dependencies are removed completely. There is no classpath trickery like with <a href='http://slf4j.org'>Slf4j</a> (which supplies its own replacement jars for the underlying logging system), no fiddling with your library path, no more need to write exclusion filters in maven. You can run it on your own code, but it&#8217;s really designed to work on 3rd party .jar files where you can&#8217;t influence the choice of logging framework.</p>

<p>Depending on your opinion on the state of Java logging frameworks, this is either wonderful or a terrible idea. Anyway, let me know what you think.</p>

<h2 id='demo'>Demo</h2>

<p>You need to have git and maven installed. Now get a bash prompt and download and install WoodChipper:</p>

<pre><code>&gt; git clone http://github.com/fred-o/woodchipper
&gt; cd woodchipper
&gt; mvn package

[lots and lots of maven output]</code></pre>

<p>This builds <code>woodchipper</code> and <code>woodchipper-test-jar</code>. The latter is a small project with trivial classes you can use for testing. Let&#8217;s start by trying to run one of the classes directly:</p>

<pre><code>&gt; java -cp woodchipper-test-jar/target/woodchipper-test-jar-1.0-SNAPSHOT.jar woodchipper.TestClass

Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: org/apache/log4j/Priority
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Priority
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)</code></pre>

<p>Ok, so we get <code>NoClassDefFoundError</code>. That&#8217;s actually expected, as I didn&#8217;t include any log4j .jar file on the classpath. Anyway, let&#8217;s run woodchipper on the sucker:</p>

<pre><code>&gt; java -jar woodchipper/target/woodchipper.jar -i \
    woodchipper-test-jar/target/woodchipper-test-jar-1.0-SNAPSHOT.jar 

Removing Log4J references from woodchipper/TestClass.class
Removing Commons Logging references from woodchipper/TestClass4.class</code></pre>

<p>Looks like it&#8217;s working. Let&#8217;s try running it again:</p>

<pre><code>&gt; java -cp woodchipper-test-jar/target/woodchipper-test-jar-1.0-SNAPSHOT.jar woodchipper.TestClass

first message!
second message!
third message!
fourth message!
fifth message (with exception!)
java.lang.IllegalArgumentException
    at woodchipper.TestClass.main(TestClass.java:29)</code></pre>

<p>Yay! No log4j dependency anymore! (Note that the exception at the end isn&#8217;t acutally an error with WoodChipper, just a logged exception).</p></content>
 </entry>
 
 <entry>
   <title>NoLOG</title>
   <link href="http://mulli.nu/2010/06/22/nolog.html"/>
   <updated>2010-06-22T00:00:00+00:00</updated>
   <id>http://mulli.nu/2010/06/22/nolog</id>
   <content type="html"><p>Java logging is fundamentally broken. This is hard fact, not a point of view. Logging is broken, and has been for years.</p>

<p>How is it broken, you ask? Let me count the ways:</p>

<ul>
<li>I&#8217;ve had application servers die on me because a logging framework was complaining over something. This shouldn&#8217;t happen. Ever.</li>

<li>One of our old production servers is running JBoss4 and has been running for five years now. About two years ago Log4J started giving strange error messages that has something to do with class loading. I have no idea if we even changed anything, it just stopped working.</li>

<li>We switched to Glassfish after that. Now, glassfish is a terrific app server, but it insists on using the JDK logging facilities. Since we use Log4J internally that means everything gets mangled through <code>System.out</code>, then logged with a severity of INFO regardless of the original severity. Glassfish also uses a really ugly multi-line logging format as default. I&#8217;d really like to change that if I could only figure out how.</li>

<li>Also, we have three Glassfish servers. Two of them write their logs like they&#8217;re supposed to. On the third machine (identical setup as far as I can tell) it stops logging right after boot. No indication of what&#8217;s going on, just silence.</li>

<li>Speaking of JDK logging, what&#8217;s up with the logging levels? I dig SEVERE, WARNING, INFO; they&#8217;re all right. Maybe even CONFIG. But FINE, FINER and FINEST? That&#8217;s just stupid.</li>

<li>And don&#8217;t get me started about Commons Logging. The problems with this sorry piece of software <a href='http://articles.qos.ch/thinkAgain.html'>is well documented</a>.</li>

<li>I have never gotten Log4j to work with Jetty, and not for lack of trying.</li>

<li>I wanted to deploy <a href='http://nexus.sonatype.org/'>Nexus</a> (an otherwise excellent product) to a Glassfish server. Shouldn&#8217;t be a problem, right? Just a standard WAR in a standard Servlet container, right? Nope, couldn&#8217;t be done because of serious bugs in the logging code. I now have to run Nexus in its own container on a different port. This is frankly ridiculous. Why should the implementors choice of logging framework dictate what web cointainer I can run?</li>
</ul>

<p>In short, I have spent way to much time just getting simple logging working through the years, and now I&#8217;ve had just about enough. Think of the man-hours lost, what we could be doing instead of fiddling with <code>log4j.properties</code> and <code>commons-logging.properties</code>! Why do we put up with this?</p>

<p>The basic principles of logging are sound, but it&#8217;s too late. There are simply <a href='http://en.wikipedia.org/wiki/Java_Logging_Frameworks'>too many logging frameworkds</a> out there, each one making the situation a little bit worse. Commons Logging was bad enough. Log4J fractured the market, and Sun cemented the situation by introducing the badly designed JDK logging package.</p>

<p><a href='http://slf4j.org'>Logging meta-frameworks</a> isn&#8217;t really helping the situation. The ship has sailed. We don&#8217;t need <em>more</em> frameworks, we need less of them. The ideal situation would be exactly <em>one</em> logging framework (that doesn&#8217;t suck), that would be the obvious choice for most projects. As it stands now all open source projects use a different one, each incompatible with each others but offering &#8216;helpful&#8217; and &#8216;seamless&#8217; integration that never seems quite to work.</p>

<p>Last time I checked the dependencies for our main webapp at work included multiple versions of Log4J, Commons Logging, Slf4J, and problably Logback and a few others I&#8217;ve missed as well. Have fun working out the classloader issues there!</p>

<h2 id='an_interim_solution_woodchipper'>An interim solution: WoodChipper</h2>

<p>I dreamt this up at work the other day, and this is all purely hypothetical, but I do believe someone should make something like this.</p>

<p>Anyway, <em>WoodChipper</em> (or whatever it will be called, I choose the name since it is a machine that disintegrates logs) is a java utility that goes through all the jar files on your class path and uses sophisticated byte code manipulation to replace all references to Commons Logging, Log4J, JDK logging, etc. with <code>System.out.println()</code>. Ideally this could be done at runtime, or as a maven plugin.</p>

<p>I know some people would argue it&#8217;d better to make it all <a href='http://slf4j.org'>Slf4J</a> instead, but then you&#8217;re kind of missing the point. I&#8217;ve had enough of logging. I don&#8217;t want to spend another minute configuring a logging framework, and I don&#8217;t see why I should have to. I&#8217;m declaring logging bancruptcy.</p>

<p>Writing to <code>stdout</code> isn&#8217;t bad. At least the results can be piped, greped and redirected using standard Unix tools.</p>

<h2 id='is_it_time_for_a_nolog_license'>Is it time for a NoLOG license?</h2>

<p>Today I started thinking that maybe what we need is a new sort of Open Source license, kind of like an inverted GPL. It would state that you&#8217;re free to use my code pretty much anyway you like, just as long as the finished product does not depend on a logging framework. Any transgressions would be punishable by catapult.</p>

<p>Extreme measures, perhaps. But if that&#8217;s what it takes to get people to stop using Commons Logging, it may well be worth it in the end.</p></content>
 </entry>
 
 <entry>
   <title>Inspiration-Driven Development</title>
   <link href="http://mulli.nu/2010/06/11/inspiration-driven-development.html"/>
   <updated>2010-06-11T00:00:00+00:00</updated>
   <id>http://mulli.nu/2010/06/11/inspiration-driven-development</id>
   <content type="html"><p>I want to talk a little about inspiration.</p>

<p>The act of programming is of a very creative nature. Abstract concepts like data structures and algorithms get crystalized to code, seemingly created <em>ex nihilo</em>. Where first there was nothing, there is now a file of source code.</p>

<p>Writing code can be an exhausting occupation, and sometimes frustrating. It can wear you out emotionally and physically. But when you&#8217;re in the zone, just going with it and letting code flow from your fingers, there&#8217;s nothing quite like it. I once tried to describe the allure of coding as <em>&#8220;Kind of a cross between knitting and crossword-solving&#8221;</em>, by which I mean a highly intellectual activity that&#8217;s also half handicraft. My girlfriend, both a crossword enthusiast and a needlework afficionado, understood completely.</p>

<p>Unfortunatly, I find myself spending far less time doing actual development work than I&#8217;d like to, and I think this is a common complaint among programmers.</p>

<h2 id='real_life'>Real Life</h2>

<p>The company I work for employs about 60 people, but only five of us work for the IT department. So, we&#8217;re a small team that are expected to do everything from refilling printer cartridges to maintaining and architecting the J2EE behemoth that is our web site. My business card says &#8216;Systems Developer&#8217;, but in reality a large part of my job seems to be to figure out what my job is. I do sysadmin stuff, Java coding, CSS troubleshooting, customer support, system integration, even business development on occasion. There&#8217;s a lot of non-programming stuff going on, I get a lot of email, and I tend to spend a substatial amount of time on the web, keeping up with current events.</p>

<p>This used to bug me a lot. I resented the constant interruptions, the emails, the meetings. <em>&#8220;Why pay me to work here when you won&#8217;t let me do my job?&#8221;</em> I used to mutter. I loathed myself for wasting company time surfing the web. The stress was getting to me. But lately I&#8217;ve come to realize one important thing:</p>

<blockquote>
<p class='box'> Even the non-coding activities are a part of my job as a developer.</p>
</blockquote>

<p>Listening to suggestions, responding to criticism and above all, managing expectations, is crucial. Reading tech blogs is a part of my job, and so is taking a break to slack off or go for a walk if I need to. Because my job isn&#8217;t to just sit in front of a computer and produce code. It is to ensure that the systems we deliver are as good as they&#8217;re ever going to be.</p>

<p>I know some of my (non-IT) co-workers would be suprised by this. <em>&#8220;Surfing the web a part of the job? Really, that&#8217;s just a poor excuse for slacking off. That&#8217;s like stealing from the Company!&#8221;</em></p>

<p>Just kidding, I have wonderful co-workers. But here&#8217;s the thing:</p>

<blockquote>
<p class='box'> The best coding happens in the brain, long before you actually sit down to write.</p>
</blockquote>

<p>I&#8217;ll be so bold as to claim that the time you&#8217;re spending at the keyboard is your <em>least</em> creative period. There&#8217;s too much distractions, too many things happening. You get too invested in what you&#8217;re building to think deep and hard about the underlying problem. That&#8217;s when you need to take a break, step away from the computer and go do something else.</p>

<h2 id='pro_tip'>Pro Tip</h2>

<p>Here&#8217;s a tip: my favourite way of solving particularly hairy coding problems is to go sit down somewhere with my moleskine and my favourite pen (<a href='http://www.cultpens.com/acatalog/Pilot_G2.html'>Pilot G2 0.7mm</a>, since you ask). I try to write down the problem as clear as I possibly can, then go about describing ways of tackling it. Usually I start with some obvious solution, then write down the reason that it won&#8217;t work. Then I come up with something else, then something else. And then suddenly the solution presents itself, like magic.</p>

<p>This method has worked, without fail, every time I&#8217;ve used it. But you can&#8217;t cheat. You need to get away from the computer, and you need to write things down. By hand. I don&#8217;t know why, but I suspect the act of writing activates a part of your brain you wouldn&#8217;t otherwise be using.</p>

<h2 id='inspiration'>Inspiration</h2>

<p>I&#8217;ve found that when the stars are aligned correctly I&#8217;m at my coding peak between 3pm and 7pm. There seems to be very little I can do about that; earlier in the day my thoughts are too scattered for me to be of any use, and later in the evening I get too hungry to focus on anything but dinner. The sad part about this is that when I&#8217;m not working from home I have to leave the office at 5pm in order to catch my train. That leaves about 2 hours of productive time a day.</p>

<p>I&#8217;ve come to accept that, and started structuring my day to accomodate this fact instead of fighting it. Before lunch I respond to mail, make notes, read blogs, get the facebooking out of the way, try to check out any bug report we&#8217;ve received, return calls to customers who are having problems, etc. Basically, I&#8217;m trying to remove all the obstacles that will impede my progress when the gears in my brain finally click and I can get to work.</p>

<p>Some days that doesn&#8217;t happen. The magic is gone, and it feels like I can&#8217;t get <em>anything</em> done. That&#8217;s okay too. There&#8217;s always going to be days like that, and it usually happens when I&#8217;m stressed out or tired. Those days I do stuff that requires as little mental effort as possible. Simple code refactoring, perhaps. Following up on email. Going through old stuff on the todo list that&#8217;s been nagging me. That is, ensuring that when inspiration returns, I will be ready for it.</p></content>
 </entry>
 
 <entry>
   <title>The Anti-iPad</title>
   <link href="http://mulli.nu/2010/05/11/the-anti-ipad.html"/>
   <updated>2010-05-11T00:00:00+00:00</updated>
   <id>http://mulli.nu/2010/05/11/the-anti-ipad</id>
   <content type="html"><p>I bought a new computer last week. A laptop. A very, very tiny laptop, in fact. It&#8217;s called a <a href='http://sharism.cc/'>Ben NanoNote</a>, and it makes standard netbooks look cumbersome and unwieldy.</p>

<h3 id='i_mean_just_look_at_the_thing'>I mean, just look at the thing</h3>

<p><img src='/img/nanonote.jpg' alt='My nanonote, dwarfed by the iMac.' /></p>

<h3 id='not_for_everyone_though'>Not for everyone, though</h3>

<p>This is not a consumer-oriented device, at least not yet. For example, the first thing you should do when you get yours is to reflash the firmware with the latest version. This is a scary operation requiring you to remove the battery, short-circuit a few pins with tinfoil, attach it to a Linux machine and run an update script (which took about 20-30 minutes to finish). This is a hard sell if you&#8217;re targeting the regular Apple customers.</p>

<p>With the newest firmware you get a graphical menu system that&#8217;s kind-of nice (but ultimatly not very useful) and an mp3 player that I can&#8217;t get to play my songs. Don&#8217;t know if that&#8217;s my fault or not but again, a hard sell for most people.</p>

<h3 id='however'>However:</h3>

<ul>
<li>At <a href='http://hackable-devices.org/shop/product/nanonote'>110 euro</a> (plus 25 for shipping), it&#8217;s relatively inexpensive.</li>

<li>It&#8217;s running <a href='http://openwrt.org'>OpenWRT</a>, a linux distribution primarily targeted at embedded devices. You can run vim, zile (bare-bones emacs), python, ruby, lua, whatever you manage to fit into the 32MB of RAM. Surprisingly, mine had a web server activated by default when I got it. I have no idea what I&#8217;d do with a web server on this little machine, but it&#8217;s still pretty cool.</li>

<li>Not only is it running free software, the hardware specs are free as well. In fact, you are encouraged to copy, modify and share the hardware design and build your own devices.</li>

<li>It&#8217;s small, really small. Smaller than my Nintendo DS, just about the size of my wallet. In fact, just by looking at pictures it&#8217;s hard to fathom just how tiny it is. You really need to hold it in your hand.</li>

<li>And yet it has a 54-key keyboard. Granted, it&#8217;s too small to accommodate touch typing (and I have pretty petite hands) but still, it&#8217;s a keyboard with <em>real</em> keys.</li>

<li>Oh yeah, and it <a href='http://www.linux-netbook.com/video/playing-doom-and-quake-on-the-ben-nanonote'>plays quake</a>.</li>
</ul>

<p>In short, it&#8217;s open (<em>truly</em> open), multi-tasking, small and affordable. It is the Anti-iPad.</p></content>
 </entry>
 
 <entry>
   <title>Aop.js</title>
   <link href="http://mulli.nu/2010/05/07/aop-js.html"/>
   <updated>2010-05-07T00:00:00+00:00</updated>
   <id>http://mulli.nu/2010/05/07/aop-js</id>
   <content type="html"><p>My excursions into the dark recess of my <code>/js</code> directory continue. This was originally something I wrote a while back when I was learning about <a href='http://en.wikipedia.org/wiki/Aspect-oriented_programming'>Aspect-oriented programming</a> in Java and wanted see how much trouble it would be to write something similar for JavaScript. As it turned out, writing the core functions wasn&#8217;t that complicated at all.</p>
<div class='highlight'><pre><code class='js'><span class='nx'>Aop</span> <span class='o'>=</span> <span class='p'>{</span>
  <span class='c1'>// Apply around advice to all matching functions in the given namespaces</span>
  <span class='nx'>around</span><span class='o'>:</span> <span class='kd'>function</span><span class='p'>(</span><span class='nx'>pointcut</span><span class='p'>,</span> <span class='nx'>advice</span><span class='p'>,</span> <span class='nx'>namespaces</span><span class='p'>)</span> <span class='p'>{</span>
    <span class='c1'>// if no namespaces are supplied, use a trick to determine the global ns</span>
    <span class='k'>if</span> <span class='p'>(</span><span class='nx'>namespaces</span> <span class='o'>==</span> <span class='kc'>undefined</span> <span class='o'>||</span> <span class='nx'>namespaces</span><span class='p'>.</span><span class='nx'>length</span> <span class='o'>==</span> <span class='mi'>0</span><span class='p'>)</span> 
      <span class='nx'>namespaces</span> <span class='o'>=</span> <span class='p'>[</span> <span class='p'>(</span><span class='kd'>function</span><span class='p'>(){</span><span class='k'>return</span> <span class='k'>this</span><span class='p'>;}).</span><span class='nx'>call</span><span class='p'>()</span> <span class='p'>];</span>
    <span class='c1'>// loop over all namespaces </span>
    <span class='k'>for</span><span class='p'>(</span><span class='kd'>var</span> <span class='nx'>i</span> <span class='k'>in</span> <span class='nx'>namespaces</span><span class='p'>)</span> <span class='p'>{</span>
      <span class='kd'>var</span> <span class='nx'>ns</span> <span class='o'>=</span> <span class='nx'>namespaces</span><span class='p'>[</span><span class='nx'>i</span><span class='p'>];</span>
      <span class='k'>for</span><span class='p'>(</span><span class='kd'>var</span> <span class='nx'>member</span> <span class='k'>in</span> <span class='nx'>ns</span><span class='p'>)</span> <span class='p'>{</span>
        <span class='k'>if</span><span class='p'>(</span><span class='k'>typeof</span> <span class='nx'>ns</span><span class='p'>[</span><span class='nx'>member</span><span class='p'>]</span> <span class='o'>==</span> <span class='s1'>&#39;function&#39;</span> <span class='o'>&amp;&amp;</span> <span class='nx'>member</span><span class='p'>.</span><span class='nx'>match</span><span class='p'>(</span><span class='nx'>pointcut</span><span class='p'>))</span> <span class='p'>{</span>
          <span class='p'>(</span><span class='kd'>function</span><span class='p'>(</span><span class='nx'>fn</span><span class='p'>,</span> <span class='nx'>fnName</span><span class='p'>,</span> <span class='nx'>ns</span><span class='p'>)</span> <span class='p'>{</span>
             <span class='c1'>// replace the member fn slot with a wrapper which calls</span>
             <span class='c1'>// the &#39;advice&#39; Function</span>
             <span class='nx'>ns</span><span class='p'>[</span><span class='nx'>fnName</span><span class='p'>]</span> <span class='o'>=</span> <span class='kd'>function</span><span class='p'>()</span> <span class='p'>{</span>
               <span class='k'>return</span> <span class='nx'>advice</span><span class='p'>.</span><span class='nx'>call</span><span class='p'>(</span><span class='nx'>ns</span><span class='p'>,</span> <span class='p'>{</span> <span class='nx'>fn</span><span class='o'>:</span> <span class='nx'>fn</span><span class='p'>,</span> 
                                          <span class='nx'>fnName</span><span class='o'>:</span> <span class='nx'>fnName</span><span class='p'>,</span> 
                                          <span class='nx'>arguments</span><span class='o'>:</span> <span class='nx'>arguments</span> <span class='p'>});</span>
             <span class='p'>};</span>
           <span class='p'>})(</span><span class='nx'>ns</span><span class='p'>[</span><span class='nx'>member</span><span class='p'>],</span> <span class='nx'>member</span><span class='p'>,</span> <span class='nx'>ns</span><span class='p'>);</span>
        <span class='p'>}</span>
      <span class='p'>}</span>
    <span class='p'>}</span>
  <span class='p'>},</span>

  <span class='nx'>next</span><span class='o'>:</span> <span class='kd'>function</span><span class='p'>(</span><span class='nx'>f</span><span class='p'>)</span> <span class='p'>{</span>
    <span class='k'>return</span> <span class='nx'>f</span><span class='p'>.</span><span class='nx'>fn</span><span class='p'>.</span><span class='nx'>apply</span><span class='p'>(</span><span class='k'>this</span><span class='p'>,</span> <span class='nx'>f</span><span class='p'>.</span><span class='nx'>arguments</span><span class='p'>);</span>
  <span class='p'>}</span>
<span class='p'>};</span>
</code></pre>
</div>
<p>This is basically all there is to it. The <code>Aop.around()</code> method takes a <a href='http://en.wikipedia.org/wiki/Pointcut'>pointcut</a> (a regexp pattern), a reference to the <a href='http://en.wikipedia.org/wiki/Advice_in_aspect-oriented_programming'>advice</a> (function reference) to be applied and an optional list of namespaces (which in this context is just fancy talk for &#8216;JavaScript object&#8217;). It then loops through all slots in each namespace (defaulting to the global object if no namespaces were specified) and replaces any functions whose name matches the pointcut pattern with a wrapper function.</p>

<p>The <code>Aop.next()</code> method is just a convenience: it is used to pass control over to the original function.</p>

<h2 id='example_time'>Example time</h2>

<p>All right, time for an example. Everyone loves examples, right? Right. Here&#8217;s me defining a trivial function in the global namespace:</p>
<div class='highlight'><pre><code class='js'><span class='nx'>js</span><span class='o'>&gt;</span> <span class='kd'>function</span> <span class='nx'>hello</span><span class='p'>(</span><span class='nx'>name</span><span class='p'>)</span> <span class='p'>{</span> <span class='nx'>print</span><span class='p'>(</span><span class='s1'>&#39;hello, &#39;</span> <span class='o'>+</span> <span class='nx'>name</span><span class='p'>);</span> <span class='p'>}</span>
<span class='nx'>js</span><span class='o'>&gt;</span> <span class='nx'>hello</span><span class='p'>(</span><span class='s1'>&#39;fredrik&#39;</span><span class='p'>)</span>
<span class='nx'>hello</span><span class='p'>,</span> <span class='nx'>fredrik</span>
<span class='nx'>js</span><span class='o'>&gt;</span> 
</code></pre>
</div>
<p>By the way, I&#8217;m using <a href='http://www.mozilla.org/rhino/'>rhino</a>, as I like having a JavaScript <a href='http://en.wikipedia.org/wiki/Read-eval-print_loop'>REPL</a> i can run directly from the command line. You should be able to embed the script in a web page and run it from the <a href='http://getfirebug.com'>Firebug</a> console as well, but you&#8217;ll have to change <code>print()</code> to <code>console.log()</code>.</p>

<p>Now, let&#8217;s apply some around advice to my greeter function:</p>
<div class='highlight'><pre><code class='js'><span class='nx'>js</span><span class='o'>&gt;</span> <span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>around</span><span class='p'>(</span><span class='s1'>&#39;hello.*&#39;</span><span class='p'>,</span> <span class='kd'>function</span><span class='p'>(</span><span class='nx'>f</span><span class='p'>)</span> <span class='p'>{</span>
  <span class='o'>&gt;</span> <span class='nx'>print</span><span class='p'>(</span><span class='s1'>&#39;[before &#39;</span> <span class='o'>+</span> <span class='nx'>f</span><span class='p'>.</span><span class='nx'>fnName</span> <span class='o'>+</span> <span class='s1'>&#39;]&#39;</span><span class='p'>);</span>
  <span class='o'>&gt;</span> <span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>next</span><span class='p'>(</span><span class='nx'>f</span><span class='p'>);</span>
  <span class='o'>&gt;</span> <span class='nx'>print</span><span class='p'>(</span><span class='s1'>&#39;[after &#39;</span> <span class='o'>+</span> <span class='nx'>f</span><span class='p'>.</span><span class='nx'>fnName</span> <span class='o'>+</span> <span class='s1'>&#39;]&#39;</span><span class='p'>);</span>
  <span class='o'>&gt;</span> <span class='p'>})</span>
<span class='nx'>js</span><span class='o'>&gt;</span> <span class='nx'>hello</span><span class='p'>(</span><span class='s1'>&#39;fredrik&#39;</span><span class='p'>)</span>
<span class='p'>[</span><span class='nx'>before</span> <span class='nx'>hello</span><span class='p'>]</span>
<span class='nx'>hello</span><span class='p'>,</span> <span class='nx'>fredrik</span>
<span class='p'>[</span><span class='nx'>after</span> <span class='nx'>hello</span><span class='p'>]</span>
<span class='nx'>js</span><span class='o'>&gt;</span> 
</code></pre>
</div>
<p>So, what&#8217;s happening here? We&#8217;re telling Aop.js that we want to add a wrapper around all functions starting with the name &#8216;hello&#8217; in the global namespace. Upon function invocation, this wrapper function calls the advice function with a map of named parameters such as the orignal name of the function being called, a reference to the original function and a list of function arguments. What happens then is up to the advice function; in this case it just calls the original method (using <code>Aop.next()</code>), printing out a message before and after the call. Not very exciting perhaps, but certainly proof of concept.</p>

<p>But wait, did I just say that the function arguments is passed on to the advice function? I sure did. Does that mean that I can manipulate them before the original function is called? You bet:</p>
<div class='highlight'><pre><code class='js'><span class='nx'>js</span><span class='o'>&gt;</span> <span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>around</span><span class='p'>(</span><span class='s1'>&#39;hello.*&#39;</span><span class='p'>,</span> <span class='kd'>function</span><span class='p'>(</span><span class='nx'>f</span><span class='p'>)</span> <span class='p'>{</span>
  <span class='o'>&gt;</span> <span class='nx'>f</span><span class='p'>.</span><span class='nx'>arguments</span><span class='p'>[</span><span class='mi'>0</span><span class='p'>]</span> <span class='o'>=</span> <span class='s1'>&#39;world&#39;</span><span class='p'>;</span>
  <span class='o'>&gt;</span> <span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>next</span><span class='p'>(</span><span class='nx'>f</span><span class='p'>);</span>
  <span class='o'>&gt;</span> <span class='p'>})</span>
<span class='nx'>js</span><span class='o'>&gt;</span> <span class='nx'>hello</span><span class='p'>(</span><span class='s1'>&#39;fredrik&#39;</span><span class='p'>)</span>                   
<span class='p'>[</span><span class='nx'>before</span> <span class='nx'>hello</span><span class='p'>]</span>
<span class='nx'>hello</span><span class='p'>,</span> <span class='nx'>world</span>
<span class='p'>[</span><span class='nx'>after</span> <span class='nx'>hello</span><span class='p'>]</span>
<span class='nx'>js</span><span class='o'>&gt;</span> 
</code></pre>
</div>
<p>Naturally, the same goes for return values, only in reverse. The advice function is free to pass on whatever the original function returns, or return something different if it wants to:</p>
<div class='highlight'><pre><code class='js'><span class='nx'>js</span><span class='o'>&gt;</span> <span class='kd'>function</span> <span class='nx'>plus</span><span class='p'>(</span><span class='nx'>a</span><span class='p'>,</span> <span class='nx'>b</span><span class='p'>)</span> <span class='p'>{</span> <span class='k'>return</span> <span class='nx'>a</span> <span class='o'>+</span> <span class='nx'>b</span><span class='p'>;</span> <span class='p'>}</span>
<span class='nx'>js</span><span class='o'>&gt;</span> <span class='nx'>plus</span><span class='p'>(</span><span class='mi'>2</span><span class='p'>,</span> <span class='mi'>2</span><span class='p'>)</span>
<span class='mi'>4</span>
<span class='nx'>js</span><span class='o'>&gt;</span> <span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>around</span><span class='p'>(</span><span class='s1'>&#39;plus&#39;</span><span class='p'>,</span> <span class='kd'>function</span><span class='p'>(</span><span class='nx'>f</span><span class='p'>)</span> <span class='p'>{</span> <span class='k'>return</span> <span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>next</span><span class='p'>(</span><span class='nx'>f</span><span class='p'>)</span> <span class='o'>+</span> <span class='mi'>1</span><span class='p'>;</span> <span class='p'>})</span>
<span class='nx'>js</span><span class='o'>&gt;</span> <span class='nx'>plus</span><span class='p'>(</span><span class='mi'>2</span><span class='p'>,</span> <span class='mi'>2</span><span class='p'>)</span>                                                
<span class='mi'>5</span>
<span class='nx'>js</span><span class='o'>&gt;</span> 
</code></pre>
</div>
<h2 id='before_and_after_advice'>Before() and after() advice</h2>

<p>It is quite easy adding support for before and after advice if we define them using <code>Aop.around()</code>:</p>
<div class='highlight'><pre><code class='js'><span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>before</span> <span class='o'>=</span> <span class='kd'>function</span><span class='p'>(</span><span class='nx'>pointcut</span><span class='p'>,</span> <span class='nx'>advice</span><span class='p'>,</span> <span class='nx'>namespaces</span><span class='p'>)</span> <span class='p'>{</span>
  <span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>around</span><span class='p'>(</span><span class='nx'>pointcut</span><span class='p'>,</span> 
             <span class='kd'>function</span><span class='p'>(</span><span class='nx'>f</span><span class='p'>)</span> <span class='p'>{</span>
               <span class='nx'>advice</span><span class='p'>.</span><span class='nx'>apply</span><span class='p'>(</span><span class='k'>this</span><span class='p'>,</span> <span class='nx'>f</span><span class='p'>.</span><span class='nx'>arguments</span><span class='p'>);</span>
               <span class='k'>return</span> <span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>next</span><span class='p'>(</span><span class='nx'>f</span><span class='p'>)</span>
             <span class='p'>},</span> 
             <span class='nx'>namespaces</span><span class='p'>);</span>
<span class='p'>};</span>

<span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>after</span> <span class='o'>=</span> <span class='kd'>function</span><span class='p'>(</span><span class='nx'>pointcut</span><span class='p'>,</span> <span class='nx'>advice</span><span class='p'>,</span> <span class='nx'>namespaces</span><span class='p'>)</span> <span class='p'>{</span>
  <span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>around</span><span class='p'>(</span><span class='nx'>pointcut</span><span class='p'>,</span>
             <span class='kd'>function</span><span class='p'>(</span><span class='nx'>f</span><span class='p'>)</span> <span class='p'>{</span>
               <span class='kd'>var</span> <span class='nx'>ret</span> <span class='o'>=</span> <span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>next</span><span class='p'>(</span><span class='nx'>f</span><span class='p'>);</span>
               <span class='nx'>advice</span><span class='p'>.</span><span class='nx'>apply</span><span class='p'>(</span><span class='k'>this</span><span class='p'>,</span> <span class='nx'>f</span><span class='p'>.</span><span class='nx'>arguments</span><span class='p'>);</span>
               <span class='k'>return</span> <span class='nx'>ret</span><span class='p'>;</span>
             <span class='p'>},</span>
             <span class='nx'>namespaces</span><span class='p'>);</span>
<span class='p'>};</span>
</code></pre>
</div>
<p>These are simpler convenience methods that adds an advice function that will be called before and after function invocation, respectively. In order to make them easy to use, I&#8217;ve chosen to simplify the advice functions a bit. Unlike <code>Aop.around()</code> they get called with the same arguments as the original function (and not with a parameter map).</p>
<div class='highlight'><pre><code class='js'><span class='nx'>js</span><span class='o'>&gt;</span> <span class='kd'>function</span> <span class='nx'>hello</span><span class='p'>(</span><span class='nx'>name</span><span class='p'>)</span> <span class='p'>{</span> <span class='nx'>print</span><span class='p'>(</span><span class='s1'>&#39;hello, &#39;</span> <span class='o'>+</span> <span class='nx'>name</span><span class='p'>);</span> <span class='p'>}</span>
<span class='nx'>js</span><span class='o'>&gt;</span> <span class='nx'>Aop</span><span class='p'>.</span><span class='nx'>before</span><span class='p'>(</span><span class='s1'>&#39;hello&#39;</span><span class='p'>,</span> <span class='kd'>function</span><span class='p'>(</span><span class='nx'>name</span><span class='p'>)</span> <span class='p'>{</span> <span class='nx'>print</span><span class='p'>(</span><span class='s1'>&#39;before hello, name=&#39;</span> <span class='o'>+</span> <span class='nx'>name</span><span class='p'>);</span> <span class='p'>})</span>
<span class='nx'>js</span><span class='o'>&gt;</span> <span class='nx'>hello</span><span class='p'>(</span><span class='s1'>&#39;fredrik&#39;</span><span class='p'>)</span>
<span class='nx'>before</span> <span class='nx'>hello</span><span class='p'>,</span> <span class='nx'>name</span><span class='o'>=</span><span class='nx'>fredrik</span>
<span class='nx'>hello</span><span class='p'>,</span> <span class='nx'>fredrik</span>
<span class='nx'>js</span><span class='o'>&gt;</span> 
</code></pre>
</div>
<p>Unfortunatly, stripping down the API like this makes certain things impossible using <code>Aop.before()</code> and <code>Aop.after()</code>. You can&#8217;t inspect the name of the current function for example, or manipulate it&#8217;s arguments or return values. But hey, in that case just use <code>Aop.around()</code> instead. It&#8217;s simple enough.</p>

<h2 id='conclusion'>Conclusion</h2>

<p>When I rediscovered this script it looked quite different, and as I started hacking more on it to write this post I found myself refactoring, adding code, and removing. And removing. And removing. Now only the really necessary parts remain, and there&#8217;s a certain beauty in that.</p>

<p>My main takeway is that doing <a href='http://en.wikipedia.org/wiki/Aspect-oriented_programming'>AOP</a> this way isnt&#8217; hard. There&#8217;s nothing revolutionary in my code, just a bit of juggling functions calling other functions. But hey, that&#8217;s the true power of JavaScript.</p></content>
 </entry>
 
 <entry>
   <title>Something Cool</title>
   <link href="http://mulli.nu/2010/04/26/something-cool.html"/>
   <updated>2010-04-26T00:00:00+00:00</updated>
   <id>http://mulli.nu/2010/04/26/something-cool</id>
   <content type="html"><p>I found something cool on my harddrive today; an old JavaScript snippet that I wrote some years ago when I was heavily into Lisp. Yes, Lisp. I love Lisp. It&#8217;s the mother of all languages and in a perfect world I&#8217;d spend my days hacking cool stuff in Lisp or Clojure rather than maintaining a behemoth of a Java web app that I secretly hate.</p>

<p>Anyway, even if you&#8217;ve never heard of Lisp you need to know two things:</p>

<ol>
<li>In Lisp, you work very close to your data structures. Everything, including the code that you write, is expressed as lists or variants of lists.</li>

<li>The most basic data structure is what is known as a <strong>cons cell</strong>. Simply put, a cons is <strong>CONS</strong>tructed from two arbitrary values, which can be either primitive or a pointers to other cons cells.</li>
</ol>

<p>Lisp has a function called <code>cons</code> which takes two arguments and returns a cons cell. Two other functions, named <code>car</code> and <code>cdr</code> for historic reasons, takes a cons cell as argument and returns the first or second value respectively. This doesn&#8217;t seem especially exciting but is cool, since if you have these three functions you can create linked lists, and all other structures (trees, hash tables, etc.) can be expressed in terms of lists.</p>

<p>So, in a minimal environment where you don&#8217;t even have basic data structures such as arrays, structs or objects with value slots, how exactly do create cons cells? Well, it turns out that as long as you have a facility for defining and applying functions, and a branching mechanism, you can express cons cells in a functional manner:</p>
<div class='highlight'><pre><code class='js'><span class='lineno'> 1</span> <span class='kd'>function</span> <span class='nx'>cons</span><span class='p'>(</span><span class='nx'>a</span><span class='p'>,</span> <span class='nx'>b</span><span class='p'>)</span> <span class='p'>{</span>
<span class='lineno'> 2</span>     <span class='k'>return</span> <span class='kd'>function</span><span class='p'>(</span><span class='nx'>d</span><span class='p'>)</span> <span class='p'>{</span>
<span class='lineno'> 3</span> 	<span class='k'>return</span> <span class='nx'>d</span> <span class='o'>==</span> <span class='mi'>0</span> <span class='o'>?</span> <span class='nx'>a</span> <span class='o'>:</span> <span class='nx'>b</span><span class='p'>;</span>
<span class='lineno'> 4</span>     <span class='p'>};</span>
<span class='lineno'> 5</span> <span class='p'>}</span>
<span class='lineno'> 6</span> 
<span class='lineno'> 7</span> <span class='kd'>function</span> <span class='nx'>car</span><span class='p'>(</span><span class='nx'>c</span><span class='p'>)</span> <span class='p'>{</span>
<span class='lineno'> 8</span>     <span class='k'>return</span> <span class='nx'>c</span><span class='p'>.</span><span class='nx'>call</span><span class='p'>(</span><span class='kc'>null</span><span class='p'>,</span> <span class='mi'>0</span><span class='p'>);</span>
<span class='lineno'> 9</span> <span class='p'>}</span>
<span class='lineno'>10</span> 
<span class='lineno'>11</span> <span class='kd'>function</span> <span class='nx'>cdr</span><span class='p'>(</span><span class='nx'>c</span><span class='p'>)</span> <span class='p'>{</span>
<span class='lineno'>12</span>     <span class='k'>return</span> <span class='nx'>c</span><span class='p'>.</span><span class='nx'>call</span><span class='p'>(</span><span class='kc'>null</span><span class='p'>,</span> <span class='mi'>1</span><span class='p'>);</span>
<span class='lineno'>13</span> <span class='p'>}</span>
</code></pre>
</div>
<p>This idea isn&#8217;t mine; it has been around for ages in Lisp and Scheme, but I implemented it in JavaScript to see what it would be like. I find this construct very simple and beautiful. By calling the <code>cons</code> function we create a closure with the variables <code>a</code> and <code>b</code> bound to the supplied arguments. <code>cons</code> then returns an anonymous function which is simply a promise to return either <code>a</code> or <code>b</code>.</p>
<div class='highlight'><pre><code class='js'><span class='o'>&gt;</span> <span class='kd'>var</span> <span class='nx'>c</span> <span class='o'>=</span> <span class='nx'>cons</span><span class='p'>(</span><span class='mi'>17</span><span class='p'>,</span> <span class='mi'>23</span><span class='p'>);</span>
<span class='kc'>undefined</span>
<span class='o'>&gt;</span> <span class='nx'>car</span><span class='p'>(</span><span class='nx'>c</span><span class='p'>)</span>
<span class='mi'>17</span>
<span class='o'>&gt;</span> <span class='nx'>cdr</span><span class='p'>(</span><span class='nx'>c</span><span class='p'>)</span>
<span class='mi'>23</span>
</code></pre>
</div>
<p>Of course, we could to without the <code>car</code> and <code>cdr</code> functions; they are merely syntactic sugar. Since the value of <code>c</code> is a function we can just call it directly:</p>
<div class='highlight'><pre><code class='js'><span class='o'>&gt;</span> <span class='nx'>c</span><span class='p'>(</span><span class='mi'>0</span><span class='p'>)</span>
<span class='mi'>17</span>
<span class='o'>&gt;</span> <span class='nx'>c</span><span class='p'>(</span><span class='mi'>1</span><span class='p'>)</span>
<span class='mi'>23</span>
</code></pre>
</div>
<p>Finally, this is how we&#8217;d go about creating and traversing that linked list:</p>
<div class='highlight'><pre><code class='js'><span class='o'>&gt;</span> <span class='kd'>var</span> <span class='nx'>l</span> <span class='o'>=</span> <span class='nx'>cons</span><span class='p'>(</span><span class='mi'>1</span><span class='p'>,</span> <span class='nx'>cons</span><span class='p'>(</span><span class='mi'>2</span><span class='p'>,</span> <span class='nx'>cons</span><span class='p'>(</span><span class='mi'>3</span><span class='p'>,</span> <span class='kc'>null</span><span class='p'>)))</span>
<span class='kc'>undefined</span>
<span class='o'>&gt;</span> <span class='k'>for</span><span class='p'>(</span><span class='nx'>i</span> <span class='o'>=</span> <span class='nx'>l</span><span class='p'>;</span> <span class='nx'>i</span> <span class='o'>!=</span> <span class='kc'>null</span><span class='p'>;</span> <span class='nx'>i</span> <span class='o'>=</span> <span class='nx'>cdr</span><span class='p'>(</span><span class='nx'>i</span><span class='p'>))</span> <span class='p'>{</span> <span class='nx'>console</span><span class='p'>.</span><span class='nx'>log</span><span class='p'>(</span><span class='nx'>car</span><span class='p'>(</span><span class='nx'>i</span><span class='p'>));</span> <span class='p'>}</span>
<span class='mi'>1</span>
<span class='mi'>2</span>
<span class='mi'>3</span>
<span class='kc'>undefined</span>
</code></pre>
</div>
<p>So, using only functions and branching logic we can derive just about <em>any</em> data structure. I remember learning about this in a CS class where we used a lot of Scheme, and it was mindblowing.</p>

<p>It still is.</p></content>
 </entry>
 
 <entry>
   <title>My setup</title>
   <link href="http://mulli.nu/2010/04/23/my-setup.html"/>
   <updated>2010-04-23T00:00:00+00:00</updated>
   <id>http://mulli.nu/2010/04/23/my-setup</id>
   <content type="html"><p>This blog is generated by <a href='http://github.com/mojombo/jekyll'>jekyll</a>, backed by a simple Apache config on the server side. One of the nice things about the site being statically generated is site is that I don&#8217;t have to fiddle with cgi scripts, content handlers and whatnot. Also, there&#8217;s no comment spam to manage; thank God for that.</p>

<p>I edit each entry in <a href='http://www.gnu.org/software/emacs'>emacs</a> (usually on the train, as that seems to be the only time I have for myself nowadays), run it through a local jekyll instance to see how it looks, then check it into a <a href='http://git-scm.com/'>git</a> repo, which serves two purposes. First, it provides version control for all my content (and I&#8217;m really anal about version control). Second, by pushing the changes to another repo on my web host, a <a href='http://matedriven.com.ar/2009/04/28/using-git-to-maintain-your-blog.html'>post-update hook</a> is triggered which runs jekyll and regenerates the site content with my updates.</p>

<p>I am not a designer, and I don&#8217;t pretend to be one. Time was when I actually enjoyed tweaking CSS and spent hours obsessing over pixel placement, semantic markup and all that jazz. The results however, were never that great, and I can&#8217;t be arsed with that anymore. Instead, I rely on the <a href='http://blueprintcss.org'>Blueprint CSS</a> framework to provide a reasonable layout and typesetting that works in most browsers. It&#8217;s not perfect, and I don&#8217;t get to have complete control over every single pixel on screen, but I&#8217;ve found that I don&#8217;t really want that anyway. This is good enough, and if I get to spend next to no time designing and tweaking, great!</p>

<p>Lastly, here&#8217;s a picture of Yojimbo, the World&#8217;s Cutest Cat:</p>

<p><img class='article_image' src='http://farm2.static.flickr.com/1347/829207928_ba01bd66b3.jpg' alt='Yojimbo' /></p></content>
 </entry>
 
 
</feed>
