<?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>Holsee&#039;s Blog &#187; Productivity</title>
	<atom:link href="http://www.blog.holsee.com/category/productivity/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blog.holsee.com</link>
	<description>Adventures in Entrepreneurship, Code, Coffee and Photography..</description>
	<lastBuildDate>Fri, 02 Jul 2010 10:48:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Closures &#8211; Object Oriented Functionality</title>
		<link>http://www.blog.holsee.com/2010/02/closures/</link>
		<comments>http://www.blog.holsee.com/2010/02/closures/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 18:29:25 +0000</pubDate>
		<dc:creator>holsee</dc:creator>
				<category><![CDATA[Productivity]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Closures]]></category>
		<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.blog.holsee.com/2010/02/closures/</guid>
		<description><![CDATA[Over the past year the way I have developed code has been changing.  I have started to think differently about how I implement my code moving beyond traditional object orientated programming, making use of numerous functional concepts in my solutions.  I think this is due to the fact I have been trying to expose myself [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past year the way I have developed code has been changing.  I have started to think differently about how I implement my code moving beyond traditional object orientated programming, making use of numerous functional concepts in my solutions.  I think this is due to the fact I have been trying to expose myself to as much code as possible across many different languages and platforms and somethings have just stuck.</p>
<p>One of the concepts I have came to know and love is that of a <strong>closure</strong>.  This was first exposed to me when C# started to incorporate more functional aspects into the language such as anonymous methods as first class citizens and later lambda expressions.</p>
<p>You might think this is quite odd that I specially call out closures &amp; anonymous functions as being a functional concept, as closures are used heavily in dynamic languages due to the fact functions can be passed about as first class citizens. Passing functions about is the very nature of a <span style="text-decoration: underline;">function</span>al language.</p>
<p>This is what Wikipedia has to say:</p>
<blockquote><p>In <a href="http://en.wikipedia.org/wiki/Computer_science">computer science</a>, a <strong>closure</strong> is a <a href="http://en.wikipedia.org/wiki/First-class_function">first-class function</a> with <a href="http://en.wikipedia.org/wiki/Free_variables_and_bound_variables">free variables</a> that are <a href="http://en.wikipedia.org/wiki/Name_binding">bound</a> in the <a href="http://en.wikipedia.org/wiki/Lexical_environment">lexical environment</a>. Such a function is said to be &#8220;closed over&#8221; its free variables. A closure is defined within the scope of its free variables, and <a href="http://en.wikipedia.org/wiki/Variable_(programming)">the extent of those variables</a> is at least as long as the lifetime of the closure itself.</p></blockquote>
<p>The closure aspect is simple&#8230; the anonymous function is &#8220;Trapping&#8221; the local variable within the functions context.  That means the local variable in question will remain in scope for the duration of the lifetime of the anonymous function. In the examples below I declare anonymous functions called &#8220;closure&#8221; and I en-close around variable called &#8220;x&#8221;.</p>
<p>So here is an example of a closure in my language of the month Groovy:</p>
<pre class="brush: groovy;">
def loopTo(n, closure)
{
	for(int i = 0; i &lt; n; i++)
	{
		if(closure(i))
			println i
	}
}

//Our variable to be &quot;Closed Over&quot;
int x = 2

// I like this syntax of '{ }' to create an anonymous method,
// in Groovy we can use 'it' by default
// to reference an undefined but passed parameter.
loopTo(100, { it % 2 == x })
</pre>
<p>Now in JavaScript:</p>
<pre class="brush: jscript;">
function loopTo(n, closure){
	for(i = 0; i &lt; n; i++){
		if(closure(i)){
			alert(i);
		}
	}
}

//Our variable being &quot;Closed over&quot;
var x = 0;
// In JS we use the 'function' keyword and specify the parameter
loopTo(10, function(i){
	return i % 2 == x;
});
</pre>
<p>Now in C#:</p>
<pre class="brush: csharp;">
static void Main(string[] args)
{
    //Our variable being &quot;Closed over&quot;
    int x = 0;

    // In C# we can use a lambda expression.
    LoopTo(100, num =&gt; num % 2 == x );
}

// As C# is strongly typed we define the expected
// expression type as a 'Func&lt;param, return type&gt;'
public static void LoopTo(int n, Func&lt;int, bool&gt; closure)
{
    for (int i = 0; i &lt; n; i++)
    {
        if(closure(i))
            Console.WriteLine();
    }
}
</pre>
<p>And now in Scala:</p>
<pre class="brush: scala;">
// Scala is sweet we store our range rather than writing a loop,
// then iterate over the list passing the index 'i' into our closure.
def main(args: Array[String]) :Unit = {
    val range = 0.until(100);
    range.foreach(num =&gt;  if(closure(num)) println(num) );
}
//Our variable to be &quot;Closed over&quot;
val x:int = 0
// Our function is defined like so
def closure(num : Int) = {  num %  2 == x;  }
</pre>
<p>We can copy the Scala way in C#.<br />
We had to cheat a tad and write a funky &#8216;Until&#8217; <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx">C# Extension Method</a>.<br />
This gives us a nice fluent way to create a range of integers stored as a list like so:</p>
<pre class="brush: csharp;">
public static void Main(string[] args)
{
    //Extension method 'Until' defined later in this post
    var range = 0.Until(100);
    range.ForEach(num =&gt; { if(Closure(num)) Console.WriteLine(num); });
}
int x = 0;
// Our function definition in C#
private static readonly Func&lt;int, bool&gt; Closure = num =&gt; num % 2 == x;
</pre>
<pre class="brush: csharp;">
public static class Extensions
{
    // Called like so: 0.Until(100)
    public static List&lt;int&gt; Until(this int lower, int upper)
    {
        var range = new List&lt;int&gt;();

        for (int i = lower; i &lt;= upper; i++)
        {
            range.Add(i);
        }

        return range;
    }
}
</pre>
<p>This is what the code would look like without using anonymous functions:</p>
<pre class="brush: csharp;">
private static void Main(string[] args)
{
    LoopTo(100);
}

public static void LoopTo(int n)
{
    int x = 0;
    for (int i = 0; i &lt; n; i++)
    {
        //Our logic for filtering is tied to the control structure
        if (i % 2 == x)
            Console.WriteLine(i);
    }
}
</pre>
<p>As you can see the code is not only verbose but breaks a the DRY rule (Don’t Repeat Yourself).  Closures are powerful because they encapsulate what might almost be described as a <strong>“Strategy”</strong>. You have separated out the control structure from the logic.  This is the strategy design pattern.</p>
<p>Closures become powerful when your reach the level of understanding that the encapsulation of logic allows for some really powerful composition of <strong>“Strategy”</strong>.</p>
<p>I find this really intriguing.  I like languages, and I love the idea creating domain specific languages / fluent APIs which make the definition of <strong>“Strategy”</strong> trivial to the end user.  By combining closures (via currying) or by logically appending delegates together we can create something truly powerful.</p>
<p>If you didn&#8217;t know what anonymous functions &amp; closures were when you started to read this post, then hopefully you do now and maybe you can see why they are worth using.</p>
<p>Just some food for thought.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.holsee.com/2010/02/closures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My New Reading / Late Night Coding Lair..</title>
		<link>http://www.blog.holsee.com/2009/12/my-new-reading-late-night-coding-lair/</link>
		<comments>http://www.blog.holsee.com/2009/12/my-new-reading-late-night-coding-lair/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 21:41:18 +0000</pubDate>
		<dc:creator>holsee</dc:creator>
				<category><![CDATA[Productivity]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Work Environment]]></category>

		<guid isPermaLink="false">http://www.blog.holsee.com/2009/12/my-new-reading-late-night-coding-lair/</guid>
		<description><![CDATA[With some cash I got for Christmas I thought I would spend it on creating a chilled out area in my house for reading and for some late night coding.&#160; I work remotely from a home office… I have a nice set-up, but I desire a space which is less formal and more relaxing.&#160; These [...]]]></description>
			<content:encoded><![CDATA[<p align="left">With some cash I got for Christmas I thought I would spend it on creating a chilled out area in my house for reading and for some late night coding.&#160; I work remotely from a home office… I have a nice set-up, but I desire a space which is less formal and more relaxing.&#160; These items will go a long way towards achieving this: </p>
<p align="center">XXL Bean Bag</p>
<p align="center"><a href="http://www.blog.holsee.com/wp-content/uploads/2009/12/image5.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.blog.holsee.com/wp-content/uploads/2009/12/image_thumb5.png" width="244" height="246" /></a> </p>
<p align="center">Lap Desk</p>
<p align="center"><a href="http://www.blog.holsee.com/wp-content/uploads/2009/12/image6.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.blog.holsee.com/wp-content/uploads/2009/12/image_thumb6.png" width="246" height="203" /></a> </p>
<p align="center">Blue Lava Lamp </p>
<p align="center"><a href="http://www.blog.holsee.com/wp-content/uploads/2009/12/image7.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.blog.holsee.com/wp-content/uploads/2009/12/image_thumb7.png" width="145" height="246" /></a> </p>
<p align="center">Blue Plasma Lamp</p>
<p><a href="http://www.blog.holsee.com/wp-content/uploads/2009/12/image8.png"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="image" border="0" alt="image" src="http://www.blog.holsee.com/wp-content/uploads/2009/12/image_thumb8.png" width="121" height="246" /></a> </p>
</p>
</p>
<p align="left">…I welcome all suggestions! Kthx =]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.holsee.com/2009/12/my-new-reading-late-night-coding-lair/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
