<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Casual Miracles</title>
	<atom:link href="http://www.casualmiracles.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.casualmiracles.com</link>
	<description></description>
	<lastBuildDate>Fri, 04 May 2012 15:26:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A Small Example of the Typeclass Pattern in Scala</title>
		<link>http://www.casualmiracles.com/2012/05/03/a-small-example-of-the-typeclass-pattern-in-scala/</link>
		<comments>http://www.casualmiracles.com/2012/05/03/a-small-example-of-the-typeclass-pattern-in-scala/#comments</comments>
		<pubDate>Thu, 03 May 2012 19:10:32 +0000</pubDate>
		<dc:creator>channing</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.casualmiracles.com/?p=905</guid>
		<description><![CDATA[Typeclasses, (or type classes), are most famously a language feature of Haskell that has gained interest in the Scala community. Here I describe the basic pattern with references for further study. What is a type class and why do I <a href="http://www.casualmiracles.com/2012/05/03/a-small-example-of-the-typeclass-pattern-in-scala/">[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Typeclasses, (or type classes), are most famously a <a href="http://learnyouahaskell.com/types-and-typeclasses#typeclasses-101" title="Typeclasses101">language feature</a> of Haskell that has gained interest in the Scala community. Here I describe the basic pattern with references for further study.<br />
<span id="more-905"></span></p>
<h3>What is a type class and why do I need it?</h3>
<blockquote><p>Type classes are useful to solve several fundamental challenges in software engineering and programming languages. In particular type classes support retroactive extension: the ability to extend existing software modules with new functionality without needing to touch or re-compile the original source. [1]</p></blockquote>
<p>The Scala library includes a few typeclasses such as scala.math.Numeric and scala.math.Ordering, and <a href="http://code.google.com/p/scalaz/" title="Scalaz">Scalaz</a> is all typeclasses.</p>
<p>Scala enables the typeclass pattern using traits and implicits, and whilst Scala&#8217;s implementation is more verbose than Haskell&#8217;s, it comes with greater flexibility. Haskell only allows a single typeclass instance globally, whereas Scala allows any number to be available.<br />
Furthermore, Scala allows default implementations to be made available if no others can be found.</p>
<p>For a deeper understanding see the references at the bottom of the blog.</p>
<h3>Show me the code!</h3>
<p>(You can copy and paste this into your favourite IDE)</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">/* Here is the first piece of the Scala typeclass puzzle, its just a trait.
 * The trait defines a concept which in this case is a transformer 
 * that transforms a type T into a R.
 */</span>
<span style="color: #0000ff; font-weight: bold;">trait</span> Transformer<span style="color: #F78811;">&#91;</span>T, R<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> transform<span style="color: #F78811;">&#40;</span>t<span style="color: #000080;">:</span> T<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> R
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #008000; font-style: italic;">/* 
 * This is a companion object for the typeclass giving default implementations for the typeclass.
 * These implementations are found after local implicits, so you can still override the default
 * behaviour. For more about the search order see [3].
 */</span>
<span style="color: #0000ff; font-weight: bold;">object</span> Transformer <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">object</span> IntToStringTransformer <span style="color: #0000ff; font-weight: bold;">extends</span> Transformer<span style="color: #F78811;">&#91;</span>Int, String<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">def</span> transform<span style="color: #F78811;">&#40;</span>t<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> t.<span style="color: #000000;">toString</span> <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/* This one is interesting. It transforms a List[T] into a String, but to do that it
   * needs a transformer for T to String. So it requires such a transformer implicitly.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">def</span> ListToStringTransformer<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> tToString<span style="color: #000080;">:</span> Transformer<span style="color: #F78811;">&#91;</span>T, String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Transformer<span style="color: #F78811;">&#91;</span>List<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span>, String<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> transform<span style="color: #F78811;">&#40;</span>t<span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> t.<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span>tToString.<span style="color: #000000;">transform</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">mkString</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;,&quot;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #004000; font-style: italic;">// This is something that makes use of the typeclass</span>
<span style="color: #0000ff; font-weight: bold;">trait</span> Transform <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// The implicit Transformer, transformer, supplies an appropriate transformer for the method</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> transform<span style="color: #F78811;">&#91;</span>T, R<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>t<span style="color: #000080;">:</span> T<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> transformer<span style="color: #000080;">:</span> Transformer<span style="color: #F78811;">&#91;</span>T, R<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> R <span style="color: #000080;">=</span> transformer.<span style="color: #000000;">transform</span><span style="color: #F78811;">&#40;</span>t<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #004000; font-style: italic;">// These examples will drop back to the default implementations in the Transformer's companion object</span>
<span style="color: #0000ff; font-weight: bold;">object</span> ExampleWithDefaults <span style="color: #0000ff; font-weight: bold;">extends</span> App <span style="color: #0000ff; font-weight: bold;">with</span> Transform <span style="color: #F78811;">&#123;</span>
  println<span style="color: #F78811;">&#40;</span>transform<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #004000; font-style: italic;">// prints 2</span>
  println<span style="color: #F78811;">&#40;</span>transform<span style="color: #F78811;">&#40;</span>List<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span>, <span style="color: #F78811;">2</span>, <span style="color: #F78811;">3</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #004000; font-style: italic;">// prints 1,2,3</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #004000; font-style: italic;">// I want my own special transformers!</span>
<span style="color: #0000ff; font-weight: bold;">object</span> ExampleWithMyTransformers <span style="color: #0000ff; font-weight: bold;">extends</span> App <span style="color: #0000ff; font-weight: bold;">with</span> Transform <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">object</span> MyIntToXmlTransformer <span style="color: #0000ff; font-weight: bold;">extends</span> Transformer<span style="color: #F78811;">&#91;</span>Int, xml.<span style="color: #000000;">Node</span><span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">def</span> transform<span style="color: #F78811;">&#40;</span>t<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #000080;">&lt;</span>aNumber<span style="color: #000080;">&gt;</span><span style="color: #F78811;">&#123;</span> t.<span style="color: #000000;">toString</span> <span style="color: #F78811;">&#125;</span><span style="color: #000080;">&lt;</span>/aNumber<span style="color: #000080;">&gt;</span> <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// As with the default List transformer, this one needs a transformer of T to Node so that it can perform the transform for lists.</span>
  <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">def</span> MyListToXmlTransformer<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> transformer<span style="color: #000080;">:</span> Transformer<span style="color: #F78811;">&#91;</span>T, xml.<span style="color: #000000;">Node</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Transformer<span style="color: #F78811;">&#91;</span>List<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span>, xml.<span style="color: #000000;">NodeSeq</span><span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">import</span> xml.<span style="color: #000080;">_</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> transform<span style="color: #F78811;">&#40;</span>t<span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> NodeSeq <span style="color: #000080;">=</span> t.<span style="color: #000000;">foldLeft</span><span style="color: #F78811;">&#40;</span>NodeSeq.<span style="color: #000000;">Empty</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>accumulator, next<span style="color: #F78811;">&#41;</span> ⇒ accumulator ++ transformer.<span style="color: #000000;">transform</span><span style="color: #F78811;">&#40;</span>next<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  println<span style="color: #F78811;">&#40;</span>transform<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #004000; font-style: italic;">// prints &lt;aNumber&gt;2&lt;/aNumber&gt;</span>
  println<span style="color: #F78811;">&#40;</span>transform<span style="color: #F78811;">&#40;</span>List<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span>, <span style="color: #F78811;">2</span>, <span style="color: #F78811;">3</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #004000; font-style: italic;">// prints &lt;aNumber&gt;1&lt;/aNumber&gt;&lt;aNumber&gt;2&lt;/aNumber&gt;&lt;aNumber&gt;3&lt;/aNumber&gt;</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// Here is a transformer from Boolean to String</span>
  <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">object</span> BooleanToStringTransformer <span style="color: #0000ff; font-weight: bold;">extends</span> Transformer<span style="color: #F78811;">&#91;</span>Boolean, String<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">def</span> transform<span style="color: #F78811;">&#40;</span>b<span style="color: #000080;">:</span> Boolean<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> b.<span style="color: #000000;">toString</span> <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// This is the clever bit. </span>
  <span style="color: #004000; font-style: italic;">// Now we can now transform List[Boolean] to String. The List transformer defined in the default Transformer trait</span>
  <span style="color: #004000; font-style: italic;">// will be used but with the new BooleanToStringTransformer. So the default transformers and the new ones</span>
  <span style="color: #004000; font-style: italic;">// work together.</span>
  println<span style="color: #F78811;">&#40;</span>transform<span style="color: #F78811;">&#40;</span>List<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span>, <span style="color: #0000ff; font-weight: bold;">false</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #004000; font-style: italic;">// prints true,false</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// Finally, if there are no implicits available then the code fails to compile.</span>
  <span style="color: #004000; font-style: italic;">// transform(&quot;3&quot;)</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<h3>Summary</h3>
<p>So we defined a typeclass which has been extended with new instances. If the transformer typeclass were part of a library, we can retroactively extend the library with new behaviour supporting new types not originally conceived of in the library. Furthermore, the library supplied types and the new ones work together perfectly.</p>
<h3>Further Study</h3>
<p>[1] <a href="http://ropas.snu.ac.kr/~bruno/papers/TypeClasses.pdf">&#8220;Type Classes as Objects and Implicits&#8221;; Bruno C. d. S. Oliveira, Adriaan Moors, Martin Odersky</a><br />
[2] <a href="http://homepages.inf.ed.ac.uk/wadler/papers/class/class.ps">&#8220;How to make ad-hoc polymorphism less ad-hoc&#8221;; Philip Wadler, Stephen Bolt, (POPL &#8217;89)</a><br />
[3] <a href="http://stackoverflow.com/a/5598107/434405/">Implicit resolution order</a><br />
[4] <a href="http://www.youtube.com/watch?v=sVMES4RZF-8">Tutorial: Typeclass with Dan Rosen</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.casualmiracles.com/2012/05/03/a-small-example-of-the-typeclass-pattern-in-scala/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Flattening the Loan Pattern</title>
		<link>http://www.casualmiracles.com/2012/04/18/flattening-the-loan-pattern/</link>
		<comments>http://www.casualmiracles.com/2012/04/18/flattening-the-loan-pattern/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 15:43:02 +0000</pubDate>
		<dc:creator>channing</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.casualmiracles.com/?p=882</guid>
		<description><![CDATA[The loan pattern is a common pattern for working with resources that should be closed or otherwise managed after use. It removes responsibility from the developer to manage a resource properly. When loaning several objects, one often ends up with <a href="http://www.casualmiracles.com/2012/04/18/flattening-the-loan-pattern/">[...]</a>]]></description>
			<content:encoded><![CDATA[<p>The <a href="https://wiki.scala-lang.org/display/SYGN/Loan" title="Loan Pattern">loan pattern</a> is a common pattern for working with resources that should be closed or otherwise managed after use. It removes responsibility from the developer to manage a resource properly. When loaning several objects, one often ends up with nested functions. Here is a way to flatten that to make things clearer.<br />
<span id="more-882"></span></p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #008000; font-style: italic;">/**
 * Functions that do the flattening
 */</span>
<span style="color: #0000ff; font-weight: bold;">object</span> Loans <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> loan<span style="color: #F78811;">&#91;</span>T1, T2<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>f<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>T1, T2<span style="color: #F78811;">&#41;</span> ⇒ Unit<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> f1<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>T1 ⇒ Unit<span style="color: #F78811;">&#41;</span> ⇒ Unit, f2<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>T2 ⇒ Unit<span style="color: #F78811;">&#41;</span> ⇒ Unit<span style="color: #F78811;">&#41;</span>
  	<span style="color: #000080;">=</span> f1 <span style="color: #F78811;">&#123;</span> a ⇒ f2 <span style="color: #F78811;">&#123;</span> b ⇒ f<span style="color: #F78811;">&#40;</span>a, b<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span> <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> loan<span style="color: #F78811;">&#91;</span>T1, T2, T3<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>f<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>T1, T2, T3<span style="color: #F78811;">&#41;</span> ⇒ Unit<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> f1<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>T1 ⇒ Unit<span style="color: #F78811;">&#41;</span> ⇒ Unit, f2<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>T2 ⇒ Unit<span style="color: #F78811;">&#41;</span> ⇒ Unit, f3<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>T3 ⇒ Unit<span style="color: #F78811;">&#41;</span> ⇒ Unit<span style="color: #F78811;">&#41;</span> 
  	<span style="color: #000080;">=</span> f1 <span style="color: #F78811;">&#123;</span> a ⇒ f2 <span style="color: #F78811;">&#123;</span> b ⇒ f3 <span style="color: #F78811;">&#123;</span> c ⇒ f<span style="color: #F78811;">&#40;</span>a, b, c<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span> <span style="color: #F78811;">&#125;</span> <span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> LoanExample <span style="color: #0000ff; font-weight: bold;">extends</span> App <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">import</span> Loans.<span style="color: #000080;">_</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// create some fake types for the purpose of illustration</span>
  <span style="color: #0000ff; font-weight: bold;">type</span> Connection <span style="color: #000080;">=</span> Unit
  <span style="color: #0000ff; font-weight: bold;">type</span> Session <span style="color: #000080;">=</span> Int
  <span style="color: #0000ff; font-weight: bold;">type</span> User <span style="color: #000080;">=</span> String
&nbsp;
  <span style="color: #004000; font-style: italic;">// implicits calling functions with loans</span>
  <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">def</span> withDatabase<span style="color: #F78811;">&#40;</span>f<span style="color: #000080;">:</span> Connection ⇒ Unit<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> f<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">def</span> withSession<span style="color: #F78811;">&#40;</span>f<span style="color: #000080;">:</span> Session ⇒ Unit<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> f<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">def</span> withUser<span style="color: #F78811;">&#40;</span>f<span style="color: #000080;">:</span> User ⇒ Unit<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> f<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Bob&quot;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// conventional loan pattern results in deep nesting</span>
  withDatabase <span style="color: #F78811;">&#123;</span> connection ⇒
    withSession <span style="color: #F78811;">&#123;</span> session ⇒
      withUser <span style="color: #F78811;">&#123;</span> user ⇒
        println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Hi from the depths.&quot;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// flattened</span>
  loan <span style="color: #F78811;">&#123;</span> <span style="color: #F78811;">&#40;</span>c<span style="color: #000080;">:</span> Connection, s<span style="color: #000080;">:</span> Session, u<span style="color: #000080;">:</span> User<span style="color: #F78811;">&#41;</span> ⇒ 
    println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;hi from flatland&quot;</span><span style="color: #F78811;">&#41;</span> 
  <span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>The nice thing about this is that the implicit argument lists in Loans enable you to call a loan method without passing those functions in explicitly. A problem will be if you need to loan two or more of the same type which is not likely since one is usually borrowing unique resources.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casualmiracles.com/2012/04/18/flattening-the-loan-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Small Example of Applicative Functors with Scalaz</title>
		<link>http://www.casualmiracles.com/2012/01/16/a-small-example-of-applicative-functors-with-scalaz/</link>
		<comments>http://www.casualmiracles.com/2012/01/16/a-small-example-of-applicative-functors-with-scalaz/#comments</comments>
		<pubDate>Mon, 16 Jan 2012 22:32:18 +0000</pubDate>
		<dc:creator>channing</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.casualmiracles.com/?p=689</guid>
		<description><![CDATA[I recently blogged about Functors and mentioned a mysterious beast called Applicative Functor. Here is a simple, complete example that shows how you can use Applicative Functors with the Scalaz library, with references for further reading. What&#8217;s the Problem? Given <a href="http://www.casualmiracles.com/2012/01/16/a-small-example-of-applicative-functors-with-scalaz/">[...]</a>]]></description>
			<content:encoded><![CDATA[<p>I recently <a href="http://www.casualmiracles.com/2012/01/08/a-small-functor-example/">blogged</a> about Functors and mentioned a mysterious beast called <em>Applicative Functor</em>. Here is a simple, complete example that shows how you can use Applicative Functors with the <a href="http://code.google.com/p/scalaz/" title="Scalaz">Scalaz</a> library, with references for further reading.<br />
<span id="more-689"></span></p>
<h3>What&#8217;s the Problem?</h3>
<blockquote><p>Given a function that takes multiple arguments A,B,… that returns a Result, how can that function by applied to arguments M[A], M[B],… and get M[Result]?</p></blockquote>
<p>where M is an Option, List, etc.</p>
<h3>Applicative Functors</h3>
<p>To understand the mechanics of how applicative functors solve our problem, please refer to the references below, in particular Heiko Seeberger&#8217;s <a href="http://hseeberger.wordpress.com/2011/01/31/applicatives-are-generalized-functors/">Applicatives are generalized functors</a>.</p>
<p>What I present below is an example of use. I hope you can take it away and benefit from it without necessarily understanding the theory. However, I thoroughly recommend further reading and study, its fun and you will gain much deeper insights.</p>
<p>So without further ado, here&#8217;s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scalaz.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> Scalaz.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> Applicatives <span style="color: #0000ff; font-weight: bold;">extends</span> App <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/*
   * Lets assume some Options which must be applied to a function.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> x<span style="color: #000080;">:</span>Option<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">2</span>.<span style="color: #000000;">some</span> <span style="color: #004000; font-style: italic;">// scalaz enrichment for options</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> y<span style="color: #000080;">:</span>Option<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">3</span>.<span style="color: #000000;">some</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> z<span style="color: #000080;">:</span>Option<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">5</span>.<span style="color: #000000;">some</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// Lets add them without applicative functors</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> usingFor <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>theX <span style="color: #000080;">&lt;</span>- x<span style="color: #000080;">;</span> theY <span style="color: #000080;">&lt;</span>- y<span style="color: #000080;">;</span> theZ <span style="color: #000080;">&lt;</span>- z<span style="color: #F78811;">&#41;</span> 
                   <span style="color: #0000ff; font-weight: bold;">yield</span> theX + theY + theZ
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">val</span> usingMaps <span style="color: #000080;">=</span> x flatMap 
                    <span style="color: #F78811;">&#40;</span>theX <span style="color: #000080;">=&gt;</span> y flatMap 
                      <span style="color: #F78811;">&#40;</span>theY <span style="color: #000080;">=&gt;</span> z map 
                        <span style="color: #F78811;">&#40;</span>theZ <span style="color: #000080;">=&gt;</span> theZ + theY + theX<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/* With scalaz we can do the following instead of for or maps
   * First we need to put the function in the right form, curried.
   * To understand why please read the references I've given below.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> addInts <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">:</span>Int, b<span style="color: #000080;">:</span>Int, c<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> a + b + c <span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">curried</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// apply the function to x, y and z</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> sum <span style="color: #000080;">=</span> x <span style="color: #000080;">&lt;*&gt;</span> <span style="color: #F78811;">&#40;</span>y <span style="color: #000080;">&lt;*&gt;</span> <span style="color: #F78811;">&#40;</span>z map addInts<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #004000; font-style: italic;">// Some(10)</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// Scalaz offers an alternative syntax that is easier to use</span>
  <span style="color: #F78811;">&#40;</span>x |<span style="color: #000080;">@</span>| y |<span style="color: #000080;">@</span>| z<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span><span style="color: #000080;">_</span> + <span style="color: #000080;">_</span> + <span style="color: #000080;">_</span><span style="color: #F78811;">&#125;</span>   <span style="color: #004000; font-style: italic;">// Some(10)</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/*
   * If one of the options is a none
   * then the result of the whole expression will be none.
   */</span>
  <span style="color: #F78811;">&#40;</span>x |<span style="color: #000080;">@</span>| none<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span><span style="color: #000080;">_</span> + <span style="color: #000080;">_</span><span style="color: #F78811;">&#125;</span> <span style="color: #004000; font-style: italic;">// None</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/*
   * The function can be any method, including 'apply'
   */</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #0000ff; font-weight: bold;">class</span> Person<span style="color: #F78811;">&#40;</span>age<span style="color: #000080;">:</span> Int, height<span style="color: #000080;">:</span>Double, name<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/*
   * Person.apply method is a function (Int, Double, String) =&gt; Person
   */</span>
  <span style="color: #F78811;">&#40;</span>some<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span> |<span style="color: #000080;">@</span>| some<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1.1</span><span style="color: #F78811;">&#41;</span> |<span style="color: #000080;">@</span>| none<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>Person.<span style="color: #000000;">apply</span> <span style="color: #000080;">_</span><span style="color: #F78811;">&#125;</span>  
  <span style="color: #004000; font-style: italic;">// none</span>
&nbsp;
  <span style="color: #F78811;">&#40;</span>some<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">4</span><span style="color: #F78811;">&#41;</span> |<span style="color: #000080;">@</span>| some<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1.1</span><span style="color: #F78811;">&#41;</span> |<span style="color: #000080;">@</span>| <span style="color: #6666FF;">&quot;Angelica&quot;</span>.<span style="color: #000000;">some</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>Person.<span style="color: #000000;">apply</span> <span style="color: #000080;">_</span><span style="color: #F78811;">&#125;</span> 
  <span style="color: #004000; font-style: italic;">// Some(Person(4, 1.1, Angelica))</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/*
   * The beauty of this is that it works with ANY higher kind, eg. List
   * or your own types!
   */</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> l1 <span style="color: #000080;">=</span> <span style="color: #F78811;">1</span> <span style="color: #000080;">::</span> <span style="color: #F78811;">2</span> <span style="color: #000080;">::</span> Nil
  <span style="color: #0000ff; font-weight: bold;">val</span> l2 <span style="color: #000080;">=</span> <span style="color: #F78811;">3</span> <span style="color: #000080;">::</span> <span style="color: #F78811;">4</span> <span style="color: #000080;">::</span> Nil
&nbsp;
  <span style="color: #F78811;">&#40;</span>l1 |<span style="color: #000080;">@</span>| l2<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span><span style="color: #000080;">_</span> + <span style="color: #000080;">_</span><span style="color: #F78811;">&#125;</span> <span style="color: #004000; font-style: italic;">// List(4, 5, 5, 6)</span>
  <span style="color: #F78811;">&#40;</span><span style="color: #000080;">&lt;</span>xml/<span style="color: #000080;">&gt;</span> |<span style="color: #000080;">@</span>| <span style="color: #000080;">&lt;</span>xml2/<span style="color: #000080;">&gt;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#123;</span><span style="color: #000080;">_</span> ++ <span style="color: #000080;">_</span><span style="color: #F78811;">&#125;</span> <span style="color: #004000; font-style: italic;">// List(&lt;xml&gt;&lt;/xml&gt;&lt;xml2&gt;&lt;/xml2&gt;)</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<h4>Further Reading</h4>
<ul>
<li>Learn You a Haskell&#8217;s Chapter on <a href="http://learnyouahaskell.com/functors-applicative-functors-and-monoids">Functors, Applicative Functors and Monoids</a>
<li>Heiko Seeberger&#8217;s <a href="http://hseeberger.wordpress.com/2011/01/31/applicatives-are-generalized-functors/">Applicatives are generalized functors</a>
<li><a href="http://scalaz.github.com/scalaz/scalaz-2.9.1-6.0.2/doc.sxr/scalaz/example/ExampleApplicative.scala.html#23252">Scalaz Applicative Examples</a>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.casualmiracles.com/2012/01/16/a-small-example-of-applicative-functors-with-scalaz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Small Functor Example</title>
		<link>http://www.casualmiracles.com/2012/01/08/a-small-functor-example/</link>
		<comments>http://www.casualmiracles.com/2012/01/08/a-small-functor-example/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 20:54:57 +0000</pubDate>
		<dc:creator>channing</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.casualmiracles.com/?p=622</guid>
		<description><![CDATA[In the world of functional programming you will quickly come across the concept of a Functor. What I present below is a simple example that might provide some intuition, with references for further reading. What&#8217;s the Problem? We often want <a href="http://www.casualmiracles.com/2012/01/08/a-small-functor-example/">[...]</a>]]></description>
			<content:encoded><![CDATA[<p>In the world of functional programming you will quickly come across the concept of a Functor. What I present below is a simple example that might provide some intuition, with references for further reading.<br />
<span id="more-622"></span></p>
<h3>What&#8217;s the Problem?</h3>
<p>We often want to use a function that takes an instance of A and returns an instance of B in a context where we have an instance of <em>M[A]</em> and need an instance of <em>M[B]</em>. The most common example is the <em>map</em> method on List, Option, etc.</p>
<p>So the problem is: </p>
<blockquote><p>Given a function f that takes an A and returns a B, how can we construct a new function that takes an M[A] and returns an M[B] <strong>for any M</strong>?</p></blockquote>
<h3>Functors</h3>
<p>A Functor accepts a function, A ⇒ B, and returns a new function M[A] ⇒ M[B], where M is any <em>kind</em>.</p>
<p>So without further ado, here&#8217;s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> Functorise <span style="color: #0000ff; font-weight: bold;">extends</span> App <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// This is a Functor</span>
  <span style="color: #0000ff; font-weight: bold;">trait</span> Functor<span style="color: #F78811;">&#91;</span>M<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">/* convert f into a function mapping M[A] to M[B]
     * eg. if M were List, and f was Int ⇒ String
     * fmap would yield List[Int] ⇒ List[String]
     */</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> fmap<span style="color: #F78811;">&#91;</span>A, B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>f<span style="color: #000080;">:</span> A ⇒ B<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> M<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span> ⇒ M<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/* Here are a couple of examples for Option and List Functors
   * They are implicit so they can be used below in enrichWithFunctor
   */</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">object</span> OptionFunctor <span style="color: #0000ff; font-weight: bold;">extends</span> Functor<span style="color: #F78811;">&#91;</span>Option<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> fmap<span style="color: #F78811;">&#91;</span>A, B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>f<span style="color: #000080;">:</span> A ⇒ B<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Option<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span> ⇒ Option<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span> 
      <span style="color: #000080;">=</span> option ⇒ option map f
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">object</span> ListFunctor <span style="color: #0000ff; font-weight: bold;">extends</span> Functor<span style="color: #F78811;">&#91;</span>List<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> fmap<span style="color: #F78811;">&#91;</span>A, B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>f<span style="color: #000080;">:</span> A ⇒ B<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span> ⇒ List<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span> 
      <span style="color: #000080;">=</span> list ⇒ list map f
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/* enrichWithFunctor is an implicit to enrich any kind 
   * with an fmap method.
   * List, Option and any other Foo[X] can be enriched with the
   * new method.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">def</span> enrichWithFunctor<span style="color: #F78811;">&#91;</span>M<span style="color: #F78811;">&#91;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#93;</span>, A<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>m<span style="color: #000080;">:</span> M<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> <span style="color: #F78811;">&#123;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">/* fmap requires an implicit functor, whose type is M, to which it
     * delegates to do the real work
     */</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> mapWith<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>f<span style="color: #000080;">:</span> A ⇒ B<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> functor<span style="color: #000080;">:</span> Functor<span style="color: #F78811;">&#91;</span>M<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> M<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span> 
      <span style="color: #000080;">=</span> functor.<span style="color: #000000;">fmap</span><span style="color: #F78811;">&#40;</span>f<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>m<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #004000; font-style: italic;">// some examples</span>
&nbsp;
  println<span style="color: #F78811;">&#40;</span>List<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span>, <span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span> mapWith <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span> + <span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #004000; font-style: italic;">// List(2, 3)</span>
&nbsp;
  println<span style="color: #F78811;">&#40;</span>some<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> mapWith <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span> + <span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> mapWith <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span> <span style="color: #000080;">*</span> <span style="color: #F78811;">3</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #004000; font-style: italic;">// Some(6)</span>
&nbsp;
  println<span style="color: #F78811;">&#40;</span>none<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> mapWith <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span> + <span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #004000; font-style: italic;">// None</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> some<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">:</span> A<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> Option<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Some<span style="color: #F78811;">&#40;</span>a<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> none<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span><span style="color: #000080;">:</span> Option<span style="color: #F78811;">&#91;</span>A<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> None
<span style="color: #F78811;">&#125;</span></pre></div></div>

<h3>What is Going On?</h3>
<p>Lets take the <em>List(1, 2) mapWith (_ + 1)</em> example and go through it. </p>
<p>There is no <em>mapWith</em> method on List, so an implicit method is searched for that can turn the List into something that does have a <em>mapWith</em> method. Thus, <em>enrichWithFunctor</em> is found and called with the <em>List(1, 2)</em> which returns an anonymous class with the <em>mapWith</em> method required. </p>
<p>By the way, to understand more about that M[_], watch <a href="http://vimeo.com/28793245">Daniel Spiewak&#8217;s High Wizardry in the Land of Scala</a> talk.</p>
<p>That anonymous class also has a type parameter, M, which has now been fixed to List, and so <em>mapWith</em> in this class looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> mapWith<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>f<span style="color: #000080;">:</span> A ⇒ B<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> functor<span style="color: #000080;">:</span> Functor<span style="color: #F78811;">&#91;</span>List<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span>B<span style="color: #F78811;">&#93;</span> 
  <span style="color: #000080;">=</span> functor.<span style="color: #000000;">fmap</span><span style="color: #F78811;">&#40;</span>f<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>m<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>The <em>mapWith</em> method requires an implicit Functor[List] to be available, and the ListFunctor object is just that. So when <em>functor.fmap(f)</em> is called, it is the ListFunctor&#8217;s <em>fmap</em> method being called.</p>
<p><em>ListFunctor.fmap(f)</em> takes a function, in our example its <em>(i:Int) ⇒ i + 1</em>, and returns a new function <em>(list:List[Int]) ⇒ list.map(_ + 1)</em>. </p>
<p>Finally, <em>m</em>, the <em>List(1, 2)</em> we started with that was enriched with the <em>mapWith</em> method, is applied to the new function which results in a new List. </p>
<h3>But Why?</h3>
<p>All we&#8217;ve done here is create a complicated looking bunch of code that just ends up calling methods on Option and List that already exist, specifically the map method.</p>
<p>Yes, in these examples thats true.</p>
<p>The real idea is that we&#8217;ve created <em>a general abstraction for all kinds, including those that don&#8217;t have a map method</em>. All a user of this code needs to do is have an implicit Functor in scope for the kind they would like to map over.</p>
<p>Furthermore, this is a building block for some interesting things that come next like Applicative Functors.</p>
<p>Finally, you don&#8217;t need to implement this yourself, <a href="http://code.google.com/p/scalaz/">Scalaz</a> has it already for you to exploit.</p>
<h4>Further Reading</h4>
<ul>
<li>Learn You a Haskell&#8217;s Chapter on <a href="http://learnyouahaskell.com/functors-applicative-functors-and-monoids">Functors, Applicative Functors and Monoids</a>
<li>Heiko Seeberger&#8217;s fantastic <a href="https://hseeberger.wordpress.com/2010/11/25/introduction-to-category-theory-in-scala/">Introduction to Category theory in Scala</a>
<li><a href="http://scalaz.github.com/scalaz/scalaz-2.9.1-6.0.2/doc.sxr/scalaz/example/ExampleFunctor.scala.html">Scalaz Functor Examples</a>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.casualmiracles.com/2012/01/08/a-small-functor-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Successful Delivery</title>
		<link>http://www.casualmiracles.com/2012/01/06/a-successful-delivery/</link>
		<comments>http://www.casualmiracles.com/2012/01/06/a-successful-delivery/#comments</comments>
		<pubDate>Fri, 06 Jan 2012 22:06:05 +0000</pubDate>
		<dc:creator>channing</dc:creator>
				<category><![CDATA[Casual Miracles]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Software Process]]></category>

		<guid isPermaLink="false">http://www.casualmiracles.com/?p=572</guid>
		<description><![CDATA[Over the last few months, we&#8217;ve been working on delivering HSBC&#8217;s Clearing Connectivity Layer and OTC Cleared Trade Acceptance System. We went live early December, and in fact, we delivered our first release early. The system enables HSBC to handle <a href="http://www.casualmiracles.com/2012/01/06/a-successful-delivery/">[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Over the last few months, we&#8217;ve been working on delivering HSBC&#8217;s Clearing Connectivity Layer and OTC Cleared Trade Acceptance System. We went live early December, and in fact, we delivered our first release early.</p>
<p><span id="more-572"></span></p>
<p>The system enables HSBC to handle the processing of all cleared interest rate and credit derivatives from multiple market participants. The first release provided full connectivity to the <a title="LCH Clearnet" href="http://www.lchclearnet.com/">London Clearing House</a>, with the <a title="CME" href="http://www.cmegroup.com/">Chicago Mercantile Exchange</a> and the <a title="ICE" href="http://www.theice.com/">Intercontinental Exchange</a> following shortly.</p>
<p>We had a tight deadline and quite a lot of things to figure out. The project had been difficult to analyse in the past as a result of changes in legislation and many other unknowns. So how did we do it?</p>
<h4>Talking</h4>
<p>The first thing we always do is talk to the business and figure out what is needed. Not in abstract business terms or strategic direction, but what the <em>actual</em> user sitting in front of our app is <em>actually</em> going to see, do and expect. Whilst the general business background is useful context and necessary, it is woefully inadequate for building a system.</p>
<p>Having the people who are going to <em>actually</em> build your system talk, <em>in person</em>, to the people that are <em>actually</em>, physically, going to use that system is critical. Not managers talking to managers but the people doing the work. And we do that every day, for every feature!</p>
<h4>Minimal Viable Product</h4>
<p>The next critical step is establishing the absolute minimum that will enable the system to be useful, and deliver that. <strong>Nothing else!</strong> (until next week)</p>
<p>It is surprisingly difficult to help stakeholders become accustomed to focussing on what is needed for the next release. IT has done a really good job of telling people they need to write down all their requirements for the next year or they will not get them. We have quite a bit of work to do to undo that ridiculous notion. But when the business understand that we aren&#8217;t saying we won&#8217;t let them have a feature, just that we want them to prioritise what they want for the next release (next week), and they can change their mind whenever they like, they are very happy.</p>
<h4>Who Else Cares?</h4>
<p>I have talked about the business as if they are the only interested party. Not so. We talk to every stakeholder we can find: senior management, BAs, support people, release people, release boards, architects; everyone who might ever have some kind of interest in the system. They all have needs that have to be addressed and prioritised. On one project we were able to name 23 different stakeholder roles for a project, which was surprising to everyone who thought there was only one stakeholder.</p>
<h4>Planning</h4>
<p>Having established a backlog of stuff, we maintain it by planning weekly with all stakeholders, and marvel at how much priorities change, new work appears and old work disappears from week to week. But thats all good! Every change is welcome, and shows how badly wrong things would have gone had we tried to <em>plan-it-up-front</em>.</p>
<h4>Where&#8217;s the Code?</h4>
<p>Every day, the team has a stand up to discuss what they are going to do. Then, we implement some features.</p>
<ol>
<li>pair up with another team member in the stand up</li>
<li>talk to the user and find out what they want, sketch ideas out on paper or mock them up until enough is understood</li>
<li>write a failing acceptance test with the user. We use <a title="Specs2" href="http://etorreborre.github.com/specs2/">Specs2</a> but you can use whatever you like so long as it produces some kind of user visible result</li>
<li>make the acceptance test pass by typing some code in your IDE, writing unit tests as necessary</li>
</ol>
<p>However, a lot of what we did involved communication with other systems too for which we required <em>real</em> samples of messages since nothing had schemas etc. This was where a lot of time was spent, again in dialog with stakeholders to ensure that the messages we process and respond with were correct.</p>
<h4>Good, all done then!</h4>
<p>No. When we think we&#8217;ve finished a feature we demo it to the user as soon as possible &#8211; we don&#8217;t wait to the end of the iteration. They can play with it and change things which they often do when they see something working for real.  And thats all good, we want them to have something useful.</p>
<h4>Builds?</h4>
<p>Part of the development practice is to commit code frequently ensuring tests always pass. People are surprised by how often we mean by <em>frequently</em>. We mean <em>every single time you complete a piece of work and all the tests pass locally</em>. That happens about once an hour but can be more frequent.</p>
<p>We also use <a title="TeamCity" href="http://www.jetbrains.com/teamcity/">TeamCity</a> to keep us sane but again there are many alternatives.</p>
<p>Finally, deployment. At the start of all project&#8217;s we always have a single mantra about deployment: <em>Thou must automate the deployment</em>. More than that, the same deployment script should work on every dev, test, uat and production environment. Sometimes, the production environment needs to be subtly different, but we try to make the other environments as close to production as possible.</p>
<h4>Technology</h4>
<p>We built the system in Scala with Liftweb. It was tested with Specs2 and we used sbt for builds. Scala is significantly more productive than Java, and Liftweb enabled us to build a robust, secure, dynamic web application very quickly.</p>
<h4>Summary</h4>
<p>Is this starting to sound familiar? Yup, its basically an agile method using practices from XP, Scrum, Kanban, anything that works <em>for this client in this project with this team</em>. And no, you can&#8217;t transplant it to another team! They will have to figure out what works for them, and when we move to another project, we have to figure out what will work for the new environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casualmiracles.com/2012/01/06/a-successful-delivery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Small Example of the Scalaz IO Monad</title>
		<link>http://www.casualmiracles.com/2012/01/03/a-small-example-of-the-scalaz-io-monad/</link>
		<comments>http://www.casualmiracles.com/2012/01/03/a-small-example-of-the-scalaz-io-monad/#comments</comments>
		<pubDate>Tue, 03 Jan 2012 18:03:31 +0000</pubDate>
		<dc:creator>channing</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.casualmiracles.com/?p=594</guid>
		<description><![CDATA[I wanted a very tiny example of using the IO Monad in the brilliant Scalaz library, and here it is. I present the example without explanation, the web does not need another set of explanations about this subject but I <a href="http://www.casualmiracles.com/2012/01/03/a-small-example-of-the-scalaz-io-monad/">[...]</a>]]></description>
			<content:encoded><![CDATA[<p>I wanted a very tiny example of using the IO Monad in the brilliant <a href="https://github.com/scalaz/scalaz" title="Scalaz">Scalaz</a> library, and here it is.</p>
<p><span id="more-594"></span>I present the example without explanation, the web does not need another set of explanations about this subject but I have found the following blogs useful, with a mix of theoretical explanation and practical examples:</p>
<ul>
<li><a href="http://www.stackmob.com/2011/12/scalaz-post-part-2/">Functional IO in Scala with Scalaz</a>
<li><a href="http://blog.sigfpe.com/2007/11/io-monad-for-people-who-simply-dont.html">The IO Monad for People who Simply Don&#8217;t Care</a>
<li><a href="http://apocalisp.wordpress.com/2011/12/19/towards-an-effect-system-in-scala-part-2-io-monad/">Towards an Effect System in Scala, Part 2: IO Monad</a>
</ul>
<p>All this example does is print out the names of files in a directory.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scalaz.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> Scalaz.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> scalaz.<span style="color: #000000;">effects</span>.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> java.<span style="color: #000000;">io</span>.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> TheIO <span style="color: #0000ff; font-weight: bold;">extends</span> App <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">val</span> dirInIO <span style="color: #000080;">=</span> io <span style="color: #F78811;">&#123;</span> <span style="color: #0000ff; font-weight: bold;">new</span> File<span style="color: #F78811;">&#40;</span>System.<span style="color: #000000;">getProperty</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;user.dir&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> filesInIO <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>dir <span style="color: #000080;">&lt;</span>- dirInIO<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">yield</span> dir.<span style="color: #000000;">listFiles</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> namesInIO <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>theFiles ← filesInIO<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">yield</span> theFiles.<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000000;">getName</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">def</span> printNamesInIO <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>all ← namesInIO<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">yield</span> all.<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span>println<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">val</span> result <span style="color: #000080;">=</span> printNamesInIO
&nbsp;
  println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Nothings happened yet, lets go!&quot;</span><span style="color: #F78811;">&#41;</span>
  result.<span style="color: #000000;">unsafePerformIO</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.casualmiracles.com/2012/01/03/a-small-example-of-the-scalaz-io-monad/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A little Scalaz magic</title>
		<link>http://www.casualmiracles.com/2011/11/22/a-little-scalaz-magic/</link>
		<comments>http://www.casualmiracles.com/2011/11/22/a-little-scalaz-magic/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 22:12:06 +0000</pubDate>
		<dc:creator>channing</dc:creator>
				<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.casualmiracles.com/?p=539</guid>
		<description><![CDATA[Here are a few useful things you can do easily with Scalaz to make you code a little simpler. The sample below makes use of Applicative Functors which you don&#8217;t need to understand to benefit from their use, but its <a href="http://www.casualmiracles.com/2011/11/22/a-little-scalaz-magic/">[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Here are a few useful things you can do easily with <a title="Scalaz" href="http://code.google.com/p/scalaz/">Scalaz</a> to make you code a little simpler.<br />
<span id="more-539"></span><br />
The sample below makes use of Applicative Functors which you don&#8217;t need to understand to benefit from their use, but its interesting to learn more about them. See the references below for more information about applicative functors.</p>
<p>Here&#8217;s some code which is based on an <a href="http://scalaz.github.com/scalaz/scalaz-2.9.1-6.0.2/doc.sxr/scalaz/example/ExampleApplicative.scala.html">example</a> on Scalaz&#8217;s website.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">import</span> scalaz.<span style="color: #000080;">_</span>
<span style="color: #0000ff; font-weight: bold;">import</span> Scalaz.<span style="color: #000080;">_</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> Applicatives <span style="color: #0000ff; font-weight: bold;">extends</span> App <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/*
   * Lets pretend you have two optional values, but you want to apply some
   * function to their contents safely.
   * You also don't want to fiddle about with flatmap / map / for / yield
   * Then do this:
   */</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> x<span style="color: #000080;">:</span>Option<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Some<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> y<span style="color: #000080;">:</span>Option<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Some<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">3</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #F78811;">&#40;</span>x |<span style="color: #000080;">@</span>| y<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span><span style="color: #000080;">_</span>+<span style="color: #000080;">_</span><span style="color: #F78811;">&#125;</span> 	<span style="color: #004000; font-style: italic;">// Some(5)</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/*
   * The |@| method is a pimp (enrichment! sorry), on Option in this case,
   * that builds 'something' that applies the function _ + _
   */</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/*
   * If one of the options is a none
   * then the result of the whole expression will be none.
   * (I am using scalaz's some and none because it means less boilerplate)
   */</span>
  <span style="color: #F78811;">&#40;</span>some<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span> |<span style="color: #000080;">@</span>| none<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span><span style="color: #000080;">_</span>+<span style="color: #000080;">_</span><span style="color: #F78811;">&#125;</span> <span style="color: #004000; font-style: italic;">// None</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/*
   * The function can be any method, including 'apply'
   */</span>
  <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #0000ff; font-weight: bold;">class</span> Person<span style="color: #F78811;">&#40;</span>age<span style="color: #000080;">:</span> Int, height<span style="color: #000080;">:</span>Double, name<span style="color: #000080;">:</span> String<span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/*
   *  ⊛ = |@| just for fun
   */</span>
  <span style="color: #F78811;">&#40;</span>some<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span> ⊛ some<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1.1</span><span style="color: #F78811;">&#41;</span> ⊛ none<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>Person.<span style="color: #000000;">apply</span> <span style="color: #000080;">_</span><span style="color: #F78811;">&#125;</span>  <span style="color: #004000; font-style: italic;">// none because an arg was none</span>
  <span style="color: #F78811;">&#40;</span>some<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">4</span><span style="color: #F78811;">&#41;</span> ⊛ some<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1.1</span><span style="color: #F78811;">&#41;</span> ⊛ <span style="color: #6666FF;">&quot;Angelica&quot;</span>.<span style="color: #000000;">some</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>Person.<span style="color: #000000;">apply</span> <span style="color: #000080;">_</span><span style="color: #F78811;">&#125;</span> <span style="color: #004000; font-style: italic;">// Some( Person(4, 1.1, Angelica) )</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/*
   * did you catch the &quot;Angelica&quot;.some - another bit of enriching by scalaz
   */</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">/*
   * It works with ANY higher kind, eg. List
   */</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> l1 <span style="color: #000080;">=</span> <span style="color: #F78811;">1</span> <span style="color: #000080;">::</span> <span style="color: #F78811;">2</span> <span style="color: #000080;">::</span> Nil
  <span style="color: #0000ff; font-weight: bold;">val</span> l2 <span style="color: #000080;">=</span> <span style="color: #F78811;">3</span> <span style="color: #000080;">::</span> <span style="color: #F78811;">4</span> <span style="color: #000080;">::</span> Nil
&nbsp;
  <span style="color: #F78811;">&#40;</span>l1 ⊛ l2<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span><span style="color: #000080;">_</span> + <span style="color: #000080;">_</span><span style="color: #F78811;">&#125;</span> <span style="color: #004000; font-style: italic;">// List(4, 5, 5, 6)</span>
&nbsp;
<span style="color: #F78811;">&#125;</span></pre></div></div>

<h3>References</h3>
<ol>
<li>Eric Torrebore has a very nice <a href="http://etorreborre.blogspot.com/2011/06/essence-of-iterator-pattern.html">article</a> explaining much of this.</li>
<li>Learn You a Haskell has a great <a href="http://learnyouahaskell.com/functors-applicative-functors-and-monoids">chapter</a> on applicative functors amongst other things.</li>
<li>Heiko Seeberger <a href="http://hseeberger.wordpress.com/2011/01/31/applicatives-are-generalized-functors/">blog post</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.casualmiracles.com/2011/11/22/a-little-scalaz-magic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Burndown, Prediction, Confidence and Risk</title>
		<link>http://www.casualmiracles.com/2011/05/16/burndown-prediction-confidence-and-risk/</link>
		<comments>http://www.casualmiracles.com/2011/05/16/burndown-prediction-confidence-and-risk/#comments</comments>
		<pubDate>Mon, 16 May 2011 20:49:18 +0000</pubDate>
		<dc:creator>lance</dc:creator>
				<category><![CDATA[Software Process]]></category>

		<guid isPermaLink="false">http://www.casualmiracles.com/blog/?p=395</guid>
		<description><![CDATA[One of Casual Miracles&#8217; clients had a long running project with a big specification up front. This is not the mode in which we usually like to work. It is, however, an opportunity for an experiment with burndown charts. Having <a href="http://www.casualmiracles.com/2011/05/16/burndown-prediction-confidence-and-risk/">[...]</a>]]></description>
			<content:encoded><![CDATA[<p>One of Casual Miracles&#8217; clients had a long running project with a big specification up front. This is not the mode in which we usually like to work. It is, however, an opportunity for an experiment with burndown charts.</p>
<p><span id="more-395"></span></p>
<p>Having obtained estimates for all of the work, and with contracts duly signed, etc., the work was begun. And so, we tracked the burndown:</p>
<p><a href="/wp-content/uploads/2011/05/burndown.jpg"><img class="aligncenter size-full wp-image-473" title="Burndown" src="/wp-content/uploads/2011/05/burndown.jpg" alt="Burndown" width="495" height="375" /></a><br />
The chart above shows the first 43 days of burndown.</p>
<p>One common question that we try to answer with the burndown charts is: &#8216;What day do we expect the burndown to reach 0?&#8217;. That is: &#8216;When do we expect to finish?&#8217; The assumption is that historical performance predicts future performance. In our case, the question is more like: &#8216;Is there any reason to suspect we might not finish by the contracted date?&#8217;</p>
<p>One simple way of answering this question is to look at ratios: we have completed (551 - 302) estimated days of work in 43 days, so we can expect to take 43 * 551 / (551 - 302) = 96 days to finish everything.</p>
<p>A more &#8216;sophisticated&#8217; approach is to put a linear regression line through the burndown and extrapolate. This tells us that we will reach 0 remaining estimate on day 85, but it fails to give an indication of the confidence we can have about this prediction. We can use the the correlation coefficient to give some idea of this, but it is difficult to know what to do with it.</p>
<p>In fact, the questions above are not really the right ones. The question we should be asking is: &#8216;How confident can we be that the burndown will reach zero <em>on or before</em> a given date?&#8217;.</p>
<p>In order to answer this, we need a different approach. One such approach is based on the <a href="http://en.wikipedia.org/wiki/Monte_Carlo_method">Monte Carlo Method</a>, which in this case translates to asking: &#8216;Assuming the future burndown is similar to the historical data, what fraction of the possible futures result in the burndown reaching zero on or before a given date (or between two dates)?&#8217;</p>
<p>We can apply this method by starting with the estimate on the last burndown day for which we have an estimate (day 43), choosing a day from the history randomly, determining the change in burndown between this day and the preceding one, and then applying that change to the estimate we have for day 43. This then becomes the estimate for day 44 and we repeat the process, until the estimate reaches 0. This then represents one simulated burndown, and we repeat the whole process thousands of times to obtain a distribution of estimates for each future day.</p>
<p>The chart below shows the first result from applying this method:</p>
<p><a href="/wp-content/uploads/2011/05/burndownWithMedian.jpg"><img class="aligncenter size-full wp-image-474" title="Burndown With Median Line" src="/wp-content/uploads/2011/05/burndownWithMedian.jpg" alt="Burndown With Median Line" width="495" height="374" /></a></p>
<p>The line extending the historical burndown is the &#8216;Median Line&#8217;. This is obtained by determining the estimate for each day for which half of the simulations yield a higher estimate and half yield a lower estimate. When this line intersects the Day Number axis (day 97), it tells us that half of the simulated burndowns have reached a zero estimate on or before day 97 and half have not. Based on this information (and speaking somewhat informally), we have a half chance of delivering on or before day 97 and a half chance of delivering afterwards.</p>
<p>Knowing this is interesting information, but it is reasonable to ask what is the spread of simulation results. After all, if all simulations terminate within a very tight spread centred around day 97, we can still reasonably accurately predict the end day. If however, the spread is very wide, some other action might be suggested.</p>
<p>We can obtain an idea of the spread in the same way as we obtained the median line. If, for each day, we ask &#8216;What value of estimates (centred around the median value) contain x% of the simulated results on each simulated day?&#8217;. This will give us &#8216;confidence intervals&#8217; around the median:</p>
<p><a href="/wp-content/uploads/2011/05/burndownWithIntervals.jpg"><img class="aligncenter size-full wp-image-475" title="Burndown With Confidence Intervals" src="/wp-content/uploads/2011/05/burndownWithIntervals.jpg" alt="Burndown With Confidence Intervals" width="497" height="376" /></a></p>
<p>The chart above shows the 50%, 90% and 99% confidence intervals. Consider the 90% confidence interval, which is enclosed by the dark green curves starting at the final known burndown day (43, 302) and terminating at (74, 0) and (124, 0). On each simulated day, the set of estimates between these two curves contain 90% of the simulated burndowns. Because we placed the intervals symmetrically around the median line, the space underneath the lower curve represents 5% of the simulated estimates (those that represent the team performing above expectations), and the space above the upper curve represents the final 5% of the simulated estimated (those that represent the team performing below expectations).</p>
<p>Clearly the spread is quite wide! If we wanted to give a date that represented 95% confidence, we should choose choose day 124, which is 27 days after the median line predicts, or a difference of 25%. It is also 28 days (also about 25%) after our simple ratio based estimate given at the beginning of this article and 39 days (40%) after the the linear regression estimate.</p>
<p>The astute reader might notice two important things:</p>
<ol>
<li>The day at which the confidence interval curves reach a Remaining Estimate of 0 spreads out alarmingly as the required confidence increases. i.e. the 50%, 90% and 99% upper confidence envelopes require 10%, 28% and 45% respectively more days to reach 0 than the median line does.</li>
<li>The confidence intervals are distributed symmetrically around the median line when a vertical slice is taken through the chart. Indeed, the histograms giving rise to these confidence intervals are near-Gaussian (courtesy of the Central Limit Theorem), at least until some of the simulations are terminated when they reach 0 remaining estimate. This is not true when a horizontal slice is taken. In particular, when those curves reach the Remaining Estimate of 0, it is plain to see that, for example, the lower envelope for the 90% confidence interval is 97 - 74 = 23 days below the median, whereas the corresponding upper curve is 124 - 97 = 27 days above the median.</li>
</ol>
<p>The first of these points suggests that, even at this advanced state of the project, requiring high confidence about the delivery date demands significant &#8216;contingency&#8217;, with the 99% upper confidence envelope prediction being more than a factor of 2 larger than the corresponding lower confidence envelope prediction. Similarly, the ratio between the upper 99% confidence envelope prediction and the median prediction is just under 1.5.</p>
<p>The second point suggests that it is easier to run late than it is to run early, which will come as no surprise to development teams and their customers everywhere. Indeed, the skew also implies that when things are running late, it is possible for even more tardiness to result. This is intuitively true since, with less work remaining to be done, it is less likely that significant estimation &#8216;errors&#8217; will occur.</p>
<p>Of course, we should not take all of this too seriously; a great deal has been made of very little historical data. Attention to a burndown chart often causes teams to adjust scope and other factors to bring the project in on time. The date is far more likely to be missed because of some unmitigated risk, such as the absence of a team member for an extended period of time.</p>
<p>In a subsequent article, once the delivery is complete, I will return to this topic with the remainder of the burndown data and see how well the confidence intervals modelled the future.</p>
<p>&nbsp;</p>
<p><em>Update: About a week after I wrote this article, a series of events occurred that made it impossible to complete the graph in the way that I&#8217;d hoped.</em></p>
<p><em>First, one of the &#8216;key&#8217; developers left the team. The notion of &#8216;key&#8217; developer (due to functional silos) is something that I&#8217;d been trying to eliminate since starting with the organisation, but the team had not had sufficient time to get to grips with each other&#8217;s work.</em></p>
<p><em>Since the team was not large, the developer leaving implied that timescales would change. This caused the client to panic somewhat, leading to a review of the planning. During this review, it was discovered that the original requirements were hopelessly inadequate in term of both &#8216;completeness&#8217; and &#8216;correctness&#8217;. After more analysis, the total scope required was estimated at six times (!) the original estimate and much of the remaining original work envisaged was de-prioritised or deemed to be no longer required.</em></p>
<p><em>Given the significantly extended timescales, a couple of the developers who had been intending to hang on until the delivery decided to resign, since the delivery was now clearly much more remote.</em></p>
<p><em>Shortly after this, a member of the executive team who I had a good relationship with was replaced by someone else (not because of this project). It very quickly became obvious that this new executive had a strong belief in process over people, big requirements up front and all manner of things I have railed against for years. This clearly spelled the end of my time with this client.</em></p>
<p><em>Although I had warned of much of this risk early in the project, I take no pleasure in its materialisation. </em><em>It could have all been completely different.</em></p>
<p><em>As I said in the first paragraph above: This is not the mode in which we usually like to work.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.casualmiracles.com/2011/05/16/burndown-prediction-confidence-and-risk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Type Names From An Earlier Age</title>
		<link>http://www.casualmiracles.com/2010/09/13/type-names-from-an-earlier-age/</link>
		<comments>http://www.casualmiracles.com/2010/09/13/type-names-from-an-earlier-age/#comments</comments>
		<pubDate>Mon, 13 Sep 2010 20:14:18 +0000</pubDate>
		<dc:creator>lance</dc:creator>
				<category><![CDATA[Quality]]></category>
		<category><![CDATA[Software Process]]></category>

		<guid isPermaLink="false">http://www.casualmiracles.com/blog/?p=349</guid>
		<description><![CDATA[The typical architectures and layering of code in 'the enterprise' frequently exhibit poor systems of naming which causes developers to jump through hoops to try to maintain the layers because of lack of abstraction. This in turn results in an unwieldy codebase that is more concerned with accidental implementation complexity than codifying domain knowledge. <a href="http://www.casualmiracles.com/2010/09/13/type-names-from-an-earlier-age/">[...]</a>]]></description>
			<content:encoded><![CDATA[<p><em>How would this be done in an earlier age?</em></p>
<p>This is the question I frequently ask myself when searching for the names of domain concepts.</p>
<p>The typical architectures and layering of code in &#8216;the enterprise&#8217; frequently exhibit poor systems of naming which causes developers to jump through hoops to try to maintain the layers because of lack of abstraction. This in turn results in an unwieldy codebase that is more concerned with accidental implementation complexity than codifying domain knowledge.</p>
<p><span id="more-349"></span></p>
<p>As an example consider the &#8216;data access object&#8217;, usually referred to as a &#8216;DAO&#8217;. It is common in enterprise Java systems to have a number of classes whose names include a DAO suffix to handle the persistence needs for the system. The problem with this naming scheme, if this is the extent of the abstraction, is that it is an implementation concern; there is nothing in the business domain called a &#8216;DAO&#8217;.</p>
<p>Once the DAO naming scheme has been adopted, it has a profoundly limiting effect on the use of the classes that are so named. Developers treat the DAO classes as something that must only be used at the edges of the system, frequently requiring a &#8216;service layer&#8217; to be bought into existence, partially in order to keep the domain model free of coupling to the persistence layer.</p>
<p>There is nothing wrong with a service layer in principle if its purpose is to orchestrate behaviour at a high level. But far too much behaviour is usually found in these service layers because domain model objects are rightly not allowed to become aware of the DAOs. The effect of this is that the service layer needs to find precisely those things required by the domain logic before finally handing control over to the domain model. The service layer therefore becomes far too familiar with the details of the domain logic which means that at least two places have to change if the domain logic changes.</p>
<p>One lamentably common solution to this problem is to move all domain logic into the  service layer, resulting in an anaemic domain model.</p>
<p>However, there is frequently something in the real world that does represent the place where certain types of objects are stored and this should be represented directly in code with a type named after the domain concept.</p>
<p>If there is no such thing, which often occurs because the modern world has replaced that mechanism with a set of database tables or some equivalent, then I appeal to my opening question: <em>How would this be done in an earlier age?</em></p>
<p>For example, consider the order management system I have used for examples in other articles. Somewhere in such a system, there is the concept of an &#8216;order&#8217;. Then, we must have a mechanism for persisting orders. If we refer to this as an &#8216;OrderDAO&#8217; and take the abstraction no further, we give ourselves all of the problems mentioned earlier. However, in an earlier age, orders would have been written down, modified and deleted in an &#8216;order book&#8217;, and this is how the concept can be named in the order management system.</p>
<p>Now, the OrderBook may be, in Java, an interface whose implementation in the running system uses all of the machinery that enterprise Java developers love: Hibernate, Spring, etc. That implementation might even be called something like HibernateOrderBook or, god help us, OrderDAO. The point is that the rest of the order management system knows only about an OrderBook, which is a strong domain concept and hence may be packaged with the domain model and passed around the domain model without reservation. More of the domain logic can therefore be kept in the domain model rather than in services.</p>
<p>Interestingly, this approach frequently clarifies another hot issue at the moment: when and what to mock when unit testing (in the specific sense of interaction testing).</p>
<p>From the point of view of a class that must make use of the OrderBook, all that matters is that the OrderBook is sent something like write(&#8230;), delete(&#8230;) or find(&#8230;) messages with the appropriate arguments. How it writes, deletes or finds is irrelevant, because that is a delegated concern whose implementation should be completely unknown to the subject under test. Bear in mind it could be Hibernate or JDBC, an in memory collection, an invocation of a Rest service using XML over HTTP or teleportation using tangled quantum effects. Any test that verifies more than the message send is overspecified, and hence suffers from the well documented, numerous effects of over-specified definitions. Therefore mocking is entirely appropriate.</p>
<h1>Conclusion</h1>
<p>Naming is important; it is a fundamental aspect of abstraction. The wrong name can profoundly warp the way people think about a problem which frequently results in a profoundly warped solution. Sometimes, it is hard to find a name because nothing like the thing you are trying to name exists in the appropriate domain.</p>
<p>In such circumstances, I often find the name by thinking about how the problem was solved before the invention of computers: <em>How would this be done in an earlier age?</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.casualmiracles.com/2010/09/13/type-names-from-an-earlier-age/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rank, Authority and Power</title>
		<link>http://www.casualmiracles.com/2010/09/06/rank-authority-and-power/</link>
		<comments>http://www.casualmiracles.com/2010/09/06/rank-authority-and-power/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 21:12:46 +0000</pubDate>
		<dc:creator>lance</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.casualmiracles.com/blog/?p=354</guid>
		<description><![CDATA[One of the characteristics of many teams that Casual Miracles has helped over the past few years is that team members have felt a lack of authority to make changes that are necessary to achieve success. Frequently this occurs despite <a href="http://www.casualmiracles.com/2010/09/06/rank-authority-and-power/">[...]</a>]]></description>
			<content:encoded><![CDATA[<p>One of the characteristics of many teams that Casual Miracles has helped over the past few years is that team members have felt a lack of authority to make changes that are necessary to achieve success. Frequently this occurs despite senior management telling them that they can make the changes they want to.</p>
<p><span id="more-354"></span></p>
<p>Sometimes this is due to organisational constraints. In some very hierarchical, command-and-control organisations, by the time the message reaches those who have the ideas, it has been corrupted beyond recognition, and appears to be an admonishment to maintain the status quo or instruction to move in a different direction completely. Even without these confusions, and within a relatively shallow local hierarchy (one or two layers of management), team members can inhibit themselves because of a sense of impotence, with each team member thinking that others have greater authority because of a whole host of factors.</p>
<p>In most organisations hierarchical rank is a major component of a person&#8217;s authority and power. But the concept of rank is a complex, multi-dimensional notion that frequently speaks to our primate brains. Furthermore, rank is dependent on context and is relational; a person only has rank with respect to others and in a particular  situation &#8211; there is no absolute rank.</p>
<h1>The Problem</h1>
<p>One such organisation we worked with recently suffered from just this issue. Various well-intentioned management attempts to help the team had actually reinforced this sense of lack of authority. </p>
<p>We ran retrospectives to try to get the team to cohere around certain issues and solutions. Almost every retrospectives followed a pattern of identifying issues, coming up with solutions and then declaring the solutions impossible because the team lacked the authority to apply the solutions.</p>
<h1>An Exercise</h1>
<p>Several years ago, I attended a course run by Ben Fuchs and Joseph Pelrine of <a href="http://www.cateams.com">CATeams</a> entitled &#8216;The Deep Dynamics of Agile Teams&#8217;. A large part of this course was about rank, and this is where the ideas and material came from for this exercise.</p>
<p>After a (too) brief introduction to the concept of rank, we gave everybody a questionnaire which asked them to assess their own rank on 25 distinct dimensions (e.g. position in the formal hierarchy, education, gender, intelligence, self-confidence, etc.) placed into 3 categories (Situational, Social and Personal).</p>
<p>Having done that, we then asked everybody to pair up. We gave each team member another copy of the questionnaire, asking them to assess their partner&#8217;s rank in the same way. Having done that, we asked the pair to discuss the differences in the two assessments of each member of the pair.</p>
<p>What is striking about this process is that usually, individuals will assess their own rank lower than their partner&#8217;s assessment on many dimensions. In this instance, almost everybody afforded their partners greater rank than their partner had afforded themselves.</p>
<p>This is quite common and was actually the point of the exercise when Fuchs and Pelrine asked us to do it on the course.</p>
<h1>Taking it a Bit Further</h1>
<p>Somewhat on a whim, we asked each person to provide us with two post-it notes.</p>
<p>On the first, we asked them to write &#8216;&lt;&#8217;, &#8216;=&#8217; or &#8216;&gt;&#8217; if they felt that, in general, their partner had assessed their rank lower, equal to or higher respectively, than their own assessment of their rank.</p>
<p>On the second, we asked them to write &#8216;&lt;&#8217;, &#8216;=&#8217; or &#8216;&gt;&#8217; if they felt that, in general, they assessed their partner&#8217;s rank lower, equal to or higher than their partner had assessed his or her own rank.</p>
<p>Of course, given that software development teams (including testers, BAs and project managers) frequently believe that they have impeccable logic, this request was regarded with incredulity; &#8216;surely the results will be symmetrical?&#8217; they asked.</p>
<p>However, once the team had done as we asked, we put the post it notes on display and found that in response to the first question, just over half of the people present felt that their partners had ranked them higher than they had ranked themselves. However, in response to the second, almost all believed that they had ranked their partners higher than than their partners had ranked themselves.</p>
<p>So to some extent the participants&#8217; assessment of their own rank even coloured their assessment of the questionnaire results.</p>
<h1>Conclusion</h1>
<p>The message is that we frequently assess our own rank lower than other people assess it. The gap between our assessment and the assessment others make of us represents rank, authority and power that we can use if only we are prepared to wield it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.casualmiracles.com/2010/09/06/rank-authority-and-power/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  www.casualmiracles.com/feed/ ) in 0.75706 seconds, on May 19th, 2012 at 8:12 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on May 19th, 2012 at 9:12 am UTC -->
