/var/log/mindhttp://blog.dhananjaynene.com/2018-01-09T00:00:00+05:30Analysing Tourism Data with Kotlin2018-01-09T00:00:00+05:302018-01-09T00:00:00+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2018-01-09:/2018/01/analysing-tourism-data-with-kotlin/<p>Here's a brief example of some of the functional processing capabilities of Kotlin.</p> <p>To get some raw data I headed out to http://data.gov.in</p> <p>The original data set can be found at <a href="https://data.gov.in/catalog/foreign-tourist-arrivals-india-top-15-source-countries">Foreign Tourist Arrivals In India From Top 15 Source Countries</a></p> <p>The modified CSV with the continents added is <a href="/files/tourists-to-india.csv">here</a></p> <p>Thus the CSV has the following columnar structure.</p> <ul> <li>Column 1: Country of Tourist</li> <li>Column 2: Continent</li> <li>Columns 3 onwards: Number of tourists visiting India starting 2001 to 2015</li> </ul> <p>The problem statement I made up is kind of contrived to be able to demonstrate some interesting coding aspects. In any case the statement is as follows:</p> <p>Read the file. Group all the data by continents. For each continent compute the following</p> <ul> <li>Total number of tourists from all countries in that continent in 2015</li> <li>Percentage growth for total number of tourists for that continent from 2001 to 2015</li> <li>Country from whom the maximum number of tourists visited India in 2015 from that continent</li> </ul> <p>Display the data in the descending order of the percentage growth rate from that continent</p> <p>The resultant Kotlin code is as follows</p> <div class="highlight"><pre><span></span><span class="k">import</span> <span class="nn">java.io.File</span> <span class="k">data</span> <span class="k">class</span> <span class="nc">CountryData</span><span class="p">(</span><span class="k">val</span> <span class="py">name</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="k">val</span> <span class="py">visitors</span><span class="p">:</span> <span class="n">List</span><span class="p">&lt;</span><span class="n">Int</span><span class="p">&gt;)</span> <span class="k">data</span> <span class="k">class</span> <span class="nc">Result</span><span class="p">(</span><span class="k">val</span> <span class="py">tourists2015</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="k">val</span> <span class="py">pctGrowth</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="k">val</span> <span class="py">maxCountry</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="k">fun</span> <span class="nf">main</span><span class="p">(</span><span class="n">args</span><span class="p">:</span> <span class="n">Array</span><span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;)</span> <span class="p">{</span> <span class="c1">// Open file</span> <span class="n">File</span><span class="p">(</span><span class="s">&quot;tourists-to-india.csv&quot;</span><span class="p">)</span> <span class="c1">// Read all lines</span> <span class="p">.</span><span class="n">readLines</span><span class="p">(</span><span class="n">Charsets</span><span class="p">.</span><span class="n">US_ASCII</span><span class="p">)</span> <span class="c1">// Drop the first line (column headers)</span> <span class="p">.</span><span class="n">drop</span><span class="p">(</span><span class="m">1</span><span class="p">)</span> <span class="c1">// Drop the last line (file totals)</span> <span class="p">.</span><span class="n">dropLast</span><span class="p">(</span><span class="m">1</span><span class="p">)</span> <span class="c1">// For each remaining row in the file</span> <span class="p">.</span><span class="n">map</span> <span class="p">{</span> <span class="n">row</span> <span class="p">-&gt;</span> <span class="c1">// split into cells using a comma as the delimiter</span> <span class="n">row</span><span class="p">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;,&quot;</span><span class="p">)</span> <span class="c1">// for the array of cells in each row</span> <span class="p">.</span><span class="n">let</span> <span class="p">{</span> <span class="n">array</span> <span class="p">-&gt;</span> <span class="c1">// create a pair. The first value is the continent name (array[1])</span> <span class="c1">// The second value is the CountryData ie.</span> <span class="c1">// list of tourists from that country each year starting 2011</span> <span class="n">array</span><span class="p">[</span><span class="m">1</span><span class="p">]</span> <span class="n">to</span> <span class="n">CountryData</span><span class="p">(</span><span class="n">array</span><span class="p">[</span><span class="m">0</span><span class="p">],</span> <span class="n">array</span><span class="p">.</span><span class="n">drop</span><span class="p">(</span><span class="m">2</span><span class="p">).</span><span class="n">map</span> <span class="p">{</span> <span class="n">it</span><span class="p">.</span><span class="n">toInt</span><span class="p">()</span> <span class="p">})</span> <span class="p">}</span> <span class="p">}</span> <span class="c1">// collate all country data for each continent into a list of countrydata</span> <span class="c1">// for that continent</span> <span class="p">.</span><span class="n">groupBy</span><span class="p">({</span><span class="n">it</span><span class="p">.</span><span class="n">first</span><span class="p">},</span> <span class="p">{</span> <span class="n">it</span><span class="p">.</span><span class="n">second</span> <span class="p">})</span> <span class="c1">// for each continent</span> <span class="p">.</span><span class="n">map</span> <span class="p">{</span> <span class="p">(</span><span class="n">continent</span><span class="p">,</span> <span class="n">countriesData</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="c1">// compute tourists from across the continent in 2001</span> <span class="k">val</span> <span class="py">tourists2001</span> <span class="p">=</span> <span class="n">countriesData</span><span class="p">.</span><span class="n">sumBy</span> <span class="p">{</span> <span class="n">it</span><span class="p">.</span><span class="n">visitors</span><span class="p">[</span><span class="m">0</span><span class="p">]</span> <span class="p">}</span> <span class="c1">// compute tourists from across the continent in 2015</span> <span class="k">val</span> <span class="py">tourists2015</span> <span class="p">=</span> <span class="n">countriesData</span><span class="p">.</span><span class="n">sumBy</span> <span class="p">{</span> <span class="n">it</span><span class="p">.</span><span class="n">visitors</span><span class="p">[</span><span class="m">14</span><span class="p">]</span> <span class="p">}</span> <span class="c1">// compute percentage growth</span> <span class="k">val</span> <span class="py">pctGrowth</span> <span class="p">=</span> <span class="p">(</span><span class="n">tourists2015</span> <span class="p">-</span> <span class="n">tourists2001</span><span class="p">)</span> <span class="p">*</span> <span class="m">100</span> <span class="p">/</span> <span class="n">tourists2001</span> <span class="c1">// now we want to find out which country in that continent sent</span> <span class="c1">// the maximum number of tourists</span> <span class="k">val</span> <span class="py">maxCountry</span> <span class="p">=</span> <span class="n">countriesData</span> <span class="c1">// sort data based on 14th element in the list ie. visitors for 2015</span> <span class="p">.</span><span class="n">sortedByDescending</span> <span class="p">{</span> <span class="n">it</span><span class="p">.</span><span class="n">visitors</span><span class="p">[</span><span class="m">14</span><span class="p">]</span> <span class="p">}</span> <span class="c1">// take the first country data and specifically its name attribute</span> <span class="p">.</span><span class="n">first</span><span class="p">().</span><span class="n">name</span> <span class="c1">// construct a pair of continent to result</span> <span class="n">continent</span> <span class="n">to</span> <span class="n">Result</span><span class="p">(</span><span class="n">tourists2015</span><span class="p">,</span> <span class="n">pctGrowth</span><span class="p">,</span> <span class="n">maxCountry</span><span class="p">)</span> <span class="p">}</span> <span class="c1">// sort the continent result pairs based on the percentage growth</span> <span class="p">.</span><span class="n">sortedByDescending</span> <span class="p">{</span> <span class="n">it</span><span class="p">.</span><span class="n">second</span><span class="p">.</span><span class="n">pctGrowth</span> <span class="p">}</span> <span class="c1">// display the results</span> <span class="p">.</span><span class="n">forEach</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="n">it</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> </pre></div>Exercises in Kotlin: Part 5 - Classes2016-04-29T08:20:00+05:302016-04-29T08:20:00+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2016-04-29:/2016/04/exercises-in-kotlin-part-5-classes/<p>After the last post <a href="http://dnblog.ddesk/2016/04/exercises-in-kotlin-part-4-control-flows-and-return/">Exercises in Kotlin: Part 4 - Control flows and return</a>, we shall be exploring classes and objects in the exercises in this post. While retaining many similarities to Java classes, there are actually significant differences in syntax as well and these help classes become a little easier to work with in Kotlin. And save a lot of unnecessary boilerplate code.</p> <h2>Minimalist Syntax</h2> <p>Probably the minimalistic (and for all practical purposes useless) declaration of a class in Kotlin will be</p> <div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Foo</span> </pre></div> <h2>A typical class</h2> <p>Thats it. No constructor, no fields, no body. However as is obvious this is not particularly useful. Let us look at a more useful one.</p> <div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Account</span> <span class="p">(</span><span class="k">val</span> <span class="py">number</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="k">var</span> <span class="py">balance</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="k">fun</span> <span class="nf">deposit</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="n">balance</span> <span class="p">+=</span> <span class="n">amount</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">withdraw</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">balance</span> <span class="p">&lt;</span> <span class="n">amount</span><span class="p">)</span> <span class="p">{</span> <span class="k">throw</span> <span class="n">Exception</span><span class="p">(</span><span class="s">&quot;Insufficient funds in the account&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="n">balance</span> <span class="p">-=</span> <span class="n">amount</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>Here <code>Account</code> is the class name. It has two properties <code>number</code> which is a <code>String</code> and <code>balance</code> which is a <code>BigDecimal</code>. Another aspect is <code>number</code> is a read-only property ie. you can't change the value within the body of the class (say in a method) after one is provided at the construction time. As mentioned in an earlier post read-only properties (aka <code>final</code> in Java terms) are identified by the keyword <code>val</code>, while read-write properties are identified by <code>var</code>. Notice the succinct constructor. The properties of the class that are required by the primary constructor are specified immediately after the class name within parenthesis. You can have variables declared in the constructor that are neither <code>val</code>s nor <code>var</code>s. However they can be used only during construction time and do not become properties of the class (more about that later). Thus unlike Java you don't need to write constructors just in order copy values from the constructor parameter to a member field. Also unlike Java you do not need to write getter/ setter methods in most cases (though you could if you had a specific reason to write custom getter/setters).</p> <p>Client code for the account class could perhaps use it as follows. Do note that the object is instantiated using the <code>ClassName(parameter_list)</code> construct. It is essentially similar to Java except that there is no <code>new</code> keyword preceding it.</p> <div class="highlight"><pre><span></span> <span class="c1">// Instantiate object</span> <span class="k">val</span> <span class="py">acc1</span> <span class="p">=</span> <span class="n">Account</span><span class="p">(</span><span class="s">&quot;123&quot;</span><span class="p">,</span> <span class="n">BigDecimal</span><span class="p">(</span><span class="s">&quot;456.70&quot;</span><span class="p">))</span> <span class="c1">// get value of a property and print it</span> <span class="n">println</span><span class="p">(</span><span class="n">acc1</span><span class="p">.</span><span class="n">number</span><span class="p">)</span> <span class="c1">// invoke a method</span> <span class="n">acc1</span><span class="p">.</span><span class="n">deposit</span><span class="p">(</span><span class="n">BigDecimal</span><span class="p">(</span><span class="s">&quot;234.56&quot;</span><span class="p">))</span> <span class="c1">// change the value of a property</span> <span class="n">acc1</span><span class="p">.</span><span class="n">balance</span> <span class="p">=</span> <span class="n">BigDecimal</span><span class="p">(</span><span class="s">&quot;567.89&quot;</span><span class="p">)</span> </pre></div> <h2>Member Visibility</h2> <p>Now <code>balance</code> needs to be a <code>var</code> since it changes. But some might be disappointed at how it can be modified from outside the class. One solution would be to make it a <code>private var</code> as follows</p> <div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Account</span> <span class="p">(</span><span class="k">val</span> <span class="py">number</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="k">private</span> <span class="k">var</span> <span class="py">balance</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="k">fun</span> <span class="nf">deposit</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="n">balance</span> <span class="p">+=</span> <span class="n">amount</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">withdraw</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">balance</span> <span class="p">&lt;</span> <span class="n">amount</span><span class="p">)</span> <span class="p">{</span> <span class="k">throw</span> <span class="n">Exception</span><span class="p">(</span><span class="s">&quot;Insufficient funds in the account&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="n">balance</span> <span class="p">-=</span> <span class="n">amount</span> <span class="p">}</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">somewhereElse</span><span class="p">()</span> <span class="p">{</span> <span class="k">val</span> <span class="py">acc1</span> <span class="p">=</span> <span class="n">Account</span><span class="p">(</span><span class="s">&quot;123&quot;</span><span class="p">,</span> <span class="n">BigDecimal</span><span class="p">(</span><span class="s">&quot;456.70&quot;</span><span class="p">))</span> <span class="c1">// The following wouldn&#39;t compile</span> <span class="n">acc1</span><span class="p">.</span><span class="n">balance</span> <span class="p">=</span> <span class="n">BigDecimal</span><span class="p">(</span><span class="s">&quot;567.89&quot;</span><span class="p">)</span> <span class="c1">// But neither would the following line</span> <span class="n">println</span><span class="p">(</span><span class="n">acc1</span><span class="p">.</span><span class="n">balance</span><span class="p">)</span> <span class="p">}</span> </pre></div> <p>What if you wanted it to be read-only from outside the class but read-write within the class? You could choose to use the following approach of passing in a parameter to the constructor, using it to declare another property within the body of the class and then declaring that variable to have a private setter. Note that in this case <code>initBalance</code> is simply a parameter passed to the constructor (since it is not declared as either a <code>val</code> or a <code>var</code>), and can be accessed in a read only fashion only during the construction stage. (Thus other methods cannot use this parameter)</p> <div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Account</span> <span class="p">(</span><span class="k">val</span> <span class="py">number</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="n">initBalance</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="k">var</span> <span class="py">balance</span> <span class="p">=</span> <span class="n">initBalance</span> <span class="k">private</span> <span class="k">set</span> <span class="k">fun</span> <span class="nf">deposit</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="n">balance</span> <span class="p">+=</span> <span class="n">amount</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">withdraw</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">balance</span> <span class="p">&lt;</span> <span class="n">amount</span><span class="p">)</span> <span class="p">{</span> <span class="k">throw</span> <span class="n">Exception</span><span class="p">(</span><span class="s">&quot;Insufficient funds in the account&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="n">balance</span> <span class="p">-=</span> <span class="n">amount</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <h2>Multiple constructors</h2> <p>Now let us assume that we wish to allow the constructor to use a String based value for balance though the balance property on the account should continue to be a BigDecimal. You could modify the class as follows.</p> <div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Account</span> <span class="p">(</span><span class="k">val</span> <span class="py">number</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="n">initBalance</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="p">{</span> <span class="k">var</span> <span class="py">balance</span> <span class="p">=</span> <span class="n">BigDecimal</span><span class="p">(</span><span class="n">initBalance</span><span class="p">)</span> <span class="k">private</span> <span class="k">set</span> <span class="k">fun</span> <span class="nf">deposit</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="n">balance</span> <span class="p">+=</span> <span class="n">amount</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">withdraw</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">balance</span> <span class="p">&lt;</span> <span class="n">amount</span><span class="p">)</span> <span class="p">{</span> <span class="k">throw</span> <span class="n">Exception</span><span class="p">(</span><span class="s">&quot;Insufficient funds in the account&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="n">balance</span> <span class="p">-=</span> <span class="n">amount</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>Here <code>balance</code> is being initialised by constructing a <code>BigDecimal</code> using the <code>String</code> value. But we know if the value passed in is not a number, a <code>NumberFormatException</code> will be thrown. But we can't see that in the constructor declaration anywhere. Huh?</p> <p>I won't dwell long on that, but note that Kotlin (unlike Java) does <em>NOT</em> use checked exceptions. Any method could throw any exception at any point in time and it does not have to be documented as a part of the method signature. A fuller discussion of this is beyond the scope of this post, but it should be stated that the conventional wisdom tends towards preferring unchecked exceptions and kotlin supports that style of programming.</p> <p>Now let us assume we wish to have both types of constructors, one taking the initial balance as a <code>String</code> and another as a BigDecimal. Each class can have one primary and zero or more secondary constructors, each one of them eventually calling the primary constructor via the keyword <code>this</code>. Note the parameter lists to secondary constructors cannot declare properties using <code>val</code> or <code>var</code> since these are anyways in turn declared by the primary constructor.</p> <div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Account</span> <span class="p">(</span><span class="k">val</span> <span class="py">number</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="n">initBalance</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="k">constructor</span><span class="p">(</span><span class="n">number</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="n">initBalance</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="p">:</span> <span class="k">this</span><span class="p">(</span><span class="n">number</span><span class="p">,</span> <span class="n">BigDecimal</span><span class="p">(</span><span class="n">initBalance</span><span class="p">))</span> <span class="k">var</span> <span class="py">balance</span> <span class="p">=</span> <span class="n">initBalance</span> <span class="k">private</span> <span class="k">set</span> <span class="k">fun</span> <span class="nf">deposit</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="n">balance</span> <span class="p">+=</span> <span class="n">amount</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">withdraw</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">balance</span> <span class="p">&lt;</span> <span class="n">amount</span><span class="p">)</span> <span class="p">{</span> <span class="k">throw</span> <span class="n">Exception</span><span class="p">(</span><span class="s">&quot;Insufficient funds in the account&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="n">balance</span> <span class="p">-=</span> <span class="n">amount</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>So far our constructors have been only initialising properties. But what if you wanted them to do more. I have shown the way to do so by adding additional <code>println()</code> statements during both the primary and secondary construction phases as below.</p> <div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Account</span> <span class="p">(</span><span class="k">val</span> <span class="py">number</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="n">initBalance</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="n">init</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;Into primary constructor&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="k">constructor</span><span class="p">(</span><span class="n">number</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="n">initBalance</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="p">:</span> <span class="k">this</span><span class="p">(</span><span class="n">number</span><span class="p">,</span> <span class="n">BigDecimal</span><span class="p">(</span><span class="n">initBalance</span><span class="p">))</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;Into secondary constructor&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="k">var</span> <span class="py">balance</span> <span class="p">=</span> <span class="n">initBalance</span> <span class="k">private</span> <span class="k">set</span> <span class="k">fun</span> <span class="nf">deposit</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="n">balance</span> <span class="p">+=</span> <span class="n">amount</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">withdraw</span><span class="p">(</span><span class="n">amount</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">balance</span> <span class="p">&lt;</span> <span class="n">amount</span><span class="p">)</span> <span class="p">{</span> <span class="k">throw</span> <span class="n">Exception</span><span class="p">(</span><span class="s">&quot;Insufficient funds in the account&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="n">balance</span> <span class="p">-=</span> <span class="n">amount</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>As can be seen the non property initialisation of primary constructor can be done in the <code>init{...}</code> block. Similarly secondary constructors can also include a block at the end which lists out the necessary statements to be performed.</p> <h2>Data Classes</h2> <p>Very often we need basic classes which are primarily place holders for a collection of different values. Though java does not have an equivalent, in C, you would have called it a <code>struct</code> and in scala you would call it a <code>case class</code>. In Kotlin we have <code>data class</code>. eg.</p> <div class="highlight"><pre><span></span><span class="k">data</span> <span class="k">class</span> <span class="nc">Point</span><span class="p">(</span><span class="k">val</span> <span class="py">x</span><span class="p">:</span> <span class="n">Double</span><span class="p">,</span> <span class="k">val</span> <span class="py">y</span><span class="p">:</span> <span class="n">Double</span><span class="p">)</span> </pre></div> <p>These classes are tailored for using them as collection of other properties. </p> <ul> <li>A data class automatically generates a sensible <code>equals()/hashcode()</code> functions for the class based on the properties declared in the primary constructor. </li> <li>A sensible <code>toString()</code> is automatically provided</li> <li>Another helper method <code>copy()</code> to selectively override some property values when creating a copy is also automatically provided. </li> <li>You can also use a data class similar to a record of values easily since it also generates <code>component1()</code>, <code>component2()</code>, ... <code>componentN()</code> functions which can treat the class as a record of sequence of properties (in the same order as declared in the primary constructor). </li> </ul> <p>Lets take a usage at a sample usage of the class above</p> <div class="highlight"><pre><span></span> <span class="k">val</span> <span class="py">p1</span> <span class="p">=</span> <span class="n">Point</span><span class="p">(</span><span class="m">1.1</span><span class="p">,</span><span class="m">2.2</span><span class="p">)</span> <span class="n">println</span><span class="p">(</span><span class="n">p1</span><span class="p">)</span> <span class="n">println</span><span class="p">(</span><span class="n">p1</span><span class="p">.</span><span class="n">x</span><span class="p">)</span> <span class="n">println</span><span class="p">(</span><span class="n">p1</span><span class="p">.</span><span class="n">component1</span><span class="p">())</span> <span class="k">val</span> <span class="py">p2</span> <span class="p">=</span> <span class="n">p1</span><span class="p">.</span><span class="n">copy</span><span class="p">(</span><span class="n">x</span><span class="p">=</span><span class="m">3.3</span><span class="p">)</span> <span class="n">println</span><span class="p">(</span><span class="n">p2</span><span class="p">)</span> <span class="k">val</span> <span class="err">(</span><span class="py">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span> <span class="p">=</span> <span class="n">p1</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;&quot;</span> <span class="p">+</span> <span class="n">x</span> <span class="p">+</span> <span class="s">&quot;:&quot;</span> <span class="p">+</span> <span class="n">y</span><span class="p">)</span> </pre></div> <p>The output for the above will be as follows</p> <div class="highlight"><pre><span></span>Point(x=1.1, y=2.2) 1.1 1.1 Point(x=3.3, y=2.2) 1.1:2.2 </pre></div> <p>Note, how a second copy (<code>p2</code>) was easily created from the first while allowing the ability to override a few values. Also how the <code>point</code> object can be destructured into its component values, in this case <code>val</code>s <code>x</code> and <code>y</code>. If the <code>Point</code> class had 3 properties, we would have had to provide 3 variables on the left hand side of the assignment operator for it to succeed</p> <h2>Enum classes</h2> <p>Let us go back to the Account class example. Let us assume we wanted to add an account type member. Account type can either be <code>Savings</code> or <code>Current</code> (aka Checking). In order to allow the capability we will declare an enum class</p> <div class="highlight"><pre><span></span><span class="k">enum</span> <span class="k">class</span> <span class="nc">AccountType</span> <span class="p">{</span> <span class="n">Savings</span><span class="p">,</span> <span class="n">Current</span> <span class="p">}</span> </pre></div> <p>Now we can go ahead and modify the Account class as follows</p> <div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Account</span> <span class="p">(</span><span class="k">val</span> <span class="py">number</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="n">initBalance</span><span class="p">:</span> <span class="n">BigDecimal</span><span class="p">,</span> <span class="k">val</span> <span class="py">type</span><span class="p">:</span> <span class="n">AccountType</span><span class="p">){</span> <span class="p">...</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">somewhereElse</span><span class="p">()</span> <span class="p">{</span> <span class="k">val</span> <span class="py">acc1</span> <span class="p">=</span> <span class="n">Account</span><span class="p">(</span><span class="s">&quot;123&quot;</span><span class="p">,</span> <span class="n">BigDecimal</span><span class="p">(</span><span class="s">&quot;456.7&quot;</span><span class="p">),</span><span class="n">AccountType</span><span class="p">.</span><span class="n">Savings</span><span class="p">)</span> <span class="n">println</span><span class="p">(</span><span class="n">acc1</span><span class="p">.</span><span class="n">type</span><span class="p">)</span> <span class="p">}</span> </pre></div> <p>Now let us take a look at some of the sample usages of the enum class</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">somewhereElse</span><span class="p">()</span> <span class="p">{</span> <span class="c1">// Iterate through all the enums</span> <span class="k">for</span><span class="p">(</span> <span class="n">accType</span> <span class="k">in</span> <span class="n">AccountType</span><span class="p">.</span><span class="n">values</span><span class="p">())</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="n">accType</span><span class="p">)</span> <span class="p">}</span> <span class="c1">// Every enum instance has two properties: name and ordinal</span> <span class="n">println</span><span class="p">(</span><span class="n">AccountType</span><span class="p">.</span><span class="n">Savings</span><span class="p">.</span><span class="n">name</span><span class="p">)</span> <span class="n">println</span><span class="p">(</span><span class="n">AccountType</span><span class="p">.</span><span class="n">Current</span><span class="p">.</span><span class="n">ordinal</span><span class="p">)</span> <span class="c1">// You can recreate an enum instance based on a String</span> <span class="n">println</span><span class="p">(</span><span class="n">AccountType</span><span class="p">.</span><span class="n">valueOf</span><span class="p">(</span><span class="s">&quot;Savings&quot;</span><span class="p">))</span> <span class="p">}</span> </pre></div> <p>This program will result in the following output</p> <div class="highlight"><pre><span></span>Savings Current Savings 1 Savings </pre></div> <p>Thus as you can see, <em> You could iterate through all the enum types by getting an array of all its possible types using </em> Every enum instance has two properties, a <code>name</code> which is a string and an <code>ordinal</code> which allows the enum values to be used a set of ordered values and allows them to be comparable. The name of course is the name as was provided in the declaration and ordinal is the sequence in the declaration of values. * You can use the string value of the enum to obtain an enum instance using <code>valueOf()</code></p> <p>Finally you can provide additional properties to each of the enum instances as follows</p> <div class="highlight"><pre><span></span><span class="k">enum</span> <span class="k">class</span> <span class="nc">Color</span><span class="p">(</span><span class="n">r</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">g</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">{</span> <span class="n">Red</span><span class="p">(</span><span class="m">255</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">),</span> <span class="n">Green</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">255</span><span class="p">,</span><span class="m">0</span><span class="p">),</span> <span class="n">Blue</span><span class="p">(</span><span class="m">0</span><span class="p">,</span><span class="m">0</span><span class="p">,</span><span class="m">255</span><span class="p">)</span> <span class="p">}</span> </pre></div> <p>We have looked at the basics of classes in this post. We will look at inheritance next.</p>Exercises in Kotlin: Part 4 - Control flows and return2016-04-27T21:20:00+05:302016-04-27T21:20:00+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2016-04-27:/2016/04/exercises-in-kotlin-part-4-control-flows-and-return/<p>After <a href="/2016/04/exercises-in-kotlin-part-3-functions/">Exercises in Kotlin: Part 3 - Functions</a> we now take a look at control flows and actually doing some basic exercises.</p> <h2>if/else</h2> <p>As mentioned earlier, <code>if/else</code> is not just a statement but can also be used as an operator (in lieu of the ternary <code>? :</code> operator.</p> <p>As an example, a familiar way to have if/else statements would be as follows.</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">greeting</span><span class="p">(</span><span class="n">hours</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">String</span> <span class="p">{</span> <span class="k">var</span> <span class="py">greeting</span><span class="p">:</span> <span class="n">String</span> <span class="p">=</span> <span class="k">null</span> <span class="k">if</span> <span class="p">(</span><span class="n">hours</span> <span class="p">&lt;</span> <span class="m">12</span><span class="p">)</span> <span class="p">{</span> <span class="n">greeting</span> <span class="p">=</span> <span class="s">&quot;Good Morning&quot;</span> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">hours</span> <span class="p">&lt;</span> <span class="m">16</span><span class="p">)</span> <span class="p">{</span> <span class="n">greeting</span> <span class="p">=</span> <span class="s">&quot;Good Afternoon&quot;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="n">greeting</span> <span class="p">=</span> <span class="s">&quot;Good Night&quot;</span> <span class="p">}</span> <span class="k">return</span> <span class="n">greeting</span> <span class="p">}</span> </pre></div> <p>But since if/else can be an expression as well, another way to write the function above would be</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">greeting2</span><span class="p">(</span><span class="n">hours</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">String</span> <span class="p">=</span> <span class="s">&quot;Good &quot;</span> <span class="p">+</span> <span class="k">if</span> <span class="p">(</span><span class="n">hours</span> <span class="p">&lt;</span> <span class="m">12</span><span class="p">)</span> <span class="p">{</span> <span class="s">&quot;Morning&quot;</span> <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">hours</span> <span class="p">&lt;</span> <span class="m">16</span><span class="p">)</span> <span class="p">{</span> <span class="s">&quot;Afternoon&quot;</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="s">&quot;Night&quot;</span> <span class="p">}</span> </pre></div> <h2>when</h2> <p>There is another expression that can be used instead of multiple nested if/else expressions or situations where you might use a switch/case block in Java. Using <code>when</code> the function above could be written as</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">greeting3</span><span class="p">(</span><span class="n">hours</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">String</span> <span class="p">=</span> <span class="s">&quot;Good &quot;</span> <span class="p">+</span> <span class="k">when</span> <span class="p">{</span> <span class="n">hours</span> <span class="p">&lt;</span> <span class="m">12</span> <span class="p">-&gt;</span> <span class="s">&quot;Morning&quot;</span> <span class="n">hours</span> <span class="p">&lt;</span> <span class="m">16</span> <span class="p">-&gt;</span> <span class="s">&quot;Afternoon&quot;</span> <span class="k">else</span> <span class="p">-&gt;</span> <span class="s">&quot;Night&quot;</span> <span class="p">}</span> </pre></div> <p>Note that <code>when</code> is an expression and evaluates to a value (in the case above it would be "Morning", "Afternoon" or "Night". You could also use it more conventionally just to write other blocks of code eg.</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">greeting4</span><span class="p">(</span><span class="n">hours</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Unit</span> <span class="p">{</span> <span class="k">when</span> <span class="p">{</span> <span class="n">hours</span> <span class="p">&lt;</span> <span class="m">12</span> <span class="p">-&gt;</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;Morning&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="n">hours</span> <span class="p">&lt;</span> <span class="m">16</span> <span class="p">-&gt;</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;Afternoon&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="k">else</span> <span class="p">-&gt;</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;Night&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>As you can note, there is no break statement (unlike switch case), and the right hand side of the conditions can be blocks which consist of multiple statements. If used as a part of a when expression, the value returned by a block is the one returned by the last expression in the block. </p> <p>When used as an expression, the compiler expects the conditions to be exhaustive and thus many times you might need to add an else block. However the else block is not required when used as a statement. </p> <p>There are many other things you can do including checking the actual instance or just the value as shown in the rather arbitrary example below</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">anotherWhen</span><span class="p">(</span><span class="n">any</span><span class="p">:</span> <span class="n">Any</span><span class="p">):</span> <span class="n">Any</span> <span class="p">=</span> <span class="k">when</span><span class="p">(</span><span class="n">any</span><span class="p">)</span> <span class="p">{</span> <span class="p">(</span><span class="n">any</span> <span class="k">is</span> <span class="n">Int</span> <span class="p">&amp;&amp;</span> <span class="n">any</span> <span class="p">&lt;</span> <span class="m">10000</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="s">&quot;Small Number&quot;</span> <span class="k">is</span> <span class="n">Int</span> <span class="p">-&gt;</span> <span class="s">&quot;Big Number&quot;</span> <span class="k">is</span> <span class="n">String</span> <span class="p">-&gt;</span> <span class="s">&quot;A string&quot;</span> <span class="k">else</span> <span class="p">-&gt;</span> <span class="s">&quot;Its just another Any&quot;</span> <span class="p">}</span> </pre></div> <p>You might note that in the first condition, the right hand side of the condition ie. (any &lt; 10000) actually compiles, because the compiler is able to figure out that any will be an Int if reaches the comparison with 10000 and allows it to be used as an Int. This smart casting is actually quite nice and helps save on verbose typecast statements (or for that matter explicit typecasts) when used in when expressions as above.</p> <h2>while and do-while</h2> <p>The familiar constructs of <code>while</code> and <code>do while</code> are also available. eg.</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">countDown</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">{</span> <span class="k">var</span> <span class="py">counter</span> <span class="p">=</span> <span class="n">n</span> <span class="k">while</span><span class="p">(</span><span class="n">counter</span> <span class="p">&gt;=</span> <span class="m">0</span><span class="p">)</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="n">counter</span><span class="p">)</span> <span class="n">counter</span><span class="p">--</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>Note that in kotlin the while block will not allow you to perform an assignment within the while expression unlike java code. eg. <code>while((n = next()) &gt; 0)</code> will not compile. In some of these situations you might actually find the <code>do while</code> construct useful. eg. the val n declared within the do while block remains in scope while evaluating the while expression.</p> <div class="highlight"><pre><span></span><span class="k">do</span> <span class="p">{</span> <span class="k">val</span> <span class="py">n</span> <span class="p">=</span> <span class="n">next</span><span class="p">()</span> <span class="c1">// do something</span> <span class="p">}</span> <span class="k">while</span><span class="p">(</span><span class="n">n</span> <span class="p">&gt;</span> <span class="m">0</span><span class="p">)</span> </pre></div> <h2>for loop</h2> <p>The for loop is much nicer and often more helpful than in Java. eg. to print each character in a string you could write</p> <div class="highlight"><pre><span></span><span class="k">for</span><span class="p">(</span><span class="n">c</span> <span class="k">in</span> <span class="s">&quot;String&quot;</span><span class="p">)</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="n">c</span><span class="p">)</span> <span class="p">}</span> </pre></div> <p>One of the useful ability of for loop is to iterate over a range which can be written as <code>startValue..endValue</code> eg</p> <div class="highlight"><pre><span></span><span class="c1">// will print 100, 101, 102... 110</span> <span class="k">for</span> <span class="p">(</span><span class="n">n</span> <span class="k">in</span> <span class="m">100.</span><span class="p">.</span><span class="m">110</span><span class="p">)</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="p">}</span> </pre></div> <p>There are many other aspects of the for loop which are better covered after some other topics, so other sample usages will be taken up later.</p> <h2>break and continue</h2> <p><code>break</code> and <code>continue</code> are also available and continue to have the same meaning as in java. For more advanced uses such as breaking to a or returning to a label, see <a href="https://kotlinlang.org/docs/reference/returns.html">Returns and jumps</a> from the kotlin reference guide.</p> <h2>Exercise - Counting Sundays</h2> <p>Here's an exercise for you to try out. (Note: the solution follows immediately after, so you might wish to avoid reading it if you want to attempt to do the exercise yourself first).</p> <p>This one comes from <a href="">Project Euler</a> Exercise #19</p> <p>The problem is stated as follows</p> <div class="highlight"><pre><span></span>You are given the following information, but you may prefer to do some research for yourself. 1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? </pre></div> <p>One of the possible solutions to this problem is described below</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">sundaysInTwentiethCentury</span><span class="p">():</span> <span class="n">Int</span> <span class="p">{</span> <span class="k">fun</span> <span class="nf">daysInMonth</span><span class="p">(</span><span class="n">month</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">year</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">=</span> <span class="k">when</span><span class="p">(</span><span class="n">month</span><span class="p">)</span> <span class="p">{</span> <span class="m">1</span><span class="p">,</span><span class="m">3</span><span class="p">,</span><span class="m">5</span><span class="p">,</span><span class="m">7</span><span class="p">,</span><span class="m">8</span><span class="p">,</span><span class="m">10</span><span class="p">,</span><span class="m">12</span> <span class="p">-&gt;</span> <span class="m">31</span> <span class="m">4</span><span class="p">,</span><span class="m">6</span><span class="p">,</span><span class="m">9</span><span class="p">,</span><span class="m">11</span> <span class="p">-&gt;</span> <span class="m">30</span> <span class="m">2</span> <span class="p">-&gt;</span> <span class="k">when</span> <span class="p">{</span> <span class="n">year</span> <span class="p">%</span> <span class="m">400</span> <span class="p">==</span> <span class="m">0</span> <span class="p">-&gt;</span> <span class="m">29</span> <span class="n">year</span> <span class="p">%</span> <span class="m">100</span> <span class="p">==</span> <span class="m">0</span> <span class="p">-&gt;</span> <span class="m">28</span> <span class="n">year</span> <span class="p">%</span> <span class="m">4</span> <span class="p">==</span> <span class="m">0</span> <span class="p">-&gt;</span> <span class="m">29</span> <span class="k">else</span> <span class="p">-&gt;</span> <span class="m">28</span> <span class="p">}</span> <span class="k">else</span> <span class="p">-&gt;</span> <span class="k">throw</span> <span class="n">Exception</span><span class="p">(</span><span class="s">&quot;Invalid Month ${month}&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">daysInYear</span><span class="p">(</span><span class="n">year</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Int</span> <span class="p">{</span> <span class="k">var</span> <span class="py">days</span> <span class="p">=</span> <span class="m">0</span> <span class="k">for</span><span class="p">(</span><span class="n">month</span> <span class="k">in</span> <span class="m">1.</span><span class="p">.</span><span class="m">12</span><span class="p">)</span> <span class="p">{</span> <span class="n">days</span> <span class="p">+=</span> <span class="n">daysInMonth</span><span class="p">(</span><span class="n">month</span><span class="p">,</span> <span class="n">year</span><span class="p">)</span> <span class="p">}</span> <span class="k">return</span> <span class="n">days</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">dayOfWeekAfter</span><span class="p">(</span><span class="n">currentDow</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">numberOfDays</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">=</span> <span class="p">((</span><span class="n">currentDow</span><span class="p">-</span><span class="m">1</span><span class="p">)</span> <span class="p">+</span> <span class="n">numberOfDays</span><span class="p">)</span> <span class="p">%</span> <span class="m">7</span> <span class="p">+</span> <span class="m">1</span> <span class="k">var</span> <span class="py">weekDayOnFirstOfMonth</span> <span class="p">=</span> <span class="n">dayOfWeekAfter</span><span class="p">(</span><span class="m">2</span><span class="p">,</span> <span class="n">daysInYear</span><span class="p">(</span><span class="m">1900</span><span class="p">))</span> <span class="k">var</span> <span class="py">totalSundays</span> <span class="p">=</span> <span class="m">0</span> <span class="k">for</span> <span class="p">(</span><span class="n">year</span> <span class="k">in</span> <span class="m">1901.</span><span class="p">.</span><span class="m">2000</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span><span class="p">(</span><span class="n">month</span> <span class="k">in</span> <span class="m">1.</span><span class="p">.</span><span class="m">12</span><span class="p">)</span> <span class="p">{</span> <span class="k">if</span> <span class="p">(</span><span class="n">weekDayOnFirstOfMonth</span> <span class="p">==</span> <span class="m">1</span><span class="p">)</span> <span class="n">totalSundays</span><span class="p">++</span> <span class="n">weekDayOnFirstOfMonth</span> <span class="p">=</span> <span class="n">dayOfWeekAfter</span><span class="p">(</span><span class="n">weekDayOnFirstOfMonth</span><span class="p">,</span> <span class="n">daysInMonth</span><span class="p">(</span><span class="n">month</span><span class="p">,</span><span class="n">year</span><span class="p">))</span> <span class="p">}</span> <span class="p">}</span> <span class="k">return</span> <span class="n">totalSundays</span> <span class="p">}</span> </pre></div> <p>We shall explore classes in the next post <a href="/2016/04/exercises-in-kotlin-part-5-classes/">Exercises in Kotlin: Part 5 - Classes</a></p>Exercises in Kotlin: Part 3 - Functions2016-04-20T08:00:00+05:302016-04-20T08:00:00+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2016-04-20:/2016/04/exercises-in-kotlin-part-3-functions/<p>We looked at a bit of syntax and variable declarations in the last post <a href="/2016/04/exercises-in-kotlin-part-2-high-level-syntax-and-variables/">Exercises in Kotlin: Part 2 - High level syntax and Variables</a>. We explore functions in this post. Here's how a typical function in Kotlin would look like.</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">add</span><span class="p">(</span><span class="n">first</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">second</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Int</span> <span class="p">{</span> <span class="c1">// will not compile</span> <span class="c1">// first = first + 2. Because first is a val and cannot be reassigned</span> <span class="k">return</span> <span class="n">first</span> <span class="p">+</span> <span class="n">second</span> <span class="p">}</span> </pre></div> <p>As noted earlier, the arguments are declared with the names followed by the types. Also even though <code>val</code> is not specified, each argument is automatically a <code>val</code> and can not be reassigned within the function. Also the return type is specified after the argument list followed by a colon.</p> <p>In this case, since the body of the function is an expression it can also alternatively be specified as </p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">addExpr</span><span class="p">(</span><span class="n">first</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">second</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">=</span> <span class="n">first</span> <span class="p">+</span> <span class="n">second</span> </pre></div> <p>Note in particular that the curly brackets are replaced by <code>=</code>. And because the compiler can infer the return type, it is no longer required to provide it (though it would work just as well if you did provide one)</p> <h3>Functions can have default arguments</h3> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">powerOf</span><span class="p">(</span><span class="n">value</span><span class="p">:</span> <span class="n">Double</span><span class="p">,</span> <span class="n">power</span><span class="p">:</span> <span class="n">Double</span> <span class="p">=</span> <span class="m">2.0</span><span class="p">)</span> <span class="p">=</span> <span class="n">Math</span><span class="p">.</span><span class="n">pow</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">power</span><span class="p">)</span> </pre></div> <p>The second argument <code>power</code> is no longer required to be specified, (when not specified its value will default to 2.0. Thus allowing both the following statements</p> <div class="highlight"><pre><span></span> <span class="n">powerOf</span><span class="p">(</span><span class="m">3.0</span><span class="p">)</span> <span class="c1">// This will return 9.0</span> <span class="n">powerOf</span><span class="p">(</span><span class="m">3.0</span><span class="p">,</span> <span class="m">4.0</span><span class="p">)</span> <span class="c1">// This will return 81.0</span> </pre></div> <p>This makes it convenient to require fewer overloaded functions. The order of the arguments needs to be retained, and all the arguments after the ones specified will need to have default values. While this makes a lot of defaulting easy, it still is a little problematic for selectively overriding defaults for only few of the arguments. This is helped by the next feature.</p> <h3>Arguments can also be named</h3> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">contrived</span><span class="p">(</span><span class="n">first</span><span class="p">:</span> <span class="n">String</span> <span class="p">=</span> <span class="s">&quot;First&quot;</span><span class="p">,</span> <span class="n">second</span><span class="p">:</span> <span class="n">String</span> <span class="p">=</span> <span class="s">&quot;Second&quot;</span><span class="p">,</span> <span class="n">third</span><span class="p">:</span> <span class="n">String</span> <span class="p">=</span> <span class="s">&quot;Third&quot;</span><span class="p">,</span> <span class="n">fourth</span><span class="p">:</span> <span class="n">String</span> <span class="p">=</span> <span class="s">&quot;Fourth&quot;</span><span class="p">,</span> <span class="n">fifth</span><span class="p">:</span> <span class="n">String</span> <span class="p">=</span> <span class="s">&quot;Fifth&quot;</span><span class="p">)</span> <span class="p">=</span> <span class="n">first</span> <span class="p">+</span> <span class="s">&quot; &quot;</span> <span class="p">+</span> <span class="n">second</span> <span class="p">+</span> <span class="s">&quot; &quot;</span> <span class="p">+</span> <span class="n">third</span> <span class="p">+</span> <span class="s">&quot; &quot;</span> <span class="p">+</span> <span class="n">fourth</span> <span class="p">+</span> <span class="s">&quot; &quot;</span> <span class="p">+</span> <span class="n">fifth</span> <span class="c1">// ....</span> <span class="k">fun</span> <span class="nf">someOtherFunction</span><span class="p">()</span> <span class="p">{</span> <span class="n">contrived</span><span class="p">(</span><span class="n">second</span><span class="p">=</span><span class="s">&quot;2nd&quot;</span><span class="p">,</span> <span class="n">fourth</span><span class="p">=</span><span class="s">&quot;4th&quot;</span><span class="p">)</span> <span class="c1">// the above returns &quot;First 2nd Third 4th Fifth&quot;</span> <span class="p">}</span> </pre></div> <p>As shown in the example above using a combination of default and named arguments we can choose to have smaller function invocations by specifying only the non defaults. Thus only the arguments whose default values need to be overridden, making the code both terse and more readable</p> <h3>Functions can have variable arguments</h3> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">showStrings</span><span class="p">(</span><span class="n">suffix</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="k">vararg</span> <span class="n">items</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="p">{</span> <span class="k">for</span><span class="p">(</span><span class="n">item</span> <span class="k">in</span> <span class="n">items</span><span class="p">)</span> <span class="p">{</span> <span class="n">print</span><span class="p">(</span><span class="n">item</span> <span class="p">+</span> <span class="s">&quot; &quot;</span><span class="p">)</span> <span class="p">}</span> <span class="n">print</span><span class="p">(</span><span class="n">suffix</span><span class="p">)</span> <span class="n">println</span><span class="p">()</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">somewhereElse</span><span class="p">()</span> <span class="p">{</span> <span class="n">showStrings</span><span class="p">(</span><span class="s">&quot;!&quot;</span><span class="p">,</span> <span class="s">&quot;Hello&quot;</span><span class="p">,</span> <span class="s">&quot;World&quot;</span><span class="p">)</span> <span class="c1">// prints &quot;Hello World !&quot;</span> <span class="p">}</span> </pre></div> <p>Function can have variable number of arguments so long as all of them are of the same type. The type of the declared argument then becomes an array of that type. Thus in the above example, <code>items</code> is an array of String or <code>Array&lt;String&gt;</code>.</p> <p>The above function also introduces you to a for statement when used with an array. The syntax is very simple and natural, <code>for itemVar in arrayVar { ... }</code></p> <p>(<em>I imagine there can be only one <code>vararg</code> type argument and that has to be declared at the end but never attempted to do anything differently</em>)</p> <h3>Functions can have nested functions</h3> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">showStrings2</span><span class="p">(</span><span class="n">suffix</span><span class="p">:</span> <span class="n">String</span><span class="p">,</span> <span class="k">vararg</span> <span class="n">items</span><span class="p">:</span> <span class="n">String</span><span class="p">):</span> <span class="n">String</span> <span class="p">{</span> <span class="k">val</span> <span class="py">sb</span> <span class="p">=</span> <span class="n">StringBuffer</span><span class="p">()</span> <span class="k">fun</span> <span class="nf">appendToBuffer</span><span class="p">(</span><span class="n">item</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="p">{</span> <span class="n">sb</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">item</span><span class="p">)</span> <span class="n">sb</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">)</span> <span class="p">}</span> <span class="k">for</span><span class="p">(</span><span class="n">item</span> <span class="k">in</span> <span class="n">items</span><span class="p">)</span> <span class="p">{</span> <span class="n">appendToBuffer</span><span class="p">(</span><span class="n">item</span><span class="p">)</span> <span class="p">}</span> <span class="n">appendToBuffer</span><span class="p">(</span><span class="n">suffix</span><span class="p">)</span> <span class="k">return</span> <span class="n">sb</span><span class="p">.</span><span class="n">toString</span><span class="p">()</span> <span class="p">}</span> <span class="p">....</span> <span class="k">fun</span> <span class="nf">somewhereElse</span><span class="p">()</span> <span class="p">{</span> <span class="n">showStrings2</span><span class="p">(</span><span class="s">&quot;!&quot;</span><span class="p">,</span> <span class="s">&quot;Hello&quot;</span><span class="p">,</span> <span class="s">&quot;World&quot;</span><span class="p">)</span> <span class="c1">// returns &quot;Hello World !&quot;</span> <span class="p">}</span> </pre></div> <p>As shown above, <code>appendToBuffer</code> is a nested function (called local function) It can access variables in the parent functions namespace (in the above situation - <code>sb</code>.). </p> <h3>Aside: if - else can also be an expression</h3> <p>In java, <code>if</code>-<code>else</code> is a statement. And has a complementary ternary operator <code>? :</code> which is an expression. Kotlin merges both by allowing use of <code>if</code> - <code>else</code> as an expression, and such if/else expressions can be used as a part of the function expression body (or a part of regular block bodies as well). eg.</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">isBlankString</span><span class="p">(</span><span class="n">arg</span><span class="p">:</span> <span class="n">String</span><span class="p">)</span> <span class="p">=</span> <span class="k">if</span> <span class="p">(</span><span class="n">arg</span><span class="p">.</span><span class="n">trim</span><span class="p">()</span> <span class="p">==</span> <span class="s">&quot;&quot;</span><span class="p">)</span> <span class="k">true</span> <span class="k">else</span> <span class="k">false</span> </pre></div> <p>For an if/else to be an expression, both if and else branches have to be specified and followed by an expression each</p> <h3>Functions can be tail recursive.</h3> <p>Yes! They can be. </p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">factorial</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Int</span> <span class="p">{</span> <span class="k">tailrec</span> <span class="k">fun</span> <span class="nf">factorial</span><span class="p">(</span><span class="n">accumulator</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">n</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Int</span> <span class="p">=</span> <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="p">==</span> <span class="m">1</span><span class="p">)</span> <span class="n">accumulator</span> <span class="k">else</span> <span class="n">factorial</span><span class="p">(</span><span class="n">accumulator</span> <span class="p">*</span> <span class="n">n</span><span class="p">,</span> <span class="n">n</span> <span class="p">-</span> <span class="m">1</span><span class="p">)</span> <span class="k">return</span> <span class="n">factorial</span><span class="p">(</span><span class="m">1</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="p">}</span> </pre></div> <p>If a function is written in a form that lends itself to <a href="https://en.wikipedia.org/wiki/Tail_call">tail recursion</a> the compiler generates optimised code that uses a loop based version which no longer risks a stack overflow.</p> <h3>Extension functions.</h3> <p>This is a very interesting and very useful feature to Kotlin. It is the ability to seemingly externally add a method to a class. Thus the function <code>isBlankString</code> introduced above could be rewritten as </p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">String</span><span class="p">.</span><span class="n">isBlankString</span><span class="p">()</span> <span class="p">=</span> <span class="k">this</span><span class="p">.</span><span class="n">trim</span><span class="p">()</span> <span class="p">==</span> <span class="s">&quot;&quot;</span> <span class="k">fun</span> <span class="nf">somewhereElse</span><span class="p">()</span> <span class="p">{</span> <span class="s">&quot; &quot;</span><span class="p">.</span><span class="n">isBlankString</span><span class="p">()</span> <span class="c1">// returns true</span> <span class="p">}</span> </pre></div> <p>Note that in an extension function, you can use <code>this</code> to refer to the receiver of the function (in this case the String object on whom the <code>isBlankString</code> function is called). Also that the receiver class is resolved statically and not dynamically. So if you had an extension function defined for a <code>Base</code> class and a <code>Derived</code> class that inherits from <code>Base</code>, and called it on a variable declared of type <code>Base</code> but actually referencing an instance of type <code>Derived</code> the <code>Base</code> version of the extension function will be called.</p> <h3>Infix functions</h3> <p>Class member functions or extension functions with a single argument, can in turn be declared as infix which allows from syntactic sugar in terms of how that function is invoked. Thus the function</p> <div class="highlight"><pre><span></span><span class="k">infix</span> <span class="k">fun</span> <span class="nf">Int</span><span class="p">.</span><span class="n">toThePowerOf</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">=</span> <span class="n">Math</span><span class="p">.</span><span class="n">pow</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="n">toDouble</span><span class="p">(),</span> <span class="n">n</span><span class="p">.</span><span class="n">toDouble</span><span class="p">()).</span><span class="n">toInt</span><span class="p">()</span> </pre></div> <p>could be invoked using either of the two ways, the latter becoming feasible only because of the keyword <code>infix</code> being used in the function declaration above.</p> <div class="highlight"><pre><span></span> <span class="m">5.</span><span class="n">toThePowerOf</span><span class="p">(</span><span class="m">3</span><span class="p">)</span> <span class="m">5</span> <span class="n">toThePowerOf</span> <span class="m">3</span> </pre></div> <h3>Higher order functions</h3> <p>Higher order functions are functions which take functions as parameters or as a return type. For that let us first understand how a function can be alternatively represented as a variable using a function type.</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">double</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">=</span> <span class="n">n</span> <span class="p">*</span> <span class="m">2</span> </pre></div> <p>What if we wish to represent the function above as a variable. We need to understand the type of the function. In this case it happens to be of the type which takes a single <code>Int</code> and returns an <code>Int</code>. Knowing that we can now declare it as a variable</p> <div class="highlight"><pre><span></span><span class="k">val</span> <span class="py">doubleAsArg</span> <span class="p">=</span> <span class="p">{</span> <span class="n">n</span> <span class="p">:</span> <span class="n">Int</span> <span class="p">-&gt;</span> <span class="n">n</span> <span class="p">*</span> <span class="m">2</span><span class="p">}</span> </pre></div> <p>The way to invoke the function above would be similar to the way double is invoked</p> <div class="highlight"><pre><span></span><span class="n">doubleAsArg</span><span class="p">(</span><span class="m">5</span><span class="p">)</span> <span class="c1">// would return 10</span> </pre></div> <p>But the more interesting capability that is now possible is that you can write a function which takes another function as an argument, and in turn pass doubleAsArg as an argument to the newly written function. eg.</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">twice</span><span class="p">(</span><span class="n">function</span><span class="p">:</span> <span class="p">(</span><span class="n">Int</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="n">Int</span><span class="p">,</span> <span class="n">arg</span><span class="p">:</span> <span class="n">Int</span><span class="p">)</span> <span class="p">=</span> <span class="n">function</span><span class="p">(</span><span class="n">function</span><span class="p">(</span><span class="n">arg</span><span class="p">))</span> <span class="k">fun</span> <span class="nf">somewhereElse</span><span class="p">()</span> <span class="p">{</span> <span class="n">twice</span><span class="p">(</span><span class="n">doubleAsArg</span><span class="p">,</span><span class="m">5</span><span class="p">)</span> <span class="c1">// will return 20</span> <span class="p">}</span> </pre></div> <p><code>doubleAsArg</code> could have been also declared using an alternative syntax as follows. For all practical purposes they are identical. However the declaration below makes the function type very explicit (it was inferred in the declaration above)</p> <div class="highlight"><pre><span></span><span class="k">val</span> <span class="py">doubleAsArgRedux</span><span class="p">:</span> <span class="p">(</span><span class="n">Int</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="n">Int</span> <span class="p">=</span> <span class="p">{</span> <span class="n">n</span> <span class="p">-&gt;</span> <span class="n">n</span> <span class="p">*</span> <span class="m">2</span><span class="p">}</span> </pre></div> <h4>Aside: Function Types</h4> <p>Just like <code>String</code>, <code>Int</code> etc, since functions are also things that can be passed around, they also have types. As is shown above, doubleAsArg above has the type <code>(Int) -&gt; Int</code>. In general a function taking n arguments each of type Type1..TypeN, and returning a type TypeReturn will have the type <code>(Type1, Type2,..TypeN) -&gt; TypeReturn</code></p> <h3>Lambdas</h3> <p>A lambda function is a function that is not declared upfront but is passed in immediately to another function. In the situation below the function that is passed as the first argument to the function twice is a lambda. A lambda expression is always surrounded by curly braces, its parameters (if any) are declared before -&gt; while the body goes after -&gt; (if required).</p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">somewhereElse</span><span class="p">()</span> <span class="p">{</span> <span class="n">twice</span><span class="p">({</span><span class="n">n</span><span class="p">:</span> <span class="n">Int</span> <span class="p">-&gt;</span> <span class="n">n</span> <span class="p">*</span> <span class="m">10</span><span class="p">},</span> <span class="m">5</span><span class="p">)</span> <span class="c1">// returns 500</span> <span class="p">}</span> </pre></div> <p>Note that for a lambda function the declaration of the parameter types is optional, and there is no way to declare the return type of the function (it has to be inferred). And it is always wrapped with curly braces. Thus the general syntax for a lambda function with n arguments is </p> <div class="highlight"><pre><span></span><span class="p">{</span> <span class="n">p1</span><span class="p">:</span> <span class="n">Type1</span><span class="p">,</span> <span class="n">p2</span><span class="p">:</span> <span class="n">Type2</span> <span class="p">..</span> <span class="n">pn</span><span class="p">:</span> <span class="n">TypeN</span> <span class="p">-&gt;</span> <span class="cm">/* do something */</span> <span class="p">}</span> </pre></div> <p>Note that the lambda function body is an expression and does not allow a return statement. It could consist of multiple statements and/or branches so long as they end with an expression. So the following is a valid lambda.</p> <div class="highlight"><pre><span></span><span class="p">{</span> <span class="n">n</span><span class="p">:</span> <span class="n">Int</span> <span class="p">-&gt;</span> <span class="k">val</span> <span class="py">twoN</span> <span class="p">=</span> <span class="m">2</span> <span class="p">*</span> <span class="n">n</span><span class="p">;</span> <span class="k">if</span> <span class="p">(</span><span class="n">twoN</span> <span class="p">&lt;</span> <span class="m">100</span><span class="p">)</span> <span class="n">twoN</span> <span class="p">*</span> <span class="m">2</span> <span class="k">else</span> <span class="n">twoN</span> <span class="p">*</span> <span class="m">3</span><span class="p">}</span> </pre></div> <h3>Single parameter lambdas</h3> <p>If a lambda has a single argument and its type can be inferred, then the argument declaration along with its type and the subsequent <code>-&gt;</code> can be omitted. In such a case that parameter will have the default name <code>it</code>.</p> <div class="highlight"><pre><span></span><span class="k">val</span> <span class="py">quadruple</span><span class="p">:</span> <span class="p">(</span><span class="n">Int</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="n">Int</span> <span class="p">=</span> <span class="p">{</span> <span class="n">it</span> <span class="p">*</span> <span class="m">4</span><span class="p">}</span> </pre></div> <h3>Anonymous functions</h3> <p>An anonymous function is a function with no name. For obvious reasons you cannot declare it by itself. But you could say use it as the right hand side of an assignment to a variable or pass it as an argument to another function eg.</p> <div class="highlight"><pre><span></span><span class="k">val</span> <span class="py">triple</span> <span class="p">=</span> <span class="k">fun</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Int</span> <span class="p">=</span> <span class="n">n</span> <span class="p">*</span> <span class="m">3</span> <span class="k">fun</span> <span class="nf">somewhereElse</span><span class="p">()</span> <span class="p">{</span> <span class="n">twice</span><span class="p">(</span><span class="n">triple</span><span class="p">,</span> <span class="m">5</span><span class="p">)</span> <span class="n">twice</span><span class="p">(</span><span class="k">fun</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Int</span> <span class="p">{</span> <span class="k">return</span> <span class="n">n</span> <span class="p">*</span> <span class="m">3</span><span class="p">},</span> <span class="m">5</span><span class="p">)</span> <span class="p">}</span> </pre></div> <p>This is particularly useful when the return type cannot be contextually inferred</p> <h3>Closures</h3> <p>Note that local functions, lambda functions and anonymous functions can access variables declared in their outer scope, ie. their closure. They can also modify such variables (assuming they are vars).</p> <h3>Receivers</h3> <p>Lambdas and anonymous functions can be used as extension functions as well. Their function type then is <code>ReceiverType.(p1: Type1 .. pn: TypeN) -&gt; ReturnType</code></p> <h3>Alternative invocation syntax</h3> <p>If a function has one or more arguments, but exactly one argument which is of a function type, then it can be invoked with an alternative syntax where the function is not specified within the parenthesis, but is instead specified after them wrapped in curly braces. </p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">doubleAndThen</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">then</span><span class="p">:</span> <span class="p">(</span><span class="n">Int</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Int</span> <span class="p">=</span> <span class="n">then</span><span class="p">(</span><span class="n">n</span> <span class="p">*</span> <span class="m">2</span><span class="p">)</span> <span class="k">fun</span> <span class="nf">somewhereElse</span><span class="p">()</span> <span class="p">{</span> <span class="n">doubleAndThen</span><span class="p">(</span><span class="m">5</span><span class="p">)</span> <span class="p">{</span> <span class="n">it</span> <span class="p">*</span> <span class="m">5</span> <span class="p">}</span> <span class="c1">// returns 50</span> <span class="p">}</span> </pre></div> <h3>Inline functions</h3> <p>You can specifically request that a function be inlined using the <code>inline</code> keyword. eg. you could've alternatively declared the <code>doubleAndThen</code> function above as </p> <div class="highlight"><pre><span></span><span class="k">inline</span> <span class="k">fun</span> <span class="nf">doubleAndThen</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="n">then</span><span class="p">:</span> <span class="p">(</span><span class="n">Int</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Int</span> <span class="p">=</span> <span class="n">then</span><span class="p">(</span><span class="n">n</span> <span class="p">*</span> <span class="m">2</span><span class="p">)</span> </pre></div> <p>In this case the compiler will emit the function (and the lambda passed to it) inlined at the call site where doubleAndThen is called. This causes your compiled code to grow in size but could pay off in performance. </p> <p>If you do not want a particular lambda passed to the function as inlined, then you could mark it as noinline. eg.</p> <div class="highlight"><pre><span></span><span class="k">inline</span> <span class="k">fun</span> <span class="nf">doubleAndThen</span><span class="p">(</span><span class="n">n</span><span class="p">:</span> <span class="n">Int</span><span class="p">,</span> <span class="k">noinline</span> <span class="n">then</span><span class="p">:</span> <span class="p">(</span><span class="n">Int</span><span class="p">)</span> <span class="p">-&gt;</span> <span class="n">Int</span><span class="p">):</span> <span class="n">Int</span> <span class="p">=</span> <span class="n">then</span><span class="p">(</span><span class="n">n</span> <span class="p">*</span> <span class="m">2</span><span class="p">)</span> </pre></div> <p>If a function is inlined, then the lambdas passed to it are allowed to have a <code>return</code></p> <p>The next post in the series is <a href="/2016/04/exercises-in-kotlin-part-4-control-flows-and-return/">Exercises in Kotlin: Part 4 - Control flows and return</a></p>Exercises in Kotlin: Part 2 - High level syntax and Variables2016-04-19T09:20:00+05:302016-04-19T09:20:00+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2016-04-19:/2016/04/exercises-in-kotlin-part-2-high-level-syntax-and-variables/<p>This post first discusses the code that was written so far in the last post, <a href="/2016/04/exercises-in-kotlin-part-1-getting-started/">Exercises in Kotlin: Part 1 - Getting Started</a>. Subsequently it focuses on the basic syntax of variables, discusses a bit about packaging and also about Type Inference</p> <h2>A review of code so far</h2> <p>Let us take a look at the code we wrote so far in the last blog post <a href="/2016/04/exercises-in-kotlin-part-1-getting-started/">first part of this series</a> and glean what we can from the same. </p> <h3>helloworld.kt</h3> <div class="highlight"><pre><span></span><span class="k">package</span> <span class="nn">com.example.kotlin.learning</span> <span class="k">fun</span> <span class="nf">main</span><span class="p">(</span><span class="n">args</span><span class="p">:</span> <span class="n">Array</span><span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;)</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;Hello World!&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">greetings</span><span class="p">()</span> <span class="p">=</span> <span class="s">&quot;Hello World!&quot;</span> </pre></div> <p>Some of the things that can be noted from this program are follows</p> <ul> <li><em>Similar package convention</em>: Kotlin follows similar package naming conventions as Java.</li> <li><em>Standalone functions</em>: Yes, Kotlin has them. That means there is no requirement to have a class just in order to wrap functions, that frankly have no reasons to be part of a class. </li> <li><em>Keyword</em> <code>fun</code>: Functions are declared using the keyword <code>fun</code>. </li> <li><em>Variable declaration order</em>: When declaring variables (or arguments to a function as in this case of the arguments to <code>main</code>), the order of declaration is reversed, with the type specification <em>following</em> the variable/argument name and a colon used to separate the two. </li> <li><em>Array is a generic type</em>: Arrays are not represented by <code>[]</code> but a generic type <code>Array&lt;T&gt;</code> instead where <code>T</code> is the type whose array is being represented. Thus at a syntactic level there is no necessity for using <code>[]</code> for arrays.</li> <li>There is a helper function called <code>println()</code> similar to <code>System.out.println()</code></li> <li><em>Semicolon as a statement terminator is optional</em>: A semi-colon does not seem to be mandatory</li> <li><em>Optional function return types</em>: Return type of the function is not required. to be specified if the return type is a <em>Unit</em>. Which is why the function <code>main</code> does not require a return type declaration.</li> <li><em>Alternative function representation</em>: As the function declaration for <code>greetings()</code> shows, function bodies can be substituted by expressions if the separator <code>=</code> is used between the function declaration and the expression. When the function body is specified as an expression, the compiler will attempt to infer the return type and in most cases no return type declaration will be required. </li> </ul> <h3>testgreet.kt</h3> <div class="highlight"><pre><span></span><span class="k">package</span> <span class="nn">com.example.kotlin.learning</span> <span class="k">import</span> <span class="nn">org.junit.Test</span> <span class="k">import</span> <span class="nn">org.junit.Assert.assertEquals</span> <span class="k">class</span> <span class="nc">TestGreet</span><span class="p">()</span> <span class="p">{</span> <span class="n">@Test</span> <span class="k">fun</span> <span class="nf">testGreetings</span><span class="p">()</span> <span class="p">{</span> <span class="n">assertEquals</span><span class="p">(</span><span class="s">&quot;Greeting should be &#39;Hello World!&#39;&quot;</span><span class="p">,</span> <span class="s">&quot;Hello World!&quot;</span><span class="p">,</span> <span class="n">greetings</span><span class="p">())</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>From this we can glean the following</p> <ul> <li><em>Class declaration</em>: A Class declaration is similar to java in the sense it starts with the keyword <code>class</code> followed by the class name. The class body is also wrapped within <code>{</code> and <code>}</code>.</li> <li><em>Constructor</em>: There is no explicit constructor that <em>seems</em> to be required. That is not true. The parenthesis after the class name, in this case with empty content, are in fact the argument list for the primary constructor. More details about that later.</li> <li><em>Method declarations</em>: Methods are also declared using the <code>fun</code> keyword. </li> <li><em>Annotations</em>: Annotations can also be used just like they are used in java (in this case the <code>@Test</code> annotation.</li> </ul> <p>The above gives us some sort of understanding about Kotlin syntax, Let us look at a few more examples to flesh out some of the more commonly used details. This has a main function. So you can copy this code into a file and compile and run it.</p> <h2>Example 1 : Comments and variables</h2> <div class="highlight"><pre><span></span><span class="cm">/**</span> <span class="cm">* This is a comment</span> <span class="cm">*/</span> <span class="cm">/*</span> <span class="cm"> So is</span> <span class="cm"> this */</span> <span class="c1">// As is this</span> <span class="k">val</span> <span class="py">PI</span> <span class="p">=</span> <span class="m">3.14159</span> <span class="k">fun</span> <span class="nf">circleArea</span><span class="p">(</span><span class="n">r</span><span class="p">:</span> <span class="n">Double</span><span class="p">)</span> <span class="p">=</span> <span class="n">PI</span> <span class="p">*</span> <span class="n">r</span> <span class="p">*</span> <span class="n">r</span> <span class="k">fun</span> <span class="nf">main</span><span class="p">(</span><span class="n">args</span><span class="p">:</span> <span class="n">Array</span><span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;)</span> <span class="p">{</span> <span class="k">var</span> <span class="py">radius</span><span class="p">:</span> <span class="n">Double</span> <span class="p">=</span> <span class="m">1.0</span> <span class="k">val</span> <span class="py">increment</span> <span class="p">=</span> <span class="m">1</span> <span class="c1">// increment = increment + 1 &lt;-- will not compile since increment is a val</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;The radius of a circle with radius ${radius} is ${circleArea(radius)}&quot;</span><span class="p">)</span> <span class="n">radius</span> <span class="p">=</span> <span class="n">radius</span> <span class="p">+</span> <span class="n">increment</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;The radius of a circle with radius ${radius} is ${circleArea(radius)}&quot;</span><span class="p">)</span> <span class="p">}</span> </pre></div> <p>The output of the above program is </p> <div class="highlight"><pre><span></span>The radius of a circle with radius 1.0 is 3.14159 The radius of a circle with radius 2.0 is 12.56636 </pre></div> <p>Note the following :</p> <h4>Comments</h4> <ul> <li>Kotlin follows the same syntax for comments as Java does. Enough said. </li> </ul> <h4>Primitive types</h4> <ul> <li>Kotlin has primitive types. Though they are not named identical to the ones in Java. They are in fact the same names with the first character capitalised viz Boolean, Byte, Int, Long, Float, Double etc. (not sure if there is an exception to that).</li> <li>There are member methods available for these primitives unlike Java. Thus you can call a <code>toString()</code> on an <code>Int</code></li> <li>Although they seem to be like objects, their storage space is the same as primitives in Java <em>unless</em> they are declared as <em>nullable</em> (more about that later) or used by generic classes in which case their storage is like boxed primitives.</li> </ul> <h4>Variables</h4> <ul> <li>As mentioned earlier, variables can be declared at the top level (eg <code>PI</code>)</li> <li>Functions can also have local variables. eg. <code>radius</code> and <code>increment</code></li> <li>Variables are of two types. <code>val</code> ie. read-only variables and <code>var</code> ie. read-write variables. In Java all variables by default are read-write and those that are to be treated as read-only are marked using the keyword <code>final</code>. However in Kotlin it is required to use one of the two keywords <code>val</code> or <code>var</code> explicitly. The statement <code>// increment = increment + 1</code> was commented out, because since <code>increment</code> is a <code>val</code> such a statement would not compile. <code>radius</code> on the other hand being a <code>var</code> can change its value, as is done at <code>radius = radius + increment</code>. </li> <li>A predominantly functional style of programming will almost always use <code>val</code>s and rarely if any <code>var</code>s. The rationale for that is beyond the scope of this post. </li> <li>A variable may or may not have an explicitly type declared. The compiler will attempt to infer it based on the value it is initialised to. However often that may not be feasible, and the programmer may be required to specify the type explicitly.</li> </ul> <h4>Parameters</h4> <ul> <li>Parameter declarations for functions are similar to a variable declaration. However they are not preceded by a <code>val</code> or a <code>var</code>. They are always <code>val</code>s. They always need an explicit type declaration.</li> <li>A function may return no value whatsoever (represented using the type <code>Unit</code>). The return type of the functional is optional for functions having an expression body (as in case of <code>circleArea</code>) or functions that have a block body but have a <code>Unit</code> return type. In other situatons the compiler will not attempt to infer the return type, and the programmer is required to provide it explicitly.</li> </ul> <h4>String templates</h4> <ul> <li>String interpolation is supported by allowing expressions to be embedded in strings using the ${<code>expr</code>} syntax as in the arguments to <code>println()</code>. This feature is called string templates in Kotlinspeak</li> </ul> <h2>(Optional) Some more advanced aspects about variables</h2> <p>There are some nuanced aspects of variables that you might use only occasionally. These are discussed here. You could chose to skip this section and instead move on to the next post since these might entangle you into learning and thinking about infrequently leveraged capabilities.</p> <p>Consider the following code, which is an extended version of the code we saw earlier. It is kind of a little odd, but has been deliberately constructed to talk about the advanced features.</p> <div class="highlight"><pre><span></span><span class="k">const</span> <span class="k">val</span> <span class="py">PI</span> <span class="p">=</span> <span class="m">3.14159</span> <span class="k">fun</span> <span class="nf">circleArea</span><span class="p">(</span><span class="n">r</span><span class="p">:</span> <span class="n">Double</span><span class="p">)</span> <span class="p">=</span> <span class="n">PI</span> <span class="p">*</span> <span class="n">r</span> <span class="p">*</span> <span class="n">r</span> <span class="k">var</span> <span class="py">defRadius</span> <span class="p">=</span> <span class="m">5.0</span> <span class="k">var</span> <span class="py">defaultRadius</span><span class="p">:</span> <span class="n">Double</span> <span class="k">get</span><span class="p">()</span> <span class="p">=</span> <span class="n">defRadius</span> <span class="k">set</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="p">{</span> <span class="n">defRadius</span> <span class="p">=</span> <span class="n">value</span> <span class="p">}</span> <span class="k">val</span> <span class="py">area</span><span class="p">:</span> <span class="n">Double</span> <span class="k">get</span><span class="p">()</span> <span class="p">=</span> <span class="n">circleArea</span><span class="p">(</span><span class="n">defaultRadius</span><span class="p">)</span> <span class="k">fun</span> <span class="nf">main</span><span class="p">(</span><span class="n">args</span><span class="p">:</span> <span class="n">Array</span><span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;)</span> <span class="p">{</span> <span class="k">var</span> <span class="py">radius</span> <span class="p">=</span> <span class="m">1.0</span> <span class="k">val</span> <span class="py">increment</span> <span class="p">=</span> <span class="m">1</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;The radius of a circle with radius ${radius} is ${circleArea(radius)}&quot;</span><span class="p">)</span> <span class="n">radius</span> <span class="p">=</span> <span class="n">radius</span> <span class="p">+</span> <span class="n">increment</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;The radius of a circle with radius ${radius} is ${circleArea(radius)}&quot;</span><span class="p">)</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;The radius of a circle with radius 5.0 is ${area}&quot;</span><span class="p">)</span> <span class="n">defaultRadius</span> <span class="p">=</span> <span class="m">6.0</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;The radius of a circle with radius 6.0 is ${area}&quot;</span><span class="p">)</span> <span class="p">}</span> </pre></div> <p>The output of the code above is as follows</p> <div class="highlight"><pre><span></span>The radius of a circle with radius 1.0 is 3.14159 The radius of a circle with radius 2.0 is 12.56636 The radius of a circle with radius 5.0 is 78.53975 The radius of a circle with radius 6.0 is 113.09723999999999 </pre></div> <p>Things to be noted.</p> <ul> <li><em>getters</em>: Variables can have getter functions as shown for the variable <code>area</code> above. Frankly, not sure why they would be used for top level variables. Though I could imagine them to be used to work around variable visibility constraints by having a private var being accessible as a read-only variable function from another file by using a getter function. </li> <li><em>setters</em>: We can also similarly use setters as in the case of <code>defaultRadius</code>. Again this is a bit of a contrived example, since one could have just as easily directly used <code>defRadius</code>. However do note, getters and setters to tend to be more useful when used with class member variables, a topic we will get to later.</li> <li><em>const variables</em>: Variables can be prefixed with a const as in case of <code>PI</code> above. However this capability is restricted to top level variables only. In addition such variables have to be initialised to a <code>String</code> or a primitive type. and may not have a custom getter. In turn such consts can be used as parameters to annotations.</li> <li><em>package namespaces and visibility</em>: Just like top level functions, you can also have top level variables/constants being declared. Also note that all the standalone functions get declared in the namespace of the package. So if you have multiple files with the same package and also same function (or top level variable) names, that will be treated as an error since one of them will end up attempting to redeclare the other. On the other hand if you declare a top level variable as private, it is not private to that package namespace, but is instead treated as private within a file</li> </ul> <p>We shall be reviewing functions in the next post <a href="/2016/04/exercises-in-kotlin-part-3-functions/">Exercises in Kotlin: Part 3 - Functions</a></p>Exercises in Kotlin: Part 1 - Getting Started2016-04-18T07:50:00+05:302016-04-18T07:50:00+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2016-04-18:/2016/04/exercises-in-kotlin-part-1-getting-started/<p>Kotlin is an exciting new language I have been working with for a couple of months now. I believe it has a lot of features going for it, and I think many will be benefited from learning a bit more about it. Hence this series of posts.</p> <p>This post helps you with getting the necessary software installed on your desktop for compiling and running kotlin code</p> <h2>Presumed Audience :</h2> <p>The reader is presumed to have a reasonable working knowledge of Java. While one of the reasons Kotlin is particularly exciting is because it is such a useful language for the Android platform, the early parts of this series shall focus on the desktop based JVMs. Since those learnings are common irrespective of whether you wish to use Kotlin for desktop/server or Android programming.</p> <p>It is also assumed that the reader has working java development kit installed on his desktop. Most or all of the discussion about syntax will also likely compare Kotlin with Java.</p> <h2>Information :</h2> <p>Some of the useful sites for Kotlin are</p> <ul> <li><a href="https://kotlinlang.org/">Kotlin Programming Language</a> : Language Home page.</li> <li><a href="https://kotlinlang.org/docs/reference/">Kotlin Reference</a> : Reference - Kotlin Programming Language</li> <li><a href="https://www.jetbrains.com/idea/">Intellij Idea</a> : IDE with built in kotlin support. From the same authors who bring you kotlin, ie. JetBrains</li> <li><a href="https://github.com/JetBrains/kotlin-eclipse">Kotlin Eclipse Plugin</a> : Kotlin plugin for eclipse. <em>Note: I haven't used it myself</em></li> </ul> <h2></h2> <h2>Getting started using an IDE</h2> <p><em>Note: if you do not prefer to use an IDE and are likely to code using text editors and build and run using command line, you can skip this section entirely and go to <a href="#cmdline">Command Line Usage</a></em></p> <p>Go to the Intellij idea page above and download the latest version. You can choose to download the community version. It has the necessary support for Kotlin. You may alternatively choose to use the Eclipse plugin described above, but I haven't used it so cannot help with getting it to work. Install the intellij idea ide and launch it. In case unsure how to do so, consult the <a href="https://www.jetbrains.com/help/idea/2016.1/installing-and-launching.html">official guide</a> for downloading, installing and launching the ide. </p> <p>Start a new project. Click on gradle in the left hand column. You should reach the new project dialog box as follows</p> <p><img alt="Intellij Idea New Project Dialog" src="/images/kotlin/kotlin-1-new-project.png"></p> <p>I always use the gradle build tool, so I end up making the selections as shown. Select any appropriate JVM between 1.6 and 1.8 (built in one should do). You could use the dialog as shown above. You should next see a dialog box for groupid/artifactid as required by gradle (these mean the same thing as they do for maven, so fill up some group id eg. <code>com.example.kotlin</code> and artifactid eg. <code>learning</code>). The rest of this post assumes these two values, so if you use different values, please replace them appropriately in the code as necessary.</p> <p><img alt="Specify groupid and artifactid" src="/images/kotlin/kotlin-3-groupid-artifactid.png"></p> <p>On the next two screens you could choose the defaults or customise as necessary</p> <p><img alt="Project Configuration" src="/images/kotlin/kotlin-4-project-config.png"></p> <p><img alt="Project Configuration 2" src="/images/kotlin/kotlin-5-project-configuration-2.png"></p> <p>Since gradle defaults to maven like directory structures, let us create the directory necessary for our work using the groupid/artifactid used earlier and maven conventions. Select the "<em>learning</em>" folder at the top of the left pane, and select from main or right click menu, File -&gt; New Directory and specify the directory as follows. (If the menu options File -&gt; New Directory do not appear, you have selected the wrong "<em>learning</em>", select the one at the top of the left pane, not the one in the toolbar above it).</p> <p><img alt="Create source directory" src="/images/kotlin/kotlin-6-create-directory.png"></p> <p>Similarly also create another directory for writing our test cases</p> <p><img alt="Create test directory" src="/images/kotlin/kotlin-7-create-test-directory.png"></p> <p>You should see the appropriate packages for the main and test areas appear in the left hand pane as follows</p> <p><img alt="IDE View" src="/images/kotlin/kotlin-8-ide-view.png"></p> <p>Right click on the package name under the src/main/kotlin directory and select "New Kotlin File/Class". It will pop up a dialog where you can enter helloworld</p> <p><img alt="New file dialog" src="/images/kotlin/kotlin-9-new-file-dialog.png"></p> <p>This will create a new file "<em>helloworld.kt</em>" and open it into your ide</p> <p><img alt="New file" src="/images/kotlin/kotlin-10-new-file.png"></p> <p>Enter the following code in the just opened file</p> <div class="highlight"><pre><span></span><span class="k">package</span> <span class="nn">com.example.kotlin.learning</span> <span class="k">fun</span> <span class="nf">main</span><span class="p">(</span><span class="n">args</span><span class="p">:</span> <span class="n">Array</span><span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;)</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;Hello World!&quot;</span><span class="p">)</span> <span class="p">}</span> </pre></div> <p>Run the program using "Run -&gt; Run". It will pop up a very small dialog box, select HelloWorldKt from it. (You could also use Alt-Shift-F10 instead of Run -&gt; Run, though not sure if the same key bindings work across operating systems, though thats what it is on Linux). This is the canonical Hello World program for Kotlin. Don't worry about understanding the syntax yet. We will get to it in the very next post, this post is to get you started.</p> <p>One more thing to do is to get a sample test case running. So, let us add a function to the file we just created that we shall be testing, as follows </p> <div class="highlight"><pre><span></span><span class="k">fun</span> <span class="nf">greetings</span><span class="p">()</span> <span class="p">=</span> <span class="s">&quot;Hello World!&quot;</span> </pre></div> <p>Next, select the package in the src/test/kotlin directory, and after a right click select "New -&gt; Kotlin File/Class". This time select the name as "<em>testgreet</em>", which will create a new file called <em>testgreet.kt</em>. Add the following code to the file.</p> <div class="highlight"><pre><span></span><span class="k">package</span> <span class="nn">com.example.kotlin.learning</span> <span class="k">import</span> <span class="nn">org.junit.Test</span> <span class="k">import</span> <span class="nn">org.junit.Assert.assertEquals</span> <span class="k">class</span> <span class="nc">TestGreet</span><span class="p">()</span> <span class="p">{</span> <span class="n">@Test</span> <span class="k">fun</span> <span class="nf">testGreetings</span><span class="p">()</span> <span class="p">{</span> <span class="n">assertEquals</span><span class="p">(</span><span class="s">&quot;Greeting should be &#39;Hello World!&#39;&quot;</span><span class="p">,</span> <span class="s">&quot;Hello World!&quot;</span><span class="p">,</span> <span class="n">greetings</span><span class="p">())</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>Again select "Run -&gt; Run..." or (Alt-Shift-F10 or equivalent). This will pop up a small dialog box. Select TestGreet. This will run the test case and should show an all green bar near the bottom of your IDE as shown in the image below. </p> <p><img alt="Kotlin Run Test" src="/images/kotlin/kotlin-11-run-test.png"></p> <p>Now, we shall repeat the exercise using text editors and command line for building and running the program. Even if you <em>always</em> use IDEs, I very much recommend that you at least do the remainder of the activities described on this post at least once. It is always helpful to have a general sense of what is happening at a command line level. However if you skip it, or it doesn't work for you for whatever reason, it won't constrain you from being able to continue with the rest of this series.</p> <p>In either case, I shall not be covering the Intellij Idea IDE any further. If you are new to it or to the Eclipse plugin, learning it is beyond the scope of this series of posts, so I encourage you to play around with them, since that is likely to be useful as we go further along in this series.</p> <p><a name="cmdline"></p> <h2>Installation for command line usage</h2> <p>The instructions for working with Kotlin from the command line are documented on the page <a href="https://kotlinlang.org/docs/tutorials/command-line.html">Working with the Command Line Compiler</a></p> <p>Goto <a href="https://github.com/JetBrains/kotlin/releases/latest">Kotlin latest release</a> to download the Kotlin compiler. It will likely be a file called <code>kotlin-compiler-&lt;version&gt;.zip</code>. The current version is 1.0.1-2 at the point in time this post was written. Unzip the file into an appropriate directory. And ensure that the executables kotlin and kotlinc (or their windows equivalent with the .bat extensions) are in your path.</p> <p>Create a work directory for this exercise and change directory to it. </p> <p>Create a file <em>helloworld.kt</em> with the following content.</p> <div class="highlight"><pre><span></span><span class="k">package</span> <span class="nn">com.example.kotlin.learning</span> <span class="k">fun</span> <span class="nf">main</span><span class="p">(</span><span class="n">args</span><span class="p">:</span> <span class="n">Array</span><span class="p">&lt;</span><span class="n">String</span><span class="p">&gt;)</span> <span class="p">{</span> <span class="n">println</span><span class="p">(</span><span class="s">&quot;Hello World!&quot;</span><span class="p">)</span> <span class="p">}</span> <span class="k">fun</span> <span class="nf">greetings</span><span class="p">()</span> <span class="p">=</span> <span class="s">&quot;Hello World!&quot;</span> </pre></div> <p>Compile it using the command</p> <div class="highlight"><pre><span></span>$ kotlinc helloworld.kt </pre></div> <p>Notice that it will have created a file <em>com/example/kotlin/learning/HelloworldKt.class</em>. This is the compiled <em>.class</em> file. It is Java byte code though you wouldn't be able to run it as you might be used to just yet. In fact go ahead and try to run it as follows</p> <div class="highlight"><pre><span></span>$ java com.example.kotlin.learning.HelloworldKt </pre></div> <p>It will fail because you do not have the necessary kotlin runtime jar in your classpath. You should've seen an error message similar to the one below</p> <div class="highlight"><pre><span></span>Exception in thread <span class="s2">&quot;main&quot;</span> java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics at com.example.kotlin.learning.HelloworldKt.main<span class="o">(</span>helloworld.kt<span class="o">)</span> Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics at java.net.URLClassLoader.findClass<span class="o">(</span>URLClassLoader.java:381<span class="o">)</span> at java.lang.ClassLoader.loadClass<span class="o">(</span>ClassLoader.java:424<span class="o">)</span> at sun.misc.Launcher<span class="nv">$AppClassLoader</span>.loadClass<span class="o">(</span>Launcher.java:331<span class="o">)</span> at java.lang.ClassLoader.loadClass<span class="o">(</span>ClassLoader.java:357<span class="o">)</span> ... <span class="m">1</span> more </pre></div> <p>That runtime is located in the kotlin zip you had just downloaded. Let us point the classpath to it and attempt to run the program again. (Replace <code>&lt;folder_where_you_unzipped_kotlin_zip&gt;</code> below with the appropriate directory where you had unzipped the file you downloaded)</p> <div class="highlight"><pre><span></span>$ java -cp &lt;folder_where_you_unzipped_kotlin_zip&gt;/kotlinc/lib/kotlin-runtime.jar:. com.example.kotlin.learning.HelloworldKt </pre></div> <p>You should see "Hello World!" getting printed to the console. Kotlin provides an easier way to do this as well, so let us repeat the process, just a little differently.</p> <p>Compile the source again, but this time create a .jar file</p> <div class="highlight"><pre><span></span>$ kotlinc helloworld.kt -include-runtime -d helloworld.jar </pre></div> <p>Now run it as follows</p> <div class="highlight"><pre><span></span>$ java -jar helloworld.jar </pre></div> <p>The program should run and "Hello World!" should get printed to the console.</p> <p>Now let us run the test case. Create a file <em>testgreet.kt</em> as follows</p> <div class="highlight"><pre><span></span><span class="k">package</span> <span class="nn">com.example.kotlin.learning</span> <span class="k">import</span> <span class="nn">org.junit.Test</span> <span class="k">import</span> <span class="nn">org.junit.Assert.assertEquals</span> <span class="k">class</span> <span class="nc">TestGreet</span><span class="p">()</span> <span class="p">{</span> <span class="n">@Test</span> <span class="k">fun</span> <span class="nf">testGreetings</span><span class="p">()</span> <span class="p">{</span> <span class="n">assertEquals</span><span class="p">(</span><span class="s">&quot;Greeting should be &#39;Hello World!&#39;&quot;</span><span class="p">,</span> <span class="s">&quot;Hello World!&quot;</span><span class="p">,</span> <span class="n">greetings</span><span class="p">())</span> <span class="p">}</span> <span class="p">}</span> </pre></div> <p>Now for compiling and running this you will need the jar for junit (which in turn requires hamcrest). The instructions for that are provided on the <a href="https://github.com/junit-team/junit4/wiki/Download-and-Install">Download and Install - junit4 wiki</a> page. For now just go ahead and download the two following jars for junit and hamcrest. </p> <ul> <li><a href="http://search.maven.org/remotecontent?filepath=junit/junit/4.12/junit-4.12.jar">junit jar</a></li> <li><a href="http://search.maven.org/remotecontent?filepath=org/hamcrest/hamcrest-all/1.3/hamcrest-all-1.3.jar">hamcrest jar</a></li> </ul> <p>You will have downloaded <em>junit-4.12.jar</em> and <em>hamcrest-all-1.3.jar</em>. Move both of them into your current directory after download. </p> <div class="highlight"><pre><span></span>$ kotlinc helloworld.kt -cp junit-4.12.jar -include-runtime -d helloworld.jar </pre></div> <div class="highlight"><pre><span></span>$ java -cp junit-4.12.jar:hamcrest-all-1.3.jar:helloworld.jar org.junit.runner.JUnitCore com.example.kotlin.learning.TestGreet </pre></div> <p>You should see something like the follows (the time obviously could vary) that indicates success.</p> <div class="highlight"><pre><span></span>JUnit version 4.12 . Time: 0.004 OK (1 test) </pre></div> <p>You should also be aware that Kotlin has a REPL that you can run using the command <code>kotlinc-jvm</code> as follows</p> <div class="highlight"><pre><span></span>$ kotlinc-jvm Welcome to Kotlin version <span class="m">1</span>.0.1-2 <span class="o">(</span>JRE <span class="m">1</span>.8.0_77-b03<span class="o">)</span> Type :help <span class="k">for</span> help, :quit <span class="k">for</span> quit &gt;&gt;&gt; println<span class="o">(</span><span class="s2">&quot;Hello World!&quot;</span><span class="o">)</span> Hello World! &gt;&gt;&gt; </pre></div> <p>Congratulations you are now able to compile and run Kotlin programs. In the next post in this series, we shall attempt to understand the code that we just wrote, and explore the Kotlin language further. You can continue to the next post <a href="/2016/04/exercises-in-kotlin-part-2-high-level-syntax-and-variables/">Exercises in Kotlin: Part 2 - Syntax: Variables</a></p>Few thoughts about Kotlin and why I like it so much2016-04-15T15:00:00+05:302016-04-15T15:00:00+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2016-04-15:/2016/04/few-thoughts-about-kotlin-and-why-i-like-it-so-much/<p>It doesn't matter how many languages you have learnt, learning yet another one is always a wonderful experience. I have been working for a couple of months with Kotlin. Have got one app to ready for production state and another in the works. </p> <p>To cut to the chase, I am really impressed by this language. As in really really impressed. And so I decided that I should write a few posts that can help readers get a better idea and learn about this language. So I started writing out the series and am already onto the third post now. But as I was writing it, I realised what I was writing was really about the mechanics of the language. Not so much about its spirit. Cos when one gets into the tactical nitty gritty details, one tends to miss the forest for the trees. So I decided to pen that here before I start that series (next week), and here's my few thoughts about why I really enjoy the language.</p> <p>One of the reasons there is a lot of enthusiasm with the language is because it can be readily used for programming to the Android platform. That is how I ended up learning it. But very soon, my enthusiasm spread into the desktop/server space where I think Kotlin deserves a shout out for a very very credible effort. </p> <p>[ EDIT: Deleted a few paragraphs]</p> <p>So what does Kotlin bring to the table? </p> <ul> <li>Runs on the JVM</li> <li>Is extremely good at interoperating with existing java code and libraries.</li> <li>Statically typed (thus far far easier to refactor)</li> <li>Has a bit more evolved type system than Java. </li> <li>Is much safer than Java. Nullability is a first class compiler level construct - something that helps address a large number of likely defects aka the billion dollar bug). There is a lot to write home about here.</li> <li>Has a clear system of distinguishing between mutable and immutable structures. While it doesn't make an opinionated stand in favour of immutability, it gives you the programmer enough helpful constructs to chart your course down that path. </li> <li>Has support for type inference which helps reduce your boiler plate and keeps code shorter</li> <li>Has a wonderful story to tell in terms of it providing programmers an ability to write extension functions to existing classes (or for that matter even primitives). </li> <li>Support for Higher Order Functions, Higher kinded types</li> <li>Support for Android programming and JavaScript execution engine (experimental).</li> </ul> <p>Does that make it the better language? I personally think it has achieved amongst the best set of tradeoffs that are attractive to me (obviously YMMV). And here's why I enjoy it so much.</p> <ul> <li>Its expressivity is comparable to and superior than python keeping your code short and sweet</li> <li>It being a statically typed language helps enormously speed up refactoring exercises. Even though I have been using it for only two months, I went through at least two brutal refactoring cycles and found Kotlin code to be a pleasure to change. </li> <li>It is safer, lot lot safer than python / java and delegates one of the most frequent tasks of programming ie. null checking to the compiler. Thats a huge effort off the back along with reduction in number of probable defects. Surely nothing to complain about here. And believe me this is a big big plus point.</li> <li>Uses the JVM which is a fine and fast runtime engine</li> <li>It is easy to learn. I actually went through the reference from start to end in 2 days and was a half decent programmer in about a weeks time. Since then I have written functional libraries for Option, Either, Stack, List, HTTP Clients, XML parsing helper libraries, rather complex X.509 certificate trust chain verifiers and a complete Java FX based app. Have started playing around with more functional constructs such as currying, functors etc. Look forward to working more with typeclasses. I just don't think I could have had such a pace of progress with any other language. </li> <li>It is non opinionated. For XML parsing I wanted to ensure very low memory footprint because I wanted to use it on android. I was able to write a library which heavily used mutation even as it maintained very low memory usage by keeping extra object allocation counts down. For another situation I wanted to use immutable functional paradigm, and Kotlin happily let me create my own Option/Either/List classes and use that paradigm end to end. It doesn't get in your way. And focuses on getting work done.</li> <li>I have been interfacing with a large number of java libraries. The interoperability has been an absolute breeze. It is actually refreshing to go through that experience. Doesn't matter if you are dealing with HTTP client APIs, integrating into a Slack HTTP service, interfacing with Java PKCS API, Kotlin does it easily and safely. </li> </ul> <p>In a nutshell, I can write code that is safer, shorter, uses better building blocks, is easier to grok, gives me all the nice capabilities such as HOFs, lambdas, pretty competent type system that gives me all the feedback necessary when writing code and makes refactoring a breeze, and interoperates nicely and leverages the JVM universe.</p>The Draft Encryption Policy - An impractical exercise2015-09-22T04:00:00+05:302015-09-22T04:00:00+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2015-09-22:/2015/09/the-draft-encryption-policy-an-impractical-exercise/<p class="first last">A view on the new draft encryption policy published by the Government of India</p> <img alt="Do Not Encrypt" class="align-center" src="/images/encryption-for-all.png" /> <p>The government of india published a <a class="reference external" href="http://www.scribd.com/doc/282239916/DRAFT-NATIONAL-ENCRYPTION-POLICY">new draft encryption policy</a> . Frankly a part of me still imagines this to be an April fools prank gone awry. And that I will wake up tomorrow to news, that this was a screwup and we can all forget about it.</p> <p>[EDIT: The government has since published an addendum, issuing clarifications. <a class="reference external" href="http://deity.gov.in/sites/upload_files/dit/files/Addendum%20-%20NEP-1_0.pdf">PROPOSED ADDENDUM TO THE DRAFT ENCRYPTION POLICY</a> This post has been updated at the end to reflect the same. In brief while it has reduced the coverage of the applications and services, the essential implementation issues as pointed out to in this post still remain and the policy seems just as silly and onerous after the addendum was published.]</p> <p>Be as it may, this policy has received substantial criticism early on, not the least of which is about the various privacy issues. This post attempts to look at this policy within the context of security, practicality and law.</p> <p>Note that encryption as referred to in this policy applies to encryption as used for transmission <strong>AND</strong> storage. Also the quoted policy text may refer to G, B and C. This refers to Government, Business and Citizens respectively.</p> <div class="section" id="an-imaginary-conversation"> <h2>An imaginary conversation</h2> <p>I am frankly dumbstruck by the policy draft. I wonder how it even resulted into its current state. My best guess is somewhere in the policy corridor a conversation along the lines below happened. Once again, to emphasise, the conversation below is imaginary.</p> <blockquote> <p>Politician: We need to have a better ability to intercept all the messages flowing on the internet. Very needed to take on terrorism and is also useful for political intelligence and rapid reaction.</p> <p>Bureaucrat: Sir, most of the internet sites and mobile apps encrypt all the conversations. So even as we can readily intercept the messages, we cannot easily decrypt them.</p> <p>Politician: But the US does it. Why can't we as well?</p> <p>Bureaucrat: Ahh, but they have the NSA and all its resources, and they can arm twist the large internet giants into making backdoor concessions via programs such as PRISM as Edward Snowden showed. They even introduced backdoors into the Dual Elliptic Curve random number generation written by RSA to easily crack the encryption. We cannot do that.</p> <p>Politician: Lets have the chinese model then.</p> <p>Bureaucrat: Ahh .. but there there is far more interception of messages and an ability to do better offline monitoring of suspicious citizens, and the government can easily get to the data of a suspicious guy, because there is lesser judicial oversight. We being a democracy with an active judiciary cannot implement that model either.</p> <p>Politician: OK, I don't really care for the encryption per se. And I don't wish to be lambasted for too much snooping. Just make sure that we can easily access the non encrypted data directly from one of the parties when we need to and there is enough of a legal framework to support that.</p> <p>Bureaucrat: OK Sir.</p> <p><em>The said bureaucrat then sets in motion a series of actions which results in this new draft policy document</em></p> </blockquote> </div> <div class="section" id="comments-on-the-draft"> <h2>Comments on the draft</h2> <p>Lets first take a tour through some of the interesting provisions in the new so called policy.</p> <div class="section" id="use-of-encryption-algorithms-and-protocols"> <h3>Use of encryption algorithms and protocols</h3> <blockquote> Use of Encryption technology for communications between G group and B / C groups (i.e. G2B and G2C sectors) with protocols and algorithms for encryption, key exchange, Digital Signature and hashing will be as specified through notification by the Government from time to time.</blockquote> <p>So the government will decide what kind of algorithms and protocols can be used. Businesses may no longer retain the liberty to choose which algorithms and protocols they may use or use any custom algorithms (even though that is not such a good idea, the freedom to use it is), or innovate to create better algorithms. The government dicates and businesses shall oblige.</p> </div> <div class="section" id="ability-to-reproduce-encryption-using-software-hardware"> <h3>Ability to reproduce encryption using software/hardware</h3> <blockquote> On demand, the user shall be able to reproduce the same Plain text and encrypted text pairs using the software / hardware used to produce the encrypted text from the given plain text.</blockquote> <p>Whenever business do any kind of encryption, they must be able to reproduce both the plain text and the encrypted text.</p> <p>First of all, if the key has any random or varying element to it the encrypted text may not be the same each time. But no, the government now mandates that you must be able to produce the same encrypted text from the given plain text as you did earlier. Randomness be damned. Which means in case of random keys, the keys will need to get stored as well.</p> <p>Secondly the user needs control over the software and hardware used to produce the encrypted text. So if you had a cloud based service to do it for you, you would need to find a way to ensure that you are able to re-do the same using the cloud based service on the day the government makes that request of you, and if your cloud based service cannot do it for you, that will now be you the consumer's fault. Or if you are using a mobile app you should be able to reproduce both the plain text and the encrypted text pair along with the key.</p> </div> <div class="section" id="storage-of-plain-text-for-90-days"> <h3>Storage of plain text for 90 days</h3> <p>This is a huge huge huge deal and perhaps the hardest part of this policy to deal with. As per the policy</p> <blockquote> Such plain text information shall be stored by the user/organisation/agency for 90 days from the date of transaction and made available to Law Enforcement Agencies as and when demanded in line with the provisions of the laws of the country</blockquote> <p>It further goes on to add</p> <blockquote> In case of communication with foreign entity, the primary responsibility of providing readable plain-text along with the corresponding Encrypted information shall rest on entity (B or C) located in India.</blockquote> <p>Elsewhere the document states</p> <blockquote> All information shall be stored by the concerned B / C entity for 90 days from the date of transaction and made available to Law Enforcement Agencies as and when demanded in line with the provisions of the laws of the country. In case of communication with foreign entity, the primary responsibility of providing readable plain-text along with the corresponding Encrypted information shall rest on entity (B or C) located in India</blockquote> <p>We will get into a number of issues this creates later on. But just the basic implementation is impractical. The browser exchanges literally hundreds if not thousands or more encrypted messages with various servers every hour. How is one expected to keep a log of all these exchanges? Both the plain text and encrypted bodies. And you have to store the keys as well since these keep on changing. How will this work with mobile apps ? Most internet services and mobile app servers are based abroad. So the entire burden of responsibility comes down on the lay user residing in India. What if the mobile is stolen and the government asks for that data in less than 90 days since it was generated? What if you delete some data (eg. messages) after you had exchanged them? What about applications that use different protocols for encryption? This is a complete nightmare. Lay users will simply not be able to even comprehend what needs to be done and how, forget actually taking on the responsibility to implement this. The only practical way out is to stop using computers and modern smart phones and go back to the PCs of the 80s without the internet and old Nokia phones which maintain an address book and an ability to make calls.</p> </div> <div class="section" id="service-provider-registration"> <h3>Service provider registration</h3> <blockquote> Service Providers located within and outside India, using Encryption technology for providing any type of services in India must enter into an agreement with the Government for providing such services in India. Government will designate an appropriate agency for entering into such an agreement with the Service provider located within and outside India. The users of any group G,B or C taking such services from Service Providers. are also responsible to provide plain text when demanded.</blockquote> <p>There are literally thousands of websites and mobile app services that use encryption. Are they supposed to enter an agreement with the Government of India?</p> <p>The policy further goes on to add in a separate section</p> <blockquote> Registration: All vendors of encryption products shall register their products with the designated agency of the Government. While seeking registration, the vendors shall submit working copies of the encryption software / hardware to the Government along with professional quality documentation, test suites and execution platform environments. The vendors shall work with the designated Government Agencies in security evaluation of their encryption products. Complete confidentiality will be maintained in respect of information shared by the vendors with designated agency. The vendors shall renew their registration as and when their products are upgraded. Mass use products like SSL / TLS are exempted from registration.</blockquote> <p>This is just ridiculous. What an encryption product is is not clear. SSL/TLS are classfied as products (they are not). Such vendors shall submit working copies of software / hardware to government along with professional quality documentation, test suites and execution platforms? So some small startup somewhere buys a piece of helpful hardware to help with the encryption, it now has to buy another one just to deposit it with Government of India? Assuming the GOI is capable enough to accept such software/hardware, the startup will now need to provide detailed documentation. Assuming one gets past that, anyone who has the slightest confidence that the government has the necessary resources and skills to deal with thousands of such submissions may please raise their hand. For a government claiming to be pro-startups, its hurting them hard.</p> <p><em>Please note: SSL/TLS are exempted from registration, not from the onerous requirement of storing 90 days of plain text, encrypted data and the session keys</em></p> <p>The document further goes on to state</p> <blockquote> The Government will notify the list of registered encryption products from time to time, without taking responsibility for security claims made by the vendors.</blockquote> <p>So even after this, each user is left to sift through a list of &quot;registered encryption products&quot; to figure out if their preferred web site or mobile app may be used in India. Every traveler to India will need to do the same as well when in India.</p> </div> <div class="section" id="lack-of-a-legal-framework"> <h3>Lack of a legal framework</h3> <p>The document adds</p> <blockquote> Government reserves the right to take appropriate action as per Law of the country for any violation of this Policy.</blockquote> <p>Ummm right. Under what IPC will you charge a user for deleting a Whatsapp message? Where is the legal framework which tells the citizens of the country what maximum jail time or penalties they might face if found in violation? What if the phone got stolen? Does that make his penalty any less? Where is the legal framework for prosecuting violations of this policy? Or the clarity if one does exist.</p> </div> <div class="section" id="promotion-of-r-d-in-cryptography"> <h3>Promotion of R&amp;D in cryptography</h3> <p>With a view to promote the R&amp;D into cryptography the document states</p> <blockquote> Research and Development programs will be initiated for the development of indigenous algorithms and manufacture of indigenous products for Encryption, hashing and other cryptographic functions. These will be carried out by Public and Private Sector / Government Agencies and Academia. Continuous intensified R&amp;D activities in the niche areas of technical analysis and evaluation of Encryption products will be strengthened.</blockquote> <p>Nice sounding. But the entire tenor of the document is meant to make encryption so onerous that people will run a thousand miles away from it. This document essentially screams &quot;DO NOT ENCRYPT&quot;. In such an environment one wonders why the government actually needs to promote indigenous algorithms and products when it intent seems to be strongly focused on completely destroying such a market (and the IT, BPO and Knowledge Processing industries as stated later).</p> </div> <div class="section" id="algorithms-to-be-used"> <h3>Algorithms to be used</h3> <p>In the annexure the policy prescribes the algorithms to be used.</p> <blockquote> Symmetric Cryptographic/Encryption products with AES, Triple DES and RC4 encryption algorithms and key sizes up to 256 bits are prescribed by the Government for use for protecting information by stakeholders.</blockquote> <p>One wonders how much the government even hopes to support encryption when one of the three suggested algorithms has been explicitly proscribed by the Internet Engineering Task Force (IETF) under RFC 7465 from using it within TLS clients. Clearly the government seems to be unaware of what is happening on the security front and is suggesting algorithms that the Internet widely now believes to be insecure. The least they could have done was at least drop RC4. While not inconvenient, this does make one wonder how well the government is conversant with the happenings in the IT industry.</p> </div> </div> <div class="section" id="disingenious-objectives"> <h2>Disingenious Objectives</h2> <p>The document in its preamble lays down its vision, mission, and objectives. And yet after reading the rest of the document, one wonders if the government even remotely understood that its policy is almost entirely inconsistent with what it set out to do. Thus either it was not entirely honest in setting out its goals properly, or it did a rather shoddy job by actually undermining these very goals. Let us take a look at the goals themselves.</p> <div class="section" id="vision"> <h3>Vision</h3> <blockquote> To enable information security environment and secure transactions in Cyber Space for individuals, businesses, Government including nationally critical information systems and networks.</blockquote> </div> <div class="section" id="mission"> <h3>Mission</h3> <blockquote> To provide confidentiality of information in cyber space for individuals, protection of sensitive or proprietary information for individuals &amp; businesses, ensuring continuing reliability and integrity of nationally critical information systems and networks.</blockquote> </div> <div class="section" id="objectives"> <h3>Objectives</h3> <blockquote> <ul class="simple"> <li>To synchronize with the emerging global digital economy / network society and use of Encryption for ensuring the Security / confidentiality of data and to protect privacy in information and communication infrastructure without unduly affecting public safety and National Security.</li> <li>To encourage wider usage of Digital Signature by all entities including Government for trusted communication, transactions and authentication.</li> <li>To encourage the adoption of information security best practices by all entities and Stakeholders in the Government, public &amp; private sector and citizens that are consistent with industry practice.</li> </ul> </blockquote> <p>I don't think a further discussion is really required as to the incogruity between the goals as set out and the implementation directives.</p> </div> </div> <div class="section" id="implementation-difficulties-with-this-policy"> <h2>Implementation difficulties with this policy</h2> <p>Lets just run through the number of issues this creates.</p> <ol class="arabic"> <li><p class="first">What does it mean to store plain text information. Does it need to be stored as a plain text? Many mobiles and computers have fully encrypted file systems or fully encrypted databases. Will storing plain text information on such file systems or databases be allowed ? If not, will organisations and end users need to change to unencrypted file systems thus substantially compromising their security?</p> </li> <li><p class="first">Continuing from above, many users (including yours truly) use full disk encryption as a mechanism to guard against accidental data leakage in case of mobile/laptop theft, loss etc. Will end users have to give up on that security? And will theft or loss of a mobile / laptop now mean someone else can easily access all the data?</p> </li> <li><p class="first">If a service provider is registered, does it absolve the end user citizen from maintaining encryption logs? It does not seem so.</p> </li> <li><p class="first">It seems a new market is being unintentionally created for transparently logging plain text data, encrypted text and keys used for encryption every time encryption is used for storage or transmission. This could be done at various levels</p> <blockquote> <ol class="arabic simple"> <li>End users - Clearly end users cannot hope to maintain a log of all this stuff. They are more likely to give up using computers and start using and the pure mobile handsets (non smartphones) thus reversing any attempts at increasing digitisation.</li> <li>Applications - There are again literally thousands if not hundreds of thousands of desktop and mobile applications. Does each one need to be modified now? Again exceptionally unlikely</li> <li>Operating systems - Perhaps Windows and Android and iOS could be enhanced. But will they? And really, why should they? And that will only cover those scenarios where such encryption is done using system level libraries. Rest of the apps are still on their own.</li> </ol> </blockquote> </li> <li><p class="first">Citizens will not be able to use web services and mobile apps that are not compliant. I mean if I edit and save an email 20 times before sending it from a mail service which stores all emails as encrypted, would an extreme interpretation mean web mail client has to save 20 triplets of plain text, encrypted text and key used for encryption? Or at least plain text and encrypted pairs? Unless the internet application or mobile application does it, no end user will be able to use it for fear of being termed a criminal</p> </li> <li><p class="first">There's a deep conflict with good practices and internationals standards. As an example PCI standards. They have a substantial focus on encryption including not storing data in plain text and using random keys and securely encrypting any key information that must be stored. A credit card processor could not find a way to obey the law of the land and the mandatory requirements of secure processing as laid down by the industry. There's no way to run that business within the territorial boundaries of India.</p> </li> <li><p class="first">Keys are often stored in Hardware Security Modules (HSMs) for storing them securely. No HSM manufacturer worth his salt will ever condescend to create an HSM that complies with the directives in this policy simply because it is incredibly stupid to do so. Thus enterprises would not be able to use HSMs. Even normal key storage itself would need to be extraordinarily insecure to be consistent with this policy.</p> </li> <li><p class="first">I think the general extrapolation of the last two points will suggest that software engineers will need to throw out of the window all known best practices for ensuring data security and privacy, and make sure data is fully open or at least open enough to be just one step away from disclosure as and when required (which also makes it far far more easily accessible to hackers)</p> </li> <li><p class="first">Assuming one does log the data, how does one ensure its availability. What happens in case of disk crash? Do mobiles now need to have RAID 5 storage?</p> </li> <li><p class="first">What happens with cloud based systems like google chrome? There is little if any storage on the local systems. Do such OS's now require to be modified to cater to the encryption policy?</p> </li> </ol> </div> <div class="section" id="political-implications"> <h2>Political implications</h2> <p>There are some other serious implications in terms of required professional confidentiality (eg. with doctors and lawyers), having secure protected communications with say a spouse, whistleblowing activities, communication with journalists, political dissidence et. al. These are all communications that must be protected within any self respecting democracy.</p> <p>In addition there is the very basic fundamental right citizens have to privacy.</p> <p>For a government which claims to promote digitisation, it is setting out to impose a huge cost on the same. Is the policy consistent with the ambitions?</p> <p>This is an explosive can of worms that threatens civil rights and encourage movement to a orwelian state.</p> <p>Basically this policy document justs seems like a really bad dream. However this being a technical blog, this entire issue is not explored or commented upon further.</p> </div> <div class="section" id="commercial-implications"> <h2>Commercial implications</h2> <p>As mentioned earlier this policy draft does not live up to its stated goals. As also further pointed out this draft basically destroys any notion of secure data storage and encourages rejecting decades of improvements in data storage best practices. But most importantly this policy if implemented (which it should not ever get there hopefully) will headline India for having a regulatory framework which totally compromises data management best practices.</p> <p>This will make it hard for businesses to outsource data to Indian companies because their data will now be stored in plain text along with the necessary encryption keys. This could be disastrous for the IT, BPO and all Knowledge Processing industries.</p> </div> <div class="section" id="edit"> <h2>EDIT:</h2> <p>The government has since published the addendum to the policy following the media uproar. <a class="reference external" href="http://deity.gov.in/sites/upload_files/dit/files/Addendum%20-%20NEP-1_0.pdf">PROPOSED ADDENDUM TO THE DRAFT ENCRYPTION POLICY</a></p> <p>Since it substantially impacts this post, am documenting my thoughts regarding the same separately as follows.</p> <ol class="arabic"> <li><p class="first">What is the definition of &quot;The mass use encryption products, which are currently being used in web applications, social media sites, and social media applications&quot;? This is such a large category that it is impossible to imagine those who authored the original document did not think of this class of services. So I suspect this was essentially a knee jerk reaction to the uproar rather than having been thought of in the first place. Also does gmail qualify for the exemption above? Lets presume for a moment it does, does my webmail client as offered by my web host provider qualify? It is unlikely to. So some webmail clients are exempt and some aren't. Does telegram, another app similar to whatsapp qualify? The above definition does not make it clear. If I wrote a similar app for my family, Will it? If not, why not?</p> <p>In short this clarification makes the whole thing even more murky.</p> </li> <li><p class="first">I presume by SSL/TSL based products it means corresponding web sites. Why call them products? And there are tons of other apps which may not qualify. eg. a health care management system, or a logistics routing application. One might argue that if they are password based, then they should also be exempt. Assuming all password based services are exempt then what is the whole policy for? What non password based services need such a disturbing policy around it? And if all password based services are not exempt, then all the issues I've pointed to are still relevant. Thus leading to a scenario where a large number of internet based services and mobile apps having to deal with a real ton of issues to sort through.</p> </li> <li><p class="first">What about outsourced IT, BPO and other backoffice services to India ? Do they qualify for exemption. Many of them will not meet the addendum requirement for exemption. And yet they will deal with very important sensitive data. Will the data owners continue to allow their Indian outsourcing partners to continue dealing with their data?</p> </li> </ol> <p>This whole addendum sounds like a knee jerk reaction to a really deficient policy in the first place. Seems to me this policy was a very very bad idea to begin with, something that the government simply did not think through. And now it is trying to put up a face saver using an addendum. Sadly that only deals with the issues raised in the popular media. It still does not address many of the implementation issues I have referred to. Perhaps the policy makers simply have very little clue about what is it that they are attempting to regulate. And leaving us very very confused, and very very scared.</p> </div> Have we misunderstood DVCS and git / hg ?2013-10-04T04:00:00+05:302013-10-04T04:00:00+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2013-10-04:/2013/10/have-we-misunderstood-dvcs-git-hg/<p class="first last">Exploring our understanding of DVCS in the context of recent github downtime.</p> <p>Yesterday github was at least partially inaccessible due to a DDOS attack.</p> <blockquote class="twitter-tweet"><p>We are working to mitigate a large scale DDoS attack.</p>&mdash; GitHub Status (@githubstatus) <a href="https://twitter.com/githubstatus/statuses/385494370864361473">October 2, 2013</a></blockquote> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script><p>And there were some interesting tweets appearing. I just show a few that appeared at the top of my twitter search results. But there were many many more.</p> <blockquote class="twitter-tweet"><p>So github goes down and apparently collaboration becomes unpossible? Guess the ‘D’ in DVCS went unnoticed by many.</p>&mdash; Christopher Darroch (@chrisdarroch) <a href="https://twitter.com/chrisdarroch/statuses/385721163487272960">October 3, 2013</a></blockquote> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script><blockquote class="twitter-tweet"><p>If you can&#39;t work when GitHub is down, you are misunderstanding the D in DVCS.</p>&mdash; ¯\_(ツ)_/¯ (@SeanTAllen) <a href="https://twitter.com/SeanTAllen/statuses/373794275966058496">August 31, 2013</a></blockquote> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script><blockquote class="twitter-tweet"><p>How many times must github go down before people grok DVCS?</p>&mdash; Robert Pitts (@rbxbx) <a href="https://twitter.com/rbxbx/statuses/368048079758696448">August 15, 2013</a></blockquote> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script><blockquote class="twitter-tweet"><p>Some clown tried to DDoS <a href="https://twitter.com/github">@github</a>? I hope they know Git is a DVCS.</p>&mdash; Postmodern (@postmodern_mod3) <a href="https://twitter.com/postmodern_mod3/statuses/315608550573473794">March 23, 2013</a></blockquote> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script><p>Perhaps the most hard hitting of them all</p> <blockquote class="twitter-tweet"><p>If Github being down stops your entire development process, you&#39;re doing DVCS wrong.</p>&mdash; Greg Hurlman (@ghurlman) <a href="https://twitter.com/ghurlman/statuses/368039139792285698">August 15, 2013</a></blockquote> <script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script><p>There is clearly a mismatch between perceived expectation of DVCS workflows and our actual experiences. I did start wondering, was it our expectations that were inflated, or were we (as a community) not using git correctly.</p> <p>Frankly all the work a developer does by himself/herself is not affected. One can continue to commit, branch, merge etc. at will. What is impacted is the collaboration with other developers. So the &quot;D&quot; in the &quot;DVCS&quot; has at least partially lived up to its promise. Since each developer can still do a lot of work even as he/she is distributed.</p> <p>The event allows us to reflect on whether we could use git a bit differently, and perhaps suggest some opportunities for improvement. However, I think the major issue is perhaps our expectation of the <strong>D</strong> in <strong>DVCS</strong> is inflated. Some of the reasons are related to the state of technology. Some others are inherent in the workflow. Here's why</p> <ul class="simple"> <li>A truly distributed (federated) workflow may not require github at all. However it will require all repositories to be able to communicate with each other in a peer to peer fashion. That would mean it would require us to open up our development workstations to be accessible to other development workstations either directly or via some protocol like bittorrent. Is it really practical in the age of don't expose your development machine, private IP addresses, proxy servers, firewalls etc.? I suspect not. We are not yet ready for a peer to peer communication over the internet between our development nodes. To the extent that developer nodes can communicate with each other, some amount of collaboration remains feasible even if the central github/origin repo goes down.</li> <li>Assuming these nodes can communicate with each other, we don't ship different versions of software from different development nodes. We typically have a reference version that is ready to test, ship etc. We need to be able to point to a branch or a tag and say thats the current state of my software under development. Granted that in this model, the reference node may not be able to reflect the current state of software development in realtime. In the sense, some changes and commits on the development nodes still haven't made it to the reference node (often called the origin). But the organisation as a whole treats the development nodes as work in progress and the origin as the current state of (partially) completed work. If we work in a fully federated model where will we have the reference version? If we annoint one dev node as the reference node - then that node is in effect the equivalent of github, and should that node be inaccessible, many collaborative activities will stop. And if we do not annoint one particular node as the first amongst equals, there are things we need to figure out from an overall business perspective, how to make sense out of a bunch of disparate nodes, and compute the current state of software as a function of all the collective development nodes.</li> <li>While I cannot readily imagine a good solution to the issue I just described, let us imagine, we did find a good solution. We would have an issue of different views of history. Different developers would see history differently. They would see the software being developed with a differently ordered commits. Plus given n-to-n communication, the complexity of doing rebases and merges would shoot up drastically. Are we sure we have understood what kind of complexity it would require us to deal with even if the technology to do so was feasible? I suspect not.</li> </ul> <p>An analogous situation would be where sales staff synced up their notebooks with a central database in the morning, and went off to do their tasks independently during the day in remote areas without any connectivity, came back in the evening and synced up their notebooks once again with the central database. This is a similar scenario to how we use git. Clearly, issues such as resolving conflicts would remain, yet the offline model allows the sales staff to do &quot;most&quot; of their work except for the coordination that might be required but not feasible due to lack of connectivity.</p> <p>Is there a way to use git better than how we've been using it so far? Perhaps yes. Perhaps we could have multiple central databases (using the analogy above) in a master slave model or multi master model. Now I dont think we have the ready technology with DVCS to use the multi master model where different masters can readily synchronise with each other without any manual intervention (I could be wrong, but I make that assumption). In which case a master slave model would work better. So perhaps we could have a master/slave arrangement between multiple &quot;origins&quot;. And the master would near instantaneously push changes out to the slaves. These would be hosted by different git providers. And if the master was to be not accessible, we could always promote another slave to become the master, and have the original master re-join the cluster as a slave when it is back online. This might require a bit of tooling but does not seem like a very difficult task.</p> <p>Perhaps, I haven't fully imagined how people would be doing it &quot;right&quot; when they design their workflows in a manner a github downtime does not impact them at all. And would be keen to learn or get answers. But at least for the moment, I think we need to temper our expectations. Based on the real constraints. It does not mean distributed VCS has failed, but instead has lived up to promises that it reasonably could. And the rest was just us letting our expectations go a little further.</p> Partially applied functions and decorators in python2013-10-03T19:00:00+05:302013-10-03T19:00:00+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2013-10-03:/2013/10/partially-applied-functions-and-decorators-in-python/<p class="first last">A detailed look at decorators and associated concepts like partial application of functions, closures et.</p> <p>This is not a quick tutorial. It will tell you all you need to know about decorators, but slowly build ups the necessary theoretical and technical background in terms of partially applied functions, closures and lexical scoping before eventually discussing decorators.</p> <div class="section" id="partial-application-of-functions"> <h2>Partial application of functions</h2> <p>As per the wikipedia article on <a class="reference external" href="http://en.wikipedia.org/wiki/Partial_application">Partial Application</a>,</p> <blockquote> In computer science, partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. Given a function <span class="formula"><span class="scriptstyle"><i>f</i></span> : (<i>X</i> × <i>Y</i> × <i>Z</i>) → <i>N</i></span> , we might fix (or 'bind') the first argument, producing a function of type <span class="formula"><span class="scriptstyle"><span class="text">partial</span></span>(<i>f</i>) : (<i>Y</i> × <i>Z</i>) → <i>N</i></span> . Evaluation of this function might be represented as <span class="formula"><i>f</i><sub><i>partial</i></sub>(2, 3)</span>. Note that the result of partial function application in this case is a function that takes two arguments.</blockquote> <p>Thus if you have a function <span class="formula"><i>f</i></span> with arguments <span class="formula"><i>x</i><sub>1</sub>, <i>x</i><sub>2</sub>, ..<i>x</i><sub><i>n</i></sub></span>, a partial application technique would allow you to</p> <ul class="simple"> <li>Create a partially applied function <span class="formula"><i>f</i><sub><i>partial</i></sub></span> specifying only a subset of the arguments <span class="formula"><i>x</i><sub>1</sub>, <i>x</i><sub>2</sub>, ..<i>x</i><sub><i>n</i></sub></span>. Assuming them to be the first two, it would be <span class="formula"><i>f</i><sub><i>partial</i></sub> = <i>f</i>(<i>x</i><sub>1</sub>, <i>x</i><sub>2</sub>)</span></li> <li>Use the partially applied function with the remainder of the arguments to get the same result as one would have got from the original function. Continuing with the above assumption that would allow you to do <span class="formula"><i>f</i><sub><i>partial</i></sub>(<i>x</i><sub>3</sub>, <i>x</i><sub>4</sub>, ..., <i>x</i><sub><i>n</i></sub>)</span></li> </ul> </div> <div class="section" id="partial-application-in-python"> <h2>Partial application in python</h2> <p>Let us start with a simple function add</p> <pre class="code python literal-block"> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Adding 2 and 3 results in {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)))</span> </pre> <pre class="literal-block"> Adding 2 and 3 results in 5 </pre> <p>Let us look at two different ways of partial application in python</p> <div class="section" id="partial-application-using-nested-functions"> <h3>Partial application using nested functions</h3> <pre class="code python literal-block"> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="p">:</span> <span class="k">def</span> <span class="nf">add_a_to</span><span class="p">(</span><span class="n">b</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="k">return</span> <span class="n">add_a_to</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Adding 2 and 3 results in {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">2</span><span class="p">)(</span><span class="mi">3</span><span class="p">)))</span> <span class="n">add2</span> <span class="o">=</span> <span class="n">add</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Calling add with 2 alone returns {}, a {} with the name '{}'&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">add2</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">add2</span><span class="p">),</span> <span class="n">add2</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span> </pre> <pre class="literal-block"> Adding 2 and 3 results in 5 Calling add with 2 alone results in &lt;function add_a_to at 0x33eb848&gt; which is a &lt;type 'function'&gt; with the name 'add_a_to' </pre> <p>Note the following :</p> <ul class="simple"> <li>In this implementation we changed the calling syntax from <strong>add(2,3)</strong> to <strong>add(2)(3)</strong></li> <li>This is because we now make two function calls. The first call is <strong>add(2)</strong> which results in a function being returned called <strong>add_a_to</strong> . We then call <strong>add_a_to(3)</strong>. However since we just chain the function calls together, it is succinctly written as <strong>add(2)(3)</strong></li> <li>The implementation technique of nesting one function inside another is often referred to as <strong>nested functions</strong>. It requires structural modification of the function definition itself.</li> </ul> </div> <div class="section" id="partial-application-using-functools-partial"> <h3>Partial application using functools.partial</h3> <p>This technique does not require us to rewrite the function differently. It does allow us to first pass any arbitrary subset of arguments (eg. the second argument ie, 3 above), create a partially applied function using functools.partial and then use that to subsequently invoke the function with the remainder of the arguments. For reference see <a class="reference external" href="http://docs.python.org/3/library/functools.html">functools.partial</a> in python documentation</p> <pre class="code python literal-block"> <span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">partial</span> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="n">added_b_as_3</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="n">add</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;add partially applied with 2nd argument as 3 results in {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">added_b_as_3</span><span class="p">))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Adding 2 to 3 using partial application results in {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">added_b_as_3</span><span class="p">(</span><span class="mi">2</span><span class="p">)))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Partial applications using functools.partial can be inlined, &quot;</span> <span class="o">+</span> <span class="s2">&quot;though unlikely to be used so.&quot;</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;2 + 3, ie. partial(add,b=3)(2) = {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">partial</span><span class="p">(</span><span class="n">add</span><span class="p">,</span><span class="n">b</span><span class="o">=</span><span class="mi">3</span><span class="p">)(</span><span class="mi">2</span><span class="p">)))</span> </pre> <pre class="literal-block"> add partially applied with 2nd argument as 3 results in &lt;functools.partial object at 0x32c8470&gt; Adding 2 to 3 using partial application results in 5 Partial applications using functools.partial can be inlined, though unlikely to be used so. 2 + 3, ie. partial(add,b=3)(2) = 5 </pre> </div> </div> <div class="section" id="why-is-partial-application-useful"> <h2>Why is partial application useful ?</h2> <p>Partial application helps us implement spatial and temporal separation.</p> <ul class="simple"> <li>Spatial separation : Partial argument lists can be specified at different places in the code. This might be convenient from just a structuring perspective or perhaps from the perspective of lexical scoping.</li> <li>Temporal separation: Partial argument lists can be partially applied at different times in the execution of the code. This is convenient say when some arguments are to be included in the code itself, some are to be partially applied only at the program startup configuration time and some others might be required at runtime.</li> </ul> </div> <div class="section" id="a-tour-of-closures-and-lexical-scope"> <h2>A tour of closures and lexical scope</h2> <p>Typical usage of decorators leverages the capabilities afforded by closures and lexical scope. As a result let us take a quick side tour of these.</p> <p>According to the wikipedia page on <a class="reference external" href="http://en.wikipedia.org/wiki/Closure_(computer_science)">Closure</a></p> <blockquote> In programming languages, a closure (also lexical closure or function closure) is a function or reference to a function together with a referencing environment—a table storing a reference to each of the non-local variables (also called free variables or upvalues) of that function. A closure—unlike a plain function pointer—allows a function to access those non-local variables even when invoked outside its immediate lexical scope.</blockquote> <p>Also on the page of <a class="reference external" href="http://en.wikipedia.org/wiki/Scope_(computer_science)#Lexical_scoping_and_dynamic_scoping">Scope : Lexical vs. dynamic scoping</a> we can find the words</p> <blockquote> In lexical scoping (or lexical scope; also called static scoping or static scope), if a variable name's scope is a certain function, then its scope is the program text of the function definition: within that text, the variable name exists, and is bound to the variable's value, but outside that text, the variable name does not exist</blockquote> <p>Let us explore this in the context of python programs</p> <pre class="code python literal-block"> <span class="n">var1</span> <span class="o">=</span> <span class="mi">3</span> <span class="n">var2</span> <span class="o">=</span> <span class="mi">4</span> <span class="k">def</span> <span class="nf">foo</span><span class="p">()</span> <span class="p">:</span> <span class="n">var2</span> <span class="o">=</span> <span class="mi">5</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Value of var2 is {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var2</span><span class="p">))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Global var1={}, var2={}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">globals</span><span class="p">()[</span><span class="s2">&quot;var1&quot;</span><span class="p">],</span> <span class="nb">globals</span><span class="p">()[</span><span class="s2">&quot;var2&quot;</span><span class="p">]))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Locals are {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">locals</span><span class="p">()))</span> <span class="n">var1</span> <span class="o">=</span> <span class="mi">9</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Value of var1 is {} and var2 is {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var1</span><span class="p">,</span> <span class="n">var2</span><span class="p">))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Global var1={}, var2={}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">globals</span><span class="p">()[</span><span class="s2">&quot;var1&quot;</span><span class="p">],</span> <span class="nb">globals</span><span class="p">()[</span><span class="s2">&quot;var2&quot;</span><span class="p">]))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Locals are {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">locals</span><span class="p">()))</span> <span class="n">foo</span><span class="p">()</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Outside the function, value of var1 is {} and var2 is {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var1</span><span class="p">,</span> <span class="n">var2</span><span class="p">))</span> </pre> <pre class="literal-block"> Value of var2 is 5 Global var1=3, var2=4 Locals are {'var2': 5} Value of var1 is 9 and var2 is 5 Global var1=3, var2=4 Locals are {'var1': 9, 'var2': 5} Outside the function, value of var1 is 3 and var2 is 4 </pre> <p>I would like to highlight the following</p> <ul class="simple"> <li>Variables declared in the global scope are accessible to a function explicitly via the <em>globals()</em> function</li> <li>Variables declared within the function scope are accessible to a function either implicitly or explicitly via the <em>locals()</em> function</li> <li>A global variable redeclared within the function scope results in two variables, one in the global and another in local scope. However the local binding shadows the global binding.</li> <li>When a function declaration concludes, its scope is over and the local bindings in that function cease to be relevant</li> </ul> <p>Now let us take a look at another program which uses nested functions and will require us to revisit the last point I make above.</p> <pre class="code python literal-block"> <span class="n">var</span> <span class="o">=</span> <span class="mi">5</span> <span class="k">def</span> <span class="nf">outer</span><span class="p">(</span><span class="n">arg</span><span class="p">):</span> <span class="n">var</span> <span class="o">=</span> <span class="n">arg</span> <span class="k">def</span> <span class="nf">inner</span><span class="p">()</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The value of var is {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var</span><span class="p">))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;local value of var is {}. Now being incremented&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var</span><span class="p">))</span> <span class="n">var</span> <span class="o">=</span> <span class="n">arg</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">return</span> <span class="n">inner</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The value of the global var is {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var</span><span class="p">))</span> <span class="n">nested1</span> <span class="o">=</span> <span class="n">outer</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span> <span class="n">nested2</span> <span class="o">=</span> <span class="n">outer</span><span class="p">(</span><span class="mi">99</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The free variables carried by nested1 from outer scope are {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="nb">zip</span><span class="p">(</span><span class="n">nested1</span><span class="o">.</span><span class="vm">__code__</span><span class="o">.</span><span class="n">co_freevars</span><span class="p">,</span> <span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">cell_contents</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">nested1</span><span class="o">.</span><span class="vm">__closure__</span><span class="p">))))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The free variables carried by nested2 from outer scope are {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="nb">zip</span><span class="p">(</span><span class="n">nested2</span><span class="o">.</span><span class="vm">__code__</span><span class="o">.</span><span class="n">co_freevars</span><span class="p">,</span> <span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">cell_contents</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">nested2</span><span class="o">.</span><span class="vm">__closure__</span><span class="p">))))</span> <span class="n">nested1</span><span class="p">()</span> <span class="n">nested2</span><span class="p">()</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The value of the global var still is {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">var</span><span class="p">))</span> </pre> <pre class="literal-block"> The value of the global var is 5 local value of var is 6. Now being incremented local value of var is 99. Now being incremented The free variables carried by nested1 from outer scope are [('var', 7)] The free variables carried by nested2 from outer scope are [('var', 100)] The value of var is 7 The value of var is 100 The value of the global var still is 5 </pre> <p>Now note the following :</p> <ul class="simple"> <li><em>var</em> is created in the global scope, its value is set to 5 and remains 5 throughout the program</li> <li>There is another <em>var</em> created within the scope of the <em>outer</em> function which shadows the global var within the lexical scope of outer.</li> <li>The local value of <em>var</em> is initialised to the value passed as a parameter to <em>outer</em>. That is the value visible to inner() at declaration time</li> <li>The local value of <em>var</em> is incremented after the <em>inner</em> function has been declared.</li> <li>In general the local value of <em>var</em> declared is no longer valid once the function declaration of <em>outer</em> ends, since thats where its lexical scope ends.</li> <li>However if a nested function is declared and returned from <em>outer</em> (in this case <em>inner</em>), then such a function continues to carry the variables available to it at declaration time. This is exactly what closures are, ie. the function <em>inner</em> closes over the scope of the function <em>outer</em>.</li> <li>The variable value in the closure is not a snapshot of the value at the point in time the function was declared. It will reflect any changes after the inner function was declared but before the outer function got over</li> <li>The variable values are passed to the nested function via its _<em>closure_</em> attribute (variable names via _<em>code</em>_.co_freevars)</li> </ul> <p>To summarise the necessary learnings :</p> <ul class="simple"> <li>Nested functions help us implement partial application of functions</li> <li>Any variables declared in the lexical scope of the outer nested functions are available to the inner nested functions even after the outer function declaration is over.</li> <li>A new local scope gets created whenever a outer nested function is called. Any returned inner functions, as a result multiple outer function invocations, get independent copies of the local scope of each execution of the outer function.</li> </ul> </div> <div class="section" id="how-is-partial-application-useful"> <h2>How is partial application useful ?</h2> <p>There are a number of uses of partial applications. Some of these are</p> <div class="section" id="creating-specialised-functions-from-general-functions"> <h3>Creating specialised functions from general functions</h3> <p>Many a times functions we can easily associate with are but specialised versions of more general functions. Writing more general functions allows is a better way to follow the <strong>Do not repeat yourself (DRY)</strong> principle. Specialising them can allow for easier readibility when using them, since they can be more intuitive to understand at call site.</p> <p>A simple generic function might be declared as follows</p> <pre class="code python literal-block"> <span class="k">def</span> <span class="nf">exponent_of</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="n">exponent</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">base</span> <span class="o">**</span> <span class="n">exponent</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;3 to the power 4 is {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">exponent_of</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">)))</span> </pre> <pre class="literal-block"> 3 to the power 4 is 81 </pre> <p>However a more specialised function might make for easier reading. While you could just as easily use <em>functools.partial</em>, the example below shows partial application using nested functions</p> <pre class="code python literal-block"> <span class="k">def</span> <span class="nf">nth_power</span><span class="p">(</span><span class="n">exponent</span><span class="p">)</span> <span class="p">:</span> <span class="k">def</span> <span class="nf">exponent_of</span><span class="p">(</span><span class="n">base</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">base</span> <span class="o">**</span> <span class="n">exponent</span> <span class="k">return</span> <span class="n">exponent_of</span> <span class="n">square</span> <span class="o">=</span> <span class="n">nth_power</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="n">cube</span> <span class="o">=</span> <span class="n">nth_power</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The square of 3 is {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">square</span><span class="p">(</span><span class="mi">3</span><span class="p">)))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The cube of 4 is {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">cube</span><span class="p">(</span><span class="mi">4</span><span class="p">)))</span> </pre> <pre class="literal-block"> The square of 3 is 9 The cube of 4 is 64 </pre> </div> <div class="section" id="temporal-separation-of-function-arguments"> <h3>Temporal separation of function arguments</h3> <p>In some cases, a function may need many arguments, some of which are specified and available at different points in time than others. A usual way of dealing with this is to carry forward all the available arguments to the eventual call site. Sometimes those that are specified early on are rarely if ever modified later. It becomes inconvenient to carry around all these variables in the namespace. An alternative is to create a partially applied function and carry forward only the partially applied function to the eventual call site. eg.</p> <pre class="code python literal-block"> <span class="c1"># Conventional method</span> <span class="k">def</span> <span class="nf">query_database</span><span class="p">(</span><span class="n">userid</span><span class="p">,</span> <span class="n">password</span><span class="p">,</span> <span class="n">query</span><span class="p">)</span> <span class="p">:</span> <span class="c1"># do query</span> <span class="c1"># return results</span> <span class="k">def</span> <span class="nf">bar</span><span class="p">(</span><span class="n">userid</span><span class="p">,</span> <span class="n">password</span><span class="p">):</span> <span class="k">return</span> <span class="n">query_database</span><span class="p">(</span><span class="n">userid</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span> <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">userid</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">bar</span><span class="p">(</span><span class="n">userid</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span> <span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">userid</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span> <span class="p">:</span> <span class="c1"># .. lot of code here .. eventually reaching</span> <span class="n">foo</span><span class="p">(</span><span class="n">userid</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span> <span class="c1"># Alternative method</span> <span class="k">def</span> <span class="nf">get_query_agent</span><span class="p">(</span><span class="n">userid</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span> <span class="k">def</span> <span class="nf">do_query</span><span class="p">(</span><span class="n">query</span><span class="p">)</span> <span class="p">:</span> <span class="c1"># do query</span> <span class="c1"># return results</span> <span class="k">return</span> <span class="n">do_query</span> <span class="k">def</span> <span class="nf">bar</span><span class="p">(</span><span class="n">querying_func</span><span class="p">):</span> <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="n">querying_func</span><span class="p">)</span> <span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">querying_func</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">bar</span><span class="p">(</span><span class="n">querying_func</span><span class="p">)</span> <span class="k">def</span> <span class="nf">main</span><span class="p">(</span><span class="n">userid</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span> <span class="p">:</span> <span class="n">query_agent</span> <span class="o">=</span> <span class="n">get_query_agent</span><span class="p">(</span><span class="n">userid</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span> <span class="c1"># .. much further down the line</span> <span class="n">foo</span><span class="p">(</span><span class="n">query_agent</span><span class="p">)</span> </pre> </div> <div class="section" id="use-closures-instead-of-classes"> <h3>Use closures instead of classes</h3> <p>It is not uncommon to see classes with only one significant public method. In many such scenarios, the class constructor is used to specify the arguments of the method, and the method itself is used to perform the desired behaviour. This gives us temporal separation (object construction and method execution) and encapsulation of the arguments. It so happens the same can be done through using closures as well. (Note: although the example below is quite similar to the one above they serve to describe different intents)</p> <pre class="code python literal-block"> <span class="c1"># traditional method</span> <span class="k">class</span> <span class="nc">Connection</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="n">userid</span><span class="p">,</span> <span class="n">password</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">userid</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">userid</span> <span class="bp">self</span><span class="o">.</span><span class="n">password</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">password</span> <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">):</span> <span class="c1"># execute SQL using userid, password.</span> <span class="c1"># return results</span> <span class="n">c</span> <span class="o">=</span> <span class="n">Connection</span><span class="p">(</span><span class="s2">&quot;myuserid&quot;</span><span class="p">,</span> <span class="s2">&quot;mypassword&quot;</span><span class="p">)</span> <span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">&quot;select 'x' from dual;&quot;</span><span class="p">)</span> <span class="c1"># Using closures</span> <span class="k">def</span> <span class="nf">get_connection</span><span class="p">(</span><span class="n">userid</span><span class="p">,</span> <span class="n">password</span><span class="p">)</span> <span class="p">:</span> <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="n">sql</span><span class="p">):</span> <span class="c1"># execute SQL using userid, password &amp; sql</span> <span class="c1"># return results</span> <span class="k">return</span> <span class="n">execute</span> <span class="n">c</span> <span class="o">=</span> <span class="n">get_connection</span><span class="p">(</span><span class="s2">&quot;myuserid&quot;</span><span class="p">,</span> <span class="s2">&quot;mypassword&quot;</span><span class="p">)</span> <span class="n">c</span><span class="p">(</span><span class="s2">&quot;select 'x' from dual;&quot;</span><span class="p">)</span> </pre> </div> <div class="section" id="aside-currying-vs-partial-applications"> <h3>Aside: Currying vs. Partial Applications</h3> <p>As per the wikipedia article on <a class="reference external" href="http://en.wikipedia.org/wiki/Currying">Currying</a>,</p> <blockquote> In mathematics and computer science, currying is the technique of transforming a function that takes multiple arguments (or a tuple of arguments) in such a way that it can be called as a chain of functions, each with a single argument (partial application).</blockquote> <p>It further goes on to define it as follows</p> <blockquote> Given a function f of type <span class="formula"><span class="scriptstyle"><i>f</i></span> : (<i>X</i> × <i>Y</i>) → <i>Z</i></span> , currying it makes a function <span class="formula"><span class="scriptstyle"><span class="text">curry</span></span>(<i>f</i>) : <i>X</i> → (<i>Y</i> → <i>Z</i>)</span> . That is, <span class="formula"><span class="scriptstyle"><span class="text">curry</span></span>(<i>f</i>)</span> takes an argument of type <span class="formula"><span class="scriptstyle"><i>X</i></span></span> and returns a function of type <span class="formula"><span class="scriptstyle"><i>Y</i></span> → <i>Z</i></span> .</blockquote> <p>With a further view to disambiguate the term from partial applications, it goes on to state that</p> <blockquote> <p>Currying and partial function application are often conflated. One of the significant differences between the two is that a call to a partially applied function returns the result right away, not another function down the currying chain; this distinction can be illustrated clearly for functions whose arity is greater than two.</p> <p>Given a function of type <span class="formula"><span class="scriptstyle"><i>f</i></span> : (<i>X</i> × <i>Y</i> × <i>Z</i>) → <i>N</i></span> , currying produces <span class="formula"><span class="scriptstyle"><span class="text">curry</span></span>(<i>f</i>) : <i>X</i> → (<i>Y</i> → (<i>Z</i> → <i>N</i>))</span> . That is, while an evaluation of the first function might be represented as <span class="formula"><span class="scriptstyle"><i>f</i></span>(1, 2, 3)</span>, evaluation of the curried function would be represented as <span class="formula"><span class="scriptstyle"><i>f</i></span><sub><span class="text">curried</span></sub>(1)(2)(3)</span>, applying each argument in turn to a single-argument function returned by the previous invocation. Note that after calling <span class="formula"><span class="scriptstyle"><i>f</i></span><sub><span class="text">curried</span></sub>(1)</span>, we are left with a function that takes a single argument and returns another function, not a function that takes two arguments.</p> </blockquote> <p>Currying thus is just a special case of partial application, where we partially apply the arguments, one argument at a time, in the order in which they are declared. In general currying (especially as defined above) is not a useful notion to attempt to use in python. However you might find it getting used in context of python, and more often than not, it might mean partial application. How would currying actually look like, well for the curious, here's a function that can curry another</p> <pre class="code python literal-block"> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span><span class="n">c</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span> <span class="k">def</span> <span class="nf">curry</span><span class="p">(</span><span class="n">func</span><span class="p">)</span> <span class="p">:</span> <span class="n">args</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">if</span> <span class="n">func</span><span class="o">.</span><span class="vm">__code__</span><span class="o">.</span><span class="n">co_argcount</span> <span class="o">==</span> <span class="mi">0</span> <span class="p">:</span> <span class="k">return</span> <span class="n">func</span><span class="p">()</span> <span class="k">def</span> <span class="nf">wrap</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span> <span class="p">:</span> <span class="n">args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">==</span> <span class="n">func</span><span class="o">.</span><span class="vm">__code__</span><span class="o">.</span><span class="n">co_argcount</span> <span class="p">:</span> <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span> <span class="k">else</span> <span class="p">:</span> <span class="k">return</span> <span class="n">wrap</span> <span class="k">return</span> <span class="n">wrap</span> <span class="n">curried_add</span> <span class="o">=</span> <span class="n">curry</span><span class="p">(</span><span class="n">add</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="n">curried_add</span><span class="p">(</span><span class="mi">3</span><span class="p">)(</span><span class="mi">4</span><span class="p">)(</span><span class="mi">5</span><span class="p">))</span> </pre> <pre class="literal-block"> 12 </pre> </div> </div> <div class="section" id="decorators"> <h2>Decorators</h2> <p>Decorators were introduced as a syntactic addition to python language in <a class="reference external" href="http://www.python.org/dev/peps/pep-0318/">PEP-318</a>. It is often considered that the name decorators comes from the gang of four book on <a class="reference external" href="http://www.amazon.com/Design-Patterns-Elements-Object-Oriented-ebook/dp/B000SEIBB8">Design Patterns</a>. The PEP in fact clarifies it is not so (and acknowledges that the name has drawn its share of criticism and perhaps a better name will come up eventually - it did not).</p> <blockquote> The name 'decorator' probably owes more to its use in the compiler area -- a syntax tree is walked and annotated</blockquote> <p>I've heard it being discussed that decorators are just nested or partially applied functions. While that is accurate, it misses an important distinction. Although both use the same technique, partial application is usually used to split a task into two. Decorators on the other hand are used to wrap one or more add-on tasks around a core task. So there are many many scenarios where you would use partially applied functions, but you would not want to use decorators.</p> <p>Let us once again start with the simple add function which we shall subsequently decorate</p> <pre class="code python literal-block"> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Adding 2 and 3 results in {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)))</span> <span class="c1"># Note the following.</span> <span class="c1"># we shall contrast this later when function wrapping kicks in</span> <span class="kn">import</span> <span class="nn">inspect</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The specification for add is name={} args={}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">add</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getargspec</span><span class="p">(</span><span class="n">add</span><span class="p">)))</span> </pre> <pre class="literal-block"> Adding 2 and 3 results in 5 The specification for add is name=add args=ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=None) </pre> <p>We shall now decorate this function with a tracer. A function which can print entry and exit to the desired function</p> <pre class="code python literal-block"> <span class="k">def</span> <span class="nf">trace</span><span class="p">(</span><span class="n">func</span><span class="p">)</span> <span class="p">:</span> <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Entering {} with arguments {} {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">))</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Leaving {} with result {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">ret</span><span class="p">))</span> <span class="k">return</span> <span class="n">ret</span> <span class="k">return</span> <span class="n">wrapper</span> <span class="nd">&#64;trace</span> <span class="c1"># &lt;--- This is how a decorator is applied</span> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Adding 2 and 3 results in {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)))</span> <span class="kn">import</span> <span class="nn">inspect</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The specification for add is name={} args={}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">add</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getargspec</span><span class="p">(</span><span class="n">add</span><span class="p">)))</span> </pre> <pre class="literal-block"> Entering add with arguments (2, 3) {} Leaving add with result 5 Adding 2 and 3 results in 5 The specification for add is name=wrapper args=ArgSpec(args=[], varargs='args', keywords='kwargs', defaults=None) </pre> <p>Note that applying the trace function as a decorator using &quot;&#64;trace&quot; did not just create a new function. It did create a new wrapped function, but also replaced the function in the namespace for the name &quot;add&quot; with the new wrapped function. The original function add is no longer available to be directly accessed from the namespace.</p> <p>Decorators are just the outer function in a nested function. The decorated function is essentially a closure which retains access to the variables as registered by the decorator in its local namespace and the rules of how such variables are lexically scoped but accessible by closures continue to apply.</p> <p>The function name is now changed if you were to inspect it. So add._<em>name_</em> now returns <em>wrapper</em> and not <em>add</em>. That can be corrected by using the <em>&#64;wraps</em> decorator from the functools module.</p> <pre class="code python literal-block"> <span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">wraps</span> <span class="k">def</span> <span class="nf">trace</span><span class="p">(</span><span class="n">func</span><span class="p">)</span> <span class="p">:</span> <span class="nd">&#64;wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span> <span class="c1"># &lt;-- Applying the wraps decorator helps in the wrapped function retaining the original name</span> <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Entering {} with arguments {} {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">))</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Leaving {} with result {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">ret</span><span class="p">))</span> <span class="k">return</span> <span class="n">ret</span> <span class="k">return</span> <span class="n">wrapper</span> <span class="nd">&#64;trace</span> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Adding 2 and 3 results in {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)))</span> <span class="kn">import</span> <span class="nn">inspect</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The specification for add is name={} args={}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">add</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getargspec</span><span class="p">(</span><span class="n">add</span><span class="p">)))</span> </pre> <pre class="literal-block"> Entering add with arguments (2, 3) {} Leaving add with result 5 Adding 2 and 3 results in 5 The specification for add is name=add args=ArgSpec(args=[], varargs='args', keywords='kwargs', defaults=None) </pre> <p>Here thankfully the name of the wrapped function is now <em>add</em> should anyone care to inspect the function. The argument specification still remains different. And it should if trace has to be a generic function which can trace all functions, not just those which have two parameters.</p> <p>The takeaway is that because decorators implement functions in place, <strong>some information about the functions can get lost to function inspectors</strong>. The same would not happen if you chose to explicitly wrap a function without using the decorator syntax as in the following. There are other effects as well eg. you can no longer easily unit test the original undecorated function. If the decorator introduces side effects or say changes the return value you have to deal with it in your unit tests.</p> <p>The code below once again demonstrates the scenario where trace is used as a wrapper, without actually using it as a decorator</p> <pre class="code python literal-block"> <span class="k">def</span> <span class="nf">trace</span><span class="p">(</span><span class="n">func</span><span class="p">)</span> <span class="p">:</span> <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Entering {} with arguments {} {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">))</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Leaving {} with result {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">ret</span><span class="p">))</span> <span class="k">return</span> <span class="n">ret</span> <span class="k">return</span> <span class="n">wrapper</span> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="c1"># Not traced</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Adding 2 and 3 results in {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">)))</span> <span class="c1"># traced add</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Adding 4 and 5 results in {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">trace</span><span class="p">(</span><span class="n">add</span><span class="p">)(</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">)))</span> <span class="kn">import</span> <span class="nn">inspect</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The specification for add is name={} args={}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">add</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getargspec</span><span class="p">(</span><span class="n">add</span><span class="p">)))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The specification for trace is name={} args={}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">trace</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getargspec</span><span class="p">(</span><span class="n">trace</span><span class="p">)))</span> </pre> <pre class="literal-block"> Adding 2 and 3 results in 5 Entering add with arguments (4, 5) {} Leaving add with result 9 Adding 4 and 5 results in 9 The specification for add is name=add args=ArgSpec(args=['a', 'b'], varargs=None, keywords=None, defaults=None) The specification for trace is name=trace args=ArgSpec(args=['func'], varargs=None, keywords=None, defaults=None) </pre> <div class="section" id="decorators-with-arguments"> <h3>Decorators with arguments</h3> <p>Once one starts using decorators, pretty soon one wants to parameterise their behaviour. For example we might want to decide whether to log the entry/exit arguments and return values at the point where we apply the trace decorator to each function. I have seen people struggle with decorators when it gets to using decorators with arguments. But if one understands the basics of how decorators work, it is actually very easy to figure it out.</p> <p>Part of the confusion stems from what exactly is a decorator. For the purposes of this discussion a decorator is a function which takes exactly one argument, a function, and returns exactly one another, which is also a function. If you want to parameterise a decorator, the way (using a term used by the PEP) is to create a <em>decomaker</em>. A decomaker is a function which takes a bunch of arguments and return a decorator.</p> <p>Let us see the parameterised implementation of <em>trace</em> below. Under the definitions we just laid down, trace is no longer a decorator. It is a decomaker. ie. it takes a bunch of arguments and returns a decorator. That decorator in turn decorates the method add.</p> <p>For the remainder of this post, I shall use decorator as a generic placeholder for both decorators and decomakers (as generally most writings in python do). But hopefully the context should allow you to disambiguate as necessary.</p> <pre class="code python literal-block"> <span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">wraps</span> <span class="k">def</span> <span class="nf">trace</span><span class="p">(</span><span class="n">trace_arguments</span> <span class="o">=</span> <span class="bp">False</span><span class="p">)</span> <span class="p">:</span> <span class="k">def</span> <span class="nf">decorator</span><span class="p">(</span><span class="n">func</span><span class="p">)</span> <span class="p">:</span> <span class="nd">&#64;wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span> <span class="c1"># &lt;-- how the wrapped function retains the original name</span> <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="p">:</span> <span class="k">if</span> <span class="n">trace_arguments</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Entering {} with arguments {} {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">))</span> <span class="k">else</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Entering {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="k">if</span> <span class="n">trace_arguments</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Leaving {} with result {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">ret</span><span class="p">))</span> <span class="k">else</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Leaving {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span> <span class="k">return</span> <span class="n">ret</span> <span class="k">return</span> <span class="n">wrapper</span> <span class="k">return</span> <span class="n">decorator</span> <span class="nd">&#64;trace</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="nd">&#64;trace</span><span class="p">()</span> <span class="k">def</span> <span class="nf">subtract</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">a</span> <span class="o">-</span> <span class="n">b</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;3 plus 5 is {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">)))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;5 minus 3 is {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">subtract</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">)))</span> </pre> <pre class="literal-block"> Entering add with arguments (3, 5) {} Leaving add with result 8 3 plus 5 is 8 Entering subtract Leaving subtract 5 minus 3 is 2 </pre> </div> <div class="section" id="composing-decorators"> <h3>Composing decorators</h3> <p>Unsurprisingly decorators can be composed. After all they are just functions which accept one function and return another function, so composing should be trivial. This is often popularly referred to as chaining.</p> <p>Here's a example</p> <pre class="code python literal-block"> <span class="kn">from</span> <span class="nn">functools</span> <span class="kn">import</span> <span class="n">wraps</span> <span class="k">def</span> <span class="nf">trace</span><span class="p">(</span><span class="n">trace_arguments</span> <span class="o">=</span> <span class="bp">False</span><span class="p">)</span> <span class="p">:</span> <span class="k">def</span> <span class="nf">decorator</span><span class="p">(</span><span class="n">func</span><span class="p">)</span> <span class="p">:</span> <span class="nd">&#64;wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span> <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="p">:</span> <span class="k">if</span> <span class="n">trace_arguments</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Entering {} with arguments {} {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span> <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">))</span> <span class="k">else</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Entering {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span> <span class="n">ret</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="k">if</span> <span class="n">trace_arguments</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Leaving {} with result {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">ret</span><span class="p">))</span> <span class="k">else</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Leaving {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">func</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span> <span class="k">return</span> <span class="n">ret</span> <span class="k">return</span> <span class="n">wrapper</span> <span class="k">return</span> <span class="n">decorator</span> <span class="c1"># using a global for brevity</span> <span class="n">function_calls</span> <span class="o">=</span> <span class="mi">0</span> <span class="k">def</span> <span class="nf">track</span><span class="p">(</span><span class="n">func</span><span class="p">)</span> <span class="p">:</span> <span class="nd">&#64;wraps</span><span class="p">(</span><span class="n">func</span><span class="p">)</span> <span class="k">def</span> <span class="nf">wrapper</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> <span class="k">global</span> <span class="n">function_calls</span> <span class="n">function_calls</span> <span class="o">=</span> <span class="n">function_calls</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">return</span> <span class="n">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> <span class="k">return</span> <span class="n">wrapper</span> <span class="nd">&#64;track</span> <span class="nd">&#64;trace</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span> <span class="k">def</span> <span class="nf">add</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Function calls so far {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">function_calls</span><span class="p">))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;3 + 5 = {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">5</span><span class="p">)))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;2 + 7 = {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">add</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="mi">7</span><span class="p">)))</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Function calls so far {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">function_calls</span><span class="p">))</span> </pre> <pre class="literal-block"> Function calls so far 0 Entering add with arguments (3, 5) {} Leaving add with result 8 3 + 5 = 8 Entering add with arguments (2, 7) {} Leaving add with result 9 2 + 7 = 9 Function calls so far 2 </pre> </div> </div> <div class="section" id="use-of-function-decorators"> <h2>Use of function decorators</h2> <p>Function decorators are frequently used and are quite helpful. I doubt if I can create a comprehensive set of use cases, but will list some of the ones where I've observed decorators are useful.</p> <div class="section" id="applying-aspects-to-functions"> <h3>Applying aspects to functions</h3> <p>Decorators are frequently used as an implementation technique for some facets of <a class="reference external" href="http://en.wikipedia.org/wiki/Aspect-oriented_programming">Aspect Oriented Programming</a>. The trace decorator (or decomaker) I described above implements a tracing aspect. You could imagine other decorators which for example update global counters (to keep track of how many web requests have been served), authorisation decorators for implementing ACLs, or <a class="reference external" href="http://en.wikipedia.org/wiki/Memoization">memoization</a> ie. to cache results computed earlier and serve them if the function was invoked with the same arguments again. eg. <a class="reference external" href="http://docs.python.org/3.4/library/functools.html">functools.lru_cache</a> is a memoization decorator.</p> </div> <div class="section" id="autoregistration-of-function-handlers"> <h3>Autoregistration of function handlers</h3> <p>Many web frameworks allow you to configure the url route around its handler function using a decorator as below.</p> <pre class="code python literal-block"> <span class="nd">&#64;route</span><span class="p">(</span><span class="s2">&quot;/show&quot;</span><span class="p">)</span> <span class="k">def</span> <span class="nf">show</span><span class="p">(</span><span class="n">request</span><span class="p">)</span> <span class="p">:</span> <span class="c1"># process the request and return the generated HTML</span> </pre> <p>In this case there is a global routes repository list which contains the routes, and their handler functions. In the above example &quot;/show&quot; is the url_prefix that if matched should result in the show handler function being called. The routes engine is matches an incoming request to the routes in the list, and if a match is found, then the corresponding handler function is called giving it the request as an argument. Without decorators, you would need a separate place where all the routes and their handlers could be configured (sometimes called wiring up). With decorators like above, this configuration can be done at the site of each function declaration. (The part where the runtime discovers all such functions and calls them once in order to actually perform the registration is beyond the scope of this post). We could use decorators similarly to register event listeners etc.</p> </div> <div class="section" id="markers-metadata-and-housekeeping"> <h3>Markers, metadata and housekeeping</h3> <p>Sometimes we want to mark a function as having a specific characteristic so that it can be processed differently at runtime. eg. celery marks each task handler using <em>&#64;app.task</em> decorator. One might want to assign more metadata to the function. Decorators can also be used for basic housekeeping (eg. the <em>wraps</em> decorator seen earlier). Python itself requires us to use <em>&#64;staticmethod</em> and <em>&#64;classmethod</em> decorators to mark appropriate methods on a class.</p> </div> </div> <div class="section" id="class-decorators"> <h2>Class decorators</h2> <p>Class decorators were introduced in Python 2.6 using <a class="reference external" href="http://www.python.org/dev/peps/pep-3129/">PEP-3129</a>. A class decorator is a function which gets a python class as an input. It performs the necessary decoration activities on the class and returns the decorated class. Much of what class decorators achieve can also be achieved through metaprogramming, and more often than not, being able to write good class decorator requires a decent understanding of the class meta model.</p> <p>The following example demonstrates how a class decorator can be used to substitute a class constructor to print a statement and then eventually call the original class constructor.</p> <pre class="code python literal-block"> <span class="k">def</span> <span class="nf">trace</span><span class="p">(</span><span class="n">class_</span><span class="p">):</span> <span class="n">original_init</span> <span class="o">=</span> <span class="n">class_</span><span class="o">.</span><span class="fm">__init__</span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kws</span><span class="p">):</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;Instantiating an object of class {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">class_</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span> <span class="n">original_init</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kws</span><span class="p">)</span> <span class="n">class_</span><span class="o">.</span><span class="fm">__init__</span> <span class="o">=</span> <span class="fm">__init__</span> <span class="k">return</span> <span class="n">class_</span> <span class="nd">&#64;trace</span> <span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span> <span class="n">foo</span> <span class="o">=</span> <span class="n">Foo</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span> <span class="k">print</span><span class="p">(</span><span class="s2">&quot;The value of foo is {}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">foo</span><span class="o">.</span><span class="n">value</span><span class="p">))</span> </pre> <pre class="literal-block"> Instantiating an object of class Foo The value of foo is 5 </pre> </div> A screencast overview of python enums as introduced by PEP 4352013-09-28T14:00:00+05:302013-09-28T14:00:00+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2013-09-28:/2013/09/screencast-overview-of-python-enums-pep-435/<p class="first last">Screencast and helper notes as a overview of python enums introduced by PEP 435</p> <p>Earlier in the week, I made my first screencast to give an overview of PEP 435 which introduces enums to the python standard library.</p> <iframe width="800" height="600" src="//www.youtube.com/embed/6sKWWlo6GW8" frameborder="0" allowfullscreen></iframe>&nbsp;<p>The code below is the set of python notes I had prepared as I made the screencast.</p> <pre class="code python literal-block"> <span class="c1"># What are enumerations ?</span> <span class="c1"># A series of immutable discrete arbitrary values</span> <span class="c1"># of some type</span> <span class="c1"># may or may not have a semantic meaning</span> <span class="c1"># Why need them as a separate construct ?</span> <span class="c1"># You can represent them as constants with values eg. ints</span> <span class="c1"># But what does GREEN * 2 mean ?</span> <span class="c1"># And if GREEN = 2, when you print / log GREEN, you expect to see GREEN not 2</span> <span class="c1"># And sometimes you want to treat all the values as a set eg. days of week</span> <span class="c1"># PEP 435 is the formal python document introducing enums</span> <span class="c1"># Implemented in 3.4. You will need a python 3.4 runtime</span> <span class="c1"># How are enumerations constructed ?</span> <span class="kn">from</span> <span class="nn">enum</span> <span class="kn">import</span> <span class="n">Enum</span> <span class="c1"># There are two ways to construct enums. The first is a functional API. eg.</span> <span class="n">Direction</span> <span class="o">=</span> <span class="n">Enum</span><span class="p">(</span><span class="s1">'Direction'</span><span class="p">,</span><span class="s1">'North East South West'</span><span class="p">)</span> <span class="c1"># Direction is an enumeration</span> <span class="c1"># It has 4 members</span> <span class="nb">list</span><span class="p">(</span><span class="n">Direction</span><span class="p">)</span> <span class="c1"># Each member has a name and an associated value</span> <span class="c1"># This is more apparent when you render the __repr__ of the member</span> <span class="nb">list</span><span class="p">(</span><span class="n">Direction</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span> <span class="c1"># in this case the values were automatically provided.</span> <span class="c1"># When values are automatically generated they are integers starting from 1</span> <span class="c1"># You could also pass the member name and value as 2 tuples</span> <span class="n">StrDirection</span> <span class="o">=</span> <span class="n">Enum</span> <span class="p">(</span><span class="s1">'StrDirection'</span><span class="p">,((</span><span class="s2">&quot;North&quot;</span><span class="p">,</span> <span class="s2">&quot;N&quot;</span><span class="p">),(</span><span class="s2">&quot;East&quot;</span><span class="p">,</span><span class="s2">&quot;E&quot;</span><span class="p">),</span> <span class="p">(</span><span class="s2">&quot;West&quot;</span><span class="p">,</span> <span class="s2">&quot;W&quot;</span><span class="p">),</span> <span class="p">(</span><span class="s2">&quot;South&quot;</span><span class="p">,</span> <span class="s2">&quot;S&quot;</span><span class="p">)))</span> <span class="nb">list</span><span class="p">(</span><span class="n">StrDirection</span><span class="p">)</span> <span class="c1"># Or as a dict</span> <span class="n">AngleDirection</span> <span class="o">=</span> <span class="n">Enum</span> <span class="p">(</span><span class="s1">'AngleDirection'</span><span class="p">,{</span><span class="s2">&quot;North&quot;</span><span class="p">:</span> <span class="mi">90</span><span class="p">,</span><span class="s2">&quot;East&quot;</span><span class="p">:</span><span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;West&quot;</span><span class="p">:</span> <span class="mi">270</span><span class="p">,</span> <span class="s2">&quot;South&quot;</span><span class="p">:</span> <span class="mi">180</span><span class="p">})</span> <span class="nb">list</span><span class="p">(</span><span class="n">AngleDirection</span><span class="p">)</span> <span class="c1"># Note: while Direction is an iterable in definition order,</span> <span class="c1"># you can't pick up the zero'th element eg. Direction[0]</span> <span class="c1"># ie. you cannot index by position</span> <span class="c1"># However Direction.North and Direction[&quot;North&quot;] are both valid</span> <span class="n">Direction</span><span class="o">.</span><span class="n">North</span> <span class="n">Direction</span><span class="p">[</span><span class="s2">&quot;North&quot;</span><span class="p">]</span> <span class="c1"># Curiously a enum can also be looked up using a value by using the call operator</span> <span class="n">Direction</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># A key property of an enum is that its members are hashable</span> <span class="c1"># Internal structure.</span> <span class="c1"># Every Enum type has a __members__ attribute of the type OrderedDict</span> <span class="n">Direction</span><span class="o">.</span><span class="n">__members__</span> <span class="nb">list</span><span class="p">(</span><span class="n">Direction</span><span class="o">.</span><span class="n">__members__</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="nb">list</span><span class="p">(</span><span class="n">Direction</span><span class="o">.</span><span class="n">__members__</span><span class="o">.</span><span class="n">values</span><span class="p">())</span> <span class="nb">list</span><span class="p">(</span><span class="n">Direction</span><span class="o">.</span><span class="n">__members__</span><span class="o">.</span><span class="n">items</span><span class="p">())</span> <span class="c1"># enums are compared by identity and can be checked for equality</span> <span class="c1"># however you cannot perform ordered comparisons</span> <span class="c1"># Aliases are not returned when iterating</span> <span class="c1"># While two members cannot share the same name, they can share the same value (aliases)</span> <span class="c1"># There is only one member that is shared in case of aliases</span> <span class="k">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">name</span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">member</span> <span class="ow">in</span> <span class="n">Colour</span><span class="o">.</span><span class="n">__members__</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">member</span><span class="o">.</span><span class="n">name</span> <span class="o">!=</span> <span class="n">name</span><span class="p">))</span> <span class="c1"># An alternative way of defining an enum is a class</span> <span class="c1"># Enum classes can have instance and classmethods</span> <span class="k">class</span> <span class="nc">Timezone</span><span class="p">:</span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">offset</span><span class="p">,</span><span class="n">dst</span><span class="p">)</span> <span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">offset</span> <span class="o">=</span> <span class="n">offset</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span> <span class="o">=</span> <span class="n">dst</span> <span class="k">def</span> <span class="nf">earlier_than</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">)</span> <span class="p">:</span> <span class="c1"># ignoring dst</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">offset</span> <span class="o">&lt;</span> <span class="n">other</span><span class="o">.</span><span class="n">offset</span> <span class="k">def</span> <span class="fm">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">other</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">offset</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">offset</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">dst</span> <span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="s2">&quot;TZ({},{})&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">offset</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">)</span> <span class="k">def</span> <span class="fm">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="s2">&quot;TZ({},{})&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">offset</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">dst</span><span class="p">)</span> <span class="k">class</span> <span class="nc">Timezones</span><span class="p">(</span><span class="n">Enum</span><span class="p">):</span> <span class="n">IST</span> <span class="o">=</span> <span class="n">Timezone</span><span class="p">(</span><span class="mi">330</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span> <span class="n">GMT</span> <span class="o">=</span> <span class="n">Timezone</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span> <span class="n">UTC</span> <span class="o">=</span> <span class="n">Timezone</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span> <span class="n">EST</span> <span class="o">=</span> <span class="n">Timezone</span><span class="p">(</span><span class="o">-</span><span class="mi">300</span><span class="p">,</span> <span class="bp">False</span><span class="p">)</span> <span class="nd">&#64;classmethod</span> <span class="k">def</span> <span class="nf">all_zones</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span> <span class="c1"># Aliases are not returned when iterating</span> <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">n</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">cls</span><span class="p">)</span> <span class="nd">&#64;classmethod</span> <span class="k">def</span> <span class="nf">aliases</span><span class="p">(</span><span class="bp">cls</span><span class="p">):</span> <span class="k">return</span> <span class="p">[(</span><span class="n">name</span><span class="p">,</span> <span class="n">member</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">member</span> <span class="ow">in</span> <span class="bp">cls</span><span class="o">.</span><span class="n">__members__</span><span class="o">.</span><span class="n">items</span><span class="p">()</span> <span class="k">if</span> <span class="n">member</span><span class="o">.</span><span class="n">name</span> <span class="o">!=</span> <span class="n">name</span><span class="p">]</span> <span class="k">def</span> <span class="nf">offset</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span> <span class="p">:</span> <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span><span class="o">.</span><span class="n">offset</span> <span class="k">for</span> <span class="n">tz</span> <span class="ow">in</span> <span class="n">Timezones</span> <span class="p">:</span> <span class="k">print</span><span class="p">(</span><span class="n">tz</span><span class="p">)</span> <span class="n">Timezones</span><span class="o">.</span><span class="n">all_zones</span><span class="p">()</span> <span class="n">Timezones</span><span class="o">.</span><span class="n">IST</span><span class="o">.</span><span class="n">offset</span><span class="p">()</span> <span class="nb">list</span><span class="p">(</span><span class="n">Timezones</span><span class="p">)</span> <span class="nb">list</span><span class="p">(</span><span class="n">Timezones</span><span class="o">.</span><span class="n">__members__</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="nb">list</span><span class="p">(</span><span class="n">Timezones</span><span class="o">.</span><span class="n">__members__</span><span class="o">.</span><span class="n">values</span><span class="p">())</span> <span class="nb">id</span><span class="p">(</span><span class="n">Timezones</span><span class="o">.</span><span class="n">GMT</span><span class="p">)</span> <span class="nb">id</span><span class="p">(</span><span class="n">Timezones</span><span class="o">.</span><span class="n">UTC</span><span class="p">)</span> <span class="n">Timezones</span><span class="o">.</span><span class="n">IST</span> <span class="o">==</span> <span class="n">Timezones</span><span class="o">.</span><span class="n">GMT</span> <span class="n">Timezones</span><span class="o">.</span><span class="n">GMT</span> <span class="o">==</span> <span class="n">Timezones</span><span class="o">.</span><span class="n">IST</span> <span class="n">Timezone</span><span class="o">.</span><span class="n">UTC</span> <span class="o">&lt;</span> <span class="n">Timezone</span><span class="o">.</span><span class="n">IST</span> <span class="c1"># An enum which does not have any members can be subclassed. One which has cannot be</span> <span class="c1"># So far the values per se have not been important (except to distinguish between distinct / aliased members)</span> <span class="c1"># However in some cases it is important, and for that a special case Enum ie. IntEnum is defined</span> <span class="kn">from</span> <span class="nn">enum</span> <span class="kn">import</span> <span class="n">IntEnum</span> <span class="k">class</span> <span class="nc">Education</span><span class="p">(</span><span class="n">IntEnum</span><span class="p">):</span> <span class="n">Kindergarten</span> <span class="o">=</span> <span class="mi">1</span> <span class="n">Primary</span> <span class="o">=</span> <span class="mi">2</span> <span class="n">Secondary</span> <span class="o">=</span> <span class="mi">3</span> <span class="n">HigherSecondary</span> <span class="o">=</span> <span class="mi">4</span> <span class="n">Graduate</span> <span class="o">=</span> <span class="mi">5</span> <span class="c1"># elsewhere eg. US, this would be under graduate</span> <span class="n">PostGraduate</span> <span class="o">=</span> <span class="mi">6</span> <span class="c1"># and this would be called graduate</span> <span class="n">Doctorate</span> <span class="o">=</span> <span class="mi">7</span> <span class="c1"># This is an example where the value (and its ordering particularly is considered important)</span> <span class="c1"># Now operators relevant to the semantics of the mixed in type (in this case int) can be used</span> <span class="n">Education</span><span class="o">.</span><span class="n">Secondary</span> <span class="o">&gt;</span> <span class="n">Education</span><span class="o">.</span><span class="n">Primary</span> <span class="c1"># Note that this opens up semantic issues of equivalence across enum types. eg.</span> <span class="k">class</span> <span class="nc">Colour</span><span class="p">(</span><span class="n">IntEnum</span><span class="p">):</span> <span class="n">Red</span> <span class="o">=</span> <span class="mi">1</span> <span class="n">Green</span> <span class="o">=</span> <span class="mi">2</span> <span class="n">Blue</span> <span class="o">=</span> <span class="mi">3</span> <span class="n">Education</span><span class="o">.</span><span class="n">Secondary</span> <span class="o">==</span> <span class="n">Colour</span><span class="o">.</span><span class="n">Blue</span> <span class="c1"># Thus you can choose to bring in any value types consistent with their appropriate semantics</span> <span class="c1"># Note that this could lead to unexpected results eg.</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">Education</span><span class="o">.</span><span class="n">Graduate</span><span class="p">))</span> <span class="c1"># However IntEnum is just a special case of being able to mix in values of any types ie.</span> <span class="k">class</span> <span class="nc">IntEnum</span><span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="n">Enum</span><span class="p">)</span> <span class="p">:</span> <span class="k">pass</span> <span class="c1"># So use value types that are semantically consistent</span> </pre> "Exploring Java8 Lambdas. Part 1"2013-02-26T09:00:00+05:302013-02-26T09:00:00+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2013-02-26:/2013/02/exploring-java8-lambdas-part-1/<p>Lambda functions and closures are coming to Java 8. This has been a project that has been in progress for a while and recently I took the opportunity to take it for a test drive. </p> <h2>Information :</h2> <p>Some of the useful documentation for the project may be found at :</p> <ul> <li><a href="http://openjdk.java.net/projects/lambda/">Project Lambda</a> Home page.</li> <li><a href="http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html">State of the Lambda</a> : A old paper (Dec 2011) describing the then status of lambda implementation. While the paper continues to be quite relevant, it may not describe a whole lot in terms of specifics of how to use the features.</li> <li><a href="http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.html">Lambda FAQ</a> : This is a must read FAQ. In many ways it covers many of the same topics already covered in the <em>state of the lambda</em>, but describes the same in a greater detail and in a Q&amp;A format which makes it easy to consume the information one small answer at a time. I refer to it frequently in this post.</li> <li><a href="http://mail.openjdk.java.net/mailman/listinfo/lambda-dev">lambda-dev mailing list</a></li> </ul> <h2>Motivation :</h2> <p>Let us understand the motivation and goals of Java 8 lambdas. This is particularly important since we often take a look at a new piece of software and attempt to map it to something we already know. That may or may not be the appropriate thing to do in a given context. I focus on the motivation so we can understand what the authors set out to do, which will help us better evaluate the implementation and how well it addresses its goals </p> <p>The page <a href="http://www.lambdafaq.org/why-are-lambda-expressions-being-added-to-java/">Why are lambda expressions being added to Java?</a> describes the primary motivation</p> <blockquote> <p>Lambda expressions (and closures) are a popular feature of many modern programming languages. Amongst the different reasons for this, the most pressing one for the Java platform is that they make it easier to distribute processing of collections over multiple threads. Currently, lists and sets are typically processed by client code obtaining an iterator from the collection, then using that to iterate over its elements and process each in turn. If the processing of different elements is to proceed in parallel, it is the responsibility of the client code, not the collection, to organise this.</p> <p>In Java 8, the intention is instead to provide collections with methods that will take functions and uses them, each in different ways, to process their elements. The advantage that this change brings is that collections can now organise their own iteration internally, transferring responsibility for parallelisation from client code into library code.</p> </blockquote> <p>On the page <a href="http://www.lambdafaq.org/are-lambda-expressions-objects/">Are lambda expressions objects?</a> we come across the following quote</p> <blockquote> <p>To understand the situation, it is useful to know that there are both short-term goals and a longer-term perspective for the implementation in Java 8. The short-term goals are to to support internal iteration of collections, in the interests of efficiently utilising increasingly parallel hardware. The longer-term perspective is to steer Java in a direction that supports a more functional style of programming. Only the short-term goals are being pursued at present, but the designers <a href="http://mail.openjdk.java.net/pipermail/lambda-dev/2011-August/003877.html">are being careful</a> to avoid compromising the future of functional programming in Java, which might in the future include fully-fledged function types such as are found in languages such as Haskell and Scala.</p> </blockquote> <p>A useful line is to be found in one of the posts in the lambda-dev mailing list referred to above ie. <a href="http://mail.openjdk.java.net/pipermail/lambda-dev/2011-August/003877.html">A peek past lambda</a></p> <blockquote> <p>Lambda is the down-payment on this evolution, but it is far from the end of the story. The rest of the story isn&#39;t written yet, but preserving our options are a key aspect of how we evaluate the decisions we make here.)</p> </blockquote> <h2>Whither Java Collections Framework</h2> <p>As one would soon realise, much of the newer features being added are <em>not</em> being added <em>directly</em> to the classes you currently know as the Java Collections Framework. There&#39;s a new interface <em>Stream</em> and helper class <em>Streams</em> thats at the center of these changes. The rationale for Stream is explained as follows in <a href="http://www.lambdafaq.org/where-is-the-java-collections-framework-going/">Where is the Java Collections Framework going?</a></p> <blockquote> <p>Analysis of the usage of the Java collections shows one pattern to be very common, in which bulk operations draw from a data source (array or collection), then repeatedly apply transformation operations like filtering and mapping to the data, often finally summarising it in a single operation such as summing numeric elements. Current use of this pattern requires the creation of temporary collections to hold the intermediate results of these transformations. However, this style of processing can be recast to a pipeline using the well-known “Pipes and Filters” pattern, with significant resulting advantages: elimination of intermediate variables, reduction of intermediate storage, lazy evaluation, and more flexible and composable operation definitions. Moreover, if each operation in the pipeline is defined appropriately, the pipeline as a whole can often be automatically parallelised (split up for parallel execution on multicore processors). The role of pipes, the connectors between pipeline operations, is taken in the Java 8 libraries by implementations of the Stream interface; examining this interface will clarify how pipelines work.</p> </blockquote> <p>Another page <a href="http://www.lambdafaq.org/why-are-stream-operations-not-defined-directly-on-collection/">Why are Stream operations not defined directly on Collection?</a> documents the rationale behind why many of the stream methods are not defined directly on the Collection interface.</p> <h2>Implementation used in this post</h2> <p>I have used the b78 version of the Java™ Platform, Standard Edition 8 Early Access with Lambda Support from <a href="http://jdk8.java.net/lambda/">here</a>. It could happen that some of the api methods I refer to continue to evolve. The JDK api docs for this particular version can be found <a href="http://download.java.net/lambda/b78/docs/api/index.html">here</a> </p> <h2>Typical pipeline structure.</h2> <p>Let us explore the typical pipeline structure. For a given collection, convert it into a stream, perform a set of transformations on the same and finally accumulate the results into a Collector. We shall look at the details of a collector later, but for the moment we can imagine the return value of executing a pipeline is an object that is returned by the collector.</p> <div class="highlight"><pre><span></span><span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">result</span> <span class="o">=</span> <span class="n">Arrays</span><span class="o">.</span><span class="na">asList</span><span class="o">(</span><span class="s">&quot;Larry&quot;</span><span class="o">,</span> <span class="s">&quot;Moe&quot;</span><span class="o">,</span> <span class="s">&quot;Curly&quot;</span><span class="o">)</span> <span class="o">.</span><span class="na">stream</span><span class="o">()</span> <span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="n">s</span> <span class="o">-&gt;</span> <span class="s">&quot;Hello &quot;</span> <span class="o">+</span> <span class="n">s</span><span class="o">)</span> <span class="o">.</span><span class="na">collect</span><span class="o">(</span><span class="n">Collectors</span><span class="o">.</span><span class="na">toList</span><span class="o">());</span> <span class="c1">// result will be a List&lt;String&gt; containing &quot;Hello Larry&quot;, &quot;Hello Moe&quot; and &quot;Hello Curly&quot;</span> </pre></div> <p>Let us evaluate the steps in the pipeline</p> <ul> <li><code>Arrays.asList("Larry", "Moe", "Curly")</code>: instantiates a list with three strings viz. "Larry", "Moe" and "Curly". </li> <li><code>.stream()</code>: Converts the list into a stream</li> <li><code>.map(s -&gt; "Hello " + s)</code>: Performs a map operation which prepends "Hello " to each item in the stream</li> <li><code>.collect(Collectors.toList())</code>: Defines a new collector which will collect the results of the pipeline into a list and invokes collect to collect the results into it (and eventually returning the collected list).</li> </ul> <p>As you can observe, the type of the original collection is not preserved during the transformation. In fact you should be able to take a collection and invoke <code>.stream()</code> on it, whereby you effectively lose the type of the original collection itself. All the transformations are defined on streams and eventually you can convert the result <code>Stream</code> into your desired collection type by supplying the appropriate <code>Collector</code>. This leads me to imagine that lambda based transformations are really stream transformers with converters at both ends - one to convert a collection into a stream and eventually to collect the stream back into a collection of a desired type. (One could of course choose to have the end result continue to be a Stream that one can use subsequently). The stream allows one to account for considerations related to efficiency, parallelisation,and even collection agnosticity. The collections at the ends provide a concrete implementation best suited for other tasks outside the pipeline.</p> <h2>Streams</h2> <p>Clearly streams are amongst the most important types to be used in lambda based transformations. So it makes sense to take a look at the two associated classes. The interface <code>java.util.stream.Stream</code> and the helper <code>java.util.stream.Streams</code>.</p> <p>Let us first take a look at <code>Streams</code> <a href="http://download.java.net/lambda/b78/docs/api/java/util/stream/Streams.html">(javadoc)</a>. This is a helper class and has lot of supporting operations for creating streams. (note: <code>.boxed()</code> as used below converts a stream of primitive ints into a stream of Integers)</p> <p>eg. some of the methods (for a full listing see the javadocs) are</p> <ul> <li><code>Streams.intRange(start, stop, step)</code> will generate a stream of primitive <code>int</code>s. (step if not provided is 1)</li> <li><code>Streams.longRange(start, stop, step)</code> will generate a stream of primitive <code>long</code>s. (step if not provided is 1)</li> <li><code>Streams.concat(stream1, stream2)</code> will concatenate two streams</li> </ul> <div class="highlight"><pre><span></span><span class="n">IntStream</span> <span class="n">str1</span> <span class="o">=</span> <span class="n">Streams</span><span class="o">.</span><span class="na">intRange</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span><span class="mi">10</span><span class="o">);</span> <span class="n">IntStream</span> <span class="n">str2</span> <span class="o">=</span> <span class="n">Streams</span><span class="o">.</span><span class="na">intRange</span><span class="o">(</span><span class="mi">21</span><span class="o">,</span><span class="mi">30</span><span class="o">);</span> <span class="n">Stream</span><span class="o">&lt;</span><span class="n">Integer</span><span class="o">&gt;</span> <span class="n">joined</span> <span class="o">=</span> <span class="n">Streams</span><span class="o">.</span><span class="na">concat</span><span class="o">(</span><span class="n">str1</span><span class="o">.</span><span class="na">boxed</span><span class="o">(),</span> <span class="n">str2</span><span class="o">.</span><span class="na">boxed</span><span class="o">());</span> <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">joined</span><span class="o">.</span><span class="na">collect</span><span class="o">(</span><span class="n">Collectors</span><span class="o">.</span><span class="na">toList</span><span class="o">()));</span> <span class="c1">// Expected output : [1, 2, 3, 4, 5, 6, 7, 8, 9, 21, 22, 23, 24, 25, 26, 27, 28, 29]</span> </pre></div> <ul> <li><code>Streams.zip(stream1, stream2, function)</code> will return a stream based on a function being applied on consecutive pairs across stream1 and stream2</li> </ul> <div class="highlight"><pre><span></span><span class="n">Stream</span><span class="o">&lt;</span><span class="n">Integer</span><span class="o">&gt;</span> <span class="n">ints</span> <span class="o">=</span> <span class="n">Streams</span><span class="o">.</span><span class="na">intRange</span><span class="o">(</span><span class="mi">0</span><span class="o">,</span><span class="mi">5</span><span class="o">).</span><span class="na">boxed</span><span class="o">();</span> <span class="n">Stream</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">strs</span> <span class="o">=</span> <span class="n">Arrays</span><span class="o">.</span><span class="na">asList</span><span class="o">(</span><span class="s">&quot;foo&quot;</span><span class="o">,</span> <span class="s">&quot;bar&quot;</span><span class="o">,</span> <span class="s">&quot;baz&quot;</span><span class="o">,</span> <span class="s">&quot;qux&quot;</span><span class="o">).</span><span class="na">stream</span><span class="o">();</span> <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Streams</span><span class="o">.</span><span class="na">zip</span><span class="o">(</span><span class="n">ints</span><span class="o">,</span><span class="n">strs</span><span class="o">,</span> <span class="o">(</span><span class="n">i</span><span class="o">,</span> <span class="n">s</span><span class="o">)</span> <span class="o">-&gt;</span> <span class="n">i</span><span class="o">.</span><span class="na">toString</span><span class="o">()</span> <span class="o">+</span> <span class="n">s</span><span class="o">).</span><span class="na">collect</span><span class="o">(</span><span class="n">Collectors</span><span class="o">.</span><span class="na">toList</span><span class="o">()));</span> <span class="c1">// Expected output: [&quot;0foo&quot;, &quot;1bar&quot;, &quot;2baz&quot;, &quot;3qux&quot;]</span> </pre></div> <p><em>Note</em>: As mentioned earlier, any collection can be converted into a stream by invoking <code>.stream()</code> on it.</p> <h2>Stream</h2> <p>Now that we&#39;ve see how to create, concatenate or merge streams, let us explore some of the more useful operations that can be performed on the stream. I won&#39;t be dwelling much on these transformations in this post. I intend to come back to them in a future post.</p> <p><a href="http://download.java.net/lambda/b78/docs/api/java/util/stream/Stream.html">(javadoc)</a></p> <table> <tr> <th>Method</th> <th>Description</th> </tr> <tr> <td>allMatch</td> <td>true if all elements of the stream match the predicate</td> </tr> <tr> <td>anyMatch</td> <td>true if any element of the stream match the predicate</td> </tr> <tr> <td>filter</td> <td>return a stream of subset of the elements matching the predicate</td> </tr> <tr> <td>findAny</td> <td>return an element of the stream matching the predicate</td> </tr> <tr> <td>findFirst</td> <td>return the first element of the stream matching the predicate</td> </tr> <tr> <td rowspan="2" valign="top">flatMap</td> <td>Return a stream where each input element is transformed into 0 or more values</td> </tr> <tr> <td>Return a stream where each input element is transformed into a stream</td> </tr> <tr> <td>forEach</td> <td>Perform an operation on each element of the stream (usually for side effects)</td> </tr> <tr> <td>limit</td> <td>Return a stream with no more than the first maxSize elements of this stream</td> </tr> <tr> <td>map</td> <td>Transform the stream into another containing the results after applying the function mapper on each element of the stream</td> </tr> <tr> <td>max</td> <td>Return the maximum element of this stream based on the supplied comparator</td> </tr> <tr> <td>min</td> <td>Return the minimum element of this stream based on the supplied comparator</td> </tr> <tr> <td>peek</td> <td>Return the same stream even as the elements are also provided to the consumer</td> </tr> <tr> <td>reduce</td> <td>Reduce the stream to a single value, performing the reducer operation on each element along with the accumulated value (accumulated value starting with the identity).</td> </tr> <tr> <td>sorted</td> <td>Sort the stream based on natural order or supplied comparator</td> </tr> <tr> <td>subStream</td> <td>Return a stream after discarding the first startingOffset elements (and those after optionally provided endingOffset elements)</td> </tr> <tr> <td>toArray</td> <td>Convert stream to array</td> </tr> </table> <p><br/></p> <p>The result of performing the operations above can result in zero or 1 element (eg. findFirst) from the stream, a boolean result (eg. allMatch), no result whatsoever (eg. forEach), a reduced value (eg. reduce) or simply, another stream (eg. map, filter, flatMap, limit, subStream etc.). When a stream is returned, similar transformations could be applied on it again leading to a pipeline of transformations. </p> <h2>Functions</h2> <p>If you looked at the javadocs for the methods above, you will have seen that many of them require some kind of functions as predicates or transformers. These are similar to the <code>.map(s -&gt; "Hello " + s)</code> invocation we saw earlier. And if you are used to the earlier versions of Java, they no doubt look very compact. </p> <p>Let us for a moment imagine we are working with Java 7 or before and imagine how such a functional transformation could get passed to another method as an input. A completely hypothetical equivalent using classic java would be</p> <div class="highlight"><pre><span></span><span class="n">Stream</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">strings</span><span class="o">;</span> <span class="n">strings</span><span class="o">.</span><span class="na">map</span><span class="o">(</span> <span class="k">new</span> <span class="n">MapperFunction</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span><span class="n">String</span><span class="o">&gt;()</span> <span class="o">{</span> <span class="kd">public</span> <span class="n">String</span> <span class="nf">map</span><span class="o">(</span><span class="n">String</span> <span class="n">str</span><span class="o">)</span> <span class="o">{</span> <span class="k">return</span> <span class="s">&quot;Hello&quot;</span> <span class="o">+</span> <span class="n">str</span><span class="o">;</span> <span class="o">}</span> <span class="o">}</span> <span class="o">);</span> </pre></div> <p>Contrast this with the compact declaration that java 8 Stream.map method accepts. You can quickly note the following :</p> <ul> <li>There is no anonymous class instantiation required (<code>MapperFunction</code> above)</li> <li>There is no function declaration (<code>public String map</code> above)</li> <li>There are no type declarations (the two <code>String</code> declarations in <code>public String map(String str)</code> above)</li> <li><code>return</code> is implicit.</li> </ul> <p>Also note that instead of traditional class being a parameter, we passed what for all practical purposes seems like a function. But, can java accept functions as arguments ? And if we do not pass the signature for the function how does it infer it?</p> <p>For answers, we need to look at Functional Interfaces.</p> <h2>Functional Interfaces</h2> <p>Functional interfaces is a very interesting (and to the extent of my awareness possibly unique) introduction to the java 8 lexicon. For details we can refer to <a href="http://www.lambdafaq.org/what-is-a-functional-interface/">What is a functional interface?</a></p> <blockquote> <p>a functional interface is defined as any interface that has exactly one abstract method. (The qualification is necessary because an interface has non-abstract methods inherited from Object and may also have non-abstract default methods). This is why functional interfaces used to be called Single Abstract Method (SAM) interfaces, a term that is still sometimes seen.</p> </blockquote> <p>So functional interface can inherit non abstract methods. It also can have non-abstract default methods. But it can have exactly one abstract method.</p> <p><em>Aside</em> : Java 8 introduces the ability to define default implementations in interfaces. The treatment of the same is beyond the scope of this post. But you could read up on <a href="http://www.lambdafaq.org/what-are-default-methods/">What are default methods?</a> and the subsequent four pages if interested.</p> <p>So, what was getting passed to map() earlier was not a function, but in java 8 terms, an implementation of a functional interface. If you look at the various parameters being passed to the Stream methods discussed above, you should find that many of the arguments are functional interfaces.</p> <p>Java 8 also introduces a substantial degree of syntactic sugar to succinctly declare the implementation of a functional interface. In the case above it was <code>s -&gt; "Hello " + s</code></p> <p>Lets look at how the compiler can make sense out of all this. First the argument provided to a <code>map</code> method is a <code>Function&lt;T,R&gt;</code> interface. If you look at the signature of this interface you will find not one, but two methods. Thankfully one of them called <code>compose</code> has a default implementation provided. Thus there is only one abstract method <code>apply</code> which takes a <code>T</code> and returns a <code>R</code>. </p> <p>Given that the requirement of a functional interface has been met (since there is a single abstract method - apply), we can look at the signature and say that since the <code>Stream&lt;T&gt;</code> is of type <code>Stream&lt;String&gt;</code>, the input argument to the apply method will be of type <code>String</code>. Now the compiler can treat the function supplied as an argument to the map method, as the body of apply method of an instance of the <code>Function&lt;T,R&gt;</code> interface. The input argument to the apply method ie. <code>T</code> is a <code>String</code>. What about the output?</p> <p>Here&#39;s where we meet another interesting aspect introduced by Java 8. ie. type inference using target types. The ultimate result of the pipeline above was a <code>List&lt;String&gt;</code>. Which was a result of a <code>.collect(Collectors.toList())</code> operation. So the input to the collect should&#39;ve been a <code>Stream&lt;String&gt;</code>. Since the output of the <code>map</code> method call has to be a <code>Stream&lt;String&gt;</code>, it means the output of the apply method must be a <code>String</code>. That fits nicely with our expectations that since the input is a <code>String</code>, <code>"Hello " + s</code> must also be a <code>String</code>. So all is well and the compilation can go ahead. You can read more about this at <a href="http://www.lambdafaq.org/what-is-the-type-of-a-lambda-expression/">What is the type of a lambda expression?</a></p> <h2>Collectors</h2> <p>Another interesting introduction to how java 8 terminates the processing pipelines is collectors. To briefly recap, pipelines can start with a collection with a <code>.stream()</code> method on in which converts it into a <code>Stream</code>. Many operations on the stream eg. <code>map</code>, <code>flatMap</code> etc. can be chained on the same which will continuously transform the stream into another stream and yet another stream and so on. At some stage you could invoke a method such as <code>findFirst</code>, <code>reduce</code> or something similar which will return just one value. eg. the following will return a single value 109 because of the last reduce invocation.</p> <div class="highlight"><pre><span></span><span class="n">Arrays</span><span class="o">.</span><span class="na">asList</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span><span class="mi">2</span><span class="o">,</span><span class="mi">3</span><span class="o">,</span><span class="mi">4</span><span class="o">,</span><span class="mi">5</span><span class="o">,</span><span class="mi">6</span><span class="o">)</span> <span class="o">.</span><span class="na">stream</span><span class="o">()</span> <span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="n">n</span> <span class="o">-&gt;</span> <span class="n">n</span> <span class="o">*</span> <span class="n">n</span><span class="o">)</span> <span class="c1">// square the number</span> <span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="n">n</span> <span class="o">-&gt;</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">3</span><span class="o">)</span> <span class="c1">// add 3 to the value</span> <span class="o">.</span><span class="na">reduce</span><span class="o">(</span><span class="mi">0</span><span class="o">,</span> <span class="o">(</span><span class="n">i</span><span class="o">,</span> <span class="n">j</span><span class="o">)</span> <span class="o">-&gt;</span> <span class="n">i</span> <span class="o">+</span> <span class="n">j</span><span class="o">));</span> <span class="c1">// add the number to the accumulator</span> </pre></div> <p>However in many other cases you might prefer a collection as the result. This is where collectors are useful. </p> <p>A collector has the capabilities necessary to </p> <ul> <li>instantiate the necessary object of the target class. eg. it could be a List<T> (or some other collection you choose)</li> <li>take each element from the Stream and appropriately modify the target object (eg. add to the List)</li> <li>take two instances of the target object and combine them into a single instance. (this is related to parallelisation - if multiple lists were constructed in parallel, this helps combine all of them into one).</li> </ul> <p>To recap, I will repeat the code block I showed earlier </p> <div class="highlight"><pre><span></span><span class="n">List</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;</span> <span class="n">result</span> <span class="o">=</span> <span class="n">Arrays</span><span class="o">.</span><span class="na">asList</span><span class="o">(</span><span class="s">&quot;Larry&quot;</span><span class="o">,</span> <span class="s">&quot;Moe&quot;</span><span class="o">,</span> <span class="s">&quot;Curly&quot;</span><span class="o">)</span> <span class="o">.</span><span class="na">stream</span><span class="o">()</span> <span class="o">.</span><span class="na">map</span><span class="o">(</span><span class="n">s</span> <span class="o">-&gt;</span> <span class="s">&quot;Hello &quot;</span> <span class="o">+</span> <span class="n">s</span><span class="o">)</span> <span class="o">.</span><span class="na">collect</span><span class="o">(</span><span class="n">Collectors</span><span class="o">.</span><span class="na">toList</span><span class="o">());</span> <span class="c1">// result will be a List&lt;String&gt; containing &quot;Hello Larry&quot;, &quot;Hello Moe&quot; and &quot;Hello Curly&quot;</span> </pre></div> <p>In this example, I used <code>Collectors.toList()</code> which returned an appropriately useful collector. In most cases one can use similar functions to create an appropriate collector, or create a collector using instances of three functional interfaces for the three capabilities listed in the prior paragraph. However should you want to carefully craft your collectors in a peculiar way, you could write your collector and use the same as well. It is the collector which eventually returns the result of the pipeline, eg. in the case above, it is a <code>List&lt;Integer&gt;</code></p> <h2>Pending topics :</h2> <p>Hopefully I have been able to provide a reasonable feel of the capabilities of Java 8 Lambdas and how they can be used. However there are many topics that I haven&#39;t been able to cover in this post and intend to cover them in later posts. Some of these are :</p> <ul> <li>Many more examples of how to use stream transformations</li> <li>Primitives vs. objects. There are a many methods and classes which help work on the primitives alone.</li> <li>Closures</li> <li>New classes like Optional<T></li> <li>Aspects related to parallelisation </li> <li>Hand crafting some of your own classes</li> </ul>Conference Report - Hasgeek jsFoo Pune 20122012-01-21T18:22:36+05:302012-01-21T18:22:36+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2012-01-21:/2012/01/conference-report-hasgeek-jsfoo-pune-2012/<p>Just finished attending <a href="http://jsfoo.in/pune2012/">jsFoo Pune 2012</a> organised by <a href="http://hasgeek.com/">HasGeek</a>. It was an interesting and a well spent day. And I've learnt if one wants to blog about a conference, it is best done immediately post the event else much gets lost with a recollection as weak as mine.</p> <p>The conference was spread over 3 tracks. So the best I can do is talk about the talks I attended. </p> <p>The first talk I attended was <a href="http://funnel.hasgeek.com/jsfoo-pune/179-node-js-html5-and-phoegap-for-high-performant-content-site-app">Node.js, HTML5 and Phonegap for high performant content site app</a> by <a href="https://twitter.com/#!/prasoonk">Prasoon Kumar</a>. I learnt some stuff about <a href="http://phonegap.com/">phonegap</a> and <a href="http://the-m-project.org/friends/espresso/">espresso</a>. Good starting point, but hardly got to see any actual code. </p> <p>Next one was <a href="http://funnel.hasgeek.com/jsfoo-pune/106-synchronized-models-using-backbone-sockets-and-node">Synchronized models using Backbone, Sockets and Node</a> by Ruben Stolk. He started up a demo and suggested that people in the room connect to it. It basically had a canvas with a image and amongst many things, one could turn the light on the light-stand on and off .. and any actions propagated to the backend and then to all the connected clients. Similarly with post-it kind of notes that one added on the canvas. Was a good example of using backbone for defining models both on the client and the server and using socket.io to stream events between the two. He went through a fair amount of code too and it was very understandable. I came back with a distinct impression that the model maintenance and ability to synchronise the model changes between a client and server and then between the server and all connected clients (thus in effect across all connected clients) was pretty cool. However I still felt there needed a fair degree of wiring up of the event related code, though I am not sure about it and need to experiment with it more.</p> <p>The next one was <a href="http://funnel.hasgeek.com/jsfoo-pune/181-how-to-apply-bdd-and-tdd-practices-using-jasmine-library">How to apply BDD and TDD practices, using Jasmine library?</a> by Anil Tarte. Anil and I have been colleagues in the past and I have a high respect for his skills. In the brief period he was able to convey how to use <a href="http://pivotal.github.com/jasmine/">jasmine</a> to write BDD Test cases for the client side code. His server was over web sockets, and he was essentially intercepting calls into specific methods that eventually communicated with the backend. He also mentioned it would be possible to intercept the over the wire HTTP traffic instead of javascript functions, though it would create more difficulties if one was not able to precisely control the server's outputs. I quite frankly had not imagined that BDD could be used so effectively and especially localised strictly to the client. So it was an insightful talk, that definitely will have me thinking about javascript BDD the next time. However he did seem rushed and under pressure and perhaps the talk lengths could be extended from the 45 mins to 55 mins or so.</p> <p>Next was <a href="http://funnel.hasgeek.com/jsfoo-pune/175-building-real-time-web-applications-introduction-to-websockets-socket-io">Building real-time web applications ... (Introduction to Websockets / Socket.IO)</a> by <a href="https://twitter.com/#!/netroy">Aditya Y</a>. He talked a bit about evolution of websockets, various libraries that were developed along the way and the current state of websockets support. There was an interesting demo at the end where changes in one browser session were almost being reflected in real time into another browser session via websockets connections back to the server. One of the important points noted was that Websockets continues to ride over port 80 so can work across many firewalls, and while its initial handshake is HTTP like, the subsequent traffic is essentially TCP like over that HTTP connection.</p> <p>A thoroughly enjoyable talk was <a href="http://funnel.hasgeek.com/jsfoo-pune/170-advanced-javascript-techniques">Advanced JavaScript Techniques</a> by <a href="https://twitter.com/#!/avranju">Rajasekharan Vengalil</a>. He looked like he had enough stuff to talk about for the whole day, and delved into some of the OO related aspects of Javascript and the additions to ECMAScript 5. He spent some good amount of time in explaining how prototype based languages work. There are just so many intricacies to the language and so many really strange behaviours (like silently ignoring assignments etc.) that if I hated the language I would've just continuously muttered WTFs and if I happened to passionately like the language, I just might've facepalm'ed my way through the talk. Thankfully my outlook towards javascript is quite neutral, so I just enjoyed it . A lot.</p> <p><a href="http://funnel.hasgeek.com/jsfoo-pune/162-node-js-patterns-and-how-we-build-activenode">Node.js Patterns and How we build ActiveNode</a> was the next talk by <a href="https://twitter.com/#!/sreeix">Sreekanth Vadagiri</a>. He talked about some of his experiences with node.js, his preference for using CoffeeScript rather than JS (despite it being harder to debug), many of the patterns he liked, some of the libraries he used and deployment matters. He was just as candid about some of the gotchas as well. A useful insight into the node.js system.</p> <p>The last one of the day was <a href="http://funnel.hasgeek.com/jsfoo-pune/180-amplify-your-stack">Amplify your stack</a> by <a href="https://twitter.com/#!/threepointone">Sunil Pai</a>. He talked about a lot of libraries that could be used for client side development and deployment, about using javascript for templating, ensuring rigorous unit test coverage at all stages. Gave me the feeling there's a lot I simply do not know about whats happening on the client side JS related libraries and frameworks. Couple of remarks I recollect - "You (the JS developer) own the browser" and "When you have a strong test case coverage, you can CODE BOLDLY". </p> <p><strong>Summary: </strong> There's a lot I need to learn about whats happening on the client side. Thats true about the server side as well. And the day helped me understand just a little bit better what I don't know. Yet I got the distinct feeling of discomfort with node.js. Not with the tool. But with the assumptions that seem to go with its usage. There was poor articulation about what kind of use cases it is good for. Except that it is really good for high thruput/no. of client connections. Either there was a misplaced understanding of it being the only good way to get this kind of thruput or there was inability to clearly articulate what other benefits developers could expect out of using node.js in situations where thruput/concurrent connections is not particularly important for them. Perhaps I was just at the wrong places when someone was offering a more insightful articulation of this. But I really did not hear it. </p> <p>On the whole, I enjoyed the sessions, and this was a day very well spent. Hats off to <a href="https://twitter.com/#!/jackerhack">Kiran</a> and his team for organising a good event. Makes me look forward to the next one they organise.</p>Why OSGi? Or why not using it makes your JVM runtime unsafe.2012-01-21T18:22:36+05:302012-01-21T18:22:36+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2012-01-21:/2012/01/why-osgi-or-why-not-using-it-makes-your-jvm-runtime-unsafe/<p>Not sure how long ago I started using OSGi. Perhaps it was 12 months ago or then perhaps 18. And yet I still find it painful using OSGi especially every time I bring in a foreign set of jars into the ecosystem. And yet I continue to be a dogged proponent. Here’s why.</p> <p>First let us understand one of the many problems OSGi solves. Let us imagine your java application has exactly three classes. One is the class you wrote called “My.java” bundled in a jar called “my.jar”. Another is a class called “Uses.java” whose api and features are leveraged by “My.java” and is in a jar called “uses.jar”. Finally there is yet another class “Transitive.java” which is leveraged by “Uses.java” and lies in a jar called “transitive.jar”.</p> <p>Ideally when you run your application you should have all the three jars available in your classpath. Since you did not write “Uses.java”, how would you know about its transitive dependency on “transitive.jar” ? If you are fortunate, the build tool used to create “uses.jar” would have created a corresponding “uses.pom” or similar file which would allow your build tool to discover the transitive dependencies and assemble them all together. But what if that wasn’t true?</p> <p>Turns out these situations are far more frequent than one would imagine. When I started using OSGi, I imagined such cases to be rather unlikely. And yet I have found a large number of situations. Sometimes the unrecorded transitive dependencies are in a profusion of xml libraries, sometimes on alternate logging tools, sometimes even on packages from a different jvm like bea and at least in one case even on dalvik.</p> <p>So how do you know the libraries you are using have all their transitive dependencies recorded (and in any typical java application today there are literally tens of such jars)? Turns out you don’t.</p> <h3>The OSGi way :</h3> <p>OSGi requires you to record the metadata for the jar within the jar itself. Thus in the case above, “my.jar” would document the fact that it depended upon packages from “uses.jar”. In turn, the metadata on the “uses.jar” would record the fact it exports a certain set of packages (which “my.jar” uses) and also requires (imports) other packages which incidentally also happens to be exported by “transitive.jar”. The OSGi runtime will make sure all the dependencies are appropriately resolved. Thus in this situation if you attempted to run the application without “transitive.jar”, OSGi would flag that off as an error since some of the package imports would not get satisified and your application will not start until you add the third jar into the classpath.</p> <h3>The JVM runtime by default is unsafe</h3> <p>Java is often considered safe given its static typing. But that is just half the story. As the example above shows, its runtime is unsafe. And if you are a library author, you are in turn contributing your share by carrying forward that unsafeness into your user’s domain. The compiler makes sure you have compiled the code correctly to the interface of a given type. But the runtime does not guarantee the presence of that type. At a code level even if you think you’ve proved your code works to the extent you could, quite frankly the poor runtime makes such a proof quite illusionary. Since you have no guarantee your code will work even if it compiled successfully. And if you want to test such errors, how on earth will you even test such conditions ?</p> <h3>Library Vendors : Please make your jars OSGi compatible</h3> <p>For every hour you save by not making your jars OSGi compatible, you are requiring your “n” users who are interested in ensuring a stronger safety of their system to spend “n” hours ie. one each. Believe me, I’ve spent enormous amounts of time doing this as a user. Thats what gives OSGi a bad rap. Thats what makes people say it is complex.</p> <p>OSGi is really trying to make your application and java runtimes in general safer (There are many other capabilities, but I’ve focused on only one). Do use it when you publish your jars. And do encourage others to do so.</p>Scala needs terraces2012-01-11T05:22:42+05:302012-01-11T05:22:42+05:30Dhananjay Nenetag:blog.dhananjaynene.com,2012-01-11:/2012/01/scala-needs-terraces/<p><em>Terrace (noun) : each of a series of flat areas made on a slope, used for cultivation, or a flight of wide, shallow steps providing standing room for spectators in a stadium</em></p> <p>Recently there was a useful discussion triggered off by the post <a href="http://yz.mit.edu/wp/true-scala-complexity/">True Scala complexity</a> by <a href="https://twitter.com/#!/yaaang">Yang Zhang</a>. Much has been debated about it in the comments on the post and <a href="http://news.ycombinator.com/item?id=3443436">other forums</a>, most of it both articulate and constructive.</p> <p>Martin Odersky proposed an idea on the ycombinator news thread as follows (next paragraph)</p> <blockquote> <p>So, I believe here is what we need do: Truly advanced, and dangerously powerful, features such as implicit conversions and higher-kinded types will in the future be enabled only under a special compiler flag. The flag will come with documentation that if you enable it, you take the responsibility. Even with the flag disabled, Scala will be a more powerful language than any of the alternatives I can think of. And enabling the flag to do advanced stuff is in any case much easier than hacking your own compiler.</p> </blockquote> <p>The basis of the idea wasn't new. Martin himself had earlier suggested <a href="http://www.scala-lang.org/node/8610">Scala Levels</a>, though the suggested enforcement of two levels via a compiler flag was. While I thought it was a useful idea, strangely enough I found many (especially in the twitter streams I follow) less than enthusiastic. I think there exists a perspective where one can look at this idea with more enthusiasm, and this post details that.</p> <p>Learning scala is non trivial. For a person who is well versed with Java programming and with little else, this will require learning at least the following (I am listing only a few items)</p> <ul> <li> <p>Learning traits and objects (and unlearning statics). There's a lot of stuff here including multiple inheritance, member overriding, whether the members should be defs or vals etc. which takes some times to get used to.</p> </li> <li> <p>Learning to deal with immutability and occasionally lazy evaluation</p> </li> <li> <p>Learning collections constructs such as for comprehensions or, map, flatMap, filter etc.</p> </li> <li> <p>Understanding the capabilities that scala offers in terms of generics including being able to define co/contravariant collections, and then learning to use these capabilities effectively</p> </li> <li> <p>Being able to understand and leverage many aspects of type theory</p> </li> </ul> <p>Typically when a person makes a sustained effort driven by passion, he can cover a lot of ground very quickly. Thus some might be able to deal with the scala learning curve quickly. While this ability to learn could be achievable for many especially smaller companies, it is likely to be quite difficult for many others. Especially when the team sizes are large and/or developer capabilities might be modest, this is a real issue. One could form an analogy with a Cheetah and an Elephant. Some animals will be able to run fast individually, and others will trod slowly as a herd. One could wish elephants could run as fast as cheetahs but thats unhelpful, since the jungle is defined, and one best deal with it the way one finds it.</p> <p>An ideal way to introduce scala would be to start with small groups of 3-5. But thats not practical in all cases. Besides for large teams, that means, most will need to wait for a much longer time before being able to use scala and profit from its substantial benefits. So lets say an organisation attempts to introduce scala in a modest team of say 20. Of which 2 are passionate, self driven souls, who tend to work much harder the rest to be able to progress much faster. Because learning to use scala the way scala wished to be used, is not a sprint but is a marathon (or at least a long distance jog), this creates issues. Soon enough the 2 are likely to run far ahead of the rest of the team and will start writing code which the others can't grok. Even as some of the remaining may actually be struggling hard but are likely to be losing enthusiasm simply because they can't find themselves being able to keep pace. An even bigger risk is the 2 continuing to make rapid progress even as they realise so much more remains to be learnt, even as the remainder are unable to start leveraging scala, since they still think there's a lot of distance yet to cover and they become far more focused on catching up rather than leveraging what they have learnt. So while learning proceeds, actual development with direct economic benefit stalls. </p> <p>Lets say hypothetically this team decided that they would only focus on reducing java boiler plate (which is a big deal) and continue to use scala in a primarily OO paradigm (since that is what they are used to) and defer learning remainder of scala by at least six months. There's likely tons of benefits to be had by shifting to scala even with this limited scope. Yet all it takes to destroy harmony is one very enthusiastic developer. Who starts inserting (as yet) alien notions such as writing highly functional code, or implementing advanced type theory concepts (eg. scalaz) or starting to use a largely un-understood symbol soup (<a href="http://www.cakesolutions.net/teamblogs/2011/11/19/u-s-scalaz-layout/">scalaz</a> or the dispatch <a href="http://www.flotsam.nl/dispatch-periodic-table.html">periodic table</a>). This would make a mockery of the phased learning and force every one else to catch up (even if just to be able to read and maintain somebody else's code).</p> <p>We've long known hills are not conducive to agriculture. If these hills are our learning curves, fields could be considered to be directly impactful deployment of our skills. Agriculture generally happens flatlands and plateaus but not hillslopes. And yet we've learnt to tame the tall hills using terraces. Instead of climbing for a long time, climb a little &amp; grow a little. If teams can learn for a month, and deploy these skills for the next 6-9 months with the cycle repeating itself, it could help in the following ways :</p> <ul> <li> <p>Organisations do not need to invest in long training times where the opportunity cost of lost development time is one large fixed cost.</p> </li> <li> <p>Intra team disparity is likely to be contained since the deployment period will allow many to catch up with those in the lead</p> </li> <li> <p>Instead of scala being viewed as one big leap, it could be viewed as a incremental leaps with managed effort. I saw a remark which said people anyways deal with complexity - its called OO. And Java programmers deal with OO. So they should be able to come to terms with scala quickly. But there is a difference. Java wasn't as big a learning curve leap over C++ as Scala is over Java. And as importantly, a learning curve one has triumphed over already, somehow seems far less onerous than an identically sized one, one still needs to deal with.</p> </li> </ul> <p>In short learning scala needs terraces. Terraces, where some developers will not run far ahead of the pack during the learning stages. Terraces where developers won't have to feel too scared of alien'ish code suddenly appearing on their monitors. Terraces where developers will learn a few things and then deploy these skills over the next few months. These terraces could be made to work in different ways. It could just be a honour system using a set of levels identical or similar to the levels Martin Odersky proposed (though it would be extremely hard to ensure the same without tooling support). Or they could implemented using (as yet unwritten) tool like a scala-lint which will flag off advanced usages as warnings. Or as compiler switches the way Martin proposed. </p> <p>There could be other ways to look at it too. Scala could be considered as a sharp weapon. Lethal in the hands of the trained. Self damaging in the hands of the untrained. So compiler switches could simply be a way to graduate from using sticks, to wooden swords, to blunt steel swords to the really sharp ones.</p> <p>I saw a remark which said a compiler switch would divide the programmers. I seem to think such steps which help create terraces will divide the learning challenges. I also saw another remark which said, implementing compiler switches would play into the hands of languages like Kotlin or Ceylon or Fantom. I believe exactly to the contrary. The lower terraces can compete with Kotlin or Ceylon, and the upper terraces can provide a growth path that many other languages will not have. Not being able to plan a phased and an economically practical learning curve for large teams will make the case for other languages stronger. </p>