vemod.net http://vemod.net/ ... def state = 'confused http://vemod.net/def-state-confused <p><small>Tags: <a href="http://vemod.net/scala" class="tag" rel="tag">Scala</a></small></p><p>I think method definitions in <a href="http://vemod.net/scala">Scala</a> can be a bit confusing. Here&#8217;s a <span class="caps">REPL</span> session with some comments and questions:</p> <pre style="color: #585858"><code>scala&gt; def A() = 'A A: ()Symbol scala&gt; def B = () =&gt; 'B B: () =&gt; Symbol scala&gt; def C = 'C C: Symbol</code></pre> <p>So far nothing strange, but note the the type of <code>C</code>; is it really a function? We&#8217;ll see later, so let&#8217;s move on:</p> <pre style="color: #585858"><code>scala&gt; A() res: Symbol = 'A scala&gt; B() res: Symbol = 'B</code></pre> <p>Note that <code>A</code> and <code>B</code> can be called in the same way, even though <code>B</code> was defined in terms of an anonymous function.</p> <pre style="color: #585858"><code>scala&gt; C() &lt;console&gt;:7: error: C of type Symbol does not take parameters C() ^</code></pre> <p>Well, seems like we&#8217;ve discovered that <code>C</code> isn&#8217;t really a function and we could have defined it with <code>val</code> instead&#8230; Or? No, it&#8217;s still a function: the function body wasn&#8217;t <em>evaluated</em> until we referred to the function. Look here:</p> <pre style="color: #585858"><code>scala&gt; def t = currentTime t: Long scala&gt; t res: Long = 1296163141271 scala&gt; t res: Long = 1296163141949</code></pre> <p>Clever and useful, but not completely obvious.</p> <p>Back to <code>A</code>, <code>B</code> and <code>C</code>. Let&#8217;s try grabbing a function reference Scheme/Haskell/Python style:</p> <pre style="color: #585858"><code>scala&gt; A res: Symbol = 'A scala&gt; B res: () =&gt; Symbol = &lt;function0&gt; scala&gt; C res: Symbol = 'C</code></pre> <p><code>B</code> worked fine and <code>C</code> was expected because of what we previously discovered, but what about <code>A</code>? It surprised me that it evaluated to <code>'A </code> instead of a function that returns <code>'A</code>. What would the result be if the method took arguments?</p> <pre style="color: #585858"><code>scala&gt; def id[T](x: T) = x id: [T](x: T)T scala&gt; id &lt;console&gt;:7: error: missing arguments for method id in object $iw; follow this method with `_' if you want to treat it as a partially applied function id ^</code></pre> <p>Different result &ndash; yes &ndash; but not valid as is. It is noteworthy that it is possible to referr to <code>id</code> like this:</p> <pre style="color: #585858"><code>scala> Some(123).map(id) res: Option[Int] = Some(123)</code></pre> <p>It seems like the expression is context-sensitive. :/</p> <p>What I&#8217;ve learnt from this that you can&#8217;t consider functions and methods as equals in Scala (like in Python); they live in two different worlds and you need to bridge them explicitly sometimes.</p> <p>To be continued&#8230;</p> <!-- def a() = 'A def b = () => 'B def c = 'C a() b() c() a b c --> Thu, 27 Jan 2011 22:22:40 +0100 http://vemod.net/def-state-confused 2011-01-27T22:22:40.898491+01:00 Using Racket to Configure Apache http://vemod.net/using-racket-to-configure-apache <p><small>Tags: <a href="http://vemod.net/racket" class="tag" rel="tag">Racket</a> and <a href="http://vemod.net/lisp" class="tag" rel="tag">Lisp</a></small></p><p><em>I have a confession to make: I really hate writing boilerplate code.</em></p> <p>I use Apache to serve lots of websites, have many lines of configuration code and have often longed for a way to capture all the patterns and reduce code duplication. I thought about using a macro language like m4 or the C preprocessor or even <a href="http://vemod.net/ruby">Ruby</a> but neither alternative was particulary attractive. Then it hit me that I could describe the Apache configuration with plain <nobr>S-expressions</nobr> and use the full power of <a href="http://vemod.net/racket">Racket</a> to manipulate these and output normal configuration files. What makes this a compelling alternative is of course that <nobr>S-expressions</nobr> allow one to express tree-structured data very succinctly, like here:</p> <pre><code class="scheme pygments"><span class="p">(</span><span class="nf">group</span> <span class="p">(</span><span class="nf">VirtualHost</span> <span class="nv">*</span><span class="p">)</span> <span class="p">(</span><span class="nf">ServerName</span> <span class="nv">www</span><span class="o">.</span><span class="nv">defsoftware</span><span class="o">.</span><span class="nv">se</span><span class="p">)</span> <span class="p">(</span><span class="nf">RedirectPermanent</span> <span class="nv">/</span> <span class="nv">http://defsoftware</span><span class="o">.</span><span class="nv">se/</span><span class="p">))</span> </code></pre> <p>Racket&#8212;being a Lisp equipped with many powerful built-in functions&#8212;makes a full translator for these kind of trees simple to write.</p> <div style="border: 1px solid #E8E8E8; padding: 5px 10px"> <p><strong>Update:</strong> I got an e-mail from <a href="http://barzilay.org/" class="external">Eli Barzilay</a> (of Racket fame) with some valuable feedback on how to improve my code by using functions in the <code>scribble/text</code> and <code>racket/list</code> libraries that are included with Racket. I&#8217;ve updated my code and put the old code at the bottom<sup><a href="#fn1">1</a></sup> for reference. Eli also wrote about using the cool <a href="http://docs.racket-lang.org/scribble/reader-internals.html#(mod-path._at-exp)"><code>at-exp</code></a> reader to mix text and code.</p> </div> <pre><code class="scheme pygments"><span class="p">(</span><span class="nf">require</span> <span class="nv">scribble/text</span> <span class="nv">racket/list</span><span class="p">)</span> <span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">apache-config</span> <span class="o">.</span> <span class="nv">complete-config</span><span class="p">)</span> <span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">format-apache-config</span> <span class="nv">config</span><span class="p">)</span> <span class="p">(</span><span class="nf">match</span> <span class="nv">config</span> <span class="p">((</span><span class="nb">list </span><span class="ss">&#39;group</span> <span class="p">(</span><span class="nb">list </span><span class="nv">group</span> <span class="o">...</span><span class="p">)</span> <span class="nv">directives</span> <span class="o">...</span><span class="p">)</span> <span class="p">(</span><span class="nb">list </span><span class="s">&quot;&lt;&quot;</span> <span class="p">(</span><span class="nf">add-between</span> <span class="nv">group</span> <span class="s">&quot; &quot;</span><span class="p">)</span> <span class="s">&quot;&gt;&quot;</span> <span class="s">&quot;\n&quot;</span> <span class="s">&quot; &quot;</span> <span class="p">(</span><span class="nf">add-newlines</span> <span class="p">(</span><span class="nb">map </span><span class="nv">format-apache-config</span> <span class="nv">directives</span><span class="p">))</span> <span class="s">&quot;\n&quot;</span> <span class="s">&quot;&lt;/&quot;</span> <span class="p">(</span><span class="nf">first</span> <span class="nv">group</span><span class="p">)</span> <span class="s">&quot;&gt;&quot;</span><span class="p">))</span> <span class="p">((</span><span class="nb">list </span><span class="p">(</span><span class="nb">list </span><span class="nv">_</span> <span class="o">...</span><span class="p">)</span> <span class="o">...</span><span class="p">)</span> <span class="p">(</span><span class="nf">add-newlines</span> <span class="p">(</span><span class="nb">map </span><span class="nv">format-apache-config</span> <span class="nv">config</span><span class="p">)))</span> <span class="p">((</span><span class="nb">list </span><span class="nv">_</span> <span class="o">...</span><span class="p">)</span> <span class="p">(</span><span class="nf">add-between</span> <span class="nv">config</span> <span class="s">&quot; &quot;</span><span class="p">))))</span> <span class="p">(</span><span class="nf">output</span> <span class="p">(</span><span class="nf">format-apache-config</span> <span class="nv">complete-config</span><span class="p">))</span> <span class="p">(</span><span class="nf">newline</span><span class="p">))</span> </code></pre> <p>The <code>output</code> function takes care of the indentation in <a href="http://docs.racket-lang.org/scribble/text.html#(part._.Indentation_in_.Preprocessed_output)">a pretty clever way</a>.</p> <p>Now&#8212;equipped with <code>apache-config</code>&#8212;we can start building functions that work with Apache S-expressions:</p> <pre><code class="scheme pygments"><span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">host-redirect</span> <span class="nv">from-hostname</span> <span class="nv">to-hostname</span><span class="p">)</span> <span class="o">`</span><span class="p">(</span><span class="nf">group</span> <span class="p">(</span><span class="nf">VirtualHost</span> <span class="nv">*</span><span class="p">)</span> <span class="p">(</span><span class="nf">ServerName</span> <span class="o">,</span><span class="nv">from-hostname</span><span class="p">)</span> <span class="p">(</span><span class="nf">RedirectPermanent</span> <span class="nv">/</span> <span class="o">,</span><span class="p">(</span><span class="nb">string-append </span><span class="s">&quot;http://&quot;</span> <span class="nv">to-hostname</span> <span class="s">&quot;/&quot;</span><span class="p">))))</span> <span class="p">(</span><span class="nf">apache-config</span> <span class="p">(</span><span class="nf">host-redirect</span> <span class="s">&quot;defsoftware.com&quot;</span> <span class="s">&quot;defsoftware.se&quot;</span><span class="p">)</span> <span class="p">(</span><span class="nf">host-redirect</span> <span class="s">&quot;defsoftware.net&quot;</span> <span class="s">&quot;defsoftware.se&quot;</span><span class="p">))</span> </code></pre> <p>Output:</p> <pre><code class="apache pygments"><span class="nt">&lt;VirtualHost</span> <span class="s">*</span><span class="nt">&gt;</span> <span class="nb">ServerName</span> defsoftware.com <span class="nb">RedirectPermanent</span> / http://defsoftware.se/ <span class="nt">&lt;/VirtualHost&gt;</span> <span class="nt">&lt;VirtualHost</span> <span class="s">*</span><span class="nt">&gt;</span> <span class="nb">ServerName</span> defsoftware.net <span class="nb">RedirectPermanent</span> / http://defsoftware.se/ <span class="nt">&lt;/VirtualHost&gt;</span> </code></pre> <p>Now I&#8217;m happy.</p> <p id="fn1"><sup>1</sup> <a href="#javascript" onclick="javascript:document.getElementById('old-code').setAttribute('style', 'display: block'); this.removeAttribute('href'); return false">Click here to see the old code.</a></p> <div id="old-code" style="display: none"><pre><code class="scheme pygments"><span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">apache-config</span> <span class="o">.</span> <span class="nv">config</span><span class="p">)</span> <span class="p">(</span><span class="k">let </span><span class="nv">self</span> <span class="p">((</span><span class="nf">config-fragment</span> <span class="nv">config</span><span class="p">)</span> <span class="p">(</span><span class="nf">indent</span> <span class="mi">0</span><span class="p">))</span> <span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">line-with-indent</span> <span class="o">.</span> <span class="nv">args</span><span class="p">)</span> <span class="p">(</span><span class="nb">display </span><span class="p">(</span><span class="nb">make-string </span><span class="p">(</span><span class="nb">* </span><span class="mi">2</span> <span class="nv">indent</span><span class="p">)</span> <span class="sc">#\ </span><span class="p">))</span> <span class="p">(</span><span class="nb">apply </span><span class="nv">printf</span> <span class="nv">args</span><span class="p">)</span> <span class="p">(</span><span class="nf">newline</span><span class="p">))</span> <span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">join</span> <span class="nv">xs</span> <span class="p">(</span><span class="nf">sep</span> <span class="s">&quot; &quot;</span><span class="p">))</span> <span class="p">(</span><span class="nf">string-join</span> <span class="p">(</span><span class="nb">map </span><span class="p">(</span><span class="nf">curry</span> <span class="nv">format</span> <span class="s">&quot;~a&quot;</span><span class="p">)</span> <span class="nv">xs</span><span class="p">)</span> <span class="nv">sep</span><span class="p">))</span> <span class="p">(</span><span class="nf">match</span> <span class="nv">config-fragment</span> <span class="p">((</span><span class="nf">list</span><span class="p">)</span> <span class="p">(</span><span class="nf">void</span><span class="p">))</span> <span class="p">((</span><span class="nb">list </span><span class="p">(</span><span class="nb">list </span><span class="nv">nested</span> <span class="o">...</span><span class="p">)</span> <span class="nv">more</span> <span class="o">...</span><span class="p">)</span> <span class="p">(</span><span class="nf">self</span> <span class="nv">nested</span> <span class="nv">indent</span><span class="p">)</span> <span class="p">(</span><span class="nf">self</span> <span class="nv">more</span> <span class="nv">indent</span><span class="p">))</span> <span class="p">((</span><span class="nb">list </span><span class="ss">&#39;group</span> <span class="p">(</span><span class="nb">list </span><span class="nv">group</span> <span class="o">...</span><span class="p">)</span> <span class="nv">directives</span> <span class="o">...</span><span class="p">)</span> <span class="p">(</span><span class="nf">line-with-indent</span> <span class="s">&quot;&lt;~a&gt;&quot;</span> <span class="p">(</span><span class="nf">join</span> <span class="nv">group</span><span class="p">))</span> <span class="p">(</span><span class="nf">self</span> <span class="nv">directives</span> <span class="p">(</span><span class="nf">add1</span> <span class="nv">indent</span><span class="p">))</span> <span class="p">(</span><span class="nf">line-with-indent</span> <span class="s">&quot;&lt;/~a&gt;&quot;</span> <span class="p">(</span><span class="nf">first</span> <span class="nv">group</span><span class="p">)))</span> <span class="p">((</span><span class="nb">list </span><span class="p">(</span><span class="k">or </span><span class="p">(</span><span class="nf">?</span> <span class="nv">string?</span><span class="p">)</span> <span class="p">(</span><span class="nf">?</span> <span class="nv">symbol?</span><span class="p">))</span> <span class="o">...</span><span class="p">)</span> <span class="p">(</span><span class="nf">line-with-indent</span> <span class="p">(</span><span class="nf">join</span> <span class="nv">config-fragment</span><span class="p">))))))</span> </code></pre></div> Sun, 10 Apr 2011 10:52:30 +0200 http://vemod.net/using-racket-to-configure-apache 2011-04-10T10:52:30.911508+02:00 Objective-R http://vemod.net/objective-r <p><small>Tags: <a href="http://vemod.net/racket" class="tag" rel="tag">Racket</a> and <a href="http://vemod.net/lisp" class="tag" rel="tag">Lisp</a></small></p><p>Objective-C-like syntax for method calls in Racket</p> <script src="https://gist.github.com/969308.js"></script> <noscript><p><a href="https://gist.github.com/969308">Code at GitHub Gist</a></p></noscript> Fri, 27 May 2011 11:38:40 +0200 http://vemod.net/objective-r 2011-05-27T11:38:40+02:00 Liquorice http://vemod.net/liquorice <p>I&#8217;m a big fan of liquorice. Here are my top dealers:</p> <h2>1. <a href="http://www.liquirizia.it/" class="external">Amarelli</a></h2> <p>Their spezzatina/spezzata (pastilles of pure liquorice) can&#8217;t be beaten. The variants flavoured with anise are great as well.</p> <h2>2. <a href="http://lakrids.nu/" class="external">Lakrids by Johan Bülow</a></h2> <p>If you like soft liquorice this is for you. Great flavours: <a href="http://lakrids.nu/site/Salmiak/" class="external">salmiak</a>, <a href="http://lakrids.nu/site/Habanero_Chili/" class="external">habanero chili</a> and more. The product names are stupid. Don&#8217;t miss the <a href="http://lakrids.nu/site/Lakrids_Drops/">raw liquorice drops</a>; they are almost as good as Amarelli&#8217;s.</p> <h2>3. <a href="http://www.lakritsfabriken.se/" class="external">Lakritsfabriken</a> (&#8220;The Liquorice Factory&#8221;)</h2> <p>Soft liquorice with simple but good flavours.</p> <div class="full-width" style="text-align: center; margin-bottom: -16px"> <img src="/stuff/amarelli.jpg" alt="" style="width: 50%" /> </div> Thu, 22 Sep 2011 19:24:17 +0200 http://vemod.net/liquorice 2011-09-22T19:24:17.368259+02:00 Music Players for OS X http://vemod.net/music-players-for-os-x <p><small>Tags: <a href="http://vemod.net/mac" class="tag" rel="tag">Mac</a></small></p><ul> <li><a href="http://cogx.org/" class="external">Cog</a></li> <li><a href="http://sbooth.org/Decibel/" class="external">Decibel</a></li> <li><a href="http://www.pixiapps.com/ecouteosx/" class="external">Ecoute</a></li> <li><a href="http://www.enqueueapp.com/" class="external">Enqueue</a></li> <li><a href="http://flavio.tordini.org/musique" class="external">Musique</a></li> <li><a href="http://www.machinecodex.com/neutrino/" class="external">Neutrino</a></li> <li><a href="http://getsongbird.com/" class="external">Songbird</a></li> <li><a href="http://getsonora.com/" class="external">Sonora</a></li> <li><a href="http://swinsian.com/" class="external">Swinsian</a></li> <li><a href="http://www.tomahawk-player.org/" class="external">Tomahawk</a></li> <li><a href="http://www.videolan.org/vlc/" class="external"><span class="caps">VLC</span></a></li> <li><a href="http://voxapp.didgeroo.com/" class="external">Vox</a> </li> <li><a href="http://www.winamp.com/mac" class="external">Winamp</a></li> <li><a href="http://www.apple.com/itunes/" class="external">iTunes</a> (<a href="http://vemod.net/itunes-complements">with several shells</a>)</li> </ul> <h2>…with Music Streaming Services</h2> <ul> <li><a href="http://www.wimpmusic.se/" class="external">WiMP</a></li> <li><a href="http://www.spotify.com/" class="external">Spotify</a></li> </ul> Wed, 04 Apr 2012 20:01:40 +0200 http://vemod.net/music-players-for-os-x 2012-04-04T20:01:40.181918+02:00 PostgreSQL http://vemod.net/postgresql <h1 style="text-align:center;"><a href="http://www.postgresql.org/"><img title="PostgreSQL" src="/stuff/postgresql.png" alt="PostgreSQL" /></a></h1> <p style="text-align:center;">Probably the best relational database system in the world.</p> Mon, 28 May 2012 21:16:39 +0200 http://vemod.net/postgresql 2012-05-28T21:16:39.549134+02:00 Ring Middleware for X-Forwarded-For http://vemod.net/ring-middleware-for-x-forwarded-for <p><small>Tags: <a href="http://vemod.net/clojure" class="tag" rel="tag">Clojure</a></small></p><script src="https://gist.github.com/2918060.js"></script> <noscript><p><a href="https://gist.github.com/2918060">Code at GitHub Gist</a></p></noscript> Tue, 12 Jun 2012 08:06:29 +0200 http://vemod.net/ring-middleware-for-x-forwarded-for 2012-06-12T08:06:29+02:00 Ring Middleware for JSONP http://vemod.net/ring-middleware-for-jsonp <p><small>Tags: <a href="http://vemod.net/clojure" class="tag" rel="tag">Clojure</a></small></p><p><code>(goto <a href="https://github.com/qerub/ring-middleware-jsonp" class="external">github.com/qerub/ring-middleware-jsonp</a>)</code></p> Sun, 08 Jul 2012 00:00:00 +0200 http://vemod.net/ring-middleware-for-jsonp 2012-07-08T00:00:00+02:00 Scheme http://vemod.net/scheme <p><small>Tags: <a href="http://vemod.net/programming-languages" class="tag" rel="tag">Programming Languages</a> and <a href="http://vemod.net/lisp" class="tag" rel="tag">Lisp</a></small></p><p><a href="http://en.wikipedia.org/wiki/Scheme_%28programming_language%29">en.wikipedia.org/wiki/Scheme_(programming_language)</a></p> Sun, 16 Sep 2012 19:35:50 +0200 http://vemod.net/scheme 2012-09-16T19:35:50.989192+02:00 Simple Futures in Ruby with Lambdas and Threads http://vemod.net/simple-futures-in-ruby-with-lambdas-and-procs <p><small>Tags: <a href="http://vemod.net/ruby" class="tag" rel="tag">Ruby</a></small></p><h1 style="font-size: 150%">Simple Futures in Ruby with Lambdas and Threads</h1> <script src="https://gist.github.com/4137421.js"></script> <noscript><p><a href="https://gist.github.com/4137421">Code at GitHub Gist</a></p></noscript> Fri, 23 Nov 2012 22:40:00 +0100 http://vemod.net/simple-futures-in-ruby-with-lambdas-and-procs 2012-11-23T22:40:00.512423+01:00 Termos