<?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>Ibis and Baboon</title>
	<atom:link href="http://jgre.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://jgre.org</link>
	<description>Janico Greifenberg&#039;s blog about the exact art and subtle science of computer programming</description>
	<lastBuildDate>Sat, 03 Jul 2010 17:49:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>IDSC V: Accessing and Modifying Vectors</title>
		<link>http://jgre.org/2010/07/03/idsc-v-accessing-and-modifying-vectors/</link>
		<comments>http://jgre.org/2010/07/03/idsc-v-accessing-and-modifying-vectors/#comments</comments>
		<pubDate>Sat, 03 Jul 2010 17:49:47 +0000</pubDate>
		<dc:creator>Janico</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[code reading]]></category>
		<category><![CDATA[data-structures]]></category>
		<category><![CDATA[functional programming]]></category>

		<guid isPermaLink="false">http://jgre.org/?p=690</guid>
		<description><![CDATA[In the previous post of the Immutable Data-Structure Canon we looked at vectors, their internal structure, how they are created, and how more elements are inserted. In this post we continue where we left off and examine the code used &#8230; <a href="http://jgre.org/2010/07/03/idsc-v-accessing-and-modifying-vectors/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In the <a title="IDSC VI: Creating and Growing Vectors" href="http://jgre.org/2010/06/28/idsc-iv-creating-and-growing-vectors/">previous post</a> of the <a href="http://jgre.org/2010/05/20/the-immutable-data-structure-canon/">Immutable Data-Structure Canon</a> we looked at vectors, their internal structure, how they are created, and how more elements are inserted. In this post we continue where we left off and examine the code used to access and remove values from a vector.
<h3>Accessing Elements</h3>
The elements of a vector are accessible by index. The way vectors are usually implemented, this is a constant time operation, as it only takes the calculation of the offset of a memory location. As we&#8217;ve seen, Clojure vectors are implemented as trees to allow for shared structures between persistent &#8220;modified&#8221; versions, so the access methods need to find their way around that structure.</p>

<p>Let&#8217;s say, for example, that we have a vector <code>v</code> with 1500 elements and we want to get the one at index 1101. There are three different ways to find an element in a vector by index: the <code>nth</code> function, the <code>get</code> function, and using the vector as a function. They differ in how they handle the vector being <code>nil</code>, the index being out of range, and whether they support a &#8220;not found&#8221; argument. For this discussion we&#8217;ll use <code>nth</code>; it returns <code>nil</code> if the vector is <code>nil</code>, throws an exception, if the index is out of range, but you can pass an optional argument that is returned, when the index is not found.</p>

<p><pre class="brush: clojure;">
(nth v 1101)
</pre></p>

<p>Internally, this function is mapped to a call to the <code>nth</code> method in PersistentVector:</p>

<p><pre class="brush: java;">
public Object nth(int i){
    Object[] node = arrayFor(i);
    return node[i &amp; 0x01f];
}
</pre></p>

<p>The <code>arrayFor</code> method handles the lookup in the internal structure and returns on of the 32-element arrays where the elements are stored &#8212; either from one of the leaf nodes or from the tail (line 2). The index in that array is calculated by applying a bit-mask to the index passed into the method (line 3). For our example that local index is 13.</p>

<p><pre class="brush: java;">
public Object[] arrayFor(int i){
    if(i &gt;= 0 &amp;&amp; i &lt; cnt)
        {
        if(i &gt;= tailoff())
            return tail;
        Node node = root;
        for(int level = shift; level &gt; 0; level -= 5)
            node = (Node) node.array[(i &gt;&gt;&gt; level) &amp; 0x01f];
        return node.array;
        }
    throw new IndexOutOfBoundsException();
}
</pre></p>

<p>The <code>arrayFor</code> method checks that the index is valid and throws an exception if it is not (lines 2, 11). If the requested index is greater than the number of values in the tree, the tail array is returned (lines 4,5). In our example, this is not the case, we need to look into the tree.</p>

<p>As a tree with two layers (root and leaves) can hold 1024 elements, the tree in for a vector with 1500 elements needs three layers. The index we&#8217;re looking for is in the second subtree, as it&#8217;s greater than 1024. The field <code>shift</code> which holds a multiple of 5 proportional to the height of the tree is 10 in our case, so that we enter the loop in line 7 with 10 as <code>level</code>.</p>

<p>Inside the loop, we find the subtree holding our value by bit-shifting the index by the current level and applying a bit-mask to get it into the 32-element frame. The index for the subtree in our example is 1, as expected. In the second iteration of the loop, we look at the leaves, so that <code>level</code> is decremented to 5. This time, we find the node with index 2. The loop terminates here, as there is not level further down. The method returns the array attached to the node we found.</p>

<p>Summarizing all the index-offsets, we have:
<ul>
    <li>the second subtree of the root (the first subtree contains 1024 elements),</li>
    <li>the third leaf of that subtree (the leaves contain 32 elements each), and</li>
    <li>the fourteenth element of the leaf (calculated in <code>nth</code>).</li>
</ul>
Thus we have the 1102nd element of the vector. Bingo!</p>

<p>Like the <code>cons</code> operation we saw last time, the number of steps necessary to find the nth element depends on the height of the tree, so the complexity here is once again <code>O(log32 N)</code>.
<h3>Deleting Elements</h3>
To finish the discussion of vectors, let&#8217;s look at deleting elements. The only position where efficient deletions are possible is the end. This can be done using the <code>pop</code> function that is conveniently mapped to the <code>pop</code> method of PersistentVector:</p>

<p><pre class="brush: java;">
public PersistentVector pop(){
    if(cnt == 0)
        throw new IllegalStateException(&quot;Can't pop empty vector&quot;);
    if(cnt == 1)
        return EMPTY.withMeta(meta());
    if(cnt-tailoff() &gt; 1)
        {
        Object[] newTail = new Object[tail.length - 1];
        System.arraycopy(tail, 0, newTail, 0, newTail.length);
        return new PersistentVector(meta(), cnt - 1, shift, root, newTail);
        }
    Object[] newtail = arrayFor(cnt - 2);
    Node newroot = popTail(shift, root);
    int newshift = shift;
    if(newroot == null)
        {
        newroot = EMPTY_NODE;
        }
    if(shift &gt; 5 &amp;&amp; newroot.array[1] == null)
        {
        newroot = (Node) newroot.array[0];
        newshift -= 5;
        }
    return new PersistentVector(meta(), cnt - 1, newshift, newroot, newtail);
}
</pre></p>

<p>First, the edge-cases are handled: <code>pop</code> on an empty vector does not work (lines 2,3), and <code>pop</code> on a one-element vector returns an empty vector (lines 4,5). Then we look at the easy case that the tail holds more than one element (line 6); we just copy all tail elements except for the last one into a new array and use it to create a new vector that shares the tree with the original (lines 8-10).</p>

<p><a href="http://jgre.org/files/2010/06/clara-empty-tail.png"><img class="alignnone size-full wp-image-705" src="http://jgre.org/files/2010/06/clara-empty-tail.png" alt="" width="425" height="412" /></a></p>

<p>The condition for the simple case is phrased so that we don&#8217;t use it on a tail with only one element. This is because we need to change the tree when the tail run empty; the last node becomes the new tail in that case.</p>

<p>Shrinking the tree starts with getting the array from the last node by calling the <code>arrayFor</code> method (line 12). Next, we call <code>popTail</code> to get a new root node for a tree without the last node (line 13). This leaves us with three cases:
<ol>
    <li>The node we removed was the only node, so that our return vector gets an empty node as root (lines 15-18).</li>
    <li>The tree has intermediate levels between the root and the leaves and the removed node was the only leaf in the second subtree, so that we can remove one layer (lines 19-23).</li>
    <li>Otherwise the new tree still has the same height as the old tree.</li>
</ol>
Like the other vector operations we looked at, <code>pop</code> also has a complexity of <code>O(log32 N)</code>, because the number of steps necessary is dependent on the height of the tree.
<h3>Summary</h3>
<ul>
    <li>Vectors are implemented based on a tree and a tail array.</li>
    <li>All arrays (both in the nodes of the tree and in the tail) have up to 32 elements.</li>
    <li>The leaves always have exactly 32 elements in them.</li>
    <li>Elements can added and removed efficiently at the end of the vector.</li>
    <li>Addition, removal, and index lookup are <code>O(log32 N)</code>, which is essentially constant time in practice.</li>
</ul></p>
]]></content:encoded>
			<wfw:commentRss>http://jgre.org/2010/07/03/idsc-v-accessing-and-modifying-vectors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IDSC IV: Creating and Growing Vectors</title>
		<link>http://jgre.org/2010/06/28/idsc-iv-creating-and-growing-vectors/</link>
		<comments>http://jgre.org/2010/06/28/idsc-iv-creating-and-growing-vectors/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 17:40:58 +0000</pubDate>
		<dc:creator>Janico</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[code reading]]></category>
		<category><![CDATA[data-structures]]></category>
		<category><![CDATA[functional programming]]></category>

		<guid isPermaLink="false">http://jgre.org/?p=650</guid>
		<description><![CDATA[Vectors provide constant time random access to any element referenced by an index. Like their fixed length cousins, arrays, they are usually implemented by storing their elements in consecutive memory locations. Such a strait forward implementation, however, doesn&#8217;t allow for &#8230; <a href="http://jgre.org/2010/06/28/idsc-iv-creating-and-growing-vectors/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Vectors provide constant time random access to any element referenced by an index. Like their fixed length cousins, arrays, they are usually implemented by storing their elements in consecutive memory locations. Such a strait forward implementation, however, doesn&#8217;t allow for immutability &#8212; at least not when we are interested in the performance characteristics, but in that case we could go with lists anyway.</p>

<p>This post is part of the <a href="http://jgre.org/2010/05/20/the-immutable-data-structure-canon/">immutable data-structure canon</a>. While the implementation of <a href="http://jgre.org/2010/05/27/idsc-ii-lists/">immutable lists</a> is relatively simple, immutable persistent vectors require quite a bit of work. I decided to split this topic into two parts: this post covers how vectors are created and how elements; we&#8217;ll also learn about the structure used to store the values. The second part to be posted next week, covers how vectors are accessed and how elements are modified and deleted.</p>

<p>Before we dive in, I need to correct an error in the previous parts of the series. Until now, I used &#8220;sequence&#8221; and &#8220;seq&#8221; synonymously as a generic term for data-structures that can hold multiple values. I realized now that the correct generic term is <em>collection</em>. <em>Sequence</em> in the Clojure context is a collection that a series of values without reordering them where values may or may not exist yet. <em>seq</em> specifically refers to an API for using collections with the funtions <code>first</code> and <code>rest</code>. Henceforth I shall use the right terminology. And now: vectors.</p>

<p>To achieve both immutability and performance, Clojure only stores limited segments of a vector in a row in memory. The overall structure used to represent the vector behind the scenes is a tree. Each of the tree&#8217;s nodes holds an array that contains a segment of the vector. Functions that &#8220;modify&#8221; a vector return a new object that shares all the nodes not affected by the operation with the original. Only the segments that are different get stored in separate node objects.</p>

<p>The implementation of vectors is in the Java class <code>clojure.lang.PersistentVector</code>. Instances of that class provide the API through which vectors are used and they hold a reference to the root of the tree. The nodes are implemented in the internal class <code>Node</code>.
<h3>Creating Vectors</h3>
To create a vector in the Java part of the language, you call the static method <code>create</code> from <a href="http://github.com/richhickey/clojure/blob/master/src/jvm/clojure/lang/PersistentVector.java">PersistentVector.java</a>:</p>

<p><pre class="brush: java;">
static public PersistentVector create(List items){
    TransientVector ret = EMPTY.asTransient();
    for(Object item : items)
        ret = ret.conj(item);
    return ret.persistent();
}
</pre></p>

<p>The actual work here is done by <code>TransientVector</code>, an internal class used for efficiently modifying vectors. The method starts by creating an empty <code>TransientVector</code> (line 2). The elements are inserted into it by one by calling <code>conj</code> (lines 3, 4). Finally, the resulting transient is converted to a persistent representation (line 5).</p>

<p><a href="http://clojure.org/transients">Transients</a> are a feature of Clojure that allows you (and the language&#8217;s internals) to use mutable data-structures in performance critical parts. Transients are thread-save and they cannot be shared with other code. As we&#8217;re looking at immutable data-structures here, we won&#8217;t go into the details of the implementation here. The vector that we get from the call to <code>persistent</code> has the same structure we would get by starting with an empty persistent vector and adding the values with immutable operations. So let&#8217;s look at that.
<h3>Adding Elements</h3>
When elements are added to a <code>PersistentVector</code>, the <code>cons</code> method is called.</p>

<p><pre class="brush: java;">
public PersistentVector cons(Object val){
    int i = cnt;
    //room in tail?
    if(cnt - tailoff() &lt; 32)
        {
        Object[] newTail = new Object[tail.length + 1];
        System.arraycopy(tail, 0, newTail, 0, tail.length);
        newTail[tail.length] = val;
        return new PersistentVector(meta(), cnt + 1, shift, root, newTail);
        }
    //full tail, push into tree
    Node newroot;
    Node tailnode = new Node(root.edit,tail);
    int newshift = shift;
    //overflow root?
    if((cnt &gt;&gt;&gt; 5) &gt; (1 &lt;&lt; shift))
        {
        newroot = new Node(root.edit);
        newroot.array[0] = root;
        newroot.array[1] = newPath(root.edit,shift, tailnode);
        newshift += 5;
        }
    else
        newroot = pushTail(shift, root, tailnode);
    return new PersistentVector(meta(), cnt + 1, newshift, newroot, new Object[]{val});
}
</pre></p>

<p>For some reason, the method starts with initializing a variable <code>i</code> that is never used (line 2). After that, we are confronted with the details of structure used to store the values.</p>

<p><a href="http://jgre.org/files/2010/06/clara-tree-tail1.png"><img class="alignnone size-full wp-image-681" src="http://jgre.org/files/2010/06/clara-tree-tail1.png" alt="" width="420" height="412" /></a></p>

<p>Trees don&#8217;t have tails &#8212; not even the strange computer science kind that grows downward. The tail here is not part of the tree, it&#8217;s an array holding values that are not part of the tree. Up to 32 values end up in the tail, the rest goes into the tree. As the name suggests, the last elements of the vector get stored in the tail.</p>

<p>32 is a magic number for vectors: the leaves of the tree have exactly 32 values, the other nodes have up to 32 children, and the aforementioned 32-element tail. The <code>tailoff</code> method called in line 4 returns the greatest multiple of 32 that&#8217;s less than the current length, i.e. the number of elements in stored in the tree. The field <code>cnt</code> is the length of the vector.</p>

<p>When there is still room in the tail, the tail array is copied into a new array (lines 6, 7) and the new value is appended (line 8). The return value is a new <code>PersistentVector</code> instance with the new tail array, an incremented count, and otherwise the same properties as the original vector (line 9). In this case adding an element is a constant time operation.</p>

<p>Only when the vector to which we&#8217;re adding has exactly <code>32*n</code> elements (with <code>n</code> being a positive integer), we need to deal with the tree. In that case, a new tree node to hold the original vector&#8217;s tail is created (line 13). Then we create a tree with the new node in it (lines 14-24), and we return a new instance with the updated tree, the incremented count, and a new tail array that only contains the added element (line 25).</p>

<p><a href="http://jgre.org/files/2010/06/clara-hand-wave1.png"><img class="alignnone size-full wp-image-683" src="http://jgre.org/files/2010/06/clara-hand-wave1.png" alt="" width="425" height="419" /></a></p>

<p>When constructing the tree, we have two cases: if the tree is completely full at the current height, the resulting tree needs a new level (lines 18-21). Otherwise, we only need to find the right place for the new node (line 24).</p>

<p>The field <code>shift</code> is the height of the tree multiplied by 5. The height is represented in this form so that it can be used with the count and the capacity in bit-shift operations for additional efficiency. When we add the 33rd element, <code>cnt</code> is 32, as that is the value of the original vector. <code>shift</code> is 5, the initial value when there is only an empty root node. As a consequence, the root of the tree for the returned vector is constructed by calling <code>pushTail</code>.</p>

<p><pre class="brush: java;">
private Node pushTail(int level, Node parent, Node tailnode){
    int subidx = ((cnt - 1) &gt;&gt;&gt; level) &amp; 0x01f;
    Node ret = new Node(parent.edit, parent.array.clone());
    Node nodeToInsert;
    if(level == 5)
        {
        nodeToInsert = tailnode;
        }
    else
        {
        Node child = (Node) parent.array[subidx];
        nodeToInsert = (child != null)?
                        pushTail(level-5,child, tailnode)
                        :newPath(root.edit,level-5, tailnode);
        }
    ret.array[subidx] = nodeToInsert;
    return ret;
}
</pre></p>

<p>The method starts by calculating the index where to add the new node depending on the length of the original vector and the level of the tree where we are looking to insert the node (line 2). In the example of adding the 33rd value, the index of the new node is 0, as this is the first node to be pushed into the root. When we add the 65th element &#8212; thus having filled up the tail for the second time &#8212; we get the index 1.</p>

<p>The return value of <code>pushTail</code> is a new node that clones the references to all the children of the original parent node, so that the child nodes are shared between the old and the new vector (line 3). If we are at the level where the child nodes are leaves, we directly insert the node we&#8217;re supposed to add at the calculated index of the returned node (lines 7, 16, 17).</p>

<p>When the tree is already higher, we cannot add the leaf node directly to the parent node. Instead we find the child node at the calculated index (line 11) and use the recursive nature of trees to call <code>pushTree</code> again for that subtree with a lower level (line 13). If no child node in the calculated place exists, we call <code>newPath</code> to create a new subtree of the appropriate hight that contains nodes with exactly one child down to our newly added leaf node (line 14). These operations only copy the subtree where the new value is added. Because of the 32-children condition these are at most <code>O(log32 N)</code> steps.</p>

<p>This was the part of the insertion process, when we can insert into the tree without adding another level. Back in the <code>cons</code> method, we still need to look at the case where we grow the tree in height (lines 18-21). Here we create an all new root for the returned vector (line 18) that gets the original vector&#8217;s root as the first child (line 19) and a new subtree of the right height with the new leaf node created by <code>newPath</code> as the second child (line 20). Finally, the field <code>shift</code> for the new vector is incremented by 5 to reflect the new height (line 21). As the number of steps for this operation is bounded by the height of the tree, the complexity here is also <code>O(log32 N)</code>, which is consequently the complexity of the whole algorithm.
<h3>Summary</h3>
In this post we have mainly looked at adding elements to immutable vectors. The algorithm and data-structure described here is used for both creating and growing vectors, although the creation process uses optimizations based on mutable state which we did not discuss.</p>

<p>I want to close this post with an illustration of the structure using a before/after example. Let us assume we have a vector with 1056 elements and want to append another value calling <code>cons</code>.</p>

<p><a href="http://jgre.org/files/2010/06/growing-vector1.png"><img class="alignnone size-full wp-image-685" src="http://jgre.org/files/2010/06/growing-vector1.png" alt="" width="484" height="352" /></a></p>

<p>The old vector (shown in blue) has a root node with 32 children each holding 32 values, and a tail with 32 more values. This means the tree is full at is current level. The new vector (shown in red) has another layer in its tree. The first child of the new root is the old root, thus sharing the structure of the old vector without copying. The second child of the new root is a node that has one leaf as child which contains a reference to the old tail &#8212; again, sharing not copying. The tail of the new vector contains a single value, the one we added.</p>
]]></content:encoded>
			<wfw:commentRss>http://jgre.org/2010/06/28/idsc-iv-creating-and-growing-vectors/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>IDSC III: Lazy Seqs</title>
		<link>http://jgre.org/2010/06/10/idsc-iii-lazy-seqs/</link>
		<comments>http://jgre.org/2010/06/10/idsc-iii-lazy-seqs/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 18:08:22 +0000</pubDate>
		<dc:creator>Janico</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[code reading]]></category>
		<category><![CDATA[data-structures]]></category>
		<category><![CDATA[functional programming]]></category>

		<guid isPermaLink="false">http://jgre.org/?p=568</guid>
		<description><![CDATA[Lazy evaluation is an important concept in functional programming. Running on the JVM, Clojure does not support general laziness, but it has a data-structure abstraction called lazy sequence that provides for many of the benefits of the more general strategy. &#8230; <a href="http://jgre.org/2010/06/10/idsc-iii-lazy-seqs/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Lazy evaluation is an important concept in functional programming. Running on the JVM, Clojure does not support general laziness, but it has a data-structure abstraction called <em>lazy sequence</em> that provides for many of the benefits of the more general strategy.</p>

<p>This post is a slight detour in the course of the <a href="http://jgre.org/2010/05/20/the-immutable-data-structure-canon/">immutable data-structure canon</a>. In the <a title="IDSC II: Lists" href="http://jgre.org/2010/05/27/idsc-ii-lists/">previous installation</a>, I described how some operations on lists return lazy sequences to retain immutability and performance characteristics. As this behavior is not specific to lists, and indeed fundamental to all Clojure data-structures, I decided that lazy seqs deserve a closer look.
<h3>The Rationale of Lazy Evaluation</h3>
In many cases it might not be necessary to use all the values in a data-structure. But it might be hard to determine what is required when the structure is defined, as the creation and the consumption could be in different parts of the program. Laziness allows you to write a general definition and later use only what you need without incurring the cost for needless computations.</p>

<p>Laziness also allows for an interesting special case: <em>infinite sequences</em>. You can specify the computation of elements for a series (e.g. Fibonacci numbers) and use parts of that series without problems, as long as you do not try to traverse the entire sequence.
<h3>Postponing Calculations</h3>
<div id="attachment_626" class="wp-caption alignnone" style="width: 332px"><img class="size-full wp-image-626" src="http://jgre.org/files/2010/06/Lazy-Guy.png" alt="Lazy Guy" width="322" height="242" /><p class="wp-caption-text">(c) iStockphoto.com</p></div></p>

<p>In a language with general support for lazy evaluation like Haskell, the compiler takes care that nothing gets evaluated before it is needed. In Clojure, however, eager evaluation is the norm. That means that when you call a function with the result of another function-call as a parameter, that second function gets evaluated immediately. In a lazy language, the second function would only be evaluated when the first function needs the respective parameter.</p>

<p>For this reason, <code>lazy-seq</code> (which as we saw last time is used to define lazy sequences), cannot be implemented as a function &#8212; the body of what we pass to it must not be evaluated at the time of definition. The solution is a construct often cited as the most powerful feature in Lisp: <em>macros</em>.</p>

<p>Macros are the extension mechanism of Clojure (and Lisp in general); they allow you as a programmer to add features to the language. This makes it possible to have a very small core language, but still provide for pleasant programming. Unlike functions, macros do not evaluate their arguments immediately. When Clojure comes across code that uses a macro it first <em>expands</em> the macro and replaces the code with the result before the compilation proceeds normally.</p>

<p>The definition of the <code>lazy-seq</code> macro in <a href="http://github.com/richhickey/clojure/blob/master/src/clj/clojure/core.clj">core.clj</a> is surprisingly short:</p>

<p><pre class="brush: clojure;">
(defmacro lazy-seq
  [&amp; body]
  (list 'new 'clojure.lang.LazySeq
    (list* '^{:once true} fn* [] body)))
</pre></p>

<p>As macro expansion and the corresponding escaping rules are beyond the scope here, let it suffice to say that <code>(lazy-seq BODY)</code> would expand to</p>

<p><pre class="brush: clojure;">
(new clojure.lang.LazySeq (fn* [] BODY))
</pre></p>

<p>The body that defines the seq is wrapped into an anonymous function and passed to the constructor of the <code>LazySeq</code> class. (<code>new</code> is a special form for Java interoperability that allows us to construct a Java object.) The main part of the implementation of lazy seqs is in Java, more specifically in the aforementioned class <code>clojure.lang.LazySeq</code>.</p>

<p><code>LazySeq</code> has three fields:
<ul>
    <li><code>fn</code>: a function object to store the definition body,</li>
    <li><code>sv</code>: a generic object to store the computed value, and</li>
    <li><code>s</code>: a representation of the current view of the sequence.</li>
</ul>
When we construct a <code>LazySeq</code> instance, only the function object is assigned a value.</p>

<p><img class="alignnone size-full wp-image-636" src="http://jgre.org/files/2010/06/Clara-Caching2.png" alt="Clara Caching" width="425" height="414" /></p>

<p>For performance reasons, <code>LazySeq</code> does not simply call the function object and return its result, when the contents are accessed. Instead the other two fields are used to do some caching.
<h3>Caching</h3>
To understand how the access works, let us look at the <code>first</code> method in <a href="http://github.com/richhickey/clojure/blob/master/src/jvm/clojure/lang/LazySeq.java">LazySeq.java</a>:</p>

<p><pre class="brush: java;">
public Object first(){
    seq();
    if(s == null)
        return null;
    return s.first();
}
</pre></p>

<p>The <code>seq</code> method that is called in line 2, is the key to how access to lazy seqs works. It is also called from all the other access methods (<code>count</code>, <code>more</code>, <code>cons</code>, etc.).</p>

<p><pre class="brush: java;">
final synchronized public ISeq seq(){
    sval();
    if(sv != null)
        {
        Object ls = sv;
        sv = null;
        while(ls instanceof LazySeq)
            {
            ls = ((LazySeq)ls).sval();
            }
        s = RT.seq(ls);
        }
    return s;
}
</pre></p>

<p>The <code>sval</code> method called from line 2 executes the function object and stores the result in the <code>sv</code> field. <code>sval</code> also takes care that the function is not executed more than once. In line 5 the calculated value is assigned to the temporary variable <code>ls</code>, and the field for the calculated value is set to <code>null</code> in line 6. Lines 7-10 resolve nested lazy seqs until we get a value of a different type assigned to the temporary variable <code>ls</code>.</p>

<p>The return value is computed and assigned to the field <code>s</code> in line 11. <code>RT</code> is the runtime class that provides the fundamental Clojure functions for the Java code implemented as static methods. The <code>seq</code> method in <code>RT</code> turns its parameter into a sequence if it implements  the <code>Iterable</code> Java interface.</p>

<p>This computation happens only once. In subsequent calls to <code>seq</code> the cached value in field <code>s</code> is returned, as <code>sv</code> is set to <code>null</code> (line 6)  so that the condition in line 3 is false and the block in lines 4-12 is not executed. The <code>sval</code> method does not set <code>sv</code> again in later invokations either.
<h3>The <code>concat</code> Example</h3>
To clarify what&#8217;s happening in <code>seq</code>, let us go through an example. We&#8217;ll use an abbreviated version of <code>concat</code>.</p>

<p><pre class="brush: clojure;">
(defn concat
  [x y]
    (lazy-seq
      (let [s (seq x)]
        (if s
          (cons (first s) (concat (rest s) y)))
          y))))
</pre></p>

<p>What happens when we take the seq resulting from a concatenation and call <code>first</code> on it? Via the <code>first</code> method we get to <code>seq</code> which in turn calls <code>sval</code>, so that the body gets executed.  The body of the <code>lazy-seq</code> use in <code>concat</code> is the call to <code>cons</code> (line 6). The result of <code>cons</code> is an instance of <code>clojure.lang.Cons</code> &#8212; an abstraction with list semantics, i.e. it has a head and a tail.  The <code>Cons</code> is assigned to the <code>sv</code> field and &#8212; in the <code>seq</code> method &#8211; to the temporary variable <code>ls</code> (line 5).  The loop for nested lazy seqs (lines 7-10) is skipped, because a <code>Cons</code> is not an instance of <code>LazySeq</code>.</p>

<p>In line 11, the <code>Cons</code> gets assigned to the field <code>s</code> (<code>RT.seq</code> does not do anything to it).  Back to <code>first</code>. <code>s</code> is not <code>null</code>, so we call <code>first</code> on the <code>Cons</code> and return the result. The first element of the <code>Cons</code> is the first element of the concatenated seq, just as expected.</p>

<p>Accessing the tail of the seq, happens analogously; the tail of the <code>Cons</code> is another lazy seq due to the recursive definition of <code>concat</code> which we discussed last time.
<h3>Summary</h3>
<ul>
    <li>Lazy sequences allow computations to be postponed. Even infinite sequences are possible</li>
    <li>Lazy seqs are created using the <code>lazy-seq</code> macro</li>
    <li>Using a macro avoids immediately evaluating the definition of the seq</li>
    <li>The implementation of lazy seqs is in the Java class <code>clojure.lang.LazySeq</code></li>
    <li>Lazy seqs cache the results of the computation</li>
</ul></p>
]]></content:encoded>
			<wfw:commentRss>http://jgre.org/2010/06/10/idsc-iii-lazy-seqs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Design of Design</title>
		<link>http://jgre.org/2010/06/04/the-design-of-design/</link>
		<comments>http://jgre.org/2010/06/04/the-design-of-design/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 18:29:27 +0000</pubDate>
		<dc:creator>Janico</dc:creator>
				<category><![CDATA[management]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[design]]></category>

		<guid isPermaLink="false">http://jgre.org/?p=515</guid>
		<description><![CDATA[Mediocre design provably wastes the world&#8217;s resources, corrupts the environment, affects internatonal competitiveness. Design is important; teaching design is important. Fred Brooks, &#8220;The Design of Design&#8221;. Page x Fredrick Brooks, the author of the &#8220;Mythical Man Month &#8221; and the &#8230; <a href="http://jgre.org/2010/06/04/the-design-of-design/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote>Mediocre design provably wastes the world&#8217;s resources, corrupts the environment, affects internatonal competitiveness. Design is important; teaching design is important.
<p style="text-align: right">Fred Brooks, &#8220;The Design of Design&#8221;. Page x</p>
</blockquote>

<p>Fredrick Brooks, the author of the &#8220;<a href="http://en.wikipedia.org/wiki/The_Mythical_Man-Month">Mythical Man Month</a> &#8221; and the project manager of the<a href="http://en.wikipedia.org/wiki/IBM_System/360">IBM System/360</a> (the most successful mainframe computer series back in the day), has written a new book called &#8220;The Design of Design&#8221;. Like &#8220;The Mythical Man Month&#8221;, it is a collection of essays, but in this one focus is on how good design can be achieved.</p>

<p>While computer-related projects are a main source of examples in the book, Brooks also draws from experience in designing buildings and other projects. The book is aimed at designers and project managers of many kinds, but computer science is clearly Brooks&#8217;s home discipline.</p>

<p>The essays are by no means how-tos trying to teach you a particular design process. Nor are there any singularly novel ideas you have never heard before, if you have some background in designing software. The value of the book is that it makes you think about the different aspects of design. The texts are well written and opinionated, inviting you to look at the issues presented in them from angles that are different from what you might be used to. In the rest of this post, I summarize some ideas that resonated with me, but I really recommend reading the book.</p>

<div id="attachment_576" class="wp-caption aligncenter" style="width: 310px">
<img class="size-full wp-image-576 " src="http://jgre.org/files/2010/06/Fred_Brooks1.jpg" alt="(c) sd&amp;m. Licensed und CC-BY-SA-3.0" width="300" height="451" /><p class="wp-caption-text">Fred Brooks (c) sd&amp;m. Licensed under CC-BY-SA-3.0 </p></div>

<p>The book is organized in six parts, I follow that structure for my notes here (not all parts are mentioned, however).
<h3>Models of Designing</h3>
<blockquote>The hardest part of design is deciding what to design
<p style="text-align: right">Page 22</p>
</blockquote>
<p style="text-align: left">In the first part of the book, Brooks explains that he rejects the waterfall model, because it is impossible to know enough about requirements and other factors influencing the design up front.</p></p>

<blockquote>
<p style="text-align: left">The Rational Model, in any of its forms [the waterfall model being one of them], leads us to demand up-front statements of design requirements. It leads us to believe that such can be formulated. [...]</p>
<p style="text-align: left">The Waterfall Model is wrong and harmful; we must outgrow it.</p>
<p style="text-align: right">Pages 33,34</p>
</blockquote>

<p><p style="text-align: left">This is old news to anyone sympathizing with agile styles of design, but it is interesting to hear it from someone who worked on mainframes for IBM in the 50ies and 60ies.</p>
<p style="text-align: left">The rejection of the waterfall model raises two questions: (1) why are project plans still often based on it and (2) what is a better model?</p>
<p style="text-align: left">There are two plausible answers to the first question. It seems obvious, clean and strait-forward. But more importantly, most projects involve some sort of contract between the designer and the customer, where the customer would not be to happy about an open-ended, pay by the hour, it&#8217;s-done-when-it&#8217;s-done deal.</p>
<p style="text-align: left">Brooks proposes decoupling the contracts for design from the contracts for implementation, but concedes that this is not a complete solution especially in software, where the boundaries between design and construction are blurry.</p>
<p style="text-align: left">So what about a better model? The purpose of a model for design is to serve as a means of teaching and as map to answer the question &#8220;where are we?&#8221; in the course of a project.</p>
<p style="text-align: left">Brooks has no definitive and final answer to this question, but he is in favor of something based on the <a href="http://portal.acm.org/citation.cfm?doid=12944.12948">spiral</a> <a href="http://en.wikipedia.org/wiki/Spiral_model">model</a> proposed by Barry Boehm. In this model, development repeatedly go through stages of planning, determining requirements and contraints, prototyping, risk analysis, and verification.</p></p>

<h3>Collaboration and Telecollaboration</h3>

<p>In the second part, Brooks questions the notion that collaboration and design in teams are good per se.
<blockquote>It is generally assumed that collaboration is, in and of itself, a &#8220;good thing.&#8221; &#8220;Plays well with others&#8221; is high praise from kindergarten onward. &#8220;All of us are smarter than any of us.&#8221; &#8220;The more participation in design, the better.&#8221; Now, these attractive propositions are far from self-evident. I will argue that they are not <em>universally</em> true.
<p style="text-align: right">Page 64</p>
</blockquote>
<p style="text-align: left">Although there are good reasons from collaborative design such as technological complexity and time contraints, the challenge is conceptual integrity. As a consequence, Brooks argues that a design team should always have one system architect who calls shots in favor of a consistent concept.</p></p>

<h3>Design Perspectives</h3>

<p>The third part of the book contains essays with ideas that are similar to those of agile development.</p>

<p>Brooks starts with an interesting perspective on design where he compares different approaches to the philosophical schools of <em>empiricism</em> and <em>rationalism</em>. Rationalists believe that given sufficient experience and careful consideration, you can come up with a perfect design. The empiricist view on the other hand is that anything you design, no matter how well considered, will be flawed. The flaws need to be fixed by trial and error.</p>

<p>Brooks is clearly in the empiricist camp:
<blockquote>Can I, by sufficient thought alone, design a complex object correctly? No; testing and iteration are in practice necessary. But careful thought helps.
<p style="text-align: right">Page 109</p>
</blockquote>
<p style="text-align: left">An important factor for the design is the model you have of your product&#8217;s user. While it is impractical to know everything about the users, Brooks argues that you should explicitly state the assumptions you make in great detail, even if some of them are wrong.</p></p>

<blockquote>
<p style="text-align: left">Wrong explicit assumptions are much better than vague ones. Wrong ones will perhaps be questioned; vague ones won&#8217;t.</p>
<p style="text-align: right">Page 117</p>
</blockquote>

<p><p style="text-align: left">Reminding me of <a title="Embrace Constraints" href="http://gettingreal.37signals.com/ch03_Embrace_Constraints.php">37signals</a> he supports the notion that constraints are your friends.</p></p>

<blockquote>
<p style="text-align: left">Since constraints are the designer&#8217;s friend, if the task originally seems unconstrained, first think harder about what is really desired, about the user and use models, and you will probably find some narrowing constraints, to the benefit of both designer and user.</p>
<p style="text-align: right">Page 135</p>
</blockquote>

<p><p style="text-align: left">Brooks than goes on to examine esthetics and style in technical design. In his analysis &#8220;logical beauty&#8221; comes from <em>parsimony</em>, <em>structural clarity</em>, and <em>consistency</em>. Parsimony roughly means &#8220;accomplishing a great deal with few elements&#8221;. Parsimony alone, however, is not enough. Without any redundancy, as design can be cryptic and infective.</p>
<p style="text-align: left">According to Brooks, &#8220;consistency underlies all principles of quality.&#8221;</p></p>

<blockquote>
<p style="text-align: left">A good architecture is consistent in the sense that, given partial knowledge of the system, one can predict the remainder.</p>
<p style="text-align: right">Page 143</p>
</blockquote>

<p><p style="text-align: left">Three design principles are important for consistency: <strong>orthogonality (do not link what is independent), propriety (do not introduce what is immaterial), and generality (do not restrict what is inherent)</strong>.</p></p>

<h3>Great Designers</h3>

<p>In the fifth part, Brooks examines the value of design processes, individual designers, and education in design. He maintains that &#8220;great designs come from great designers, not from great design processes.&#8221;
<blockquote>I believe that standard corporate product design processes do indeed work against truly great and innovative design.
<p style="text-align: right">Page 233</p>
</blockquote>
<p style="text-align: left">This does not mean, however, that he is rejects processes entirely. The key is some middle ground where processes prevent grave errors, but are not followed to the letter stifling creativity.</p></p>

<blockquote>
<p style="text-align: left">First and foremost, the top leader of the organization must passionately want innovative products with great designs</p>
<p style="text-align: right">Page 238</p>
</blockquote>

<p><p style="text-align: left">Concerning education, the most important idea in the book is that you should learn by <em>critiqued practice</em>, instead of getting taught the basics before starting to make stuff. Another component in learning is teaching to others. A third part in the education of a designer should be studying exemplars &#8212; designs by other designers.</p></p>
]]></content:encoded>
			<wfw:commentRss>http://jgre.org/2010/06/04/the-design-of-design/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IDSC II: Lists</title>
		<link>http://jgre.org/2010/05/27/idsc-ii-lists/</link>
		<comments>http://jgre.org/2010/05/27/idsc-ii-lists/#comments</comments>
		<pubDate>Thu, 27 May 2010 19:39:36 +0000</pubDate>
		<dc:creator>Janico</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[code reading]]></category>
		<category><![CDATA[data-structures]]></category>
		<category><![CDATA[functional programming]]></category>

		<guid isPermaLink="false">http://jgre.org/?p=532</guid>
		<description><![CDATA[Lists are the simplest data-structures that can easily grow. In this post, as part of the immutable data-structure canon, we&#8217;re going to look at how immutable lists are implemented in Clojure. Let us review the definition of list in general. &#8230; <a href="http://jgre.org/2010/05/27/idsc-ii-lists/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Lists are the simplest data-structures that can easily grow. In this post, as part of the <a href="http://jgre.org/2010/05/20/the-immutable-data-structure-canon/">immutable data-structure canon</a>, we&#8217;re going to look at how immutable lists are implemented in Clojure.</p>

<p>Let us review the definition of list in general. A list is either empty or has a <em>head</em> and a <em>tail</em>. The head contains a data element and the tail is another list. A simple recursive definition. The list <code>(1 2 3)</code> for example has <code>1</code> as its head and the list <code>(2 3)</code> as tail. The head of that list is <code>2</code> and the tail is the list <code>(3)</code>. Finally, that list has <code>3</code> as head and the empty list as tail.</p>

<p><img class="alignnone size-full wp-image-547" src="http://jgre.org/files/2010/05/List-1.png" alt="List 1" width="453" height="219" /></p>

<p>This structure is implemented as <a href="http://github.com/richhickey/clojure/blob/3da8a12112332d15a91b140fab5e535f0d2528e8/src/jvm/clojure/lang/PersistentList.java">clojure.lang.PersistentList</a> in the Java part of Clojure.</p>

<p>We can create a list using the <code>list</code> function</p>

<p><pre class="brush: clojure;">
(def l1 (list 1 2 3))
</pre></p>

<p>We also saved the list in a binding named <code>l1</code>. The general convention (at least in Lisps) is that adding elements happens at the front of the list. To add an element, we use <code>cons</code>.</p>

<p><pre class="brush: clojure;">
(def l2 (cons 42 l1))
l2
=&gt; (42 1 2 3)
l1
=&gt; (1 2 3)
</pre></p>

<p><img class="alignnone size-full wp-image-552" src="http://jgre.org/files/2010/05/List-21.png" alt="List 2" width="576" height="273" /></p>

<p><code>cons</code> returns a list with the added element, but <code>l</code> still contains only <code>1 2 3</code>. This property is called <em>persistence</em>. Conceptually this is easy. We create a new list with <code>42</code> as the head and what we called <code>l</code> as the tail. <code>l</code> still refers to the same list as before.</p>

<p>Because the addition does not have to go through the existing list, this operation takes constant time.</p>

<p>Given the recursive definition of list, immutably and persistently removing elements from the front of the list is simple as well. Things get trickier when we concatenate two lists.</p>

<p><pre class="brush: clojure;">
(def ls (concat l1 l2))
l1
=&gt; (1 2 3)
l2
=&gt; (42 1 2 3)
ls
=&gt; (1 2 3 42 1 2 3)
</pre></p>

<p>In a mutable implementation, we could simply change the last element of the first list to refer to the second list as tail, but that would destroy the persistence of <code>l1</code>.</p>

<p><img class="alignnone size-full wp-image-570" src="http://jgre.org/files/2010/05/List-31.png" alt="List 3" width="574" height="171" /></p>

<p>What is the performance expectation here? You might be tempted to say, that it is constant, because we only need to change one pointer. But remember, that we are talking about a singly-linked list. That means, we can only find the last element of <code>l1</code> by traversing the whole list. Thus the operation takes linear time.</p>

<p>Back to our original question, how do we concatenate two lists without modifying them?</p>

<p><img class="alignnone size-full wp-image-558" src="http://jgre.org/files/2010/05/Clara-Copy1.png" alt="Clara Copy" width="425" height="413" /></p>

<p>For Clojure the answer is both yes and no.</p>

<p>Clojure&#8217;s solution here could be described as &#8220;copying light&#8221;. We create a new data-structure that has new entries for all the elements, but each entry is only really created when it is used. This is called a <em>lazy sequence</em>.</p>

<p>A lazy sequence behaves like a normal collection. The special power of lazy seqs is that they don&#8217;t contain all the data elements, they only know how they are generated. This means that the values in the seq are only created when they are accessed, allowing expensive calculations to be postponed. Lazy seqs can even represent infinite data sets &#8212; as long as the data-structure is not traversed completely. We will look into this construct in greater depth in the next part of this series.</p>

<p>Back to our immutable lists. How do lazy seqs help with concatenating two lists? The <code>concat</code> function does not actually return a lists, it returns a lazy seq. When we go through the list, a new list entry is created. Thus for the whole list, we get a newly created entry of each element of both lists, resulting in linear complexity. The data elements, however, are not copied as we can be sure that they are also values and thus won&#8217;t change.</p>

<p>Here is the implementation of <code>concat</code> in <a href="http://github.com/richhickey/clojure/blob/master/src/clj/clojure/core.clj">core.clj</a>:</p>

<p><pre class="brush: clojure;">
(defn concat
  &quot;Returns a lazy seq representing the concatenation of the elements in the supplied colls.&quot;
  ([] (lazy-seq nil))
  ([x] (lazy-seq x))
  ([x y]
    (lazy-seq
      (let [s (seq x)]
        (if s
          (if (chunked-seq? s)
            (chunk-cons (chunk-first s) (concat (chunk-rest s) y))
            (cons (first s) (concat (rest s) y)))
          y))))
</pre></p>

<p>(I&#8217;ve omitted the implementation of concatenating more than two lists, as that is not essential to this discussion.)</p>

<p>Let us walk through this code. There are three definitions of of the function here, the dispatch depends on arity (i.e. with how many parameters this function is called). Lines 3 and 4 handle the cases that <code>concat</code> is called with zero or one parameters the obvious way, by just returning a lazy seq with the same contents that were passed as parameters.</p>

<p>Starting in line 5, we have the &#8220;real&#8221; case for <code>concat</code>: two parameters. Line 6 tells us that the result is a lazy seq. Lines 7-12 are only executed when an element of the list is read &#8212; that&#8217;s the laziness.</p>

<p>Line 7 makes sure the rest of the function is working with a sequence type, and line 8 makes sure that the first list to concatenate is not nil, otherwise the second list is returned in line 12.</p>

<p><a title="Presentation slides by Rich Hickey about chunked seqs" href="http://clojure.googlegroups.com/web/chunks.pdf?gda=WIF8ADwAAAC-wnUK1KQ919yJcmM1ACuZUsYXlXWR5Y8qvjzEXQCX1uwyCdwt79_BXi8-B36MGsn9Wm-ajmzVoAFUlE7c_fAt">Chunked seqs</a> are an optimization that we&#8217;ll ignore here, so we can ignore lines 9 and 10. That leaves us with line 11. Here, we take the head of the first list, and add it (using <code>cons</code>) to the result of a recursive call to <code>concat</code> passing the tail of the first list and the second list.</p>

<p>When we go through the above example of concatenating <code>(1 2 3)</code> and <code>(42 1 2 3)</code>, we get a lazy seq back immediately, without the function looking at any of the elements. Only when we go through that result seq, lines 7-12 get invoked. For the first element, we get an entry that contains <code>1</code> as head and another lazy seq &#8212; the result of <code>(concat (list 2 3) (list 42 1 2 3))</code> &#8212; as the tail. Getting the second happens the same way,  as we resolve the second lazy seq. The third, ditto. After the third element, the first list passed to the inner call to <code>concat</code>is empty, so that the else part of the <code>if</code> in line 8 kicks in, and the result of the call is simply the second list (line 12).
<h3>In summary</h3>
<ul>
    <li>Lists in clojure are immutable</li>
    <li>By default, elements are added to the front of the list</li>
    <li>The linked structure makes it easy to implement immutable adding of elements</li>
    <li>Concatenating lists results in a <em>lazy sequence</em> where new list entries are created &#8220;on-demand&#8221;</li>
    <li>The data elements are not copied, as they are immutable values</li>
</ul></p>
]]></content:encoded>
			<wfw:commentRss>http://jgre.org/2010/05/27/idsc-ii-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Immutable Data-Structure Canon</title>
		<link>http://jgre.org/2010/05/20/the-immutable-data-structure-canon/</link>
		<comments>http://jgre.org/2010/05/20/the-immutable-data-structure-canon/#comments</comments>
		<pubDate>Thu, 20 May 2010 17:28:23 +0000</pubDate>
		<dc:creator>Janico</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[code reading]]></category>
		<category><![CDATA[data-structures]]></category>
		<category><![CDATA[functional programming]]></category>

		<guid isPermaLink="false">http://jgre.org/?p=490</guid>
		<description><![CDATA[In the April issue of Communications of the ACM, George V. Neville-Neil (a.k.a. Kode Vicious) reviews the fundamental data-structures &#8212; array, list, tree, and hash table. Because of the importance of data-structures for all programming, he calls it the &#8220;data-structure &#8230; <a href="http://jgre.org/2010/05/20/the-immutable-data-structure-canon/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In the April issue of Communications of the ACM, George V. Neville-Neil (a.k.a. Kode Vicious) reviews the fundamental data-structures &#8212; array, list, tree, and hash table. Because of the importance of data-structures for all programming, he calls it the &#8220;<a href="http://cacm.acm.org/magazines/2010/4/81499-the-data-structure-canon">data-structure canon</a>&#8221; (requires subscription). Taking a cue from that article, I&#8217;m starting a series of posts about a special kind of implementation of the basic data-structures: the immutable lists, vectors, and maps in <a href="http://clojure.org">Clojure</a>.</p>

<p><div id="attachment_493" class="wp-caption alignnone" style="width: 435px"><img class="size-full wp-image-493" src="http://jgre.org/files/2010/05/iStock_000009329256XSmall.jpg" alt="(c) iStockphoto.com/DaddyBit" width="425" height="282" /><p class="wp-caption-text">(c) iStockphoto.com</p></div>
<h3>Clojure</h3>
Clojure is a Lisp dialect for the Java Virtual Machine with a focus on functional programming. It has strong support for clean multithreaded design and a key ingredient are immutable, persistent data-structures.</p>

<p>Immutability is important for clean multithreaded programs, because it allows you to separate <em>values</em> from <em>identities</em>. Rich Hickey, Clojure&#8217;s creator, <a title="Values and Change - Clojure's approach to Identity and State" href="http://clojure.org/state">defines these terms</a> as follows:
<blockquote>By identity I mean <strong><em>a stable logical entity associated with a series of different values over time</em></strong>. Models need identity for the same reasons humans need identity &#8212; to represent the world. How could it work if identities like &#8216;today&#8217; or &#8216;America&#8217; had to represent a single constant value for all time? Note that by identities I don&#8217;t mean names (I call my mother Mom, but you wouldn&#8217;t).</p>

<p>So, for this discussion, an identity is an entity that has a state, which is its value at a point in time. And <strong><em>a value is something that doesn&#8217;t change</em></strong>. 42 doesn&#8217;t change. June 29th 2008 doesn&#8217;t change. Points don&#8217;t move, dates don&#8217;t change, no matter what some bad class libraries may cause you to believe. Even aggregates are values. The set of my favorite foods doesn&#8217;t change, i.e. if I prefer different foods in the future, that will be a different set.</blockquote>
When you have pure functions dealing with values, problems with multiple threads simply go away, because there is no danger of a value being changed from under a function&#8217;s backside. Of course, the world is not as easy as that, and programs need to model state that can change.</p>

<p>In Clojure, an identity can be in different states &#8212; i.e. have different values &#8212; over time. Identities are implemented as atomic references to values using so called <a href="http://clojure.org/Refs">Refs</a> and <a href="http://clojure.org/Agents">Agents</a>. But these constructs do not concern us in this series, I mentioned them only to give a background on the rationale for using immutable data-structures to model values.</p>

<p>Abstractly speaking, new values are <em>functions</em> of old values. For example, let us define a list and bind it to the name <code>l1</code>.</p>

<p><pre class="brush: clojure;">
(def l1 (list 1 2 3))
</pre></p>

<p>When we want to add another element, we do not modify the existing list, but we call the function <code>cons</code> create new list with one more element.</p>

<p><pre class="brush: clojure;">
(def l2 (cons 42 l1))
l2
=&gt; (42 1 2 3)
l1
=&gt; (1 2 3)
</pre></p>

<p>If we had passed <code>l1</code> to a function in another thread, it could happily work on the initial three elements without being bothered by our addition of <code>42</code>.</p>

<p>Your initial impression is probably that there is a lot of copying involved in the implementation of the immutable data-structures, but that ain&#8217;t so. Clojure&#8217;s data-structures maintain close to the same performance guarantees that their mutable counterparts make.</p>

<p>In the following posts, we look at the implementation of lists, vectors, and maps one by one.</p>
]]></content:encoded>
			<wfw:commentRss>http://jgre.org/2010/05/20/the-immutable-data-structure-canon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More on HTML5</title>
		<link>http://jgre.org/2010/05/16/more-on-html5/</link>
		<comments>http://jgre.org/2010/05/16/more-on-html5/#comments</comments>
		<pubDate>Sun, 16 May 2010 12:35:41 +0000</pubDate>
		<dc:creator>Janico</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jgre.org/?p=517</guid>
		<description><![CDATA[Some more links about the state of affairs around HTML5. A survey of video on the web shows that 26% of videos are available through HTML5 &#60;video&#62; tags, up from 10% in January. (via the Register) An overview of which &#8230; <a href="http://jgre.org/2010/05/16/more-on-html5/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Some more links about the state of affairs around HTML5.
<ul>
    <li>A <a href="http://blog.mefeedia.com/html5-video-stats">survey</a> of video on the web shows that 26% of videos are available through HTML5 &lt;video&gt; tags, up from 10% in January. (via <a href="http://www.theregister.co.uk/2010/05/16/mefeedia_html5_survey/">the Register</a>)</li>
    <li>An <a href="http://html5readiness.com/">overview</a> of which browsers support which features of HTML5 and CSS3</li>
    <li>A <a href="http://alteredqualia.com/canvasmol/">visualization</a> of Molecules using the &lt;canvas&gt; tag</li>
</ul></p>
]]></content:encoded>
			<wfw:commentRss>http://jgre.org/2010/05/16/more-on-html5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How Hard Can It Be to Read a File?</title>
		<link>http://jgre.org/2010/05/09/how-hard-can-it-be-to-read-a-file/</link>
		<comments>http://jgre.org/2010/05/09/how-hard-can-it-be-to-read-a-file/#comments</comments>
		<pubDate>Sun, 09 May 2010 17:23:25 +0000</pubDate>
		<dc:creator>Janico</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[code reading]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[radius of comprehension]]></category>

		<guid isPermaLink="false">http://jgre.org/?p=459</guid>
		<description><![CDATA[Reading code is good for you. Everyone knows this, but few actually follow the advice. It&#8217;s like healthy eating: Nobody would oppose it, but it is often ignored out of inertia. I&#8217;m certainly guilty of skipping my healthy dose of &#8230; <a href="http://jgre.org/2010/05/09/how-hard-can-it-be-to-read-a-file/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Reading code is good for you. Everyone knows this, but few actually follow the advice. It&#8217;s like healthy eating: Nobody would oppose it, but it is often ignored out of inertia. I&#8217;m certainly guilty of skipping my healthy dose of code reading in the past, but I&#8217;m going to make a habit out of it from now on. To support the process, I will write about the code I&#8217;m reading.</p>

<div id="attachment_496" class="wp-caption alignnone" style="width: 445px"><img class="size-full wp-image-496 " src="http://jgre.org/files/2010/05/iStock_000010343670XSmall.jpg" alt="(c) iStockphoto.com/skodonnell" width="435" height="223" /><p class="wp-caption-text">(c) iStockphoto.com/skodonnell</p></div>

<p>For the first exercise, I&#8217;m going to take a look at a simple bit of library code: reading a file into a string. I choose this example, because it turns out that reading a file with Java is more involved than I though it should be.</p>

<p>Coming from Ruby I&#8217;m used to being able to read the contents of a file into a string using one line of code like this:</p>

<p><pre class="brush: ruby;">
str = File::read &quot;bla.txt&quot;
</pre></p>

<p>The simplest solution I found for Java is this</p>

<p><pre class="brush: java;">
    String str = &quot;&quot;;
    try {
        BufferedReader in = new BufferedReader(new FileReader(&quot;bla.txt&quot;));
        String line;
        while((line = in.readLine()) != null) {
            str += line + &quot;\n&quot;;
        }
        in.close();
    } catch(IOException e) {
    }
    </pre></p>

<p>Let us ignore the missing error handling and the inefficiency of concatenating strings for the moment. The number of lines is not of interest either &#8212; this not some &#8220;Java sucks&#8221;-rant, there are more than enough of that already.</p>

<p>The difficulty of this trivial task illustrates a concept I have been thinking about regularly lately: the <a href="http://www.pragprog.com/magazines/2010-04/tangled-up-in-tools">Radius of Comprehension </a>which I wrote about <a href="http://jgre.org/2010/04/25/engineering-and-framework-fever/">here</a>. It is a property of a codebase defined as follows
<blockquote>If you are looking at a given fragment of code, how far away from that  bit of the code do you need to have in your mind at that time in order  to understand the fragment at hand?</blockquote>
So how far from the code in our examples do we have to have in our heads? In Ruby, we need to understand one method in one class. (I&#8217;m not including <code>String</code> here, as I consider that to be part of fundamental understanding of the language.) However, to actually find the <code>read</code> method, you need to look into the <code>IO</code> class, the parent of <code>File</code>. The names of the classes and the method are pretty obvious, so you quickly find them when looking through the documentation.</p>

<p>I&#8217;m going to make a naive attempt at quantifying the radius of comprehension here. The Ruby solution tentatively gets a score of three: two classes and one method each with meaningful names. I&#8217;ll talk about the merits of this quantification at the end of the post, but first let&#8217;s get the comparison with Java.</p>

<p>In the Java version, we have two classes, <code>BufferedReader</code> and <code>FileReader</code>, and one method, <code>readLine</code>. Having to loop through the file and concatenate all the lines makes the code more verbose and thus harder to read, but I wouldn&#8217;t say it makes it harder to understand. A source of confusion in this code is the indirect relationship between the <code>FileReader</code> and the actual reading. <code>FileReader</code> is a self-explanatory name, but <code>BufferedReader</code>? Having one reader and passing it to another reader from which you can actually read strings in your program increases the radius of comprehension by more than an obvious inheritance. As a consequence I assign the Java code a radius four: one for the method, one for the <code>FileReader</code>, and two for <code>BufferedReader</code>.
<h3>Error Handling</h3>
There is another factor I want to take into account here: error handling. In both examples an exception could be thrown, and we need to understand what kind of exception that is in order to write robust code. So I would add one to both radius scores.</p>

<p>In the Java code there is an additional complication: an error could occur when creating the readers or in the call to <code>readLine</code>. In the latter case, we should to close the reader to avoid leaking resources. In Ruby we don&#8217;t have to worry about closing anything. Understanding this additional error case in Java adds to the radius. Thus, with error handling we end up with: Ruby: 4, Java: 6.</p>

<p>Again, the intention of this post is not to prove that Java stinks and Ruby is great. I&#8217;m looking at a very limited use case here and actively ignore scenarios where the added complexity in Java&#8217;s library might be useful. The point is understand more about the radius of comprehension.</p>

<p>What I like about this metric is that the difference between the examples is a factor of 1.5 while the more straight forward metric of lines of code differs by a factor of 10. This score feels about right. The Java code is more complicated, but not hugely so.
<h3>Why Quantify the Radius of Comprehension?</h3>
While I&#8217;m satisfied with the result of my calculation here, the ad-hoc way I pulled the number from my pants is not where near scientific or generally useful. Maybe I can improve and formalize the process while reading more code. There is, however, another deeper question: Is it even worthwhile to try and formalize the radius of comprehension?</p>

<p>Mike Taylor, who invented the term, <a href="http://www.pragprog.com/magazines/2010-04/tangled-up-in-tools">writes</a>
<blockquote>I’m talking about a human issue here (and therefore, sadly, an all but  impossible one to measure, though we know it when we see it)</blockquote>
While we may not be able to measure it given the vagueness of the definition, it might be possible to approximate it. Like any metric about code quality, it brings the danger of creating an arbitrary goal that is pursued at the expense of others, with a negative overall effect on quality. But I also see potential in quantifying the radius of comprehension.</p>

<p>Code always has two audiences: the compiler or interpreter and other programmers who have to use or maintain it. While we can reliably and immediately assess how well the machine understands our code, feedback on how well others understand it is rare and fuzzy. If we had a way to determine the radius of comprehension, we could judge how well other programmers can understand what we wrote &#8212; and take steps to improve it.</p>
]]></content:encoded>
			<wfw:commentRss>http://jgre.org/2010/05/09/how-hard-can-it-be-to-read-a-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Estimation</title>
		<link>http://jgre.org/2010/05/02/project-estimation/</link>
		<comments>http://jgre.org/2010/05/02/project-estimation/#comments</comments>
		<pubDate>Sun, 02 May 2010 07:24:01 +0000</pubDate>
		<dc:creator>Janico</dc:creator>
				<category><![CDATA[management]]></category>
		<category><![CDATA[estimation]]></category>
		<category><![CDATA[projects]]></category>

		<guid isPermaLink="false">http://jgre.org/?p=394</guid>
		<description><![CDATA[Why don&#8217;t we just call plans what they really are: guesses. [...] When you turn guesses into plans, you enter a danger zone. Plans let the past drive the future. [...] The timing of long-range plans is screwed up too. &#8230; <a href="http://jgre.org/2010/05/02/project-estimation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote>Why don&#8217;t we just call plans what they really are: guesses. [...]

When you turn guesses into plans, you enter a danger zone. Plans let the past drive the future. [...]

The timing of long-range plans is screwed up too. You have the most information when you&#8217;re doing something, not before you&#8217;ve done it.
<p style="text-align: right">&#8211; Jason Fried and David Heinemeier Hanson &#8220;Planning is guessing&#8221; from <a href="http://37signals.com/rework/">Rework</a>, page 19</p>

<dl> </dl></blockquote>

<p>Estimating how long a software project will take is hard. Everyone  working in software can agree with this. That&#8217;s however where the  agreement ends. Is it even possible to make realistic estimates? Is it  worth the hassle? Do you estimate big blocks of tasks or do you break  the project into small detailed ones before you estimate? How do you deal with changing requirements? Do you estimate the calender time  required for a given sub-project or the pure time spend on working on it?</p>

<div id="attachment_344" class="wp-caption aligncenter" style="width: 435px"><img class="size-full  wp-image-344 " src="http://jgre.org/files/2009/07/istock_000004977344xsmall.jpg" alt="Crystal Gazing" width="425" height="282" /><p class="wp-caption-text">(c) iStockphoto.com/pidjoe</p></div>

<p><dl> <dt> </dt> </dl>All of these questions are highly debatable and even though they are  phrased as simple binary decisions, the answers are most likely on a  continuum between yes and no. Most software professionals will, for  example, respond to the first questions with something  between the extremes &#8220;It cannot be done&#8221; and &#8220;You can get it exactly  right&#8221;.</p>

<p>The article quoted above is not specifically about planning software projects, but I think these views reflect what many programmers think of planning projects. However, as few projects are implemented in splendid isolation, coordination with colleagues and partners and communication with bosses and customers usually confronts us with the need to present estimates.</p>

<p>Using the past to inform decisions about the future, is not generally seen as negative as Hanson and Fried do. <a href="http://www.joelonsoftware.com/items/2007/10/26.html">Evidence-based scheduling</a> is a method for using data about estimates and actual work times of past projects to create more accurate estimates for a future project.</p>

<p>The problem that you don&#8217;t know enough to make a realistic estimate at the beginning of the project is sometimes called &#8220;<a href="http://en.wikipedia.org/wiki/Cone_of_Uncertainty">Cone of Uncertainty</a>&#8220;. The range of uncertainty is enormously wide at the beginning and narrows as the project moves forward.</p>

<p><dl> <dt><img class="size-full wp-image-439   alignnone" src="http://jgre.org/files/2010/05/cone_of_uncertainty1.gif" alt="Cone of Uncertainty (Source: McConnell, Software Estimation, page  28)" width="408" height="237" /></dt>
<p style="text-align: center">Cone of Uncertainty (Source: McConnell, Software Estimation, page 28)</p></p>

<p></dl>In his book <a href="http://www.stevemcconnell.com/est.htm">Software Estimation</a> Steve McConnell talks about working to narrow down the cone by making decisions that reduce variability.
<blockquote>The Cone narrows only as you make decisions that eliminate variability.[..]
Defining the product vision (including committing to what you will <em>not</em> do) reduces variability. Defining requirements — again, including what you are <em>not</em> going to do — eliminates variability further. Designing the user interface helps to reduce the risk of variability arising from misunderstood requirements. Of course, if the product isn’t really defined, or if the product definition gets redefined later, the Cone will widen, and estimation accuracy will be poorer.
<p style="text-align: right">&#8211; Steve McConnell &#8220;The Cone of Uncertainty&#8221; from <a href="http://www.stevemcconnell.com/est.htm">Software Estimation</a></p>
</blockquote>
Now, this doesn&#8217;t sound to hard. But it omits an important problem. Possibly the greatest problem in planning projects: changing requirements. In another book, McConnell acknowledges this problem.
<blockquote>Maybe you think the Pontiac Aztek was the greatest car ever made, belong to the Flat Earth Society, and make a pilgrimage to the alien landing site at Roswell, New Mexico, every four years. If you do, go ahead and believe that requirements won&#8217;t change on your projects.
<p style="text-align: right">&#8211; Steve McConnell &#8220;The Myth of Stable Requirements&#8221; from <a href="http://www.stevemcconnell.com/cc.htm">Code Complete</a>, page 32</p>
</blockquote>
I don&#8217;t think this completely invalidates the process of narrowing down the Cone of Uncertainty but I wouldn&#8217;t be as optimistic as McConnell about how early you can get to +/- 20%.</p>

<p>When the requirements change, developers or project managers need to update their estimates and fight to get the stakeholders accept that the project will take longer than initially estimated (requirements chages that cause the project to finish earlier are a theoretical possibility &#8212; so is life on other planets). Getting this across has a lot more to do with communication than with project estimation.</p>

<p>A major source of communication problems is the confusion of <strong>estimates</strong> and <strong>targets</strong>. The estimate is what you think when the work will be done. The often-cited trade show where the finished project is supposed to be introduced is a target. When you&#8217;re asked to change your estimate so that you finish in time for the trade show, you&#8217;re in a spot of communication bother.</p>
]]></content:encoded>
			<wfw:commentRss>http://jgre.org/2010/05/02/project-estimation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Engineering and Framework Fever</title>
		<link>http://jgre.org/2010/04/25/engineering-and-framework-fever/</link>
		<comments>http://jgre.org/2010/04/25/engineering-and-framework-fever/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 17:21:53 +0000</pubDate>
		<dc:creator>Janico</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[engineering]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[library]]></category>

		<guid isPermaLink="false">http://jgre.org/?p=400</guid>
		<description><![CDATA[Great article by Mike Taylor in this month&#8217;s PragPub magazine in which he examines the tension between hand-coding all of your application&#8217;s code and using libraries and frameworks. Briefly summarized, the problems Taylor has with libraries are: bad quality, documentation &#8230; <a href="http://jgre.org/2010/04/25/engineering-and-framework-fever/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Great <a title="Tangled Up In Tools" href="http://www.pragprog.com/magazines/2010-04/tangled-up-in-tools">article</a> by <a title="The Reinvigorated Programmer" href="http://reprog.wordpress.com/">Mike Taylor</a> in this month&#8217;s <a title="PragPub Magazine" href="http://www.pragprog.com/magazines">PragPub magazine</a> in which he examines the tension between hand-coding all of your application&#8217;s code and using libraries and frameworks.</p>

<p>Briefly summarized, the problems Taylor has with libraries are:
<ul>
    <li> bad quality,</li>
    <li>documentation that fails to explain what the library is about, and</li>
    <li>too libraries many to choose from with no guidance about their strengths and weaknesses.</li>
</ul>
This leads to a feeling of helplessness for programmers, as their work seems to consist mainly of plugging together third party code with varying degrees of success and elegance. Frameworks are particularly intimidating in this respect, as they take control over the flow of the program. This makes it hard to understand what is happening and why. On one hand, re-use is good engineering practice as it lets you focus on high-level problems, on the other hand, it is somewhat unsatisfying.</p>

<p>The best part of the article is that Taylor offers ideas on how to deal with this problem instead of just complaining. And the approach is not some unrealistic, back-to-the-roots suggestion that we all write our own search functions in machine language. His suggestions are formulated as what he calls a &#8220;two-prong manifesto&#8221;.</p>

<p><strong>Prong 1: Simpler, clearer, shorter documentation</strong>. I have long felt that the common Javadoc (or RDoc, or doxygen, or whatever) format is suboptimal for understanding how to use a library, as it doesn&#8217;t tell you where to begin. Taylor suggests that a library should have a one-page overview. If a library cannot be summarized in a single page, it is to complex and should be redesigned.</p>

<p><strong>Prong 2: Minimize the radius of comprehension</strong>. This one is more abstract, but possibly even more important. Taylor defines the radius of comprehension of a codebase as follows:
<blockquote>if you are looking at a given fragment of code, how far away from that bit of the code do you need to have in your mind at that time in order to understand the fragment at hand? It is a sort of a measurement of how good encapsulation is across the whole codebase, although when I say “encapsulation” here, I am using that term in a broad sense that means more than just technical issues such as what proportion of data members are marked private. I’m talking about a human issue here (and therefore, sadly, an all but impossible one to measure, though we know it when we see it).</blockquote></p>
]]></content:encoded>
			<wfw:commentRss>http://jgre.org/2010/04/25/engineering-and-framework-fever/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
