<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 <title>Aki's Blog</title>
 <link href="http://akisaarinen.fi/blog-rss.xml" rel="self"/>
 <link href="http://akisaarinen.fi/blog"/>
 <updated>2014-04-16T19:08:12+09:00</updated>
 <id>http://akisaarinen.fi/blog/</id>
 <author>
   <name>Aki Saarinen</name>
   <email>firstname@akisaarinen.fi</email>
 </author>

 
 <entry>
   <title>Boilerplate-free Functional Lenses for Scala</title>
   <link href="http://akisaarinen.fi/blog/2012/12/07/boilerplate-free-functional-lenses-for-scala/"/>
   <updated>2012-12-07T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2012/12/07/boilerplate-free-functional-lenses-for-scala</id>
   <content type="html">&lt;p&gt;This post introduces boilerplate-free functional lens creation to Scala using a library called &lt;a href='http://github.com/akisaarinen/rillit'&gt;Rillit&lt;/a&gt;. Lenses are composable getters and setters for immutable data structures, which can significantly ease your pain when writing functional code. &lt;a href='http://github.com/akisaarinen/rillit'&gt;Rillit&lt;/a&gt; makes lens creation easier than any other lens library I know of by utilizing new features from Scala 2.10: &lt;a href='http://www.scalamacros.org'&gt;macros&lt;/a&gt; and &lt;a href='http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/library/index.html#scala.Dynamic'&gt;dynamic invocations&lt;/a&gt;.&lt;/p&gt;

&lt;h4 id='why_lenses'&gt;Why lenses?&lt;/h4&gt;

&lt;p&gt;Say you have the following case class structure for describing a person. The nesting seems unncessary in this small example, but that&amp;#8217;s what you need to do with larger data structures, so bear with me:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='scala'&gt;&lt;span class='k'&gt;case&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Email&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;user&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;String&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;domain&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;String&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
&lt;span class='k'&gt;case&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Contact&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;email&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Email&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;web&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;String&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
&lt;span class='k'&gt;case&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Person&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;name&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;String&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;contact&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Contact&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;

&lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;person&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='nc'&gt;Person&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;
  &lt;span class='n'&gt;name&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='s'&gt;&amp;quot;Aki Saarinen&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt;
  &lt;span class='n'&gt;contact&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='nc'&gt;Contact&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;
    &lt;span class='n'&gt;email&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='nc'&gt;Email&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='s'&gt;&amp;quot;aki&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='s'&gt;&amp;quot;akisaarinen.fi&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;),&lt;/span&gt;
    &lt;span class='n'&gt;web&lt;/span&gt;   &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='s'&gt;&amp;quot;http://akisaarinen.fi&amp;quot;&lt;/span&gt;
  &lt;span class='o'&gt;)&lt;/span&gt;
&lt;span class='o'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, say you want to modify the &lt;code&gt;user&lt;/code&gt; of the email address from &amp;#8216;aki&amp;#8217; to &amp;#8216;john&amp;#8217;. And because we&amp;#8217;re working with immutable data structures, we can&amp;#8217;t just assign a new value, but we want to create a new instance of &lt;code&gt;Person&lt;/code&gt; with the &lt;code&gt;user&lt;/code&gt; field updated. This is a common pattern when writing functional code.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s the idiomatic solution using the &lt;code&gt;copy&lt;/code&gt; method provided by case classes:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='scala'&gt;&lt;span class='n'&gt;scala&lt;/span&gt;&lt;span class='o'&gt;&amp;gt;&lt;/span&gt; &lt;span class='n'&gt;person&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;copy&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;contact&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;person&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;contact&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;copy&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;email&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;person&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;contact&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;email&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;copy&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;user&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='s'&gt;&amp;quot;john&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;)))&lt;/span&gt;
&lt;span class='n'&gt;res0&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Person&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='nc'&gt;Person&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='nc'&gt;Aki&lt;/span&gt; &lt;span class='nc'&gt;Saarinen&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt;&lt;span class='nc'&gt;Contact&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='nc'&gt;Email&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;john&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt;&lt;span class='n'&gt;akisaarinen&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;fi&lt;/span&gt;&lt;span class='o'&gt;),&lt;/span&gt;&lt;span class='n'&gt;http&lt;/span&gt;&lt;span class='o'&gt;://&lt;/span&gt;&lt;span class='n'&gt;akisaarinen&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;fi&lt;/span&gt;&lt;span class='o'&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The field gets updated, but the syntax is verbose and ugly. Lenses can ease the pain. They provide a lens to a certain part of a larger immutable data structure, allowing you to get and set that particular value.&lt;/p&gt;

&lt;h4 id='the_beauty_of_rillit'&gt;The beauty of Rillit&lt;/h4&gt;

&lt;p&gt;This is what you could do in Rillit:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='scala'&gt;&lt;span class='n'&gt;scala&lt;/span&gt;&lt;span class='o'&gt;&amp;gt;&lt;/span&gt; &lt;span class='nc'&gt;Lenser&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='kt'&gt;Person&lt;/span&gt;&lt;span class='o'&gt;].&lt;/span&gt;&lt;span class='n'&gt;contact&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;email&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;user&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;set&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;person&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='s'&gt;&amp;quot;john&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
&lt;span class='n'&gt;res1&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Person&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='nc'&gt;Person&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='nc'&gt;Aki&lt;/span&gt; &lt;span class='nc'&gt;Saarinen&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt;&lt;span class='nc'&gt;Contact&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='nc'&gt;Email&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;john&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt;&lt;span class='n'&gt;akisaarinen&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;fi&lt;/span&gt;&lt;span class='o'&gt;),&lt;/span&gt;&lt;span class='n'&gt;http&lt;/span&gt;&lt;span class='o'&gt;://&lt;/span&gt;&lt;span class='n'&gt;akisaarinen&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;fi&lt;/span&gt;&lt;span class='o'&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This performs the exact same thing as our nested &lt;code&gt;copy&lt;/code&gt; soup above, but looks a lot more civilized.&lt;/p&gt;

&lt;p&gt;Now, what exactly just happened there? &lt;code&gt;Lenser&lt;/code&gt; creates a new lens for your &lt;code&gt;user&lt;/code&gt; field using a combination of &lt;a href='http://www.scalamacros.org'&gt;macros&lt;/a&gt;, &lt;a href='http://www.scala-lang.org/archives/downloads/distrib/files/nightly/docs/library/index.html#scala.Dynamic'&gt;dynamic invocations&lt;/a&gt; and implicit conversions.&lt;/p&gt;

&lt;p&gt;Lenses in Rillit are defined as follows (with some irrelevant details omitted):&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='scala'&gt;&lt;span class='k'&gt;trait&lt;/span&gt; &lt;span class='nc'&gt;Lens&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='kt'&gt;A&lt;/span&gt;, &lt;span class='kt'&gt;B&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;get&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;A&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;B&lt;/span&gt;
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;set&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;A&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;v&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;B&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;A&lt;/span&gt;
&lt;span class='o'&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A &lt;code&gt;Lens&lt;/code&gt; provides a view to a particular part of a larger immutable data structure. In our example the larger part, &lt;code&gt;A&lt;/code&gt;, is a &lt;code&gt;Person&lt;/code&gt; and the lensed part, &lt;code&gt;B&lt;/code&gt;, is the user of the email (whose type is a &lt;code&gt;String&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;A lens can be used to &lt;code&gt;get&lt;/code&gt; or &lt;code&gt;set&lt;/code&gt; the part of &lt;code&gt;Person&lt;/code&gt; that it points to. Exactly what happened in our example above. And the cool thing is that Rillit&amp;#8217;s &lt;code&gt;Lenser&lt;/code&gt; gives you an easy way to build lenses to arbitrary nested case classes.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s split the earlier expression so it&amp;#8217;s easier to understand what happens:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='scala'&gt;&lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;lenser&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='nc'&gt;Lenser&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='kt'&gt;Person&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt;
&lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;userLens&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;lenser&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;contact&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;email&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;user&lt;/span&gt;
&lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;updatedPerson&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;userLens&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;set&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;person&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='s'&gt;&amp;quot;john&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So we first instantiate a &lt;code&gt;Lenser&lt;/code&gt; for &lt;code&gt;Person&lt;/code&gt;, then describe the fields for which this lens should be built (this is all type-safe, done in compile-time using macros), and finally use that lenser to create a new &lt;code&gt;Person&lt;/code&gt; with username updated. If you want to know the details how the Lenser works, &lt;a href='http://github.com/akisaarinen/rillit'&gt;look at the source&lt;/a&gt; Luke.&lt;/p&gt;

&lt;h4 id='whats_next'&gt;What&amp;#8217;s next?&lt;/h4&gt;

&lt;p&gt;There is a lot more benefits to using lenses than just getting rid of ugly &lt;code&gt;copy&lt;/code&gt;s. You can for example compose your lenses together, forming new lenses.&lt;/p&gt;

&lt;p&gt;There are also many other implementations of lenses for Scala, including ones in &lt;a href='https://github.com/scalaz/scalaz'&gt;Scalaz&lt;/a&gt;, &lt;a href='https://github.com/milessabin/shapeless'&gt;Shapeless&lt;/a&gt; and &lt;a href='https://github.com/retronym/macrocosm'&gt;Macrocosm&lt;/a&gt;. At the moment they have far more advanced lens implementations than Rillit, except for one part: their creation of lenses requires more boilerplate. So I focused on implementing a boilerplate-free &lt;code&gt;Lenser&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As lenses seem an incredibly useful feature in functional programming, I would hope to see a boilerplate-free, production-ready, stand-alone lens library for Scala soon after 2.10 is released. I&amp;#8217;m planning on using &lt;a href='http://github.com/akisaarinen/rillit'&gt;Rillit&lt;/a&gt; for myself, we&amp;#8217;ll see if someone else is interested in it as well. And maybe the same &lt;code&gt;Lenser&lt;/code&gt; approach could be adopted to fit Scalaz and Shapeless as well.&lt;/p&gt;

&lt;p&gt;Oh, and the name, Rillit? It&amp;#8217;s Finnish for glasses, and happens to sound funny to me.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>The mismatch between OO design and function composition</title>
   <link href="http://akisaarinen.fi/blog/2012/11/24/mismatch-between-oo-and-function-composition/"/>
   <updated>2012-11-24T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2012/11/24/mismatch-between-oo-and-function-composition</id>
   <content type="html">&lt;p&gt;Scala is a multi-paradigm language, giving developers the ability to mix both functional and object-oriented designs.&lt;/p&gt;

&lt;p&gt;Combining these two can be painful. I have a hunch that part of this friction comes from the difficulty of using function composition together with object-oriented designs.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s not that you can&amp;#8217;t combine these two, it&amp;#8217;s just that OO designs nudge you away from function composition. This post is trying to exemplify that mismatch.&lt;/p&gt;

&lt;h4 id='implicit_receivers_lead_to_method_chaining'&gt;Implicit receivers lead to method chaining&lt;/h4&gt;

&lt;p&gt;Methods of a class (in object-oriented sense) are functions, which have one implicit parameter, &lt;code&gt;this&lt;/code&gt;. They are always tied to a specific instance of the class.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s look at a simple example. Say we want to implement a numerical algorithm, which first adds 8 to a number, then takes the square root and sine of the result.&lt;/p&gt;

&lt;p&gt;Also, for the sake of making of a point, let&amp;#8217;s assume we are implementing this API ourselves and won&amp;#8217;t rely on any standard library interfaces. I&amp;#8217;m only using standard library in the implementations.&lt;/p&gt;

&lt;p&gt;I am assuming we are civilized people here and don&amp;#8217;t implement this by mutating the state of &lt;code&gt;Number&lt;/code&gt;, but return a new immutable instance. This is what our OO solution could look like:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='scala'&gt;&lt;span class='k'&gt;object&lt;/span&gt; &lt;span class='nc'&gt;OO&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
  &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Number&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;value&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Double&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
    &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;add&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Double&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='k'&gt;new&lt;/span&gt; &lt;span class='nc'&gt;Number&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;value&lt;/span&gt; &lt;span class='o'&gt;+&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
    &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;sqrt&lt;/span&gt;           &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='k'&gt;new&lt;/span&gt; &lt;span class='nc'&gt;Number&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;math&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;sqrt&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;value&lt;/span&gt;&lt;span class='o'&gt;))&lt;/span&gt;
    &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;sin&lt;/span&gt;            &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='k'&gt;new&lt;/span&gt; &lt;span class='nc'&gt;Number&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;math&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;sin&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;value&lt;/span&gt;&lt;span class='o'&gt;))&lt;/span&gt;
  &lt;span class='o'&gt;}&lt;/span&gt;

  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;algorithm&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Number&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;x&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;add&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='mi'&gt;8&lt;/span&gt;&lt;span class='o'&gt;).&lt;/span&gt;&lt;span class='n'&gt;sqrt&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;sin&lt;/span&gt;

  &lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;result&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;algorithm&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='k'&gt;new&lt;/span&gt; &lt;span class='nc'&gt;Number&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='mi'&gt;17&lt;/span&gt;&lt;span class='o'&gt;)).&lt;/span&gt;&lt;span class='n'&gt;value&lt;/span&gt;
&lt;span class='o'&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4 id='functions_lead_to_new_functions'&gt;Functions lead to new functions&lt;/h4&gt;

&lt;p&gt;If we would write this in a purely functional manner, i.e. not binding the implicit receiver, a comparable solution in Scala would look something like this:&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;code class='scala'&gt;&lt;span class='k'&gt;object&lt;/span&gt; &lt;span class='nc'&gt;FP&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
  &lt;span class='k'&gt;type&lt;/span&gt; &lt;span class='kt'&gt;Number&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='nc'&gt;Double&lt;/span&gt;

  &lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;add&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;Number&lt;/span&gt; &lt;span class='o'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='kt'&gt;Number&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='k'&gt;_&lt;/span&gt; &lt;span class='o'&gt;+&lt;/span&gt; &lt;span class='mi'&gt;8&lt;/span&gt;
  &lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;sqrt&lt;/span&gt;                    &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;math&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;sqrt&lt;/span&gt; &lt;span class='k'&gt;_&lt;/span&gt;
  &lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;sin&lt;/span&gt;                     &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;math&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;sin&lt;/span&gt; &lt;span class='k'&gt;_&lt;/span&gt;

  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;algorithm&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;add&lt;/span&gt; &lt;span class='n'&gt;andThen&lt;/span&gt; &lt;span class='n'&gt;sqrt&lt;/span&gt; &lt;span class='n'&gt;andThen&lt;/span&gt; &lt;span class='n'&gt;sin&lt;/span&gt;

  &lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;result&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;algorithm&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='mi'&gt;17&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
&lt;span class='o'&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;em&gt;Note&lt;/em&gt;: I updated the FP version from the original a bit, as suggested by Antoine Gourlay in the comments, to make the function composition less clumsy.&lt;/p&gt;

&lt;h4 id='wheres_the_difference'&gt;Where&amp;#8217;s the difference?&lt;/h4&gt;

&lt;p&gt;Now, these solutions both give you the same result, &lt;code&gt;-0.9589242746631385&lt;/code&gt;. They also look kind of similar, both having a chain of &lt;code&gt;add&lt;/code&gt;, &lt;code&gt;sqrt&lt;/code&gt; and &lt;code&gt;sin&lt;/code&gt; in the algorithm part.&lt;/p&gt;

&lt;p&gt;There are conceptual differences, though. In the OO version, the algorithm is constructed by chaining a sequence of method calls for the given instance of &lt;code&gt;Number&lt;/code&gt;. To do this, we naturally need to have a handle to the instance of &lt;code&gt;Number&lt;/code&gt;. That&amp;#8217;s where the chaining starts.&lt;/p&gt;

&lt;p&gt;In the FP version, we are taking functions, and creating a new function without ever needing to know the instance of &lt;code&gt;Number&lt;/code&gt; while doing the composition. The parts we are composing don&amp;#8217;t have to have anything in common, but we can still compose them to create a new function. In this sense function composition is more flexible.&lt;/p&gt;

&lt;h4 id='which_one_is_better'&gt;Which one is better?&lt;/h4&gt;

&lt;p&gt;The disappointing answer is that it depends. I think the functional approach is more flexible, and when I&amp;#8217;m writing Scala I try to start with that, but there is no single answer as what to do.&lt;/p&gt;

&lt;p&gt;The thing to keep in mind is that when you write something in Scala as a method (or a function), &lt;em&gt;you are making a choice&lt;/em&gt;. You define what will be the most natural way of using your piece of code.&lt;/p&gt;

&lt;p&gt;The natural way of using methods will be to chain calls on object instances. Functions, on the other hand, will naturally lead you into thinking about new functions.&lt;/p&gt;

&lt;p&gt;Now, what do you want to see done in your codebase?&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Thinking in terms of data transformations</title>
   <link href="http://akisaarinen.fi/blog/2012/09/22/thinking-in-terms-of-data-transformations/"/>
   <updated>2012-09-22T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2012/09/22/thinking-in-terms-of-data-transformations</id>
   <content type="html">&lt;p&gt;This is a successor to my previous post, &lt;a href='/blog/2012/09/10/haskellify-your-scala/'&gt;Haskellify your Scala&lt;/a&gt;, giving another example of how to design Scala in a Haskellish fashion. I am describing a way of thinking design in terms of data transformations, instead of a sequence of instructions to mutate state.&lt;/p&gt;

&lt;p&gt;&lt;a href='http://en.wikipedia.org/wiki/Imperative_programming'&gt;Imperative programming&lt;/a&gt; focuses on executing a series of statements to mutate the program state into something desirable. It is fundamentally based on the idea of executing instructions on the CPU. Modern languages provide high level abstractions for specifying the instructions, of course. Java programmers rarely need to care about the Java bytecode instructions &lt;sup id='fnref:1'&gt;&lt;a href='#fn:1' rel='footnote'&gt;1&lt;/a&gt;&lt;/sup&gt;, and even less so about the actual CPU instructions.&lt;/p&gt;

&lt;p&gt;However, none of these abstractions change the underlying mindset. The program is still a sequence of statements, albeit well structured sequence of statements, that are executed in the defined order to achieve the goal.&lt;/p&gt;

&lt;p&gt;Functional programming offers a different way of reasoning about programs. Instead of thinking in terms of sequential statements, programs can be viewed as ways of transforming input data into something more useful than the input itself.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s about first modelling the problem by defining appropriate data structures, such as lists, queues and trees, and then defining functions, which transform this data.&lt;/p&gt;

&lt;h4 id='a_tale_of_two_pong_bots'&gt;A tale of two Pong bots&lt;/h4&gt;

&lt;p&gt;To illustrate my point, I&amp;#8217;ll show you two imaginary designs that could take part in the &lt;a href='http://helloworldopen.fi/'&gt;Hello World Open&lt;/a&gt; competition. In Hello World Open, programmers compete against each other by writing bots for the game of &lt;a href='http://en.wikipedia.org/wiki/Pong'&gt;Pong&lt;/a&gt;. Bots receive events over the network about the current state of the game, while controlling their own paddle by sending commands to the server.&lt;/p&gt;

&lt;p&gt;The point of this example is not to describe a great Pong bot, but the difference between designing imperatively and functionally. I am presenting two ways of structuring a Pong bot in Scala.&lt;/p&gt;

&lt;p&gt;An imperative approach to implementing the core bot logic and the main loop could look something like this (&lt;a href='https://gist.github.com/3766321'&gt;Full source as Gist&lt;/a&gt;).&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Bot&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;connection&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;PongConnection&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
  &lt;span class='k'&gt;var&lt;/span&gt; &lt;span class='n'&gt;myDirection&lt;/span&gt;      &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='mi'&gt;0&lt;/span&gt;
  &lt;span class='k'&gt;var&lt;/span&gt; &lt;span class='n'&gt;missileInventory&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='mi'&gt;0&lt;/span&gt;
  &lt;span class='k'&gt;var&lt;/span&gt; &lt;span class='n'&gt;enemyPosition&lt;/span&gt;    &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='mi'&gt;0&lt;/span&gt;

  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;update&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;event&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Event&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
    &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;conditionA&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;event&lt;/span&gt;&lt;span class='o'&gt;))&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
      &lt;span class='n'&gt;goUp&lt;/span&gt;&lt;span class='o'&gt;()&lt;/span&gt;
    &lt;span class='o'&gt;}&lt;/span&gt; &lt;span class='k'&gt;else&lt;/span&gt; &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;conditionB&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;event&lt;/span&gt;&lt;span class='o'&gt;))&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
      &lt;span class='n'&gt;shootMissile&lt;/span&gt;&lt;span class='o'&gt;()&lt;/span&gt;
    &lt;span class='o'&gt;}&lt;/span&gt;
  &lt;span class='o'&gt;}&lt;/span&gt; 
    
  &lt;span class='k'&gt;private&lt;/span&gt; &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;goUp&lt;/span&gt;&lt;span class='o'&gt;()&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
    &lt;span class='n'&gt;myDirection&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='mi'&gt;1&lt;/span&gt;
    &lt;span class='n'&gt;connection&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;moveUp&lt;/span&gt;&lt;span class='o'&gt;()&lt;/span&gt;
  &lt;span class='o'&gt;}&lt;/span&gt;

  &lt;span class='k'&gt;private&lt;/span&gt; &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;shootMissile&lt;/span&gt;&lt;span class='o'&gt;()&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
    &lt;span class='n'&gt;missileInventory&lt;/span&gt; &lt;span class='o'&gt;-=&lt;/span&gt; &lt;span class='mi'&gt;1&lt;/span&gt;
    &lt;span class='n'&gt;connection&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;shootMissile&lt;/span&gt;&lt;span class='o'&gt;()&lt;/span&gt;
  &lt;span class='o'&gt;}&lt;/span&gt;
  
  &lt;span class='k'&gt;private&lt;/span&gt; &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;conditionA&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;event&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Event&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Boolean&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='n'&gt;sys&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;error&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='s'&gt;&amp;quot;not implemented&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
  &lt;span class='k'&gt;private&lt;/span&gt; &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;conditionB&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;event&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Event&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Boolean&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='n'&gt;sys&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;error&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='s'&gt;&amp;quot;not implemented&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
&lt;span class='o'&gt;}&lt;/span&gt;

&lt;span class='k'&gt;object&lt;/span&gt; &lt;span class='nc'&gt;Pong&lt;/span&gt; &lt;span class='k'&gt;extends&lt;/span&gt; &lt;span class='nc'&gt;App&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
  &lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;connection&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='k'&gt;new&lt;/span&gt; &lt;span class='nc'&gt;PongConnection&lt;/span&gt;
  &lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;bot&lt;/span&gt;        &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='k'&gt;new&lt;/span&gt; &lt;span class='nc'&gt;Bot&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;connection&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
  &lt;span class='k'&gt;while&lt;/span&gt; &lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;connection&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;isConnected&lt;/span&gt;&lt;span class='o'&gt;())&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
    &lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;event&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;connection&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;readEvent&lt;/span&gt;&lt;span class='o'&gt;()&lt;/span&gt;
    &lt;span class='n'&gt;bot&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;update&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;event&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
  &lt;span class='o'&gt;}&lt;/span&gt;
&lt;span class='o'&gt;}&lt;/span&gt; 
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;A functional version, structured around the idea of transforming data, could look more like this (&lt;a href='https://gist.github.com/3766325'&gt;Full source as Gist&lt;/a&gt;).&lt;/p&gt;
&lt;div class='highlight'&gt;&lt;pre&gt;&lt;span class='k'&gt;object&lt;/span&gt; &lt;span class='nc'&gt;Bot&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;actions&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;state&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;State&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;event&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Event&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='kt'&gt;State&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='kt'&gt;Seq&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='kt'&gt;Action&lt;/span&gt;&lt;span class='o'&gt;])&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;state&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;event&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='k'&gt;match&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
    &lt;span class='k'&gt;case&lt;/span&gt; &lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt;&lt;span class='n'&gt;e&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='n'&gt;conditionA&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt;&lt;span class='n'&gt;e&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='k'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='nc'&gt;State&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;up&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;state&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;    &lt;span class='o'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='nc'&gt;Actions&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;up&lt;/span&gt;
    &lt;span class='k'&gt;case&lt;/span&gt; &lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt;&lt;span class='n'&gt;e&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='k'&gt;if&lt;/span&gt; &lt;span class='n'&gt;conditionB&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;s&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt;&lt;span class='n'&gt;e&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='k'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='nc'&gt;State&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;shoot&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;state&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='o'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='nc'&gt;Actions&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;shoot&lt;/span&gt;
    &lt;span class='k'&gt;case&lt;/span&gt; &lt;span class='k'&gt;_&lt;/span&gt;                        &lt;span class='k'&gt;=&amp;gt;&lt;/span&gt; &lt;span class='n'&gt;state&lt;/span&gt;              &lt;span class='o'&gt;-&amp;gt;&lt;/span&gt; &lt;span class='nc'&gt;Actions&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;none&lt;/span&gt;
  &lt;span class='o'&gt;}&lt;/span&gt;

  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;conditionA&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;state&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;State&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;event&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Event&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Boolean&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='n'&gt;sys&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;error&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='s'&gt;&amp;quot;not implemented&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
  &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;conditionB&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;state&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;State&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;event&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Event&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Boolean&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='n'&gt;sys&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;error&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='s'&gt;&amp;quot;not implemented&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
&lt;span class='o'&gt;}&lt;/span&gt;

&lt;span class='k'&gt;object&lt;/span&gt; &lt;span class='nc'&gt;Pong&lt;/span&gt; &lt;span class='k'&gt;extends&lt;/span&gt; &lt;span class='nc'&gt;App&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
  &lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;events&lt;/span&gt;       &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;createSource&lt;/span&gt;&lt;span class='o'&gt;()&lt;/span&gt;
  &lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='n'&gt;initialState&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='nc'&gt;State&lt;/span&gt;&lt;span class='o'&gt;()&lt;/span&gt;
  
  &lt;span class='n'&gt;events&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;foldLeft&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;initialState&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt; &lt;span class='k'&gt;case&lt;/span&gt; &lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;state&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;msg&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='k'&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class='k'&gt;val&lt;/span&gt; &lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;newState&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;actions&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='nc'&gt;Bot&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;actions&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;state&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;msg&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
    &lt;span class='n'&gt;actions&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;foreach&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;execute&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
    &lt;span class='n'&gt;newState&lt;/span&gt;
  &lt;span class='o'&gt;}&lt;/span&gt;

  &lt;span class='k'&gt;private&lt;/span&gt; &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;createSource&lt;/span&gt;&lt;span class='o'&gt;()&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Iterator&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='kt'&gt;Event&lt;/span&gt;&lt;span class='o'&gt;]&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;sys&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;error&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='s'&gt;&amp;quot;not implemented&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
  &lt;span class='k'&gt;private&lt;/span&gt; &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;execute&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;action&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Action&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt; &lt;span class='k'&gt;=&lt;/span&gt; &lt;span class='n'&gt;sys&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;error&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='s'&gt;&amp;quot;not implemented&amp;quot;&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
&lt;span class='o'&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;h4 id='key_differences_in_the_two_designs'&gt;Key differences in the two designs&lt;/h4&gt;

&lt;p&gt;There are countless ways of making both of these implementations better, but I want to concentrate on the key differences between the thought process behind them. This is not a competition between the imperative and the functional version, but an illustration of the differences.&lt;/p&gt;

&lt;p&gt;The functional version is built around the concept of transforming the current state and incoming event into a sequence of formalized actions. The imperative version, on the other hand, is executing a series of instructions by calling other objects, in hopes of leading the bot to a victory.&lt;/p&gt;

&lt;p&gt;Structuring program as data transformations leads to some nice things. In the functional version it&amp;#8217;s easy to isolate all side-effects out of the core bot logic. The decision function is only transforming &lt;code&gt;(State, Event)&lt;/code&gt; tuples into new a new state and a sequence of Actions. The imperative version, in contrast, is using &lt;code&gt;Connection&lt;/code&gt; to move our world into the desired new state. Nothing prevents you from implementing similar separations for the imperative version, but it usually doesn&amp;#8217;t feel as natural.&lt;/p&gt;

&lt;p&gt;Functional data transformations are also conveniently testable using techniques like &lt;a href='https://github.com/rickynils/scalacheck'&gt;ScalaCheck&lt;/a&gt;. It generates a set of random inputs and asserts that given properties are true for all outputs. Similar testing is harder to achieve for imperative code, even if you would utilize e.g. mock objects.&lt;/p&gt;

&lt;h4 id='summary'&gt;Summary&lt;/h4&gt;

&lt;p&gt;Thinking in terms of data transformations can lead to better designs. It is one of the ways you can &lt;a href='/blog/2012/09/10/haskellify-your-scala/'&gt;Haskellify&lt;/a&gt; Scala code. Transformations, and the data-centric approach in general, forces you to clarify and formalize concepts. It also makes isolating side-effects easy.&lt;/p&gt;
&lt;div class='footnotes'&gt;&lt;hr /&gt;&lt;ol&gt;&lt;li id='fn:1'&gt;
&lt;p&gt;Unless you&amp;#8217;re doing performance optimizations, at which point &lt;a href='http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/javap.html'&gt;javap&lt;/a&gt; becomes an invaluable tool, especially with Scala. I could rant a lot about this, but this is not the post for that :)&lt;/p&gt;
&lt;a href='#fnref:1' rev='footnote'&gt;&amp;#8617;&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;</content>
 </entry>
 
 <entry>
   <title>Haskellify your Scala</title>
   <link href="http://akisaarinen.fi/blog/2012/09/10/haskellify-your-scala/"/>
   <updated>2012-09-10T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2012/09/10/haskellify-your-scala</id>
   <content type="html">&lt;p&gt;After working with &lt;a href='http://scala-lang.org' title='Scala'&gt;Scala&lt;/a&gt; daily for almost 3 years, I am still struggling to find the best practices for using it. Scala is a language of many choices and compromises: it does not do much to enforce a certain style.&lt;/p&gt;

&lt;p&gt;One of the better guidelines was suggested by my colleagues, &lt;a href='https://twitter.com/penberg'&gt;Pekka Enberg&lt;/a&gt; and &lt;a href='https://twitter.com/jussi_v'&gt;Jussi Virtanen&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Always consider what you would do in Haskell.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let&amp;#8217;s take an example.&lt;/p&gt;

&lt;h4 id='separate_data_and_logic'&gt;Separate data and logic&lt;/h4&gt;

&lt;p&gt;In Haskell, the first step to solving any problem is to design the data types. That is followed by writing an appropriate set of &lt;a href='http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)' title='Referential transparency (computer science'&gt;referentially transparent&lt;/a&gt; functions, which transform that data model as necessary. A very clear separation of data and logic.&lt;/p&gt;

&lt;p&gt;In imperative object-oriented programming, data and logic are more mixed. Classes contain data, but also provide the operations for manipulating that data.&lt;/p&gt;

&lt;p&gt;How to apply the Haskell principle to Scala? One concrete example is that case classes should only contain data, while data manipulation functions live their own life in the companion object.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s an example:&lt;/p&gt;
&lt;div class='highlight'&gt;
&lt;pre&gt;
    &lt;span class='k'&gt;case&lt;/span&gt; &lt;span class='k'&gt;class&lt;/span&gt; &lt;span class='nc'&gt;Portfolio&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;positions&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Map&lt;/span&gt;&lt;span class='o'&gt;[&lt;/span&gt;&lt;span class='kt'&gt;Instrument&lt;/span&gt;, &lt;span class='kt'&gt;Quantity&lt;/span&gt;&lt;span class='o'&gt;])&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
      &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;get&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;instrument&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Instrument&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Quantity&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='n'&gt;positions&lt;/span&gt;&lt;span class='o'&gt;.&lt;/span&gt;&lt;span class='n'&gt;getOrElse&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;instrument&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='mi'&gt;0L&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;
    &lt;span class='o'&gt;}&lt;/span&gt;

    &lt;span class='k'&gt;object&lt;/span&gt; &lt;span class='nc'&gt;Portfolio&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
      &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;add&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;a&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Portfolio&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;b&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Portfolio&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Portfolio&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
        &lt;span class='c1'&gt;// whatever (sometimes complex) logic to add two portfolios&lt;/span&gt;
      &lt;span class='o'&gt;}&lt;/span&gt;
      &lt;span class='k'&gt;def&lt;/span&gt; &lt;span class='n'&gt;subtract&lt;/span&gt;&lt;span class='o'&gt;(&lt;/span&gt;&lt;span class='n'&gt;a&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Portfolio&lt;/span&gt;&lt;span class='o'&gt;,&lt;/span&gt; &lt;span class='n'&gt;b&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Portfolio&lt;/span&gt;&lt;span class='o'&gt;)&lt;/span&gt;&lt;span class='k'&gt;:&lt;/span&gt; &lt;span class='kt'&gt;Portfolio&lt;/span&gt; &lt;span class='o'&gt;=&lt;/span&gt; &lt;span class='o'&gt;{&lt;/span&gt;
        &lt;span class='c1'&gt;// whatever (sometimes complex) logic to subtract two portfolios&lt;/span&gt;
      &lt;span class='o'&gt;}&lt;/span&gt;
    &lt;span class='o'&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Complex portfolio manipulation functions are not part of &lt;code&gt;Portfolio&lt;/code&gt; case class, but part of the companion object instead. This provides a natural separation for data and logic. The example also contains one instance method, &lt;code&gt;get&lt;/code&gt;, for the case class. There are two reasons for that. First, it is effectively part of the data model by defining the values for non-defined instruments. Second, it keeps the code using portfolios concise because &lt;code&gt;get&lt;/code&gt; is called repeatedly for Portfolio objects all over the place.&lt;/p&gt;

&lt;p&gt;To put this simply:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Do not write data manipulation logic to case classes&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Following this convention establishes two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When writing code, it encourages to focus more on the data model&lt;/li&gt;

&lt;li&gt;When reading and reviewing the code, the data model is more visible&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While Haskell does not always have the right answers, keeping it as a role model certainly guides you towards more functional and data-oriented habits. Hence when writing scala, always imagine what you would do in Haskell. After that, use your judgement.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Functional Finite-state Machines using Akka</title>
   <link href="http://akisaarinen.fi/blog/2012/05/13/functional-finite-state-machines-using-akka/"/>
   <updated>2012-05-13T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2012/05/13/functional-finite-state-machines-using-akka</id>
   <content type="html">&lt;p&gt;This post is about combining functional programming, finite-state machines and the actor model of computation using &lt;a href='http://doc.akka.io/docs/akka/snapshot/scala/fsm.html'&gt;Akka FSM&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href='http://en.wikipedia.org/wiki/Finite-state_machine'&gt;Finite-state machine&lt;/a&gt;, or FSM for short, is a mathematical model for structuring a computer program. Many programmers have come across them, either in a CS course in university or in practical applications. FSM is a useful tool, because it forces the programmer to lay out the states and flow between them explicitly. I often use FSMs as a mental model for reasoning about programs, even if the program would not be explicitly structures as one.&lt;/p&gt;

&lt;p&gt;&lt;a href='http://en.wikipedia.org/wiki/Actor_model'&gt;Actor model of computation&lt;/a&gt;, extensively used in e.g. &lt;a href='http://www.erlang.org/'&gt;Erlang&lt;/a&gt;, is another interesting concept: application is modelled as a group of independent actors, who only communicate with each other using asynchronous message passing. Actors are an old concept, first presented in 1973, but are becoming increasingly popular as a way of handling concurrency and parallelism as the number of cores increase. There is &lt;a href='http://channel9.msdn.com/%28A%28DIZWlv8LzQEkAAAAOTQ0NWI2ZTUtM2ZlYS00Yjg1LTg4NzMtNzJhZjA1MmUwZmMxAmqfHykWJRBKmZh75HL0--PjXeY1%29%29/Shows/Going+Deep/Hewitt-Meijer-and-Szyperski-The-Actor-Model-everything-you-wanted-to-know-but-were-afraid-to-ask'&gt;a great talk about actors&lt;/a&gt; on Channel 9, involving &lt;a href='http://carlhewitt.info/'&gt;Carl Hewitt&lt;/a&gt;, the inventor of actors, and &lt;a href='http://en.wikipedia.org/wiki/Erik_Meijer_%28computer_scientist%29'&gt;Eric Meijer&lt;/a&gt; from Microsoft Research. I recommend watching it, regardless of your level of experience with the actor model.&lt;/p&gt;

&lt;p&gt;Regarding the third component in this post, functional programming, Michael Feathers recently wrote about &lt;a href='http://michaelfeathers.typepad.com/michael_feathers_blog/2012/03/tell-above-and-ask-below-hybridizing-oo-and-functional-design.html'&gt;hybridizing object-oriented and functional design&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Object-orientation is better for the higher levels of a system, and functional programming is better for the lower levels.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Michael throws in the usual disclaimers about this not applying to every case, to which I agree, but I still think there is something to it. I &lt;em&gt;love&lt;/em&gt; functional programming: filtering, mapping and folding lists allow me to write very concise code, fast. However, pure code with no side-effects is ultimately of no use on itself: someone somewhere must see the results. How to orchestrate the side-effects and build the larger application around the functional pieces is a very interesting question, one which does not have a clear-cut answer.&lt;/p&gt;

&lt;p&gt;In the spirit of Michael&amp;#8217;s post, but instead of using traditional objects on the higher level, could we structure the application as a set of functional finite-state machine actors? Independent entities, whose behaviour is well specified in terms of states and state transitions. Entities which communicate asynchronously using actor message passing. Entities whose state transitions are written as purely functional pieces of code. This seems like an intriguing concept.&lt;/p&gt;

&lt;p&gt;The idea itself is nothing new. The &lt;a href='http://www.erlang.org/documentation/doc-4.8.2/doc/design_principles/fsm.html'&gt;Erlang design principles&lt;/a&gt;, for example, describe how to implement finite-state machines using Erlang actors. I have, however, rarely seen this as the base for application design.&lt;/p&gt;

&lt;p&gt;A few weeks ago I was excited to find &lt;a href='http://doc.akka.io/docs/akka/snapshot/scala/fsm.html'&gt;Akka FSM&lt;/a&gt;, a finite-state machine implementation library for Akka, an actor framework for &lt;a href='http://scala-lang.org'&gt;Scala&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Scala is well suited for functional programming, and Akka FSM does hence provide the possibility to combine the aforementioned three pieces into a single clean package: implementing the application as a set of actors which are internally functional finite-state machines. I have been writing code using Akka FSM for a few weeks now, and it seems like a natural way of structuring programs. So far I am very pleased.&lt;/p&gt;

&lt;p&gt;The official documentation contains &lt;a href='http://doc.akka.io/docs/akka/snapshot/scala/fsm.html#A_Simple_Example'&gt;plenty of examples&lt;/a&gt;, so I won&amp;#8217;t replicate them here. Check the docs and give it a go.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Running this site with Jekyll, Compass and Twitter Bootstrap</title>
   <link href="http://akisaarinen.fi/blog/2012/04/09/running-this-site-with-jekyll-compass-and-twitter-bootstrap/"/>
   <updated>2012-04-09T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2012/04/09/running-this-site-with-jekyll-compass-and-twitter-bootstrap</id>
   <content type="html">&lt;p&gt;During the Easter holidays I updated the technology for this site.&lt;/p&gt;

&lt;p&gt;My goal was to find a simple and elegant solution for hosting two things: &lt;a href='http://akisaarinen.fi'&gt;my static home page&lt;/a&gt; and this blog. Until yesterday, the blog was hosted at &lt;a href='http://posterous.com'&gt;Posterous&lt;/a&gt;. Posterous was recently &lt;a href='http://blog.posterous.com/big-news'&gt;acquired by Twitter&lt;/a&gt; for their talent, which means the service is likely no longer actively developed. On a related note, there was &lt;a href='http://news.ycombinator.com/item?id=3695407'&gt;good discussion&lt;/a&gt; in Hacker News about talent acquisitions.&lt;/p&gt;

&lt;p&gt;In addition to the fact that Posterous platform is dying, I was never really happy with writing the blog posts in a WYSIWYGish web interface. Writing in &lt;a href='http://www.vim.org/'&gt;Vim&lt;/a&gt; with basic &lt;a href='http://daringfireball.net/projects/markdown/'&gt;Markdown&lt;/a&gt; syntax is an attractive alternative.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s what I came up with. A pretty standard stack these days, I guess. I have been reading about almost all of these tools for a while and it was nice to see how well everything worked. Not always the case with hyped tools.&lt;/p&gt;

&lt;p&gt;Everything is based on the static site generator, &lt;a href='http://jekyllrb.com/'&gt;Jekyll&lt;/a&gt;. Installation is straightforward using RubyGems, just install RubyGems in your preferred way and run &lt;code&gt;gem install jekyll&lt;/code&gt;. Then all you need to do is run &lt;code&gt;jekyll&lt;/code&gt; and the whole site is compiled to static HTML, ready to be hosted where ever you want. I am using &lt;a href='http://www.kapsi.fi'&gt;Kapsi&lt;/a&gt; for hosting, but basically any provider, like &lt;a href='http://pages.github.com/'&gt;Github pages&lt;/a&gt; or &lt;a href='http://www.heroku.com/'&gt;Heroku&lt;/a&gt; would do. Nice thing about this setup is that it requires nothing except a basic HTTP server from the hosting provider.&lt;/p&gt;

&lt;p&gt;Jekyll contains support for &lt;a href='http://daringfireball.net/projects/markdown/'&gt;Markdown&lt;/a&gt; based page rendering and &lt;a href='http://liquidmarkup.org/'&gt;Liquid&lt;/a&gt; templates out-of-the-box. There are at least two major projects that try to bring one-click blog hosting around Jekyll: &lt;a href='http://octopress.org/'&gt;Octopress&lt;/a&gt; and &lt;a href='http://jekyllbootstrap.com/'&gt;Jekyll-Bootstrap&lt;/a&gt;. I decided against using these, for two reasons. First, I wanted to keep things as simple as possible and second, I wanted to learn in the process.&lt;/p&gt;

&lt;p&gt;To make the site look like it does, I used two tools: &lt;a href='http://compass-style.org/'&gt;Compass&lt;/a&gt; and &lt;a href='http://twitter.github.com/bootstrap/'&gt;Twitter Bootstrap&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Compass is a CSS Authoring Framework, as they call it. In practice, it lets me write style sheets in &lt;a href='http://sass-lang.com/'&gt;Sass&lt;/a&gt;. Sass an excension of CSS3 which compass then compiles to standard CSS3. End result is cleaner stylesheet syntax and less duplication than with traditional CSS files. Being also a rubygem, installation is again simple: &lt;code&gt;gem install compass&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Basic layout is achieved using Twitter Bootstrap styles, with some custom CSS thrown in. In case you are not already familiar with Bootstrap, I suggest you &lt;a href='http://twitter.github.com/bootstrap/'&gt;take a look&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Sources are hosted in &lt;a href='http://github.com/akisaarinen/akisaarinen.fi'&gt;a git repository&lt;/a&gt; and deployed to production using a simple shell script. Previewing posts and changes locally is easy:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;compass watch
jekyll --server --auto&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This results into compass watching for changes in Sass files and compiling them to CSS as soon as the file is modified. Jekyll starts a standalone HTTP server in port 4000 and lets me preview the latest changes by accessing http://localhost:4000/. The &lt;code&gt;--auto&lt;/code&gt; flag automatically re-compiles the site as soon as there are any changes.&lt;/p&gt;

&lt;p&gt;The feedback loop for testing is extremely fast, something I value a lot. Writing and publishing this blog post is now all sunshine and happiness.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Smooth application development with Qt</title>
   <link href="http://akisaarinen.fi/blog/2011/12/02/smooth-application-development-with-qt/"/>
   <updated>2011-12-02T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2011/12/02/smooth-application-development-with-qt</id>
   <content type="html">&lt;p&gt;I wrote an article about C++ development with Qt, together with my colleagues Sami Rosendahl and Tuomas Järvensivu at Reaktor. Check it out at &lt;a href='http://reaktor.fi/en/our_expertise/qt/'&gt;Reaktor&amp;#8217;s site&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s interesting to see what happens to Qt in the future. It&amp;#8217;s clearly one of the best C++ libraries I&amp;#8217;ve used and I&amp;#8217;m sincerely hoping they&amp;#8217;ll stay on the right path.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>A cleaner Google Reader</title>
   <link href="http://akisaarinen.fi/blog/2011/11/07/a-cleaner-google-reader/"/>
   <updated>2011-11-07T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2011/11/07/a-cleaner-google-reader</id>
   <content type="html">&lt;p&gt;The new design of Google Reader has been &lt;a href='http://fury.com/2011/11/my-offer-to-google-reader/'&gt;debated&lt;/a&gt; &lt;a href='http://www.betabeat.com/2011/11/02/sharebros-everybody-hates-the-new-google-reader/'&gt;a lot&lt;/a&gt;. I was also frustrated by unnecessary large margins and useless bars, especially when reading from my 13&amp;#8221; laptop screen.&lt;/p&gt;

&lt;p&gt;I made a simple Chrome extension to fix things up a little. You can install &lt;a href='https://chrome.google.com/webstore/detail/eigifncndbofnhijdnkfcgooiolblpbl'&gt;CleanReader&lt;/a&gt; from the Chrome Web Store for free. Sources are available at &lt;a href='https://github.com/akisaarinen/cleanreader'&gt;GitHub&lt;/a&gt;.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Little things that make the customer feel good</title>
   <link href="http://akisaarinen.fi/blog/2011/10/30/little-things-that-make-the-customer-feel-good/"/>
   <updated>2011-10-30T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2011/10/30/little-things-that-make-the-customer-feel-good</id>
   <content type="html">&lt;p&gt;Yesterday I wanted to add a new post to the social bookmarking service, &lt;a href='http://trunk.ly'&gt;trunk.ly&lt;/a&gt;, by emailing a link to my username+magicnumber@post.trunk.ly address. I was typing the address out of my memory and unfortunately sent my link to @trunk.ly instead of the correct address at @post.trunk.ly.&lt;/p&gt;

&lt;p&gt;In many services a mistake like this could result in a silent failure and a lost email. Or some technical “Deliveration failed: error 0xF3F3”. Afterall, I did email to a stupid address, right?&lt;/p&gt;

&lt;p&gt;Trunk.ly did something different. I received a personalized reply, with trunk.ly co-founder Tim Bull as the sender:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;From: Tim Bull &amp;lt;tim@trunk.ly&amp;gt;
To: Aki Saarinen &amp;lt;..&amp;gt;
Subject: Re: LiveReload

Hi Aki,

Just FYI - the links need to mailed to post.trunk.ly NOT trunk.ly :-)

Cheers,
Tim Bull&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;How amazing is that! It made me feel that they really want to take good care of me and my links. In a technical sense things like this are not hard to implement, it&amp;#8217;s just that we often don&amp;#8217;t even think about it.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>DNS for internal service resolution and configuration</title>
   <link href="http://akisaarinen.fi/blog/2011/09/03/dns-for-internal-service-resolution-and-configuration/"/>
   <updated>2011-09-03T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2011/09/03/dns-for-internal-service-resolution-and-configuration</id>
   <content type="html">&lt;p&gt;&lt;a href='https://twitter.com/#!/ricardovice'&gt;Ricardo Vice Santos&lt;/a&gt; gave an interesting presentation about the &lt;a href='http://www.spotify.com/'&gt;Spotify&lt;/a&gt; architecture yesterday at &lt;a href='http://reaktordevday.fi/'&gt;Reaktor Dev Day&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What struck me as a clever yet rarely used idea was his description of their usage of DNS for internal service resolution and configuration in the Spotify backend.&lt;/p&gt;

&lt;p&gt;Connections from clients come through an access point, a relatively thin proxy to various backend services. Services provide authentication, decryption keys, track information and of course the actual music data, among other things. How did they configure the whole network of services? They used DNS. It&amp;#8217;s pretty easy to store configuration parameters as TXT records in DNS alongside other records. Access point queries the internal DNS server and then decides where to connect the incoming request.&lt;/p&gt;

&lt;p&gt;Setting up a DNS server is really easy and DNS servers are one of the most battle tested pieces of code. When was the last time you had to worry about DNS or heard about DNS server failure? You almost never do, because it just works.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Project Ghettoblaster: How we built a kick-ass portable boombox</title>
   <link href="http://akisaarinen.fi/blog/2011/08/31/project-ghettoblaster-how-we-built-a-kickass-portable-boombox/"/>
   <updated>2011-08-31T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2011/08/31/project-ghettoblaster-how-we-built-a-kickass-portable-boombox</id>
   <content type="html">&lt;p&gt;Just finished an article about building a portable boombox with no previous experience with DIY audio systems. &lt;a href='http://akisaarinen.fi/gb'&gt;Check it out!&lt;/a&gt;&lt;/p&gt;
&lt;img alt='Ghettoblaster' src='/assets/img/blog/ghettoblaster.jpg' style='width:400px; margin-bottom: 10px;' /&gt;</content>
 </entry>
 
 <entry>
   <title>Running oprofiler in Nexus One with Android 2.2</title>
   <link href="http://akisaarinen.fi/blog/2011/08/09/running-oprofiler-in-nexus-one-with-android-22/"/>
   <updated>2011-08-09T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2011/08/09/running-oprofiler-in-nexus-one-with-android-22</id>
   <content type="html">&lt;p&gt;&lt;a href='http://oprofile.sourceforge.net/'&gt;Oprofiler&lt;/a&gt; is a system-wide profiling tool for Linux that utilizes &lt;a href='http://en.wikipedia.org/wiki/Hardware_performance_counter'&gt;hardware performance counters&lt;/a&gt; in the CPU to provide a wide range of interesting statistics about program execution. Oprofiler is not enabled in the standard Android distribution, but with some modifications, it can be used under Android also.&lt;/p&gt;

&lt;p&gt;I was able to run oprofiler under Nexus One and Android 2.2, with one limitation. I could only profile once after a restart, further attempts to start the profiler would not succeed in collecting any data. This limits the usability of oprofiler, but it was enough for my immediate profiling needs. If you manage to fix this issue, let me know.&lt;/p&gt;

&lt;h4 id='compile_kernel_with_support_for_profiling'&gt;Compile kernel with support for profiling&lt;/h4&gt;

&lt;p&gt;Oprofiler needs support in the Linux kernel, and the prebuilt kernel for Nexus One doesn&amp;#8217;t include this support. You need to compile your own version of the Android kernel for your device, with profiling support enabled.&lt;/p&gt;

&lt;p&gt;You can check what kernel version your phone is currently running from Settings-&amp;gt;About Phone. I suggest you try to compile the same version of the kernel that the phone is running as default. A good article about building the kernel and platform for Nexus One is available here. I used kernel version 2.6.32 for my phone with Android Froyo 2.2 (FRG83).&lt;/p&gt;

&lt;p&gt;You should use standard kernel configuration tools (&amp;#8220;make menuconfig&amp;#8221;) to enable the support, but you can compare results with &lt;a href='http://akisaarinen.fi/public/android/profiling-support-to-2.6.32.patch'&gt;this patch&lt;/a&gt; to see you&amp;#8217;re enabling the right things (&lt;a href='http://cateee.net/lkddb/web-lkddb/OPROFILE.html'&gt;CONFIG_OPROFILE&lt;/a&gt;, etc).&lt;/p&gt;

&lt;p&gt;In order to keep the kernel small enough (there&amp;#8217;s a limitation what can be fit into the phone), I additionally needed to disable ext3 and ext2 support.&lt;/p&gt;

&lt;h4 id='interrupt_bug_in_2632_kernel_for_nexus_one'&gt;Interrupt bug in 2.6.32 kernel for Nexus One&lt;/h4&gt;

&lt;p&gt;Oprofiler module in 2.6.32 kernel is missing an interrupt for Nexus One CPU. The bug is &lt;a href='http://groups.google.com/group/android-platform/browse_thread/thread/3f17699acfbd3f04/81d79fcb3a2412a7'&gt;discussed&lt;/a&gt; in the android-platform mailing list and original patch are available &lt;a href='https://review.source.android.com/15707'&gt;here&lt;/a&gt;. I also have a &lt;a href='http://akisaarinen.fi/public/android/oprofiler-interrupt-2.6.32.patch'&gt;mirror of the patch&lt;/a&gt;, just in case. Apply the patch, include the kernel with your custom Android 2.2 distribution and start profiling.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Android radio firmware checksums for Google Nexus One</title>
   <link href="http://akisaarinen.fi/blog/2011/08/08/android-radio-firmware-checksums-for-google-nexus-one/"/>
   <updated>2011-08-08T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2011/08/08/android-radio-firmware-checksums-for-google-nexus-one</id>
   <content type="html">&lt;p&gt;I recently needed to downgrade my radio firmware of an european Google Nexus One for research purposes. To my surprise, HTC or Google didn&amp;#8217;t provide official packages of the older radio firmware. If you want to get an older radio firmware, you&amp;#8217;re on your own. Only way of getting them is to dig deep into Android developer forums and download a radio image from unofficial sources.&lt;/p&gt;

&lt;p&gt;Because flashing the radio firmware is one of those things that has a serious risk of bricking the phone, it&amp;#8217;s not too fun to try with shady images and see what happens. If the image is incompatible or damaged, you&amp;#8217;ll end up with a brick.&lt;/p&gt;

&lt;p&gt;I can&amp;#8217;t distribute any radio images, because their license doesn&amp;#8217;t allow redistribution. But I can publish the checksums of the images I successfully used, so you can verify the files you&amp;#8217;ve got to see if it&amp;#8217;s the same that I&amp;#8217;ve been using. I hope this can be another data point you can use to decide whether a radio image is reliable or not.&lt;/p&gt;

&lt;p&gt;Note that I don&amp;#8217;t have any visibility what&amp;#8217;s inside these, they&amp;#8217;re just a result of a few days worth of forum searches and experiments. They could even contain malware. What I can tell is that they did not brick my phone and the radio was still working after these were in. The checksums are of the radio.img file only.&lt;/p&gt;

&lt;p&gt;I have a newer Nexus One model (the one with SLCD), microp firmware version 0c15, and hboot version 0.35.0017.&lt;/p&gt;

&lt;h4 id='radio_5120008'&gt;Radio 5.12.00.08&lt;/h4&gt;

&lt;p&gt;Tested to work with Android 2.2.1 Froyo (FRG83).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MD5: 263fb298d747f9e5632b373c69ce7893&lt;/li&gt;

&lt;li&gt;SHA1: 2ad521b954178f0962d25c13ba45014df7d2c455&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id='radio_5080004'&gt;Radio 5.08.00.04&lt;/h4&gt;

&lt;p&gt;Tested to work with Android 2.2.1 Froyo (FRG83).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MD5: dee19eddd42cd0166398bcab37663f62&lt;/li&gt;

&lt;li&gt;SHA1: 802656e261433400d3a56a978b0350b180bc8884&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id='radio_4040003_2'&gt;Radio 4.04.00.03_2&lt;/h4&gt;

&lt;p&gt;Tested to work with Android 2.1 Eclair (ERE36B).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MD5: 310d85c4998163818f7dcdef730c2a12&lt;/li&gt;

&lt;li&gt;SHA1: 1bc692631d33f8b885a5152d602cb3f2e812250d&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>Booting Android 2.1 in Nexus One with SLCD</title>
   <link href="http://akisaarinen.fi/blog/2011/08/05/booting-android-21-in-nexus-one-with-slcd/"/>
   <updated>2011-08-05T00:00:00+09:00</updated>
   <id>http://akisaarinen.fi/blog/2011/08/05/booting-android-21-in-nexus-one-with-slcd</id>
   <content type="html">&lt;p&gt;I wanted to downgrade my Google Nexus One momentarily back to stock Android 2.1 (called Eclair), in order to build my own version of the &lt;a href='http://appanalysis.org/'&gt;TaintDroid system&lt;/a&gt;. I first needed the stock Eclair to fetch the proprietary 2.1 compatible libraries from the phone. These libraries are not allowed to be distributed by anyone but the vendors, which means they are only embedded into the stock images. I had Android 2.2 installed to the phone, but the libraries there were incompatible with 2.1.&lt;/p&gt;

&lt;p&gt;I wrote this post because there&amp;#8217;s quite a lot confusing and contradictory information about downgrading, scattered around various forums. Hopefully someone can learn from my experiences. I&amp;#8217;m using a phone with unlocked bootloader and most of the tricks only work after unlocking, so if you want to keep your phone locked, this is not the way for you.&lt;/p&gt;

&lt;p&gt;Using these steps I managed to boot up stock 2.1 Eclair in Nexus One, even though admittedly I not in a 100% working shape. It kept throwing some exceptions into my face, but it was in a good enough condition to use adb to download the libraries. After that I successfully compiled and installed the modified 2.1 in TaintDroid, which worked just fine. Probably the stock installation could&amp;#8217;ve fixed too, but I didn&amp;#8217;t invest my time in finding what was wrong with it.&lt;/p&gt;

&lt;h4 id='slcd_or_amoled'&gt;SLCD or AMOLED?&lt;/h4&gt;

&lt;p&gt;First piece of the puzzle when downgrading is that there are two kinds of Nexus Ones. An older generation with an AMOLED display and a newer one with an SLCD display. You can boot up your phone to the bootloader and check microp version to see which one you have: 0b15 is AMOLED, 0c15 is SLCD.&lt;/p&gt;

&lt;p&gt;I have the newer one, which becomes a problem: the kernel in stock Android 2.1 does not support SLCD, so you need to compile a kernel by yourself, as described later on in the post. Android 2.1 can be booted up with SLCD, you just have to have support for it in the kernel. I have no experience with the AMOLED Nexus One, so I&amp;#8217;m not sure about that, but I believe it will work out-of-the-box.&lt;/p&gt;

&lt;h4 id='hboot_microp_and_radio_firmware'&gt;Hboot, microp and radio firmware&lt;/h4&gt;

&lt;p&gt;I had a recent version of the bootloader firmware, also called hboot. My bootloader is in version 0.35.0017. Some sources claim the newer bootloader wouldn&amp;#8217;t work with Eclair. I had no problems with the newer bootloader, so I have proof that it does work just fine. Nexus one with SLCD also has newer version of the microp firmware (0c15) than originally used for by Eclair (0b15), and it also works just fine. No need to worry about those.&lt;/p&gt;

&lt;p&gt;A possible troublemaker could be the radio driver. I had some problems along the way, which led me to downgrade my radio firmware back to 4.04.00.03_2. When I was all finished, though, I also successfully booted up Eclair with radios 5.08.00.04 and 5.12.00.08, so I believe they should all work and my problems were cause by other factors at the time.&lt;/p&gt;

&lt;p&gt;If you run into trouble you can&amp;#8217;t figure out, you can try downgrading the radio, some checksums of working radio images available in &lt;a href='/blog/2011/08/08/android-radio-firmware-checksums-for-google-nexus-one/'&gt;my other post&lt;/a&gt;.&lt;/p&gt;

&lt;h4 id='getting_a_kernel_with_slcd_support'&gt;Getting a kernel with SLCD support&lt;/h4&gt;

&lt;p&gt;In order to boot Android 2.1 Eclair with the SLCD, you need to have a new enough kernel. I used 2.6.32, newer ones might do as well.&lt;/p&gt;

&lt;p&gt;Simplest way to get a working kernel is to use the boot image from Android 2.2 (Froyo) distribution. You can get one for Nexus one from &lt;a href='http://developer.htc.com/'&gt;developer.htc.com&lt;/a&gt;. I used &lt;a href='http://dl4.htc.com/RomCode/Source_and_Binaries/signed-passion-img-FRG83_0923.zip'&gt;Nexus One FRG83 system image&lt;/a&gt;. Unzip the zip, and you get a boot.img loaded with 2.6.32 kernel, which you can eventually flash into the device with &amp;#8216;fastboot flash boot boot.img&amp;#8217;.&lt;/p&gt;

&lt;p&gt;If you want, you can also compile your own 2.6.32 kernel. This will become handy (and even a necessity) later on, if you want to customize your platform at all, but it&amp;#8217;s not strictly necessary at this point. If you don&amp;#8217;t want to compile your own kernel, you can skip the next section.&lt;/p&gt;

&lt;h4 id='compiling_your_own_2632_kernel'&gt;Compiling your own 2.6.32 kernel&lt;/h4&gt;

&lt;p&gt;A good writeup about how to compile 2.6.32 kernel for Android is available at &lt;a href='http://randomizedsort.blogspot.com/2010/08/building-android-and-linux-kernel-for.html'&gt;here&lt;/a&gt;. First part of the post is about compiling Android 2.2 distribution, but take a look at the kernel part. After compiling the kernel, you can use &amp;#8216;fastboot boot /path/to/my/kernel/zImage&amp;#8217; to boot up Android with your custom-built kernel instead of the one flashed to the device. This is a handy way of testing a new kernel, without having to flash it.&lt;/p&gt;

&lt;p&gt;I compiled my kernel while still having Android 2.2.1 installed on the phone. This was convenient, as the phone already had a 2.6.32 kernel, so I could use that as a baseline for my kernel configuration. That way I could also modify one thing at once, so if the system didn&amp;#8217;t boot, I knew the reason was my kernel, and the kernel only. Only after having a fully functioning 2.6.32 kernel image did I proceed with installing Eclair.&lt;/p&gt;

&lt;h4 id='downloading_and_flashing_stock_eclair_image'&gt;Downloading and flashing stock Eclair image&lt;/h4&gt;

&lt;p&gt;You can download the stock image for Android 2.1 from &lt;a href='http://developer.htc.com'&gt;developer.htc.com&lt;/a&gt;, I used &lt;a href='http://dl4.htc.com/RomCode/Source_and_Binaries/NexusOne_ERE36B_TMOUS.zip'&gt;Nexus One (ERE36B) Official OS Image&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In order to flash the zip with &amp;#8216;fastboot update NexusOne_ERE36B_TMOUS.zip&amp;#8217;, you need to unzip the package, modify android-info.txt so that you allow the newer bootloader and microp versions (and radio if you didn&amp;#8217;t downgrade):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require version-bootloader=0.33.2012|0.33.0012|0.35.0017
require version-microp=0b15|0c15
require version-baseband=4.04.00.03_2|5.12.00.08&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&amp;#8230;and then zip it back in.&lt;/p&gt;

&lt;p&gt;This will just tell fastboot that you want to flash the images even though you have a newer bootloader, microp and radio versions. I&amp;#8217;m not that experienced on this subject, so I&amp;#8217;m not 100% sure if there&amp;#8217;s a way this can screw things (flashing always has a hint of danger to it), but I had no problems myself. I believe this to be safe, if the flashing is done properly, but be warned though, I&amp;#8217;m not going to take the blame if you brick your phone.&lt;/p&gt;

&lt;h4 id='booting_eclair_with_custom_kernel'&gt;Booting Eclair with custom kernel&lt;/h4&gt;

&lt;p&gt;After flashing the stock Eclair, you can try booting up the phone, but with an SLCD you&amp;#8217;ll end up with a black screen. My phone also seemed to jam up so I had to take out the battery before being able to reset it.&lt;/p&gt;

&lt;p&gt;What you need now, is the 2.6.32 kernel image. If you want to use the one from stock Froyo, just issue &amp;#8216;fastboot flash boot boot.img&amp;#8217; for the boot-image coming with froyo and you should be good to go.&lt;/p&gt;

&lt;p&gt;If you compiled it yourself, simplest way is to use &amp;#8216;fastboot boot myKernelImage&amp;#8217;, so you don&amp;#8217;t have to craft a boot.img to be flashed. If you want to be able to boot the 2.1 without some assistance from a computer and the fastboot utility, you&amp;#8217;ll need to do that eventually, too. I didn&amp;#8217;t because I only used the stock 2.1 for a while in order to fetch the proprietary drivers.&lt;/p&gt;

&lt;h4 id='fixing_network_drivers'&gt;Fixing network drivers&lt;/h4&gt;

&lt;p&gt;Since the bundled bcm4329 network device driver in the system image is compiled to work with the exact kernel version thats bundled with the distribution, it won&amp;#8217;t work when booting up with a different kernel using &amp;#8216;fastboot boot&amp;#8217;. To make networking work, you need to patch the bcm4329.ko from your custom kernel to the system partition of the phone, where it&amp;#8217;s loaded from in the boot procedure. This might apply to other drivers too, however I was only experiencing problems with networking. If you only want to fetch the proprietary drivers, you won&amp;#8217;t even need the networking, anyway.&lt;/p&gt;

&lt;h4 id='a_working_android_21_in_nexus_one'&gt;A working Android 2.1 in Nexus One&lt;/h4&gt;

&lt;p&gt;If everything went well, you should now have booted up stock Android 2.1 in your Nexus One. Happy hacking, whatever it is you wish to do with it!&lt;/p&gt;</content>
 </entry>
 
 
</feed>
