vemod.net http://termos.vemod.net/ ... Post-rock http://termos.vemod.net/post-rock <p>Post-rock rocks. Here are some good bands:</p> <ul> <li>Caspian (<a href="http://spotifyfy.net/artist/Caspian" class="external">Spotify</a>)</li> <li>Explosions in the Sky (<a href="http://spotifyfy.net/artist/Explosions%20in%20the%20Sky" class="external">Spotify</a>)</li> <li>Godspeed You! Black Emperor (<a href="http://spotifyfy.net/artist/Godspeed%20You!%20Black%20Emperor" class="external">Spotify</a>)</li> <li>Mono (<a href="http://spotifyfy.net/artist/Mono" class="external">Spotify</a>)</li> <li>Red Sparowes (<a href="http://spotifyfy.net/artist/Red%20Sparowes" class="external">Spotify</a>)</li> <li>Russian Circles (<a href="http://spotifyfy.net/artist/Russian%20Circles" class="external">Spotify</a>)</li> </ul><p><small>Tags: <a href="http://termos.vemod.net/music" class="tag" rel="tag">Music</a></small></p> Sat, 24 Jul 2010 10:35:18 +0200 http://termos.vemod.net/post-rock 2010-07-24T10:35:18+02:00 A Predicate Combinator http://termos.vemod.net/a-predicate-combinator <p>I&#8217;ve experimented a lot with <a href="http://termos.vemod.net/plt-scheme"><span class="caps">PLT</span> Scheme</a> lately. I found myself testing values against several predicate functions a couple of times and writing this kind of code:</p> <pre><code class="ultraviolet">(<span class="Keyword">define</span> (<span class="FunctionName">integer-or-string?</span><span class="FunctionArgument"> x</span>) (<span class="Keyword">or</span> (<span class="CommandMethod">integer?</span> x) (<span class="CommandMethod">string?</span> x))) </code></pre> <p>Not too pretty. Inspired by <span class="caps">PLT</span> Scheme&#8217;s contract combinator <a href="http://docs.plt-scheme.org/search/index.html?q=or%2Fc" class="external">or/c</a> I wrote this:</p> <pre><code class="ultraviolet">(<span class="Keyword">define</span> (<span class="FunctionName">or/p</span><span class="FunctionArgument"> . ps</span>) (λ (x) (ormap values (<span class="CommandMethod">map</span> (λ (f) (f x)) ps)))) </code></pre> <p>And now I can write this:</p> <pre><code class="ultraviolet">(<span class="Keyword">define</span> <span class="Variable">integer-or-string?</span> (or/p <span class="CommandMethod">integer?</span> string?)) </code></pre> <p>And then I lived happily ever after.</p> <p><strong>Update</strong>: <a href="http://planet.plt-scheme.org/display.ss?package=scheme.plt&#38;owner=cce" class="external">cce/scheme</a> has this function as <a href="http://planet.plt-scheme.org/package-source/cce/scheme.plt/7/0/planet-docs/manual/function.html#(def._((planet._function..rkt._(cce._scheme..plt._7._0))._disjoin))" class="external">disjoin</a>.</p> <h2>Haskell</h2> <p>For comparison, the function would be written like this in <a href="http://termos.vemod.net/haskell">Haskell</a>:</p> <pre><code class="ultraviolet">or_p ps x = <span class="FunctionName">or</span> $ <span class="FunctionName">map</span> ($x) ps </code></pre> <p>(Yes, I like dollars&#8212;why?)</p> <h2>Scala</h2> <p>And something more to compare with:</p> <pre><code class="ultraviolet"><span class="Keyword">def</span> orP[T](ps: <span class="Storage">L</span><span class="Storage">i</span><span class="Storage">s</span><span class="Storage">t</span><span class="Storage">[T =&gt; Boolean]</span>)(x: <span class="Storage">T</span>) = ps.map(_(x)).foldLeft(<span class="Keyword">false</span>)(_||_) </code></pre><p><small>Tags: <a href="http://termos.vemod.net/haskell" class="tag" rel="tag">Haskell</a>, <a href="http://termos.vemod.net/scala" class="tag" rel="tag">Scala</a>, and <a href="http://termos.vemod.net/plt-scheme" class="tag" rel="tag">PLT Scheme</a></small></p> Mon, 28 Jun 2010 00:08:46 +0200 http://termos.vemod.net/a-predicate-combinator 2010-06-28T00:08:46+02:00 The Web Site Hall of Shame http://termos.vemod.net/the-web-site-hall-of-shame <p><code>GOTO <a href="http://world-wide-fail.tumblr.com/" class="external">world-wide-fail.tumblr.com</a></code></p><p><small>Tags: <a href="http://termos.vemod.net/web" class="tag" rel="tag">Web</a></small></p> Wed, 23 Jun 2010 20:20:28 +0200 http://termos.vemod.net/the-web-site-hall-of-shame 2010-06-23T20:20:28+02:00 Scala: Runnable and Callable http://termos.vemod.net/scala-runnable-and-callable <p><a href="http://termos.vemod.net/scala">Scala</a> functions don&#8217;t implement the <a href="http://java.sun.com/javase/6/docs/api/java/lang/Runnable.html" class="external"><code>Runnable</code></a> and <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/Callable.html" class="external"><code>Callable</code></a> interfaces even though there&#8217;s a simply mapping. Luckily, <a href="http://termos.vemod.net/scala">Scala</a> provides us with a mechanism to extend functions on our own: <em>implicits conversions</em>. Here are two usable definitions:</p> <pre><code class="ultraviolet"><span class="Keyword">implicit</span> <span class="Keyword">def</span> runnable(f: <span class="Storage">()</span><span class="Storage"> </span>=&gt; Unit): <span class="Storage">R</span><span class="Storage">u</span><span class="Storage">n</span><span class="Storage">n</span><span class="Storage">a</span><span class="Storage">b</span><span class="Storage">l</span><span class="Storage">e</span><span class="Storage"> </span>= <span class="Keyword">new</span> Runnable() { <span class="Keyword">def</span> run() = f() } <span class="Keyword">import</span> java.util.concurrent.Callable <span class="Keyword">implicit</span> <span class="Keyword">def</span> callable[T](f: <span class="Storage">()</span><span class="Storage"> </span>=&gt; T): <span class="Storage">C</span><span class="Storage">a</span><span class="Storage">l</span><span class="Storage">l</span><span class="Storage">a</span><span class="Storage">b</span><span class="Storage">l</span><span class="Storage">e</span><span class="Storage">[T]</span><span class="Storage"> </span>= <span class="Keyword">new</span> Callable[T]() { <span class="Keyword">def</span> call() = f() } </code></pre> <p>Enjoy.</p><p><small>Tags: <a href="http://termos.vemod.net/scala" class="tag" rel="tag">Scala</a></small></p> Mon, 21 Jun 2010 18:10:49 +0200 http://termos.vemod.net/scala-runnable-and-callable 2010-06-21T18:10:49+02:00 Purely Functional Heap Sort in Scala http://termos.vemod.net/purely-functional-heap-sort-in-scala <p>Inspired by the blog post <a href="http://flyingfrogblog.blogspot.com/2010/05/purely-functional-heap-sort-in-ocaml-f.html" class="external">Purely functional Heap Sort in OCaml, F# and Haskell</a> on <a href="http://flyingfrogblog.blogspot.com/" class="external">The Flying Frog Blog</a> I decided to implement the same datastructure and operations in <a href="http://termos.vemod.net/scala">Scala</a> to get one little step closer to <a href="http://termos.vemod.net/scala">Scala</a> mastery. This code is <em>not idiomatic</em> <a href="http://termos.vemod.net/scala">Scala</a>, but I think it is decent anyway. If you have any comments, feel free to <a href="http://termos.vemod.net/contact">contact me</a> as usual.</p> <p><strong>Challenge:</strong> Rewrite the code to be more object-oriented. (Disclaimer: I don&#8217;t think it&#8217;s possible to write <code>toList</code> as a method and have the tail recursion optimization kick in, but I&#8217;d love to be proven wrong.)</p> <p>Don&#8217;t forget to take a look at the <a href="http://flyingfrogblog.blogspot.com/2010/05/purely-functional-heap-sort-in-ocaml-f.html" class="external">OCaml, F# and Haskell versions</a> and compare. Note the amount of types that <a href="http://termos.vemod.net/scala">Scala</a> can&#8217;t infer.</p> <p><strong>Update:</strong> The code has been updated with some improvements suggested by Robbert.</p> <pre style="font-size: 80%"><code>sealed abstract class Heap[+A] { def rank: Int } case object EmptyHeap extends Heap[Nothing] { def rank = 0} case class NonEmptyHeap[A](rank: Int, element: A, left: Heap[A], right: Heap[A]) extends Heap[A] object Heap { def apply[A](x: A): Heap[A] = this(x, EmptyHeap, EmptyHeap) def apply[A](x: A, a: Heap[A], b: Heap[A]): Heap[A] = if (a.rank &gt; b.rank) NonEmptyHeap(b.rank + 1, x, a, b) else NonEmptyHeap(a.rank + 1, x, b, a) def merge[A &lt;% Ordered[A]](a: Heap[A], b: Heap[A]): Heap[A] = (a, b) match { case (x, EmptyHeap) =&gt; x case (EmptyHeap, x) =&gt; x case (x: NonEmptyHeap[A], y: NonEmptyHeap[A]) =&gt; if (x.element &gt;= y.element) Heap(x.element, x.left, merge(x.right, y)) else Heap(y.element, y.left, merge(x, y.right)) } def toList[A &lt;% Ordered[A]](heap: Heap[A]) = toListWithMemory(List(), heap) @annotation.tailrec def toListWithMemory[A &lt;% Ordered[A]](memo: List[A], heap: Heap[A]): List[A] = heap match { case EmptyHeap =&gt; memo case x: NonEmptyHeap[A] =&gt; toListWithMemory(x.element :: memo, merge(x.left, x.right)) } def heapSort[A &lt;% Ordered[A]](xs: Seq[A]): Seq[A] = toList(xs.foldLeft(EmptyHeap: Heap[A])((memo, x) =&gt; merge(Heap(x), memo))) } object HeapSortTest { def main(args: Array[String]) = { Heap.heapSort(Range(1, 1000000)) System.out.println("Done!") } }</code></pre><p><small>Tags: <a href="http://termos.vemod.net/scala" class="tag" rel="tag">Scala</a></small></p> Tue, 15 Jun 2010 14:28:03 +0200 http://termos.vemod.net/purely-functional-heap-sort-in-scala 2010-06-15T14:28:03+02:00 How to Set Cache-Control for Rails Assets with Apache http://termos.vemod.net/how-to-set-cache-control-for-rails-assets-with-apache <pre><code class="ultraviolet"><span class="NamedConstant">RewriteCond</span> <span class="String">&quot;</span><span class="LibraryVariable"><span class="LibraryVariable">%{</span>QUERY_STRING}</span><span class="String">&quot;</span> <span class="String">&quot;[0-9]+$&quot;</span> <span class="NamedConstant">RewriteRule</span> <span class="String">&quot;^.*$&quot;</span> <span class="String">&quot;$0&quot;</span> <span class="String">[L,E=CACHE:True]</span> <span class="String">#</span> <span class="String">Match</span> <span class="String">all.</span> <span class="NamedConstant">Header</span> onsuccess set <span class="String"><span class="String">&quot;</span>Cache-Control<span class="String">&quot;</span></span> <span class="String"><span class="String">&quot;</span>public, max-age=31536000<span class="String">&quot;</span></span> env=CACHE </code></pre> <p>Dependencies: <code>mod_rewrite</code> and <code>mod_headers</code></p> <p>Please <a href="http://termos.vemod.net/contact">contact me</a> if you know an even nicer way.</p><p><small>Tags: <a href="http://termos.vemod.net/rails" class="tag" rel="tag">Rails</a></small></p> Mon, 14 Jun 2010 21:54:24 +0200 http://termos.vemod.net/how-to-set-cache-control-for-rails-assets-with-apache 2010-06-14T21:54:24+02:00 Datorbutiker i Malmö http://termos.vemod.net/datorbutiker-i-malmo <p> Här är en nästan komplett lista över datorbutiker i <a href="http://www.malmo.se/">Malmö</a>. Var vänlig och <a href="mailto:christoffer.sawicki@gmail.com?subject=Datorbutiker i Malmö"> meddela mig</a> om någon butik saknas. </p> <p>Se även: <a href="http://termos.vemod.net/datorbutiker-i-lund">Datorbutiker i Lund</a> och <a href="http://maps.google.com/maps?q=Datorbutiker+i+Malmö&#38;hq=Datorbutiker&#38;hnear=Malmö">Datorbutiker i Malmö på Google Maps</a></p> <h2>Datorbutiker</h2> <ul> <li><a href="http://www.dos.ac/"><span class="caps">DOS</span> (Datorer och service)</a></li> <li><a href="http://www.jmedata.se/"><span class="caps">JME</span> Data</a></li> <li><a href="http://www.jostydata.se/">Josty Data</a></li> <li><a href="http://www.malmodata.se/">Malmö Data</a></li> <li><a href="http://www.net2world.se/">Net2World</a></li> <li><a href="http://www.pcoffice.org/">PC Office</a></li> <li><a href="http://www.protelo.se/">Protelo</a></li> <li><a href="http://www.skansor.se/">Skansor</a></li> <li><a href="http://www.kjell.com/">Kjell &amp; Company</a></li> <li><a href="http://www.sensusdata.se/">Sensus Data</a></li> </ul> <h3>Apple-återförsäljare</h3> <ul> <li><a href="http://www.inwarehouse.se/StoreMalmo.aspx">inWarehouse</a></li> <li><a href="http://www.kullander.se/">Kullander &amp; Kullander</a></li> <li><a href="http://www.macsupport.se/">MacSupport</a></li> <li><a href="http://makomer.se/">Makomer</a></li> </ul> <h2>Hemelektronikkedjor</h2> <ul> <li><a href="http://www.elgiganten.se/">El-Giganten</a></li> <li><a href="http://www.expert.se/">Expert</a></li> <li><a href="http://www.mediamarkt.se/">Media Markt</a></li> <li><a href="http://www.netonnet.se/">NetOnNet</a></li> <li><a href="http://www.onoff.se/"><span class="caps">ONOFF</span></a></li> <li><a href="http://www.siba.se/"><span class="caps">SIBA</span></a></li> </ul> Mon, 24 May 2010 11:07:24 +0200 http://termos.vemod.net/datorbutiker-i-malmo 2010-05-24T11:07:24+02:00 PHP: render_template http://termos.vemod.net/php-render-template <p>I try to avoid <a href="http://termos.vemod.net/php"><span class="caps">PHP</span></a> but a university course mandated its use some time ago and I decided to do the best of the situation. One of the things I did was to implement this simple function for template rendering:</p> <pre><code class="ultraviolet">&lt;? <span class="Storage">function</span> <span class="FunctionName">render_template</span>(<span class="Variable"><span class="Variable">$</span>filename</span>, <span class="Variable"><span class="Variable">$</span>variables</span>) { <span class="CommandMethod">extract</span>(<span class="Variable"><span class="Variable">$</span>variables</span>); <span class="CommandMethod">ob_start</span>(); <span class="Keyword">require</span>(<span class="String"><span class="String">&quot;</span><span class="String">templates/</span><span class="String">&quot;</span></span> <span class="Operator">.</span> <span class="Variable"><span class="Variable">$</span>filename</span> <span class="Operator">.</span> <span class="String"><span class="String">&quot;</span><span class="String">.php</span><span class="String">&quot;</span></span>); <span class="Variable"><span class="Variable">$</span>contents</span> <span class="Operator">=</span> <span class="CommandMethod">ob_get_contents</span>(); <span class="CommandMethod">ob_end_clean</span>(); <span class="Keyword">return</span> <span class="Variable"><span class="Variable">$</span>contents</span>; } ?&gt; </code></pre> <p>It can be used like this:</p> <pre><code class="ultraviolet">&lt;? <span class="Storage">function</span> <span class="FunctionName">layout</span>(<span class="Variable"><span class="Variable">$</span>title</span>, <span class="Variable"><span class="Variable">$</span>body</span>) { <span class="Keyword">return</span> render_template(<span class="String"><span class="String">&quot;</span><span class="String">layout.html</span><span class="String">&quot;</span></span>, <span class="CommandMethod">array</span>(<span class="String"><span class="String">&quot;</span><span class="String">title</span><span class="String">&quot;</span></span> <span class="Operator">=&gt;</span> <span class="Variable"><span class="Variable">$</span>title</span>, <span class="String"><span class="String">&quot;</span><span class="String">body</span><span class="String">&quot;</span></span> <span class="Operator">=&gt;</span> <span class="Variable"><span class="Variable">$</span>body</span>)); } ?&gt; </code></pre> <p>This allows for very simple and nice composition of templates. Additionally, passing around all the template data explicitly like this makes it easier to stay sane.</p><p><small>Tags: <a href="http://termos.vemod.net/php" class="tag" rel="tag">PHP</a></small></p> Tue, 18 May 2010 00:36:24 +0200 http://termos.vemod.net/php-render-template 2010-05-18T00:36:24+02:00 PLT Scheme http://termos.vemod.net/plt-scheme <blockquote> <p><a href="http://en.wikipedia.org/wiki/PLT_Scheme" class="external"><span class="caps">PLT</span> Scheme is a programming language based on Scheme</a></p> </blockquote> Tue, 18 May 2010 00:12:20 +0200 http://termos.vemod.net/plt-scheme 2010-05-18T00:12:20+02:00 Implementing Your Own Control Structures in Scala http://termos.vemod.net/implementing-your-own-control-structures-in-scala <p>I just discovered that <a href="http://termos.vemod.net/scala">Scala</a> has the necessary power to handle implemention of custom control structures without any ugliness. The secret ingredient is that Scala has <em>call-by-name parameters</em>. Here&#8217;s an example (note the <code>=></code>):</p> <pre><code class="ultraviolet"><span class="Keyword">def</span> unless[A](condition: <span class="Storage">B</span><span class="Storage">o</span><span class="Storage">o</span><span class="Storage">l</span><span class="Storage">e</span><span class="Storage">a</span><span class="Storage">n</span>, ifFalse: =&gt; A, ifTrue: =&gt; A) = <span class="Keyword">if</span> (condition) ifTrue <span class="Keyword">else</span> ifFalse </code></pre> <p>Demonstration: Scala won&#8217;t evaluate <code>ifFalse</code> or <code>ifTrue</code> unless necessary, like here:</p> <pre><code class="ultraviolet">unless(<span class="Keyword">true</span>, <span class="Keyword">throw</span> <span class="Keyword">new</span> Error, <span class="String"><span class="String">&quot;</span>Hello!<span class="String">&quot;</span></span>) =&gt; <span class="String"><span class="String">&quot;</span>Hello!<span class="String">&quot;</span></span> </code></pre> <p>Sweet!</p><p><small>Tags: <a href="http://termos.vemod.net/scala" class="tag" rel="tag">Scala</a></small></p> Mon, 17 May 2010 02:17:50 +0200 http://termos.vemod.net/implementing-your-own-control-structures-in-scala 2010-05-17T02:17:50+02:00 Termos