<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:blogChannel="http://backend.userland.com/blogChannelModule" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
  <channel>
    <title>self.write_blog</title>
    <description>Software development, computer science, and a few unrelated topics</description>
    <link>http://www.raine-tech.com/blogs/jr/</link>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <generator>BlogEngine.NET 1.4.5.0</generator>
    <language>en-US</language>
    <blogChannel:blogRoll>http://www.raine-tech.com/blogs/jr/opml.axd</blogChannel:blogRoll>
    <blogChannel:blink>http://www.dotnetblogengine.net/syndication.axd</blogChannel:blink>
    <dc:creator>Jose R Calzada</dc:creator>
    <dc:title>self.write_blog</dc:title>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/jrcalzadaBTE" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
      <title>It’s quiet in here…</title>
      <description>&lt;p&gt;If you happen to be a regular visitor, then you might have noticed it has been almost a month since my last post. Although I had some interesting posts lined up for this month, everything got side-tracked once the date of the wedding approached. For those not in the know, I got married on May 16th and finally put an end to the very stressful and intense process of planning such an event. Everything was perfect and both me and my wife had the time of our lives. After the wedding we traveled to Spain, where we spent our honeymoon. I just got back yesterday and, realizing I had neglected the blog, thought it would be appropriate to post a small update.&lt;/p&gt;  &lt;p&gt;As I mentioned in one of my first posts in this blog, I played a few songs at the wedding with the help of some friends. Unfortunately, I didn’t actually record the performance, so I can’t honestly say whether it sounded decent or not. Still, here’s a nice picture taken during the third song in the set: Oasis’s Live Forever.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.raine-tech.com/blogs/jr/image.axd?picture=WindowsLiveWriter/Itsquietinhere/7763A54C/IMG_7180.jpg"&gt;&lt;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="IMG_7180" border="0" alt="IMG_7180" src="http://www.raine-tech.com/blogs/jr/image.axd?picture=WindowsLiveWriter/Itsquietinhere/12D88183/IMG_7180_thumb.jpg" width="491" height="653" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I’ll try and post often these next few weeks. I still have some pending posts I’d promised before and I also wanted to mention some topics that caught my attention. I also need to get back on Twitter, since I haven’t posted an update in ages either. Until then, I’ll go back to enjoying the newly-wed life!&lt;/p&gt;&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.raine-tech.com/blogs/jr/post/Ite28099s-quiet-in-heree280a6.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.raine-tech.com/blogs/jr/post/Ite28099s-quiet-in-heree280a6.aspx&amp;amp;title=It’s quiet in here…" target="_blank" title="DZone It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Ite28099s-quiet-in-heree280a6.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.raine-tech.com/blogs/jr/" target="_blank" title="Technorati"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Ite28099s-quiet-in-heree280a6.aspx&amp;amp;title=It’s quiet in here…" target="_blank" title="Reddit"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.raine-tech.com/blogs/jr/post/Ite28099s-quiet-in-heree280a6.aspx&amp;amp;title=It’s quiet in here…" target="_blank" title="Del.icio.us"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.raine-tech.com/blogs/jr/post/Ite28099s-quiet-in-heree280a6.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/N_fQOEpZ5HJW7vNrRUeExrSYmq4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/N_fQOEpZ5HJW7vNrRUeExrSYmq4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/N_fQOEpZ5HJW7vNrRUeExrSYmq4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/N_fQOEpZ5HJW7vNrRUeExrSYmq4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <link>http://www.raine-tech.com/blogs/jr/post/Ite28099s-quiet-in-heree280a6.aspx</link>
      <author>jr.calzada.nospam@nospam.raine-tech.com (jr.calzada)</author>
      <comments>http://www.raine-tech.com/blogs/jr/post/Ite28099s-quiet-in-heree280a6.aspx#comment</comments>
      <guid>http://www.raine-tech.com/blogs/jr/post.aspx?id=a20cda87-e662-4efd-b963-a2c2bd6a0cef</guid>
      <pubDate>Thu, 28 May 2009 18:48:59 -0600</pubDate>
      <category>Personal</category>
      <dc:publisher>jr.calzada</dc:publisher>
      <pingback:server>http://www.raine-tech.com/blogs/jr/pingback.axd</pingback:server>
      <pingback:target>http://www.raine-tech.com/blogs/jr/post.aspx?id=a20cda87-e662-4efd-b963-a2c2bd6a0cef</pingback:target>
      <slash:comments>3</slash:comments>
      <trackback:ping>http://www.raine-tech.com/blogs/jr/trackback.axd?id=a20cda87-e662-4efd-b963-a2c2bd6a0cef</trackback:ping>
      <wfw:comment>http://www.raine-tech.com/blogs/jr/post/Ite28099s-quiet-in-heree280a6.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.raine-tech.com/blogs/jr/syndication.axd?post=a20cda87-e662-4efd-b963-a2c2bd6a0cef</wfw:commentRss>
    </item>
    <item>
      <title>Genetic Algorithms in F# Part 1: Generations, Selection, Crossover, and Mutation</title>
      <description>&lt;p&gt;Genetic algorithms are a technique in artificial intelligence used to find the answer to a problem we don’t really know how to solve. The solution process is stochastic in nature, employing several ideas based on the theory of evolution. The main hypothesis is that, given a randomly generated set of possible solutions to a problem and a way to measure how effective they are, we can find increasingly better solutions by selecting the best ones from our set, mixing them, and then making very small changes. This mirrors the basic evolutionary process where the fittest individuals of a certain species mate and, sometimes, their genes are mutated leading to new characteristics that might be beneficial or not.&lt;/p&gt;  &lt;p&gt;There are two main questions we must ask ourselves when trying to approach a problem with a genetic algorithm: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;How can I encode a possible solution? &lt;/li&gt;    &lt;li&gt;How can I measure a solution’s effectiveness? &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;We usually call the encoded version of a solution a &lt;em&gt;chromosome. &lt;/em&gt;There’s no definite rule as to how we should build our chromosome, but using bit arrays is a common approach. Whatever your choice, it is safe to assume that they will usually be a collection of values and all chromosomes should be the same size. Now we just need to find a way to translate a possible solution to our problem to our data structure. The second question leads us to our &lt;em&gt;fitness function &lt;/em&gt;which is a function that measures how effective a certain ‘&lt;em&gt;chromosome’ &lt;/em&gt;is at solving our problem. Depending on the problem you’re trying to solve, it might make sense to normalize the fitness or not. These two elements are completely domain-dependent. The evolutionary process, on the other hand, is mostly domain-independent, which means it can be coded in a generic way. I will delve deeper into these subjects in a later post.&lt;/p&gt;  &lt;p&gt;In a way, selection, crossover, and mutation are the workhorses within a genetic algorithm. They are, after all, the ones that process the initial batch of solutions to generate new ones that might be more effective. Depending on our problem, different selection, crossover, and mutation strategies may be appropriate. Ideally, we would like to be able to choose which algorithm we’d like to use for each of these processes depending on our problem. I present one algorithm of each further below.&lt;/p&gt;  &lt;h2&gt;&lt;/h2&gt;  &lt;h2&gt;Generations&lt;/h2&gt;  &lt;p&gt;The way we represent a batch of solutions is with a &lt;em&gt;generation, &lt;/em&gt;which is simply a collection of &lt;em&gt;individuals. &lt;/em&gt;These individuals are our possible solutions to the problem at hand and contain a &lt;em&gt;chromosome&lt;/em&gt; and a &lt;em&gt;fitness&lt;/em&gt;, which determines how effective they are at solving the problem. The code to implement these data structures is very simple. I defined the &lt;em&gt;Individual&lt;/em&gt; type to be a record and the &lt;em&gt;Generation&lt;/em&gt; to be a class.&lt;/p&gt;  &lt;div style="overflow: auto" class="csharpcode"&gt;   &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;#light&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; RaineTech.AI.GeneticAlgorithms&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    type &lt;span class="str"&gt;'a Individual =&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;        {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;            Chromosome : '&lt;/span&gt;a list&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;            Fitness : &lt;span class="kwrd"&gt;double&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;    type &lt;span class="str"&gt;'a Generation(individuals : '&lt;/span&gt;a Individual list) =&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;        let orderedIndividuals = List.sort (fun a b -&amp;gt; (b.Fitness).CompareTo(a.Fitness)) individuals&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;        let accumulatedFitness = &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;            individuals&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;            |&amp;gt; List.fold_left (fun accumulatedFitness individual -&amp;gt; accumulatedFitness + individual.Fitness) 0.0&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        member x.GetFittest(number, copies) = &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;            [&lt;span class="kwrd"&gt;for&lt;/span&gt; i &lt;span class="kwrd"&gt;in&lt;/span&gt; 0 .. number - 1 &lt;span class="kwrd"&gt;do&lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;                &lt;span class="kwrd"&gt;for&lt;/span&gt; j &lt;span class="kwrd"&gt;in&lt;/span&gt; 0 .. copies - 1 &lt;span class="kwrd"&gt;do&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;                    &lt;span class="kwrd"&gt;yield&lt;/span&gt; orderedIndividuals.[i]]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;        member x.Individuals with get() = individuals&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;        member x.AccumulatedFitness with get() = accumulatedFitness&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;        member x.FittestIndividual with get() = orderedIndividuals.Head&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the &lt;em&gt;Individual&lt;/em&gt; record consists of the &lt;em&gt;Chromosome&lt;/em&gt;, which is a list of values; and its fitness. A generation is built with a list of individuals and immediately does two things: Stores a sorted list of the individuals and calculates the accumulated fitness. &lt;/p&gt;

&lt;p&gt;The sorted list is created in line 10 with a call to &lt;em&gt;List.sort&lt;/em&gt; passing in a simple function that compares the individuals according to their fitness value. I’ll go explain how these individuals are created in a later post. The accumulated fitness is simply the sum of each individual’s fitness, which we accomplish with a call to &lt;em&gt;List.fold_left&lt;/em&gt;. The function argument simply accumulates each individual’s fitness using 0.0 as the starting value.&lt;/p&gt;

&lt;p&gt;The properties on lines 19 – 21 should be self-explanatory. The method on line 15, however, is a bit more interesting. Its purpose is to create a list that contains several copies of the fittest individuals. You’ll see the importance of this method in my next blog post on the subject when I talk about elitism. In terms of implementation, it’s a simple list expression that returns &lt;em&gt;copies&lt;/em&gt; copies of the &lt;em&gt;number&lt;/em&gt; fittest individuals.&lt;/p&gt;

&lt;p&gt;With the basic data structures covered, we can move on to our selection, crossover, and mutation algorithms.&lt;/p&gt;

&lt;h2&gt;Selection: Roulette Wheel&lt;/h2&gt;

&lt;p&gt;The selection algorithm I implemented is called “Roulette Wheel Selection”. The concept is very simple: Imagine you have a pie chart representing the fitness of all chromosomes in the current generation. If you choose a random number between 0.0 and 1.0 and, afterwards, multiply it by the total accumulated fitness (The sum of the fitness of each solution in the current generation) you could use the resulting number to stochastically choose an individual from the generation, giving a larger probability to those individuals with a high fitness value. You can visualize it as if the pie chart were really a roulette and you were dropping a ball on it. Any individual might get chosen, but those with the highest fitness value have a better chance. This ensures that, overall, the fittest individuals survive but also prevents stagnation by giving other solutions with lower fitness a chance to go through. We can also specify how many individuals we would like to select from the current generation.&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; margin: 10px auto 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.raine-tech.com/blogs/jr/image.axd?picture=WindowsLiveWriter/GeneticAlgorithmsinFPart1SelectionCrosso/49C92310/image_thumb.png" width="465" height="282" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The code for this algorithm is rather simple, although not as functional as I would have liked it to be. I considered implementing it using several list transformations, but it traversed the list way too many times for my liking, which led to a more imperative solution. It still showcases some nice things about F#:&lt;/p&gt;

&lt;div style="overflow: auto" class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;#light&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;module RaineTech.AI.GeneticAlgorithms.SelectionFunctions&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    open System&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    let RouletteWheelSelection (generation : 'a Generation) (size : int) (randomGenerator : Random) =&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        let generationFitness = generation.AccumulatedFitness&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;        let rec getIndividualAtRoulette (individual : 'a Individual) (restOfGeneration : 'a Individual list) roulettePosition accumulatedFitness =&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;            if accumulatedFitness &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; roulettePosition then&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;                individual&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;            else&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;                getIndividualAtRoulette restOfGeneration.Head restOfGeneration.Tail roulettePosition (accumulatedFitness + restOfGeneration.Head.Fitness)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;        [for i in 0 .. size - 1 do&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;            let roulettePosition = randomGenerator.NextDouble() * generationFitness&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            yield getIndividualAtRoulette generation.Individuals.Head generation.Individuals.Tail roulettePosition generation.Individuals.Head.Fitness&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;        ]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;            &lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;The function receives the generation from which to select the individuals, the number of individuals to select, and a random number generator as parameters. We start by storing the generation’s accumulated fitness in line 6 and then we define a recursive function that is responsible for returning the individual at a certain position in our imaginary roulette. What we do is from the first solution in the generation, accumulate the fitness of the solutions until we obtain a value greater than the roulette position we received. Once this happens, we can return the individual that made us go over. In lines 8 – 9 we simply perform that check and return the individual. If we still haven’t gone over the roulette position value, then we call recursively in line 11 passing the next solution as the first parameter, the rest of the generation as the second, the same roulette position as the third, and the result of adding the current individual’s fitness to the running total as the fourth. You can visualize this process as follows:&lt;/p&gt;

&lt;div align="center"&gt;
  &lt;table border="1" cellspacing="0" cellpadding="2" width="402" align="center"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" width="80"&gt;
          &lt;p align="center"&gt;Solution 1&lt;/p&gt;
        &lt;/td&gt;

        &lt;td valign="top" width="80"&gt;Solution 2&lt;/td&gt;

        &lt;td valign="top" width="80"&gt;Solution 3&lt;/td&gt;

        &lt;td valign="top" width="80"&gt;Solution 4&lt;/td&gt;

        &lt;td valign="top" width="80"&gt;Solution 5&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="80"&gt;31&lt;/td&gt;

        &lt;td valign="top" width="80"&gt;15&lt;/td&gt;

        &lt;td valign="top" width="80"&gt;29&lt;/td&gt;

        &lt;td valign="top" width="80"&gt;42&lt;/td&gt;

        &lt;td valign="top" width="80"&gt;5&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;

&lt;div align="center"&gt;&amp;#160;&lt;/div&gt;

&lt;div align="center"&gt;
  &lt;table border="1" cellspacing="0" cellpadding="2" width="522" align="center"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" width="99"&gt;individual&lt;/td&gt;

        &lt;td valign="top" width="118"&gt;restOfGeneration&lt;/td&gt;

        &lt;td valign="top" width="139"&gt;roulettePosition&lt;/td&gt;

        &lt;td valign="top" width="164"&gt;accumulatedFitness&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="98"&gt;Solution 1&lt;/td&gt;

        &lt;td valign="top" width="122"&gt;[Solution 2; Solution 3; Solution 4; Solution 5]&lt;/td&gt;

        &lt;td valign="top" width="138"&gt;51 (random)&lt;/td&gt;

        &lt;td valign="top" width="163"&gt;31&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="97"&gt;Solution 2&lt;/td&gt;

        &lt;td valign="top" width="124"&gt;[Solution 3; Solution 4; Solution 5]&lt;/td&gt;

        &lt;td valign="top" width="138"&gt;51&lt;/td&gt;

        &lt;td valign="top" width="163"&gt;46&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="97"&gt;Solution 3&lt;/td&gt;

        &lt;td valign="top" width="126"&gt;[Solution 4; Solution 5]&lt;/td&gt;

        &lt;td valign="top" width="138"&gt;51&lt;/td&gt;

        &lt;td valign="top" width="163"&gt;75 (Return Solution 3)&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;

&lt;div align="center"&gt;&amp;#160;&lt;/div&gt;

&lt;p&gt;After defining the function responsible for getting the individual at a certain position, we use a list expression in lines 12 – 15 to select the number of individuals specified by the size parameter. Within the list expression we calculate the roulette’s position by retrieving a random double and multiplying it by the total fitness. Afterwards we just yield elements returned by making calls to the previously explained function. The resulting list contains the individuals that will be ‘mated’ with the crossover algorithm in order to produce a new generation. &lt;/p&gt;

&lt;h2&gt;Crossover: Single Point Crossover&lt;/h2&gt;

&lt;p&gt;The crossover function is responsible for taking two chromosomes and ‘mating’ them. This means producing a new chromosome based on the values included in each of the chromosomes passed in to the function. One of the simplest kinds of crossover is single point crossover, which picks a certain point within the chromosome at random, called the crossover point, and takes the values from the first chromosome up to that point, and the values from the second one after that. Consider the following example with a crossover point of 3:&lt;/p&gt;

&lt;div align="center"&gt;
  &lt;table border="0" cellspacing="0" cellpadding="2" width="400" align="center"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" width="133"&gt;Father&lt;/td&gt;

        &lt;td valign="top" width="133"&gt;Mother&lt;/td&gt;

        &lt;td valign="top" width="133"&gt;Son&lt;/td&gt;
      &lt;/tr&gt;

      &lt;tr&gt;
        &lt;td valign="top" width="133"&gt;[1; 2; 3; 4; 5]&lt;/td&gt;

        &lt;td valign="top" width="133"&gt;[6; 7; 8; 9; 10]&lt;/td&gt;

        &lt;td valign="top" width="133"&gt;[1; 2; 3; 9; 10]&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;

&lt;p&gt;Crossover functions must produce chromosomes that are the same length as their parents and the parents must be of equal lengths (A restriction that usually applies to all chromosomes handled by a genetic algorithm). The code is very simple, as you can see below:&lt;/p&gt;

&lt;div style="overflow: auto" class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;#light&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;module RaineTech.AI.GeneticAlgorithms.CrossoverFunctions&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    open System&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    let SinglePointCrossover(fatherChromosome : 'a list, motherChromosome : 'a list, crossoverRate : double, randomGenerator : Random) =&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        if randomGenerator.NextDouble() &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; crossoverRate then&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;            fatherChromosome&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        else&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;            let crossoverPoint = randomGenerator.Next(fatherChromosome.Length)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;            List.mapi2 (fun i a b -&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; if i &lt;span class="kwrd"&gt;&amp;lt;&lt;/span&gt; crossoverPoint then a else b) fatherChromosome motherChromosome&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;The first two parameters are the chromosomes to mate. The third one is the crossover rate, which is the probability that we will in fact mate these two chromosomes. You can see this in action in line 6 where we generate a random double. If this random double is greater than the crossover rate, then we don’t mate the chromosomes and simply return the father. One of the main tweaking points in genetic algorithms is finding the right rates for your particular problem. Crossover rates, however, tend to be quite high (Keep in mind it’s a probability, which means it must have a value between 0.0 and 1.0).&lt;/p&gt;

&lt;p&gt;On line 9 we get a random integer that must be less than the length of the father chromosome and use it as the crossover point. Afterwards we simply use the &lt;em&gt;List.mapi2&lt;/em&gt; higher-order function which allows us to apply a given transformation to two lists at the same time. Our function simply checks if the index is less than the crossover point. If so, then it returns the value from the first list, otherwise it returns the value from the second list.&lt;/p&gt;

&lt;h2&gt;Mutation: Insertion Mutation&lt;/h2&gt;

&lt;p&gt;After we have mated our chromosomes and generated a new population, we pass them through a mutation process. Mutation functions act on one chromosome and are responsible for making very small changes on it. Whether a mutation will be performed or not depends on the mutation rate, which is usually a very small value. Once again, it is important to tweak this rate to fit your particular problem.&lt;/p&gt;

&lt;p&gt;Insertion mutation is one of the simplest mutation algorithms. It picks two points at random within the chromosome and then simply swaps the values in those positions. The code to pull this off is very simple, although since we are using immutable lists to represent our chromosomes it’s not quite as efficient as it could be if we were using a different data structure.&lt;/p&gt;

&lt;div style="overflow: auto" class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;#light&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;module RaineTech.AI.GeneticAlgorithms.MutationFunctions&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    open System&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    let InsertionMutation(chromosome : 'a list, mutationRate : double, randomGenerator : Random) =&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;        if randomGenerator.NextDouble() &lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; mutationRate then&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;            chromosome&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;        else&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;            let sourcePosition = randomGenerator.Next(chromosome.Length)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;            let targetPosition = randomGenerator.Next(chromosome.Length)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;            chromosome |&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; List.mapi (fun index element -&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;                            match index with&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;                            | x when x = sourcePosition -&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; chromosome.[targetPosition]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;                            | x when x = targetPosition -&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; chromosome.[sourcePosition]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;                            | _ -&lt;span class="kwrd"&gt;&amp;gt;&lt;/span&gt; element)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;                            &lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;As with the crossover function, we start by generating a random double to check against the mutation rate. In case it is decided a mutation will happen, we get two random numbers to act as the source and target of the swap. Both these numbers must be less than the chromosome’s length. Once we have those numbers we use the &lt;em&gt;List.mapi&lt;/em&gt; to generate the resulting chromosome. The match expression returns the element at the target position if the index is equal to the source position and vice-versa. In all other cases it simply returns the current element in the map function.&lt;/p&gt;

&lt;h2&gt;&lt;/h2&gt;

&lt;h2&gt;Coming Up Next&lt;/h2&gt;

&lt;p&gt;Now that we have a small set of operators and data structures to work with, I will show how they all come together in the form of a genetic algorithm in the next blog post in the series. I will also mention some other selection, crossover, and mutation functions that might be more useful for certain types of problems.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:ca04b7aa-2c7d-4ec1-ab5c-ca1608e539c4" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/F%23" rel="tag"&gt;F#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Genetic+Algorithms" rel="tag"&gt;Genetic Algorithms&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Artificial+Intelligence" rel="tag"&gt;Artificial Intelligence&lt;/a&gt;&lt;/div&gt;&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.raine-tech.com/blogs/jr/post/Genetic-Algorithms-in-F-Part-1-Generations-Selection-Crossover-and-Mutation.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.raine-tech.com/blogs/jr/post/Genetic-Algorithms-in-F-Part-1-Generations-Selection-Crossover-and-Mutation.aspx&amp;amp;title=Genetic Algorithms in F# Part 1: Generations, Selection, Crossover, and Mutation" target="_blank" title="DZone It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Genetic-Algorithms-in-F-Part-1-Generations-Selection-Crossover-and-Mutation.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.raine-tech.com/blogs/jr/" target="_blank" title="Technorati"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Genetic-Algorithms-in-F-Part-1-Generations-Selection-Crossover-and-Mutation.aspx&amp;amp;title=Genetic Algorithms in F# Part 1: Generations, Selection, Crossover, and Mutation" target="_blank" title="Reddit"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.raine-tech.com/blogs/jr/post/Genetic-Algorithms-in-F-Part-1-Generations-Selection-Crossover-and-Mutation.aspx&amp;amp;title=Genetic Algorithms in F# Part 1: Generations, Selection, Crossover, and Mutation" target="_blank" title="Del.icio.us"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.raine-tech.com/blogs/jr/post/Genetic-Algorithms-in-F-Part-1-Generations-Selection-Crossover-and-Mutation.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ozRoezi9sJoikfT0XE7IP9tNNM0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ozRoezi9sJoikfT0XE7IP9tNNM0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ozRoezi9sJoikfT0XE7IP9tNNM0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ozRoezi9sJoikfT0XE7IP9tNNM0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <link>http://www.raine-tech.com/blogs/jr/post/Genetic-Algorithms-in-F-Part-1-Generations-Selection-Crossover-and-Mutation.aspx</link>
      <author>jr.calzada.nospam@nospam.raine-tech.com (jr.calzada)</author>
      <comments>http://www.raine-tech.com/blogs/jr/post/Genetic-Algorithms-in-F-Part-1-Generations-Selection-Crossover-and-Mutation.aspx#comment</comments>
      <guid>http://www.raine-tech.com/blogs/jr/post.aspx?id=320de344-0dec-40c4-bab4-94eebf77f2b8</guid>
      <pubDate>Sun, 03 May 2009 03:33:35 -0600</pubDate>
      <category>Computer Science</category>
      <dc:publisher>jr.calzada</dc:publisher>
      <pingback:server>http://www.raine-tech.com/blogs/jr/pingback.axd</pingback:server>
      <pingback:target>http://www.raine-tech.com/blogs/jr/post.aspx?id=320de344-0dec-40c4-bab4-94eebf77f2b8</pingback:target>
      <slash:comments>8</slash:comments>
      <trackback:ping>http://www.raine-tech.com/blogs/jr/trackback.axd?id=320de344-0dec-40c4-bab4-94eebf77f2b8</trackback:ping>
      <wfw:comment>http://www.raine-tech.com/blogs/jr/post/Genetic-Algorithms-in-F-Part-1-Generations-Selection-Crossover-and-Mutation.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.raine-tech.com/blogs/jr/syndication.axd?post=320de344-0dec-40c4-bab4-94eebf77f2b8</wfw:commentRss>
    </item>
    <item>
      <title>Parallel Quicksort in Erlang</title>
      <description>&lt;p&gt;When I first read about processes in Erlang and how to program for concurrency within the language, I decided to write a simple parallel implementation of the well-known Quicksort algorithm as an exercise. The reason I chose this algorithm in particular is because it is, by nature, easily parallelizable. If it’s been a while since you last implemented Quicksort, here’s a short overview of what the algorithm does:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;The function receives a list of elements &lt;/li&gt;    &lt;li&gt;A pivot is chosen from this list (Usually the first element) &lt;/li&gt;    &lt;li&gt;The original list is separated into two new lists: One that contains all elements that are less than the pivot and one that contains all elements that are greater than the pivot &lt;/li&gt;    &lt;li&gt;The two new lists are sorted separately &lt;/li&gt;    &lt;li&gt;The sorted lists are joined by placing the pivot between them &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The step with the most potential for parallelization is the fourth one, since the the sorting of each of the two new lists is side-effect free and there is no need to share information. Because of this we should be able to simply sort the two lists concurrently, wait for both of them to finish, and then join them together as explained in the fifth step. &lt;/p&gt;  &lt;p&gt;I’ve chosen to keep the implementation clear and make the parallelization process explicit, in an effort to make the algorithm as understandable as possible. Keep in mind, however, that it can be cleaned up significantly by delegating the process of sorting the two lists to an auxiliary function responsible for spawning the required processes, waiting for them to finish, and simply returning the results in the order expected (A parallel implementation of the well-known ‘map’ function, an example of which can be found in “Programming Erlang” by Joe Armstrong).&lt;/p&gt;  &lt;div style="overflow: auto" class="csharpcode"&gt;   &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;-module(parallel_sorting).&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;-export([quicksort/1]).&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;quicksort([H|T]) -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    {Lesser, Greater} = separate(H, T, [], []),&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    Pid = self(),&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    LeftSortPid = spawn(fun() -&amp;gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;                            SortedList = quicksort(Lesser), Pid ! {self(), SortedList} &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;                        end),&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;    RightSortPid = spawn(fun() -&amp;gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;                            SortedList = quicksort(Greater), Pid ! {self(), SortedList} &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;                        end),&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;    receive&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;        {LeftSortPid, LeftList} -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;            receive&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;                {RightSortPid, RightList} -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;                    LeftList ++ [H|RightList]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;            end;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;        {RightSortPid, RightList} -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;            receive&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;                {LeftSortPid, LeftList} -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;                    LeftList ++ [H|RightList]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;            end&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;    end;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;quicksort([]) -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;    [].&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;    &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;separate(E, [H|T], Lesser, Greater) -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; H &amp;lt; E -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;        separate(E, T, [H|Lesser], Greater);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;    &lt;span class="kwrd"&gt;true&lt;/span&gt; -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;        separate(E, T, Lesser, [H|Greater])&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;    end;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;separate(_, [], Lesser, Greater) -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;    {Lesser, Greater}.&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;You can see we match calls to the &lt;em&gt;quicksort&lt;/em&gt; function against two cases: A list with elements and an empty list. In case of receiving an empty list we just return an empty list, there’s no sorting to be performed. If we receive a list with elements instead, we start by separating it into the two lists outlined in step three of the algorithm description. The head of the list is chosen as the pivot, which is the first argument to the &lt;em&gt;separate&lt;/em&gt; function. The other three arguments are the remaining elements in the list and accumulators for what will eventually be the new lists. I will not explain the &lt;em&gt;separate&lt;/em&gt; function since it should be fairly self-explanatory.&lt;/p&gt;

&lt;p&gt;Once we have the lists, we spawn new processes that will take care of sorting them. We must first store the id of the current process, so that we can, eventually, send a message to it containing the sorted lists. The spawn function takes as its argument another function that will start by calling &lt;em&gt;quicksort&lt;/em&gt; on one of the lists and then send a message to the parent process with the sorted list. We do the same thing for both the list with the lesser elements and the list with the greater elements.&lt;/p&gt;

&lt;p&gt;Once these processes are spawned, the function needs to wait for the results, which happens between lines 13-24. This is the part of the code that could be cleaned up the most, but I believe it is easier to see what’s going on in its current form. We start a receive expression which will wait for messages to arrive. Since we cannot determine which of the two lists will be sorted first, we must consider both cases, which is what we specify when waiting for either a message from &lt;em&gt;LeftSortPid&lt;/em&gt; or &lt;em&gt;RightSortPid. &lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Once we receive either of the sorted sub-lists, we wait to receive the missing one. You can see this in lines 15-18 and 20-23. After receiving the other list we join them as mentioned in the fifth step of the algorithm outline above. This will be the result of the receive expression and, therefore, the result of the &lt;em&gt;quicksort&lt;/em&gt; function.&lt;/p&gt;

&lt;p&gt;On a final note, here is what the code would look like if we had an auxiliary &lt;em&gt;parallel_map&lt;/em&gt; function:&lt;/p&gt;

&lt;div style="overflow: auto" class="csharpcode"&gt;
  &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;quicksort([H|T]) -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    {Lesser, Greater} = separate(H, T, [], []),&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    [LesserSorted, GreaterSorted] = parallel_map(fun quicksort/1, [Lesser, Greater])&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    LesserSorted ++ [H] ++ GreaterSorted&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;    end;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;quicksort([]) -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    [].&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;Which goes to show the effect higher-order functions can have on your code.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:b8a6e5a3-de0f-4bfd-94cd-82b745e180ee" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Quicksort" rel="tag"&gt;Quicksort&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Erlang" rel="tag"&gt;Erlang&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Concurrency" rel="tag"&gt;Concurrency&lt;/a&gt;&lt;/div&gt;&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.raine-tech.com/blogs/jr/post/Parallel-Quicksort-in-Erlang.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.raine-tech.com/blogs/jr/post/Parallel-Quicksort-in-Erlang.aspx&amp;amp;title=Parallel Quicksort in Erlang" target="_blank" title="DZone It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Parallel-Quicksort-in-Erlang.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.raine-tech.com/blogs/jr/" target="_blank" title="Technorati"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Parallel-Quicksort-in-Erlang.aspx&amp;amp;title=Parallel Quicksort in Erlang" target="_blank" title="Reddit"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.raine-tech.com/blogs/jr/post/Parallel-Quicksort-in-Erlang.aspx&amp;amp;title=Parallel Quicksort in Erlang" target="_blank" title="Del.icio.us"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.raine-tech.com/blogs/jr/post/Parallel-Quicksort-in-Erlang.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/eFQag4y35LBBCaxhyHKiXnlnBIc/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eFQag4y35LBBCaxhyHKiXnlnBIc/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/eFQag4y35LBBCaxhyHKiXnlnBIc/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/eFQag4y35LBBCaxhyHKiXnlnBIc/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <link>http://www.raine-tech.com/blogs/jr/post/Parallel-Quicksort-in-Erlang.aspx</link>
      <author>jr.calzada.nospam@nospam.raine-tech.com (jr.calzada)</author>
      <comments>http://www.raine-tech.com/blogs/jr/post/Parallel-Quicksort-in-Erlang.aspx#comment</comments>
      <guid>http://www.raine-tech.com/blogs/jr/post.aspx?id=9601fb3c-c295-4106-963a-53e9333d810c</guid>
      <pubDate>Fri, 01 May 2009 03:39:18 -0600</pubDate>
      <category>Computer Science</category>
      <category>Development</category>
      <dc:publisher>jr.calzada</dc:publisher>
      <pingback:server>http://www.raine-tech.com/blogs/jr/pingback.axd</pingback:server>
      <pingback:target>http://www.raine-tech.com/blogs/jr/post.aspx?id=9601fb3c-c295-4106-963a-53e9333d810c</pingback:target>
      <slash:comments>14</slash:comments>
      <trackback:ping>http://www.raine-tech.com/blogs/jr/trackback.axd?id=9601fb3c-c295-4106-963a-53e9333d810c</trackback:ping>
      <wfw:comment>http://www.raine-tech.com/blogs/jr/post/Parallel-Quicksort-in-Erlang.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.raine-tech.com/blogs/jr/syndication.axd?post=9601fb3c-c295-4106-963a-53e9333d810c</wfw:commentRss>
    </item>
    <item>
      <title>Scoping in Groovy Closures</title>
      <description>&lt;p&gt;As I’ve been playing around with Groovy something that immediately caught my attention was the way scoping worked within closures. I came across a code example involving using closures within double quoted strings in “Programming Groovy” by Venkat Subramaniam that made it seem, for a moment, that closures actually had dynamic scoping. Dynamic scoping, in contrast to static or lexical scoping, means that the bindings in the scope of a block of code is determined not according to the source code, as is the case with most popular programming languages, but during evaluation. This pretty much means that you can have a function that uses a variable named ‘a’ and, even if you haven’t defined that variable in its enclosing lexical scope, you can call that function within another block of code that defines the aforementioned variable. The variable inside the function will actually refer to the one inside the block of code that made the call to the function.&lt;/p&gt;  &lt;p&gt;Take a look at this Groovy code and you will see something similar happening:&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;def closure = {-&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;    x&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;}&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;def closure2 = {v -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    x = v&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    println closure()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;}&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;closure2(5)&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;If you execute this script you will get ‘5’ as an output, which might not be that surprising. Consider, however, that when the closure is defined in lines 1-3, it’s referring to a non-existent variable ‘x’. Lexical scoping would consider this an error, since there is no variable named ‘x’ in the code’s enclosing scope. Somehow, the closure is actually referencing the value assigned to x within ‘closure2’. One could take a look at this and think this is dynamic scoping in action, and that’s what I thought at first too… Then I started digging a bit more.&lt;/p&gt;

&lt;p&gt;Turns out that if you delete line 6 and then rename the parameter to ‘closure2’ in line 5 from ‘v’ to ‘x’ the code doesn’t work. This makes it clear that it’s not really dynamic scoping what we’re dealing with here, since there’s still a variable named ‘x’ in the scope of the block of code that is making the call to the first closure. What’s really happening is one of the main characteristics of closures in Groovy.&lt;/p&gt;

&lt;p&gt;Every closure in Groovy has three special members: ‘this’, ‘owner’, and ‘delegate’. The ‘this’ member is set to the object in which the closure was defined and the ‘owner’ and ‘delegate’ members are set to either a surrounding closure, if the closure is defined within another closure, or the same as the ‘this’ member. When referring to a variable inside the closure, Groovy will first look for the variable in the closure’s lexical scope, which is typical static scoping at its finest. If it doesn’t find the variable, however, it will look for it in the three members I just mentioned. In my current Groovy Console session it shows that ‘ConsoleScript50@c21095’ is the value of all three members. We already determined the closure isn’t really getting the value of ‘x’ from the scope of the call, since changing the closure’s parameter name from ‘v’ to ‘x’ doesn’t work. You might also be thinking that there is no ‘x’ variable defined in the global scope either. So, what’s going on?&lt;/p&gt;

&lt;p&gt;It turns out that the assignment in line 6 is actually creating the ‘x’ variable in the global scope. When we assign to ‘x’ Groovy apparently looks for the symbol in the ‘this’ member which happens to be the script environment, which pretty much always allows defining new variables. So, the statement in line 6 actually creates the variable ‘x’ in the global scope and assigns to it the value passed in to the closure. You can verify this by adding a ‘println x’ statement after line 10 and you’ll see the number ‘5’ printed a second time. When the first closure is finally executed it eventually looks for the variable ‘x’ in its ‘this’ member, which is the ‘ConsoleScript50@c21095’ object, where the previous call just created a variable named ‘x’. This will allow the closure to find the symbol and get its value, which explains why this code works as it does.&lt;/p&gt;

&lt;p&gt;The first time I came across this it struck me as somewhat obscure, but once you know what’s going on things start making sense and you realize this is actually a nifty feature. It’s also, in a way, the backbone of several meta-programming strategies in Groovy. But that’ll be a topic for another day.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:d3adb42b-b3b9-4e17-8a3c-965df69bdf10" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Groovy" rel="tag"&gt;Groovy&lt;/a&gt;&lt;/div&gt;&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.raine-tech.com/blogs/jr/post/Scoping-in-Groovy-Closures.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.raine-tech.com/blogs/jr/post/Scoping-in-Groovy-Closures.aspx&amp;amp;title=Scoping in Groovy Closures" target="_blank" title="DZone It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Scoping-in-Groovy-Closures.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.raine-tech.com/blogs/jr/" target="_blank" title="Technorati"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Scoping-in-Groovy-Closures.aspx&amp;amp;title=Scoping in Groovy Closures" target="_blank" title="Reddit"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.raine-tech.com/blogs/jr/post/Scoping-in-Groovy-Closures.aspx&amp;amp;title=Scoping in Groovy Closures" target="_blank" title="Del.icio.us"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.raine-tech.com/blogs/jr/post/Scoping-in-Groovy-Closures.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/uDX_QvITz2YW98RUnVQzvdODFoA/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/uDX_QvITz2YW98RUnVQzvdODFoA/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/uDX_QvITz2YW98RUnVQzvdODFoA/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/uDX_QvITz2YW98RUnVQzvdODFoA/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <link>http://www.raine-tech.com/blogs/jr/post/Scoping-in-Groovy-Closures.aspx</link>
      <author>jr.calzada.nospam@nospam.raine-tech.com (jr.calzada)</author>
      <comments>http://www.raine-tech.com/blogs/jr/post/Scoping-in-Groovy-Closures.aspx#comment</comments>
      <guid>http://www.raine-tech.com/blogs/jr/post.aspx?id=6eb15da1-5527-440c-a186-b19c2e067067</guid>
      <pubDate>Mon, 27 Apr 2009 03:23:51 -0600</pubDate>
      <category>Computer Science</category>
      <category>Development</category>
      <dc:publisher>jr.calzada</dc:publisher>
      <pingback:server>http://www.raine-tech.com/blogs/jr/pingback.axd</pingback:server>
      <pingback:target>http://www.raine-tech.com/blogs/jr/post.aspx?id=6eb15da1-5527-440c-a186-b19c2e067067</pingback:target>
      <slash:comments>4</slash:comments>
      <trackback:ping>http://www.raine-tech.com/blogs/jr/trackback.axd?id=6eb15da1-5527-440c-a186-b19c2e067067</trackback:ping>
      <wfw:comment>http://www.raine-tech.com/blogs/jr/post/Scoping-in-Groovy-Closures.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.raine-tech.com/blogs/jr/syndication.axd?post=6eb15da1-5527-440c-a186-b19c2e067067</wfw:commentRss>
    </item>
    <item>
      <title>Pass by reference… It’s really not that hard, people!</title>
      <description>&lt;p&gt;So, I was checking my RSS feeds and came across &lt;a href="http://www.khelll.com/blog/ruby/c-passes-by-reference-java-and-ruby-dont/" target="_blank"&gt;this&lt;/a&gt; post that explains, in very simple code, how Ruby and Java don’t really pass method arguments by reference, but do it by value, in contrast to what you can do in C++ by defining parameters as ‘pass by reference’. &lt;/p&gt;  &lt;p&gt;The post is clear and well written, so I never really expected the surprisingly huge amount of crap I found in the comments section. I mean, really, this is not rocket science!&lt;/p&gt;  &lt;p&gt;What’s happening in Java and Ruby is actually pretty simple: Let’s say you have a reference to a shiny object. This reference is, for all intents and purposes, a pointer to the actual instance that was allocated in the heap. When you pass this reference to a method, you are actually creating a copy of the reference, which is the argument the method receives. Yes, the method is still receiving a reference, but it’s not the original reference, it’s a copy. Therefore, the reference was passed by value. It’s the same thing that happens with all primitive types: You pass them to a method and the method receives a copy because they were passed by value. The only difference is that you’re creating a copy of a reference, so if you make any modifications to the object within the method those changes will affect the original object. This is because even though you’re acting on a copy of the original reference, they’re both still references to the same real object that lives somewhere in the heap. The point is that a copy of the argument is being made.&lt;/p&gt;  &lt;p&gt;With C++’s pass by reference things are different. When you pass by reference in C++ you are not creating a copy of the reference, but you are actually passing that same reference to the method. No copy of the reference is being created. If a reference is a pointer to an object, then what the method is really receiving as an argument is a pointer to the reference (A pointer to a pointer). This is, of course, hidden from the programmer by the reference’s particular syntax, which is probably why so many programmers struggle with them and never fully understand them. But still, with an explanation as clear as the one in the post mentioned at the beginning, I just can’t believe how many people kept trying to spin it around in some misguided attempt to be right. This is not some great, new discovery, it’s been widely explored in the existing literature and is something anyone with a firm grasp on programming languages should get.&lt;/p&gt;  &lt;p&gt;By the way, C# actually supports both. By using the ‘ref’ keyword you can actually pass a parameter by reference, which is why any assignments made to a ‘ref’ parameter affect the original value, which means the swap function implemented in the original post would actually work as intended. One way to look at is that references are really value types (I’m referring to the reference itself, not the object to which it points), so when you use a ‘ref’ parameter you are just passing a reference to the reference, just as you would with an int or any other primitive.&lt;/p&gt;  &lt;p&gt;So, now that I have that off my chest, I’ll go back to doing something a bit more productive.&lt;/p&gt;  &lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:f6405bbb-47bd-4337-bc1b-ea565e5ee39f" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/C%23" rel="tag"&gt;C#&lt;/a&gt;,&lt;a href="http://technorati.com/tags/C%2b%2b" rel="tag"&gt;C++&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Ruby" rel="tag"&gt;Ruby&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Java" rel="tag"&gt;Java&lt;/a&gt;&lt;/div&gt;&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.raine-tech.com/blogs/jr/post/Pass-by-referencee280a6-Ite28099s-really-not-that-hard-people!.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.raine-tech.com/blogs/jr/post/Pass-by-referencee280a6-Ite28099s-really-not-that-hard-people!.aspx&amp;amp;title=Pass by reference… It’s really not that hard, people!" target="_blank" title="DZone It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Pass-by-referencee280a6-Ite28099s-really-not-that-hard-people!.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.raine-tech.com/blogs/jr/" target="_blank" title="Technorati"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Pass-by-referencee280a6-Ite28099s-really-not-that-hard-people!.aspx&amp;amp;title=Pass by reference… It’s really not that hard, people!" target="_blank" title="Reddit"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.raine-tech.com/blogs/jr/post/Pass-by-referencee280a6-Ite28099s-really-not-that-hard-people!.aspx&amp;amp;title=Pass by reference… It’s really not that hard, people!" target="_blank" title="Del.icio.us"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.raine-tech.com/blogs/jr/post/Pass-by-referencee280a6-Ite28099s-really-not-that-hard-people!.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/4DpWx-7SzFwQ-QLi-kLPWUGr5lE/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4DpWx-7SzFwQ-QLi-kLPWUGr5lE/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/4DpWx-7SzFwQ-QLi-kLPWUGr5lE/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4DpWx-7SzFwQ-QLi-kLPWUGr5lE/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <link>http://www.raine-tech.com/blogs/jr/post/Pass-by-referencee280a6-Ite28099s-really-not-that-hard-people!.aspx</link>
      <author>jr.calzada.nospam@nospam.raine-tech.com (jr.calzada)</author>
      <comments>http://www.raine-tech.com/blogs/jr/post/Pass-by-referencee280a6-Ite28099s-really-not-that-hard-people!.aspx#comment</comments>
      <guid>http://www.raine-tech.com/blogs/jr/post.aspx?id=52eaa3fb-ea9d-4a39-b595-434f276b5a0e</guid>
      <pubDate>Fri, 24 Apr 2009 03:38:54 -0600</pubDate>
      <category>Development</category>
      <dc:publisher>jr.calzada</dc:publisher>
      <pingback:server>http://www.raine-tech.com/blogs/jr/pingback.axd</pingback:server>
      <pingback:target>http://www.raine-tech.com/blogs/jr/post.aspx?id=52eaa3fb-ea9d-4a39-b595-434f276b5a0e</pingback:target>
      <slash:comments>6</slash:comments>
      <trackback:ping>http://www.raine-tech.com/blogs/jr/trackback.axd?id=52eaa3fb-ea9d-4a39-b595-434f276b5a0e</trackback:ping>
      <wfw:comment>http://www.raine-tech.com/blogs/jr/post/Pass-by-referencee280a6-Ite28099s-really-not-that-hard-people!.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.raine-tech.com/blogs/jr/syndication.axd?post=52eaa3fb-ea9d-4a39-b595-434f276b5a0e</wfw:commentRss>
    </item>
    <item>
      <title>Wave File Information Decoder in Erlang</title>
      <description>&lt;p&gt;One of the features of Erlang that immediately caught my attention was the bit manipulation syntax. Despite whatever gripes I may have with other parts of the language’s syntax, this stands out as a gem. So, as en exercise to myself I decided to write a set of functions that would allow me to extract some information from a wave file. I wanted the function to return a record that contained the file’s size followed by a list of the RIFF chunks the file contains (If you don’t know what a chunk refers to, it’s how wave files are organized. You can read more &lt;a href="http://www.sonicspot.com/guide/wavefiles.html" target="_blank"&gt;here&lt;/a&gt;). The chunks would be represented by a tuple that contained the chunk’s ID, its size and its data. The only exception would be the format chunk, which would be represented by a record that nicely held all its relevant values.&lt;/p&gt;  &lt;p&gt;Since the chunks in wave files are nicely structured, Erlang’s bit manipulation syntax was a perfect fit for the task, allowing me to easily specify which bits made up which values. Here’s the source code:&lt;/p&gt;  &lt;div class="csharpcode" style="overflow:auto"&gt;   &lt;pre&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;-module(wave_tools).&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;-export([decode_wave_file/1]).&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;-record(wave_file, {fileSize, chunks}).&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;-record(fmt_chunk, {id = &lt;span class="str"&gt;&amp;quot;fmt &amp;quot;&lt;/span&gt;, size, compression_code, channels, sample_rate, bytes_per_second, block_align, significant_bits_per_sample, extra_format_bytes}).&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;decode_wave_file(&amp;lt;&amp;lt;&lt;span class="str"&gt;&amp;quot;RIFF&amp;quot;&lt;/span&gt;, Size:32/little, &lt;span class="str"&gt;&amp;quot;WAVE&amp;quot;&lt;/span&gt;, Data/binary&amp;gt;&amp;gt;) -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;  Chunks = extract_chunks(Data),&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;  #wave_file{fileSize = Size + 8, chunks = Chunks}.&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;  &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;extract_chunks(&amp;lt;&amp;lt;ID:4/binary, Size:32/little, ChunkData:Size/binary, Rest/binary&amp;gt;&amp;gt;) -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;  Chunk = build_chunk(ID, Size, ChunkData),&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;  &lt;span class="kwrd"&gt;if&lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;    Size rem 2 =:= 0 -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;      [Chunk|extract_chunks(Rest)];&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;    &lt;span class="kwrd"&gt;true&lt;/span&gt; -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;      &amp;lt;&amp;lt;_:1/binary, Rest2/binary&amp;gt;&amp;gt; = Rest,&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;      [Chunk|extract_chunks(Rest2)]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;  end;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;extract_chunks(&amp;lt;&amp;lt;&amp;gt;&amp;gt;) -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;  [].&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;build_chunk(ID, Size, Data) -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  24:  &lt;/span&gt;  &lt;span class="kwrd"&gt;case&lt;/span&gt; ID of&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  25:  &lt;/span&gt;    &amp;lt;&amp;lt;&lt;span class="str"&gt;&amp;quot;fmt &amp;quot;&lt;/span&gt;&amp;gt;&amp;gt; -&amp;gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  26:  &lt;/span&gt;      extract_fmt_chunk(Size, Data);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  27:  &lt;/span&gt;    _ -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  28:  &lt;/span&gt;      {ID, Size, Data}&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  29:  &lt;/span&gt;  end.&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  30:  &lt;/span&gt;  &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  31:  &lt;/span&gt;extract_fmt_chunk(Size, &amp;lt;&amp;lt;CompressionCode:16/little, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  32:  &lt;/span&gt;                          Channels:16/little,&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  33:  &lt;/span&gt;                          SampleRate:32/little,&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  34:  &lt;/span&gt;                          BytesPerSecond:32/little,&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  35:  &lt;/span&gt;                          BlockAlign:16/little,&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  36:  &lt;/span&gt;                          SignificantBitsPerSample:16/little,&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  37:  &lt;/span&gt;                          Rest/binary&amp;gt;&amp;gt;) -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  38:  &lt;/span&gt;  ExtraFormatBytes = &lt;span class="kwrd"&gt;case&lt;/span&gt; Rest of&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  39:  &lt;/span&gt;                       &amp;lt;&amp;lt;NumberExtraFormatBytes:16/little, EFB:NumberExtraFormatBytes/binary&amp;gt;&amp;gt; -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  40:  &lt;/span&gt;                         EFB;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  41:  &lt;/span&gt;                       _ -&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  42:  &lt;/span&gt;                         &amp;lt;&amp;lt;&amp;gt;&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  43:  &lt;/span&gt;                      end,&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  44:  &lt;/span&gt;  #fmt_chunk{size = Size, compression_code = CompressionCode, channels = Channels, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  45:  &lt;/span&gt;            sample_rate = SampleRate, bytes_per_second = BytesPerSecond, block_align = BlockAlign, &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  46:  &lt;/span&gt;            significant_bits_per_sample = SignificantBitsPerSample, extra_format_bytes = ExtraFormatBytes}.&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;The first few lines are very simple, the only interesting parts being the record definitions. The only function being exported is ‘decode_wavefile’ which relies on the other functions to correctly decode the data passed in. You can see the function argument is matched against a bit pattern which specifies that the data must contain the letters “RIFF” followed by four bytes that represent the file size (minus eight). After the file size we expect the letters “WAVE” and we wrap up by binding the ‘Rest’ variable to the rest of the data. Once this is done the function calls the ‘extract_chunks’ function and then fills out the ‘wave_file’ record.&lt;/p&gt;

&lt;p&gt;The ‘extract_chunks’ function that starts in line 11 is a bit more interesting. We use some more matching to extract the chunk’s ID, its size, the data, and whatever’s left. We want to capture the size as an integer in order to use it cleanly when specifying how many bytes to match for the chunk’s data. Wave files are stored in little-endian order, so we specify that to retrieve the correct result. With this data extracted we call the build_chunk function and then check whether the chunk’s size is even. Since chunks in a wave file have to be word-aligned, whenever a chunk’s size is odd we need to skip a byte that is used as padding after the data. After figuring that out we just build a cons cell that contains the retrieved chunk and whatever chunks can be extracted from the rest of the data. This could be turned into a tail-recursive call by using an accumulator argument, but I think this is a bit more readable. We must also take into account that eventually there will be no more data from which to extract a chunk, in which case we return the empty list.&lt;/p&gt;

&lt;p&gt;The ‘build_chunk’ function simply checks whether the ID matches “fmt “ or not. If it doesn’t then we just build a tuple containing the ID, the size, and the data; if it does then we call the ‘extract_fmt_chunk’ function which performs a pretty long, though straightforward, match against the data. We start by extracting the compression code, the number of channels, the sample rate, the average bytes per second, the block alignment, the number of significant bits per sample, and whatever information is left. Format chunks can sometimes hold additional format bytes, but this field is entirely optional. Because of this, we match ‘Rest’ to either 2 bytes that specify a size followed by the adequate number of bytes which would be the extra format bytes, or against nothing in which case there are no extra format bytes. Once that’s done we’ve extracted all of the information in the chunk and simply proceed to filling out the record.&lt;/p&gt;

&lt;p&gt;As you can see, most of the code is rather straightforward if you’re familiar with the syntax. The bit manipulation syntax seems to be extremely powerful and impressive, making tasks that would be daunting or tedious in other programming languages entirely trivial. I’ll keep playing around with Erlang a bit more, maybe if I come across enough stuff that’s as good as this I’ll get over some not-so-nice aspects of its syntax.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9dec3531-9ff0-4125-8693-22e707caaa99" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Erlang" rel="tag"&gt;Erlang&lt;/a&gt;&lt;/div&gt;&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.raine-tech.com/blogs/jr/post/Wave-File-Information-Decoder-in-Erlang.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.raine-tech.com/blogs/jr/post/Wave-File-Information-Decoder-in-Erlang.aspx&amp;amp;title=Wave File Information Decoder in Erlang" target="_blank" title="DZone It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Wave-File-Information-Decoder-in-Erlang.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.raine-tech.com/blogs/jr/" target="_blank" title="Technorati"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Wave-File-Information-Decoder-in-Erlang.aspx&amp;amp;title=Wave File Information Decoder in Erlang" target="_blank" title="Reddit"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.raine-tech.com/blogs/jr/post/Wave-File-Information-Decoder-in-Erlang.aspx&amp;amp;title=Wave File Information Decoder in Erlang" target="_blank" title="Del.icio.us"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.raine-tech.com/blogs/jr/post/Wave-File-Information-Decoder-in-Erlang.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/RHIadRqxLZ4bDNu3d_sIxfd_aWU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RHIadRqxLZ4bDNu3d_sIxfd_aWU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/RHIadRqxLZ4bDNu3d_sIxfd_aWU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/RHIadRqxLZ4bDNu3d_sIxfd_aWU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <link>http://www.raine-tech.com/blogs/jr/post/Wave-File-Information-Decoder-in-Erlang.aspx</link>
      <author>jr.calzada.nospam@nospam.raine-tech.com (jr.calzada)</author>
      <comments>http://www.raine-tech.com/blogs/jr/post/Wave-File-Information-Decoder-in-Erlang.aspx#comment</comments>
      <guid>http://www.raine-tech.com/blogs/jr/post.aspx?id=07a4f0d5-06ae-4969-9984-d8dfa2b01cc1</guid>
      <pubDate>Mon, 20 Apr 2009 07:15:48 -0600</pubDate>
      <category>Development</category>
      <dc:publisher>jr.calzada</dc:publisher>
      <pingback:server>http://www.raine-tech.com/blogs/jr/pingback.axd</pingback:server>
      <pingback:target>http://www.raine-tech.com/blogs/jr/post.aspx?id=07a4f0d5-06ae-4969-9984-d8dfa2b01cc1</pingback:target>
      <slash:comments>7</slash:comments>
      <trackback:ping>http://www.raine-tech.com/blogs/jr/trackback.axd?id=07a4f0d5-06ae-4969-9984-d8dfa2b01cc1</trackback:ping>
      <wfw:comment>http://www.raine-tech.com/blogs/jr/post/Wave-File-Information-Decoder-in-Erlang.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.raine-tech.com/blogs/jr/syndication.axd?post=07a4f0d5-06ae-4969-9984-d8dfa2b01cc1</wfw:commentRss>
    </item>
    <item>
      <title>Wedding Invitations Are Here!</title>
      <description>&lt;p&gt;Things have been a bit busy lately and I also have been feeling a bit under the weather so I haven’t been able to post as often as I would’ve liked. Aside from that, things have been progressing smoothly and hopefully will continue to do so for the next couple of months.&lt;/p&gt;  &lt;p&gt;The wedding is now less than a month away and am pretty excited. We finally received the wedding invitations, after a slight delay, and they should be going out in the next couple of days which means people will stop asking me why I didn’t invite them. They came out pretty great and it’s fair to say the delay was worth it.&lt;/p&gt;  &lt;p&gt;On a more blog-related note, I do have a few blog posts waiting to be finished and published. I’m working on a post about formal methods and how we can include certain aspects of them into our typical development workflows in a way that we can reap many of their benefits, yet without having to go to extremes. I’m also working on a blog post that talks about F#’s asynchronous workflows, which is a wonderful feature when working with asynchronous methods in .NET. &lt;/p&gt;  &lt;p&gt;Hopefully I’ll be able to get those two posts finished before the end of next week. I’ve been trying to do at least eight posts per month, but it looks like I’m not gonna make it this time. Hopefully I’ll get some time next month to make up for that.&lt;/p&gt;&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.raine-tech.com/blogs/jr/post/Wedding-Invitations-Are-Here!.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.raine-tech.com/blogs/jr/post/Wedding-Invitations-Are-Here!.aspx&amp;amp;title=Wedding Invitations Are Here!" target="_blank" title="DZone It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Wedding-Invitations-Are-Here!.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.raine-tech.com/blogs/jr/" target="_blank" title="Technorati"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Wedding-Invitations-Are-Here!.aspx&amp;amp;title=Wedding Invitations Are Here!" target="_blank" title="Reddit"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.raine-tech.com/blogs/jr/post/Wedding-Invitations-Are-Here!.aspx&amp;amp;title=Wedding Invitations Are Here!" target="_blank" title="Del.icio.us"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.raine-tech.com/blogs/jr/post/Wedding-Invitations-Are-Here!.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/jBNSPg5df2-jKpNLBCxCFIxVDH4/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jBNSPg5df2-jKpNLBCxCFIxVDH4/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/jBNSPg5df2-jKpNLBCxCFIxVDH4/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/jBNSPg5df2-jKpNLBCxCFIxVDH4/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <link>http://www.raine-tech.com/blogs/jr/post/Wedding-Invitations-Are-Here!.aspx</link>
      <author>jr.calzada.nospam@nospam.raine-tech.com (jr.calzada)</author>
      <comments>http://www.raine-tech.com/blogs/jr/post/Wedding-Invitations-Are-Here!.aspx#comment</comments>
      <guid>http://www.raine-tech.com/blogs/jr/post.aspx?id=07a84c28-0d13-4ae5-8c69-e18ecb2007b7</guid>
      <pubDate>Sun, 19 Apr 2009 01:23:00 -0600</pubDate>
      <category>Personal</category>
      <dc:publisher>jr.calzada</dc:publisher>
      <pingback:server>http://www.raine-tech.com/blogs/jr/pingback.axd</pingback:server>
      <pingback:target>http://www.raine-tech.com/blogs/jr/post.aspx?id=07a84c28-0d13-4ae5-8c69-e18ecb2007b7</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.raine-tech.com/blogs/jr/trackback.axd?id=07a84c28-0d13-4ae5-8c69-e18ecb2007b7</trackback:ping>
      <wfw:comment>http://www.raine-tech.com/blogs/jr/post/Wedding-Invitations-Are-Here!.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.raine-tech.com/blogs/jr/syndication.axd?post=07a84c28-0d13-4ae5-8c69-e18ecb2007b7</wfw:commentRss>
    </item>
    <item>
      <title>WPF Dispatcher Decorator in IronPython</title>
      <description>&lt;p&gt;Lately I’ve been working on an application hosting environment that dynamically loads applications written in IronPython and provides them with certain infrastructure. Windows Presentation Foundation is being used throughout and hosted applications are required to provide, at least, two classes: An application controller and a user interface. The application controller class will receive an instance of the application’s UI from the hosting environment and from that instance it can decide which events to handle and call methods on the UI that perform certain modifications. In one of the applications I’m working on, most of the actions taken by the controller are in response to asynchronous communications through a network, which means that the code will actually be executing on a different thread (The callbacks passed to the BeginSomething methods in the .NET framework execute in another thread to avoid blocking the main thread, something essential in UI applications).&lt;/p&gt;  &lt;p&gt;If you have some experience with WPF, you may know that only the UI thread can make modifications to the UI and that if you want to make changes from another thread, then you need to use a Dispatcher object to which you pass a delegate containing the code to execute. This code will, eventually, execute in the UI thread. There are no surprises when doing this from IronPython, fortunately, but I thought it would be nice to have a way to take care of this issue without adding unnecessary code to my methods. That’s where decorators come in. &lt;/p&gt;  &lt;p&gt;What I wanted was a decorator that replaced functions with equivalents wrapped in a Dispatcher. To pull this off we need to define a delegate since the Dispatcher.Invoke call receives an instance of System.Delegate. We need to make this delegate available to IronPython, which means adding a reference to whatever assembly owns it and then importing it from its namespace. I named my delegate DispatcherDelegate and will, from now on, infer its available when showing the code. In my case, the dispatcher object itself was also being made available by the hosting environment in a variable named “__uiDispatcher__”. Your best bet is to store the dispatcher in the global scope inside the module that needs to use the decorator.&lt;/p&gt;  &lt;p&gt;The first logical version of the decorator code is probably as follows:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;def uiModifier(function):
  def dispatchedFunction(*args):
    __uiDispatcher__.Invoke(DispatcherPriority.Render, DispatcherDelegate(function), *args)
  &lt;span class="kwrd"&gt;return&lt;/span&gt; dispatchedFunction&lt;/pre&gt;

&lt;p&gt;If you do this, however, IronPython will choke with a ParameterMiscount exception. For some reason the multiple arguments are not extended correctly when passing them in place of the ‘params’ parameter. The solution is simply to use another internal function that invokes the function passed in by closing over the parameters and then use the dispatcher to invoke this additional internal function that takes no parameters. The result is the following:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;def uiModifier(function):
  def dispatchedFunction(*args):
    def inner():
      function(*args)
    __uiDispatcher__.Invoke(DispatcherPriority.Render, DispatcherDelegate(inner))
  &lt;span class="kwrd"&gt;return&lt;/span&gt; dispatchedFunction&lt;/pre&gt;

&lt;p&gt;This solves the problem wonderfully and allows me to simply write “@uiModifier” over any function that needs to work on the UI from a different thread. This little function can be easily extended to allow the decorator to take the DispatcherPriority as an argument, but I’ll leave that one out for now. In the case of my project, this significantly improves the development experience from the hosted applications’ point of view. I hated those few extra lines that had nothing to do with what I wanted to do and was delighted to find them reduced to a simple line at the top of the relevant functions. I hope you’ll find this useful as well.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:d89f3103-8080-426c-9b9f-81fa41b47650" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/IronPython" rel="tag"&gt;IronPython&lt;/a&gt;,&lt;a href="http://technorati.com/tags/WPF" rel="tag"&gt;WPF&lt;/a&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.raine-tech.com/blogs/jr/post/WPF-Dispatcher-Decorator-in-IronPython.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.raine-tech.com/blogs/jr/post/WPF-Dispatcher-Decorator-in-IronPython.aspx&amp;amp;title=WPF Dispatcher Decorator in IronPython" target="_blank" title="DZone It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.raine-tech.com/blogs/jr/post/WPF-Dispatcher-Decorator-in-IronPython.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.raine-tech.com/blogs/jr/" target="_blank" title="Technorati"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.raine-tech.com/blogs/jr/post/WPF-Dispatcher-Decorator-in-IronPython.aspx&amp;amp;title=WPF Dispatcher Decorator in IronPython" target="_blank" title="Reddit"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.raine-tech.com/blogs/jr/post/WPF-Dispatcher-Decorator-in-IronPython.aspx&amp;amp;title=WPF Dispatcher Decorator in IronPython" target="_blank" title="Del.icio.us"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.raine-tech.com/blogs/jr/post/WPF-Dispatcher-Decorator-in-IronPython.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/ihhMlBlYYjLCWS0pFpAvVPmc36Y/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ihhMlBlYYjLCWS0pFpAvVPmc36Y/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/ihhMlBlYYjLCWS0pFpAvVPmc36Y/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/ihhMlBlYYjLCWS0pFpAvVPmc36Y/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <link>http://www.raine-tech.com/blogs/jr/post/WPF-Dispatcher-Decorator-in-IronPython.aspx</link>
      <author>jr.calzada.nospam@nospam.raine-tech.com (jr.calzada)</author>
      <comments>http://www.raine-tech.com/blogs/jr/post/WPF-Dispatcher-Decorator-in-IronPython.aspx#comment</comments>
      <guid>http://www.raine-tech.com/blogs/jr/post.aspx?id=26ed3b92-b706-4242-b007-4f62bcc500ef</guid>
      <pubDate>Wed, 08 Apr 2009 14:32:46 -0600</pubDate>
      <category>Development</category>
      <dc:publisher>jr.calzada</dc:publisher>
      <pingback:server>http://www.raine-tech.com/blogs/jr/pingback.axd</pingback:server>
      <pingback:target>http://www.raine-tech.com/blogs/jr/post.aspx?id=26ed3b92-b706-4242-b007-4f62bcc500ef</pingback:target>
      <slash:comments>11</slash:comments>
      <trackback:ping>http://www.raine-tech.com/blogs/jr/trackback.axd?id=26ed3b92-b706-4242-b007-4f62bcc500ef</trackback:ping>
      <wfw:comment>http://www.raine-tech.com/blogs/jr/post/WPF-Dispatcher-Decorator-in-IronPython.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.raine-tech.com/blogs/jr/syndication.axd?post=26ed3b92-b706-4242-b007-4f62bcc500ef</wfw:commentRss>
    </item>
    <item>
      <title>Memoization… And an example in IronPython</title>
      <description>&lt;p&gt;Memoization is one of those programming techniques that make an awful lot of sense in theory yet I almost never see in practice. For those not familiar with the term, memoization refers to maintaining a store of function results so that when a function is invoked several times with the same arguments the result can just be retrieved from the store instead of being computed every single time. &lt;/p&gt;  &lt;p&gt;This technique can have huge performance implications for doubly recursive functions, like the typical Fibonacci implementation. You can find a typical and naïve Fibonacci implementation in Python below:&lt;/p&gt;  &lt;pre class="csharpcode"&gt;def fibonacci(n):
  print &lt;span class="str"&gt;&amp;quot;Calculating Fibonacci&amp;quot;&lt;/span&gt;
  &lt;span class="kwrd"&gt;if&lt;/span&gt; n &amp;lt;= 2:
    &lt;span class="kwrd"&gt;return&lt;/span&gt; 1
  &lt;span class="kwrd"&gt;return&lt;/span&gt; fibonacci(n - 1) + fibonacci(n - 2)&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;&lt;/pre&gt;

&lt;p&gt;The print statement is there so you can see the result of invoking this function on large numbers. The number of times “Calculating Fibonacci” is printed increases from 3 for n = 3 to 41 for n = 8. This means that when calculating the eight fibonacci number the function is executed a total of 41 times. Try calculating the 30th fibonacci number and you’ll see why the constant re-computation of values is not feasible. Memoization is an elegant solution to this problem (Although for our current example you could just implement the algorithm iteratively, but a high computation cost can come from many different places). Below is a solution that applies this technique:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;fibonacciValues = dict()&lt;br /&gt;def fibonacciMemoized(n):
  print &lt;span class="str"&gt;&amp;quot;Calculating Fibonacci&amp;quot;&lt;/span&gt;
  &lt;span class="kwrd"&gt;if&lt;/span&gt; n &amp;lt;= 2:
    &lt;span class="kwrd"&gt;return&lt;/span&gt; 1
  elif fibonacciValues.has_key(n):
    &lt;span class="kwrd"&gt;return&lt;/span&gt; fibonacciValues[n]
  &lt;span class="kwrd"&gt;else&lt;/span&gt;:
    result = fibonacciMemoized(n - 1) + fibonacciMemoized(n - 2)
    fibonacciValues[n] = result
    &lt;span class="kwrd"&gt;return&lt;/span&gt; result&lt;style type="text/css"&gt;.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }
&lt;/style&gt;&lt;/pre&gt;

&lt;p&gt;This version only prints “Calculating Fibonacci” 13 times compared to the 41 times of the previous version for the eighth fibonacci number. It also has the nice feature that the second time we request the eighth fibonacci number or any other number that was calculated previously there is no computation required. The code has several drawbacks, however. First amongst them is that the function requires an external dictionary to store the results. One can also see that the memoization code makes the function quite a bit less understandable than our original version. We are also mixing code with two different purposes, one of which is a simple optimization detail. It turns out there is a very easy way to do away with these problems and that is by using a function that is responsible for generating a memoized function. &lt;/p&gt;

&lt;pre class="csharpcode"&gt;def memoize(&lt;span class="kwrd"&gt;function&lt;/span&gt;):
  results = dict()
  def callFunction(*args):
    &lt;span class="kwrd"&gt;if&lt;/span&gt; results.has_key(args):
      &lt;span class="kwrd"&gt;return&lt;/span&gt; results[args]
    result = &lt;span class="kwrd"&gt;function&lt;/span&gt;(*args)
    results[args] = result
    &lt;span class="kwrd"&gt;return&lt;/span&gt; result
  &lt;span class="kwrd"&gt;return&lt;/span&gt; callFunction&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;This function builds an internal dictionary which is then closed over by the internal function. The internal function is defined to take a variable number of arguments which are used as the key to the previously created dictionary in order to store the values (Notice that when using the arguments as a key we use them in their tuple form and only expand them when using them to call the function). If there is no result for a particular set of arguments in the dictionary then the function is called and its return value stored. The internal function is returned so that it can be used. Here’s how you would use it:&lt;/p&gt;

&lt;pre class="csharpcode"&gt;memoizedFibonacci = memoize(fibonacci)
memoizedFibonacci(8)&lt;/pre&gt;

&lt;p&gt;If you do this, you might be surprised to find “Calculating Fibonacci” printed 41 times again. Weren’t we supposed to use memoization to avoid computing a certain value so many times? The reason this happened is that the memoize function has no control over the function that is passed in and if that function makes additional calls to itself then these additional calls are not going through our memoization mechanism. This means that, effectively, we are only applying memoization to the top-level call. The good news is that if you execute that last line of code again, you’ll get an immediate result with no computation, which means it is still kind of working.&lt;/p&gt;

&lt;p&gt;What we need to get a perfect version is to re-bind the function’s name to the memoized version, making sure that each and every call to that function goes through the memoization mechanism. Turns out there is a very nice way to do this in Python which also makes enabling the memoization technique on any function a breeze. The trick is to use decorators and make sure that the wrapping function used as the decorator performs the memoization. Let´s re-define the fibonacci function to use the decorator.&lt;/p&gt;

&lt;pre class="csharpcode"&gt;@memoize
def fibonacci(n):
  print &lt;span class="str"&gt;&amp;quot;Calculating Fibonacci&amp;quot;&lt;/span&gt;
  &lt;span class="kwrd"&gt;if&lt;/span&gt; n &amp;lt;= 2:
    &lt;span class="kwrd"&gt;return&lt;/span&gt; 1
  &lt;span class="kwrd"&gt;return&lt;/span&gt; fibonacci(n - 1) + fibonacci(n - 2)&lt;/pre&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;

&lt;p&gt;If you execute this version with 8 as an argument as before, you might be surprised to find this one performs even better than the first memoized fibonacci we implemented (The previous version never performed any memoization when 1 or 2 were passed in as arguments). It also takes care of any issues regarding readability since the only modification we have to do is to add the decorator. This is still, of course, a relatively rough implementation. It could be improved by providing mechanisms to control in some way the stored values so that we could clear them when appropriate. This can be done by returning objects of custom callable classes that implement the desired methods instead of functions from the decorator function, but this post is already a bit on the long side so I’ll let you figure that one out.&lt;/p&gt;

&lt;p&gt;Although it’s true that memoization is not always applicable (And downright dangerous when applied to functions with significant side-effects) it can be a nice way to reduce computation of redundant values and, best of all, can be implemented rather elegantly. Hopefully you’ll find some use for it in your programming endeavors.&lt;/p&gt;

&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:f26ac45a-c302-4947-9aa1-c4c826d06659" class="wlWriterEditableSmartContent"&gt;Technorati Tags: &lt;a href="http://technorati.com/tags/Memoization" rel="tag"&gt;Memoization&lt;/a&gt;,&lt;a href="http://technorati.com/tags/IronPython" rel="tag"&gt;IronPython&lt;/a&gt;,&lt;a href="http://technorati.com/tags/Python" rel="tag"&gt;Python&lt;/a&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.raine-tech.com/blogs/jr/post/Memoizatione280a6-And-an-example-in-IronPython.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.raine-tech.com/blogs/jr/post/Memoizatione280a6-And-an-example-in-IronPython.aspx&amp;amp;title=Memoization… And an example in IronPython" target="_blank" title="DZone It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Memoizatione280a6-And-an-example-in-IronPython.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.raine-tech.com/blogs/jr/" target="_blank" title="Technorati"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.raine-tech.com/blogs/jr/post/Memoizatione280a6-And-an-example-in-IronPython.aspx&amp;amp;title=Memoization… And an example in IronPython" target="_blank" title="Reddit"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.raine-tech.com/blogs/jr/post/Memoizatione280a6-And-an-example-in-IronPython.aspx&amp;amp;title=Memoization… And an example in IronPython" target="_blank" title="Del.icio.us"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.raine-tech.com/blogs/jr/post/Memoizatione280a6-And-an-example-in-IronPython.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/PrORWk-uWfnGMElh9T56nT44oto/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/PrORWk-uWfnGMElh9T56nT44oto/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/PrORWk-uWfnGMElh9T56nT44oto/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/PrORWk-uWfnGMElh9T56nT44oto/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <link>http://www.raine-tech.com/blogs/jr/post/Memoizatione280a6-And-an-example-in-IronPython.aspx</link>
      <author>jr.calzada.nospam@nospam.raine-tech.com (jr.calzada)</author>
      <comments>http://www.raine-tech.com/blogs/jr/post/Memoizatione280a6-And-an-example-in-IronPython.aspx#comment</comments>
      <guid>http://www.raine-tech.com/blogs/jr/post.aspx?id=4cf89d39-9ebd-44b5-84e4-ecba80846251</guid>
      <pubDate>Mon, 06 Apr 2009 03:59:20 -0600</pubDate>
      <category>Computer Science</category>
      <dc:publisher>jr.calzada</dc:publisher>
      <pingback:server>http://www.raine-tech.com/blogs/jr/pingback.axd</pingback:server>
      <pingback:target>http://www.raine-tech.com/blogs/jr/post.aspx?id=4cf89d39-9ebd-44b5-84e4-ecba80846251</pingback:target>
      <slash:comments>1</slash:comments>
      <trackback:ping>http://www.raine-tech.com/blogs/jr/trackback.axd?id=4cf89d39-9ebd-44b5-84e4-ecba80846251</trackback:ping>
      <wfw:comment>http://www.raine-tech.com/blogs/jr/post/Memoizatione280a6-And-an-example-in-IronPython.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.raine-tech.com/blogs/jr/syndication.axd?post=4cf89d39-9ebd-44b5-84e4-ecba80846251</wfw:commentRss>
    </item>
    <item>
      <title>The joys of sharing the world with trolls</title>
      <description>&lt;p&gt;There’s one thing I can’t seem to wrap my head around, no matter how hard I try: Senseless trolling. Do &lt;a href="http://linuxlock.blogspot.com/2009/03/ars-technica-windows-drm-were-ok-with.html" target="_blank"&gt;these people&lt;/a&gt; actually think that their hate-ridden, poorly written, fanatical diatribes actually matter or make much of an impact beyond giving like-minded so-called individuals an incentive to swim around in each other’s crap? It’s just a bunch of uninformed self-righteous crap that serves no purpose but to inflate the author’s ego while he and his buddies keep participating in this pathetically ridiculous circle-jerk.&lt;/p&gt;  &lt;p&gt;Well, now that I have that off my chest, I’ll gladly apologize for the language in the previous paragraph. But it does infuriate me to come across such worthless arguments so often when all they do is reinforce certain stereotypes and brew hatred amongst what would otherwise be simply consumers. Yet, for some reason, some people find it necessary to use unfounded hyperbole in an effort to validate whatever choice they make regarding what software they install on their machines. &lt;/p&gt;  &lt;p&gt;Sometimes it seems like the overall internet population never left and will never leave puberty…&lt;/p&gt;  &lt;p&gt;BTW, go &lt;a href="http://arstechnica.com/microsoft/news/2009/02/oh-the-humanity-windows-7s-draconian-drm.ars" target="_blank"&gt;here&lt;/a&gt; for the actual facts.&lt;/p&gt;&lt;div class="socialBookmarksContainer"&gt;&lt;a rel="nofollow" href="http://digg.com/submit/?url=http://www.raine-tech.com/blogs/jr/post/The-joys-of-sharing-the-world-with-trolls.aspx" target="_blank" title="Digg It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/digg_24.png" style="border: 0;" alt="Digg It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.dzone.com/links/add.html?url=http://www.raine-tech.com/blogs/jr/post/The-joys-of-sharing-the-world-with-trolls.aspx&amp;amp;title=The joys of sharing the world with trolls" target="_blank" title="DZone It!"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/dzone_24.png" style="border: 0;" alt="DZone It!" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.stumbleupon.com/submit?url=http://www.raine-tech.com/blogs/jr/post/The-joys-of-sharing-the-world-with-trolls.aspx" target="_blank" title="StumbleUpon"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/stumbleupon_24.png" style="border: 0;" alt="StumbleUpon" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://technorati.com/ping?url=http://www.raine-tech.com/blogs/jr/" target="_blank" title="Technorati"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/technorati_24.png" style="border: 0;" alt="Technorati" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://reddit.com/submit?url=http://www.raine-tech.com/blogs/jr/post/The-joys-of-sharing-the-world-with-trolls.aspx&amp;amp;title=The joys of sharing the world with trolls" target="_blank" title="Reddit"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/reddit_24.png" style="border: 0;" alt="Reddit" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://del.icio.us/post?url=http://www.raine-tech.com/blogs/jr/post/The-joys-of-sharing-the-world-with-trolls.aspx&amp;amp;title=The joys of sharing the world with trolls" target="_blank" title="Del.icio.us"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/delicious_24.png" style="border: 0;" alt="Del.icio.us" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://www.newsvine.com/_wine/save?u=http://www.raine-tech.com/blogs/jr/post/The-joys-of-sharing-the-world-with-trolls.aspx" target="_blank"title="NewsVine"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/newsvine_24.png" style="border: 0;" alt="NewsVine" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://furl.net" target="_blank" title="Furl"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/furl_24.png" style="border: 0;" alt="Furl" /&gt;&lt;/a&gt;&lt;a rel="nofollow" href="http://blinklist.com/submit/" target="_blank" title="BlinkList"&gt;&lt;img src="/blogs/jr/themes/images//socialbookmarks/circle/blinklist_24.png" style="border: 0;" alt="BlinkList" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/VHC3WlF49npwq7ZXKZKFLOC-Kz0/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VHC3WlF49npwq7ZXKZKFLOC-Kz0/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/VHC3WlF49npwq7ZXKZKFLOC-Kz0/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/VHC3WlF49npwq7ZXKZKFLOC-Kz0/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;</description>
      <link>http://www.raine-tech.com/blogs/jr/post/The-joys-of-sharing-the-world-with-trolls.aspx</link>
      <author>jr.calzada.nospam@nospam.raine-tech.com (jr.calzada)</author>
      <comments>http://www.raine-tech.com/blogs/jr/post/The-joys-of-sharing-the-world-with-trolls.aspx#comment</comments>
      <guid>http://www.raine-tech.com/blogs/jr/post.aspx?id=695ced0e-c4b7-451e-a0ed-a8bf032bb95f</guid>
      <pubDate>Wed, 25 Mar 2009 19:52:29 -0600</pubDate>
      <category>Software</category>
      <dc:publisher>jr.calzada</dc:publisher>
      <pingback:server>http://www.raine-tech.com/blogs/jr/pingback.axd</pingback:server>
      <pingback:target>http://www.raine-tech.com/blogs/jr/post.aspx?id=695ced0e-c4b7-451e-a0ed-a8bf032bb95f</pingback:target>
      <slash:comments>0</slash:comments>
      <trackback:ping>http://www.raine-tech.com/blogs/jr/trackback.axd?id=695ced0e-c4b7-451e-a0ed-a8bf032bb95f</trackback:ping>
      <wfw:comment>http://www.raine-tech.com/blogs/jr/post/The-joys-of-sharing-the-world-with-trolls.aspx#comment</wfw:comment>
      <wfw:commentRss>http://www.raine-tech.com/blogs/jr/syndication.axd?post=695ced0e-c4b7-451e-a0ed-a8bf032bb95f</wfw:commentRss>
    </item>
  </channel>
</rss>
