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

<channel>
	<title>Invisible Blocks</title>
	<atom:link href="http://invisibleblocks.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://invisibleblocks.wordpress.com</link>
	<description>for building invisible machines</description>
	<lastBuildDate>Wed, 01 Jul 2009 11:40:40 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<image>
		<url>http://www.gravatar.com/blavatar/4f043f3049fde8b61c72e5cf324315fd?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Invisible Blocks</title>
		<link>http://invisibleblocks.wordpress.com</link>
	</image>
			<item>
		<title>Higher-Order Functions and Function Composition, with Processing</title>
		<link>http://invisibleblocks.wordpress.com/2009/05/04/higher-order-functions-and-function-composition-with-processing/</link>
		<comments>http://invisibleblocks.wordpress.com/2009/05/04/higher-order-functions-and-function-composition-with-processing/#comments</comments>
		<pubDate>Mon, 04 May 2009 17:41:53 +0000</pubDate>
		<dc:creator>Daniel Bernier</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://invisibleblocks.wordpress.com/?p=250</guid>
		<description><![CDATA[I was looking for a good way to illustrate functional composition and higher order functions, and thought that something could be done with Processing, a java-based graphics tool. Among other things, Processing exposes raw pixel data for the images it renders, and you can update the pixels programatically, providing a simple kind of image filtering.
For [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=250&subd=invisibleblocks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I was looking for a good way to illustrate functional composition and higher order functions, and thought that something could be done with <a href="http://processing.org">Processing</a>, a java-based graphics tool. Among other things, Processing exposes raw pixel data for the images it renders, and you can update the pixels programatically, providing a simple kind of image filtering.</p>
<p>For example, here&#8217;s some code that converts a color image to grayscale. Just like with HTML, colors are represented<sup><a name="f1" href="#1">1</a></sup> as 3 channels (red, green, blue), and each channel has 255 increments. A gray pixel has equal amounts of red, green, and blue, so if you get the overall brightness of a pixel, and set its color to that much red, green, and blue, it should turn the image gray.</p>
<pre name="code" class="java">

PImage img = loadImage(&quot;tattoo.jpg&quot;);
size(img.width, img.height);  // set the window size
image(img, 0, 0);  // render the image at x:y = 0:0

loadPixels(); // load the image&#039;s pixels into an array
for (int i = 0; i &lt; pixels.length; i++) {
    // get the color for this pixel
    color c = pixels[i];

    // get its brightness
    float bright = brightness(c);

    // Change its color to its grayscale equivalent
    pixels[i] = color(bright, bright, bright);
}
updatePixels();  // render the new pixels to the screen
</pre>
<p>Here&#8217;s the original image:</p>
<p><a href="http://invisibleblocks.files.wordpress.com/2009/05/tattoo.jpg"><img class="size-medium wp-image-265" src="http://invisibleblocks.files.wordpress.com/2009/05/tattoo.jpg?w=300&#038;h=199" alt="the original image" width="300" height="199" /></a></p>
<p>&#8230;and here&#8217;s the filtered version:</p>
<p><a href="http://invisibleblocks.files.wordpress.com/2009/05/tattoo_grayscale.jpg"><img class="size-medium wp-image-266" src="http://invisibleblocks.files.wordpress.com/2009/05/tattoo_grayscale.jpg?w=300&#038;h=199" alt="the image in grayscale" width="300" height="199" /></a></p>
<p>You can define a bunch of routines like this, each looping through the pixels the same way, but adjusting the colors differently. But if you can separate the pixel-changing part from the pixel-looping part, then you can swap in ANY pixel-changing routine, giving you a flexible image filtering system.</p>
<p>The pixel-changing code is essentially a function that turns one pixel&#8217;s color into a new color, and the pixel-looping code uses it to replace each pixel&#8217;s original color.  The pixel-changing function could be described like this:</p>
<pre name="code" class="java">

color transform(color c) {
    ...
}
</pre>
<p>Many modern programming languages support first-class functions, which are a natural way to model this. Processing uses a form of Java, which doesn&#8217;t have first-class functions, but we can wrap the functions in a class, giving us an object called a functor.  I called this one ColorTrans, for &#8220;color transformer&#8221;.</p>
<pre name="code" class="java">

abstract class ColorTrans {
    public abstract color transform(color c);
}
</pre>
<p>The ColorTrans class&#8217; only method, <code>transform</code>, is our pixel-changing function. We can re-write the loop from before to use a ColorTrans filter, and while we&#8217;re at it, we&#8217;ll move it into a method that takes the filename as a parameter, too.</p>
<pre name="code" class="java">

void filterImage(String path, ColorTrans filter) {
    PImage img = loadImage(path);
    size(img.width, img.height);
    image(img, 0, 0);

    loadPixels();
    for (int i = 0; i &lt; pixels.length; i++) {
        // use the filter parameter
        pixels[i] = filter.transform(pixels[i]);
    }
    updatePixels();
}
</pre>
<p>We can easily recreate the grayscale filter from earlier as a ColorTrans.</p>
<pre name="code" class="java">

ColorTrans grayscale = new ColorTrans() {
    color transform(color c) {
        float bright = brightness(c);
        return color(bright, bright, bright);
    }
};

filterImage(&quot;tattoo.jpg&quot;, grayscale);
</pre>
<p>Another really easy filter to write is an inverter. If a pixel has R:100, G:30, B:255, an inverter will return the color R:(255-100), G:(255-30), B:(255-255), or R:155, G:225, B:0.</p>
<pre name="code" class="java">

ColorTrans invert = new ColorTrans() {
    color transform(color c) {
        return color(255 - red(c),
                     255 - green(c),
                     255 - blue(c));
    }
};

filterImage(&quot;tattoo.jpg&quot;, invert);
</pre>
<p>The image produced by an inverter is like a film negative.</p>
<p><a href="http://invisibleblocks.files.wordpress.com/2009/05/tattoo_invert.jpg"><img class="size-medium wp-image-268" src="http://invisibleblocks.files.wordpress.com/2009/05/tattoo_invert.jpg?w=300&#038;h=199" alt="inverted, like a negative" width="300" height="199" /></a></p>
<p>Now we begin to see the benefits of keeping the-parts-that-change separate from the-parts-that-don&#8217;t: we can easily define and swap in new filters. This is one of the big parts of higher-order programming: writing routines that are configured by passing in functions (or functors, in this case) to do part of the work.</p>
<h3>Manufacturing a ColorTrans</h3>
<p>A useful kind of filter is one that increases (or decreases) the color on one channel: add some red, or remove some green. This kind of filter is easy to create:</p>
<pre name="code" class="java">

ColorTrans aFifthMoreRed = new ColorTrans() {
    color transform(color c) {
    	return color(red(c) * 1.2, green(c), blue(c));
    }
};
</pre>
<p>This filter will increase the amout of red in the image by 20%. But 20% is a pretty arbitrary number; it&#8217;d be better if we could tell the filter how much to increase the red by. Generally, you&#8217;d add an &#8220;amount&#8221; parameter to the transform method, but then filterImage would have to know that this ColorTrans object takes an extra parameter. It&#8217;s kind of nice having filterImage treat all ColorTrans objects the same, just passing in the color.</p>
<p>Instead, we can make a method that builds ColorTrans objects: we tell it how much to increase the red by, and it builds a ColorTrans that does it for us.</p>
<pre name="code" class="java">

ColorTrans ampRed(final float amount) {
    return new ColorTrans() {
        color transform(color c) {
            color(red(c) * amount, green(c), blue(c));
        }
    };
}

ColorTrans aQuarterMoreRed = ampRed(1.25);
ColorTrans aThirdLessRed = ampRed(2/3);
ColorTrans noRedAtAll = ampRed(0);
</pre>
<p>(If you&#8217;re curious why <code>amount</code> is final, the short answer is &#8220;because the compiler says so,&#8221; but there&#8217;s a better answer<sup><a name="f2" href="#2">2</a></sup>.)</p>
<p>This is pretty nice, because we can use this inside an animation loop, animating the amount of color amplification.</p>
<pre name="code" class="java">

float theta = 0.0;

void setup() {
    filterImage(&quot;tattoo.jpg&quot;, noChange);
}

void draw() {
    float ampRedAmount = sin(theta) * 1.2;
    filterImage(&quot;tattoo.jpg&quot;, ampRed(ampRedAmount));
    theta += 0.1;
}
</pre>
<p>[Here's where I'd link to the applet, nicely hosted somewhere, if I had a hosting service that allowed applets.  I'll try to find a place to put all the code, so you can try it yourself.]</p>
<p>Processing calls <code>setup</code> to initialize the animation, and calls <code>draw</code> once per &#8220;tick&#8221;, to advance the animation. Here, in each frame of the animation, a new ColorTrans is constructed by <code>ampRed</code>, with the amount tied to the sine wave function, oscillating between 0 and 1.2. When viewed, the amount of red in the image swells and falls, and back again<sup><a name="f3" href="#3">3</a></sup>.</p>
<p>This is another big part of higher-order programming: writing functions that build other functions, based on some arguments. Combined with routines that take functions as arguments, it&#8217;s a handy way to break down some problems. If done well, the routines that take functions as arguments, and the functions that build those functions, can become a sort of mini-language, a fluent interface, or almost an embedded DSL.</p>
<h3>Plugging filters together &#8211; filter composition</h3>
<p>This is where it gets fun. Suppose you&#8217;ve created an ampBlue that works just like ampRed, and now you want to filter an image with <em>both</em> of them.  One approach might be something like this:</p>
<pre name="code" class="java">

void draw() {
    filterImage(&quot;tattoo.jpg&quot;, ampRed(sin(theta) * 1.2));
    filterImage(&quot;tattoo.jpg&quot;, ampBlue(cos(theta) * 1.2));
}
</pre>
<p>Using the sine and cosine functions, the image should pulse nicely through reds and blues. The only problem is that it doesn&#8217;t really work, because filterImage loads the image fresh from the filesystem each time, so you only get the effect of the ampBlue filter. So how can we apply multiple filters?</p>
<p>We plug them together. We want a filter that does the work of two other filters, and we want it to look like any other filter, so filterImage won&#8217;t know the difference. To get this, we can add a method to ColorTrans that returns a <em>new</em> ColorTrans, which calls first the one, and then the other.</p>
<pre name="code" class="java">

class ColorTrans {
    ...
    public ColorTrans then(final ColorTrans applySecond) {
        final ColorTrans applyFirst = this;
        return new ColorTrans() {
	    color transform(color c) {
	        return applySecond(applyFirst(c));
	    }
	};
    }
}

filterImage(&quot;tattoo.jpg&quot;, grayscale.then(invert));
</pre>
<p><a href="http://invisibleblocks.files.wordpress.com/2009/05/tattoo_grayscale_invert.jpg"><img class="size-medium wp-image-268" src="http://invisibleblocks.files.wordpress.com/2009/05/tattoo_grayscale_invert.jpg?w=300&#038;h=199" alt="first grayscaled, then inverted" width="300" height="199" /></a></p>
<p>Combining filters becomes a matter of chaining them together through <code>then</code>. The red-and-blue example becomes:</p>
<pre name="code" class="java">

void draw() {
   filterImage(&quot;tattoo.jpg&quot;,
        ampRed(sin(theta) * 1.2).then(
            ampBlue(cos(theta) * 1.2)));
}
</pre>
<h3>Processing does it kind of this way, too</h3>
<p>If you look at the <a href="http://dev.processing.org/source/">source for Processing</a>, in <a href="http://dev.processing.org/source/index.cgi/trunk/processing/core/src/processing/core/PImage.java?view=markup&amp;rev=5417">PImage.java</a> on line 619, you&#8217;ll find code that looks kind of like this:</p>
<pre name="code" class="java">

  public void filter(int kind) {
    loadPixels();

    switch (kind) {
      case BLUR: ...
      case GRAY: ...
      case INVERT: ...
      case POSTERIZE: ...
      case RGB: ...
      case THRESHOLD: ...
      case ERODE: ...
      case DILATE: ...
    }
    updatePixels();  // mark as modified
  }
</pre>
<p>It basically does just what I&#8217;ve been doing, except the operations are hard-coded into the source, rather than being separated behind a class interface. The filters aren&#8217;t composable directly, though you can call a number of them in a row:</p>
<pre name="code" class="java">

filter(INVERT);
filter(THRESHOLD);
filter(DILATE);
</pre>
<p>One benefit of this approach is that it&#8217;s easy to see, line-by-line, exactly what&#8217;s happening.  I&#8217;d bet it beats the pants off of the ColorTrans version in benchmarks, too. But filters aren&#8217;t composeable, and it&#8217;s certainly not extendable. When you&#8217;re doing computation-intensive graphics, every bit of speed is important; when you&#8217;re illustrating a programming idea, it&#8217;s not.  Decide for yourself which is more important for your needs.</p>
<p>____________________________________</p>
<p><a name="1"></a><br />
1. It may seem weird, if you know Java, that the parameter&#8217;s type is <code>color</code> &#8212; it&#8217;s not a java primitive, but it doesn&#8217;t follow the normal classname conventions. It&#8217;s just the way Processing does it. You can read more about the <a href="http://processing.org/reference/color_.html">color constructor in the Processing reference</a>.  <a href="#f1">[back]</a></p>
<p><a name="2"></a><br />
2. Taken from the <a href="http://c2.com/cgi/wiki?AnonymousInnerClass">AnonymousInnerClasses</a> page on the Portland Patterns Repository, 2009-04-30:</p>
<blockquote><p>AnonymousInnerClasses can also be used to create something like closures in the JavaLanguage. However they do not &#8220;close over&#8221; the lexical environment so they aren&#8217;t TrueClosures. (They can capture the value of local variables, but they do not have access to the variable binding so they can&#8217;t change the original variable. Which Java makes quite clear by only allowing InnerClasses to refer to local variables that have been declared final (so no-one can change them)).</p></blockquote>
<p><a href="#f2">[back]</a></p>
<p><a name="3"></a><br />
3. The <code>noChange</code> filter returns the same color it was given &#8212; an identity function.  <code>filterImage</code> is called inside <code>setup</code> only so the window size is set correctly, since setting the size inside <code>draw</code> has no effect. And <em>theta</em> is just a Greek letter often used for angles and trigonometry.<a href="#f3">[back]</a></p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/invisibleblocks.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/invisibleblocks.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/invisibleblocks.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/invisibleblocks.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/invisibleblocks.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/invisibleblocks.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/invisibleblocks.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/invisibleblocks.wordpress.com/250/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/invisibleblocks.wordpress.com/250/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/invisibleblocks.wordpress.com/250/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=250&subd=invisibleblocks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://invisibleblocks.wordpress.com/2009/05/04/higher-order-functions-and-function-composition-with-processing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/360300f6962b973dae989991dc204f75?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bonsaigiant</media:title>
		</media:content>

		<media:content url="http://invisibleblocks.files.wordpress.com/2009/05/tattoo.jpg?w=300" medium="image">
			<media:title type="html">the original image</media:title>
		</media:content>

		<media:content url="http://invisibleblocks.files.wordpress.com/2009/05/tattoo_grayscale.jpg?w=300" medium="image">
			<media:title type="html">the image in grayscale</media:title>
		</media:content>

		<media:content url="http://invisibleblocks.files.wordpress.com/2009/05/tattoo_invert.jpg?w=300" medium="image">
			<media:title type="html">inverted, like a negative</media:title>
		</media:content>

		<media:content url="http://invisibleblocks.files.wordpress.com/2009/05/tattoo_grayscale_invert.jpg?w=300" medium="image">
			<media:title type="html">first grayscaled, then inverted</media:title>
		</media:content>
	</item>
		<item>
		<title>Book Review: Pragmatic Thinking and Learning</title>
		<link>http://invisibleblocks.wordpress.com/2009/04/15/book-review-pragmatic-thinking-and-learning/</link>
		<comments>http://invisibleblocks.wordpress.com/2009/04/15/book-review-pragmatic-thinking-and-learning/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 20:48:36 +0000</pubDate>
		<dc:creator>Daniel Bernier</dc:creator>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Productivity]]></category>
		<category><![CDATA[Recommended Reading]]></category>
		<category><![CDATA[Software Thinking]]></category>

		<guid isPermaLink="false">http://invisibleblocks.wordpress.com/?p=230</guid>
		<description><![CDATA[I was planning on reading Andy Hunt&#8217;s Pragmatic Thinking and Learning: Refactor Your Wetware for a while, but kept putting it off, thinking it wasn&#8217;t really central to what I&#8217;m interested in, what I&#8217;m doing.  I was wrong.
People have different skill levels, from novice to expert.  One sign of expertise is the ability to intuit [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=230&subd=invisibleblocks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I was planning on reading Andy Hunt&#8217;s <a href="http://www.pragprog.com/titles/ahptl/">Pragmatic Thinking and Learning: Refactor Your Wetware</a> for a while, but kept putting it off, thinking it wasn&#8217;t really central to what I&#8217;m interested in, what I&#8217;m doing.  I was wrong.</p>
<p>People have different skill levels, from novice to expert.  One sign of expertise is the ability to intuit solutions not available through linear thinking.  Intuition happens on the right side of the brain, so we need to use the right side more effectively.  Rather than &#8220;left brain&#8221; and &#8220;right brain&#8221;, he calls them &#8220;L-mode&#8221; and &#8220;R-mode&#8221;, for &#8220;linear&#8221; and &#8220;rich&#8221;, to emphasize that it&#8217;s not literally two halves of your brain working differently, but two different modes of thinking.  Most of the book is about using R-mode more, and using L-mode appropriately, to fact-check your R-mode.</p>
<p>It draws on psychology, neurobiology, cognition studies, managing, teaching, the arts, product design, and math.  The bibliography is ten pages long, and I&#8217;ve got a bunch of them highlighted for my next evening at the bookstore.</p>
<p>The most valuable changes I&#8217;ve made since finishing the book are:</p>
<p><em>- Be ready when your R-mode strikes.</em> Since &#8220;querying&#8221; the R-mode can take a long time, we run it asynchronously, and we never know when the results will come in.  (This explains the &#8220;ah-ha!&#8221; moments in the shower, on the drive home, and when you&#8217;re falling asleep.)  So keep a notebook handy for writing things down.  I&#8217;m writing in a notebook almost daily (I&#8217;ve joined the cult of <a href="http://www.amazon.com/gp/product/8883701038?ie=UTF8&amp;tag=invisblock-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=8883701038">moleskine</a>), writing down ideas as they come up.  It&#8217;s also a tider home for all the scraps of paper that have lived in my wallet for years, ideas scrawled and smudged into them.  And I&#8217;m on my 4th or 5th <a href="http://www.pocketmod.com/">pocket mod</a>.</p>
<p><em>- Maintain your &#8216;exo-cortex,&#8217; your external memory. </em>I wrote a mini-wiki engine with <a href="http://www.sinatrarb.com/">Sinatra</a>, and so far, I&#8217;ve used it for everything from clojure, to saving quotes, to robot rights.  I wrote it for practice, but it&#8217;s nice to have a searchable, non-linear notebook.</p>
<p>The book mentions Gerald Weinberg&#8217;s fieldstone method of writing: to build a stone wall, you gradually gather stones from the field as you clear it, and pile them up.  When you have enough, build your wall.  Both the wiki and the notebook serve this purpose.</p>
<p><em>- Map out your thinking, and doodle. </em>Drawing pictures engages your R-mode, making it more active, helping you make connections, and understand more clearly.  Besides, it&#8217;s fun. This is my 3rd time learning emacs, and it&#8217;s finally sticking &#8212; I think it&#8217;s mostly because I drew up a <a href="http://invisibleblocks.files.wordpress.com/2009/04/emacs_cheat_sheet.jpg">cheat-sheet</a>, pictorially explaining each command&#8217;s effect.  I also map out my learning and career goals this way.</p>
<p><img src="http://invisibleblocks.files.wordpress.com/2009/04/emacs_cheat_sheet.jpg?w=300&#038;h=218" alt="emacs cheat sheet" title="emacs cheat sheet" width="300" height="218" class="alignnone size-medium wp-image-245" /></p>
<p>One of my favorite parts is, after you&#8217;ve mapped something out, and the drawing looks like a pile of yarn, re-draw it on a clean sheet, fixing up all the awkward placements and messy bits.  The benefit of this second version is in its creation &#8212; by considering how to re-organize it, you&#8217;re thinking more about the problem, in ways you might not have before.  This works for note-taking, too.</p>
<p><em>- Take advantage of </em><em>your brain&#8217;s </em><em>plasticity. </em>You can change how your brain works by <em>believing</em> it works differently: make it smarter, more creative, cheerier.  This is a pretty incredible claim, but he cites <a href="http://www.amazon.com/gp/product/0345472322?ie=UTF8&amp;tag=invisblock-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0345472322">two</a> <a href="http://www.amazon.com/gp/product/0143113100?ie=UTF8&amp;tag=invisblock-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0143113100">books</a> I&#8217;d like to check out.  It sounds impossible, but I&#8217;ve seen it work on me, to some extent.  My biggest problem is <em>continuing</em> to believe it works.</p>
<p>Some things I have yet to try:</p>
<p>- Take a walk.  A meditative walk, like around a <a href="http://en.wikipedia.org/wiki/Labyrinth">labyrinth</a>.</p>
<p>- Write morning pages: for three weeks, as soon as you wake, write down three pages of text.  It doesn&#8217;t have to be anything specific.  The idea is, right after we wake, our R-mode is more active, so the gems will be more accessible, and they&#8217;ll pop out onto the page.  I&#8217;m more curious to see what I come up with.</p>
<p>- I have to get better at setting SMART goals: specific, measurable, achievable, relevant, and time-boxed.  I&#8217;ve never been much for this kind of discipline &#8212; I made a few, but didn&#8217;t stick to them, and haven&#8217;t gotten into the habit yet.  But like the book says, change is hard:</p>
<blockquote><p>Change is always harder than it looks — that’s a physical reality, not just an aphorism. An old, ingrained habit makes the equivalent of a neural highway in your brain. These old habits don’t go away. You can make new neural highways alongside, going a different route and making short-cuts, but the old highways remain. They are always there for you to revert to—to fall back on. Practice may not make perfect, but it sure makes permanent.</p></blockquote>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/invisibleblocks.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/invisibleblocks.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/invisibleblocks.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/invisibleblocks.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/invisibleblocks.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/invisibleblocks.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/invisibleblocks.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/invisibleblocks.wordpress.com/230/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/invisibleblocks.wordpress.com/230/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/invisibleblocks.wordpress.com/230/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=230&subd=invisibleblocks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://invisibleblocks.wordpress.com/2009/04/15/book-review-pragmatic-thinking-and-learning/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/360300f6962b973dae989991dc204f75?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bonsaigiant</media:title>
		</media:content>

		<media:content url="http://invisibleblocks.files.wordpress.com/2009/04/emacs_cheat_sheet.jpg?w=300" medium="image">
			<media:title type="html">emacs cheat sheet</media:title>
		</media:content>
	</item>
		<item>
		<title>Heading to the Int&#8217;l Lisp Conf, 2009</title>
		<link>http://invisibleblocks.wordpress.com/2009/02/24/heading-to-the-intl-lisp-conf-2009/</link>
		<comments>http://invisibleblocks.wordpress.com/2009/02/24/heading-to-the-intl-lisp-conf-2009/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 01:30:50 +0000</pubDate>
		<dc:creator>Daniel Bernier</dc:creator>
				<category><![CDATA[Functional Programming]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software Thinking]]></category>

		<guid isPermaLink="false">http://invisibleblocks.wordpress.com/?p=225</guid>
		<description><![CDATA[My next step on the road to lisp is at the International Lisp Conference in Cambridge, MA, this March.
A lot of people are surprised to hear I&#8217;m interested in Lisp, but I&#8217;ve been studying scheme informally for about 3 years, reading SICP, and finishing The Little Schemer last year.  I&#8217;ve been interested in clojure for [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=225&subd=invisibleblocks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>My next step on <a href="http://www.google.com/search?q=the+road+to+lisp">the road to lisp</a> is at the <a href="http://www.international-lisp-conference.org/2009/index">International Lisp Conference</a> in Cambridge, MA, this March.</p>
<p>A lot of people are surprised to hear I&#8217;m interested in Lisp, but I&#8217;ve been studying scheme informally for about 3 years, reading <a href="http://www.amazon.com/gp/product/0262011530?ie=UTF8&amp;tag=invisblock-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0262011530">SICP</a>, and finishing <a href="http://www.amazon.com/gp/product/0262560992?ie=UTF8&amp;tag=invisblock-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0262560992">The Little Schemer</a> last year.  I&#8217;ve been interested in <a href="http://clojure.org">clojure</a> for about 18 months, especially after <a href="http://news.e-scribe.com/411">seeing Rich Hickey talk about it last March</a>.  Now I&#8217;m studying it in earnest with Stu Halloway&#8217;s <a href="http://www.amazon.com/gp/product/1934356336?ie=UTF8&amp;tag=invisblock-20&amp;link_code=as3&amp;camp=211189&amp;creative=373489&amp;creativeASIN=1934356336">Programming Clojure</a>.</p>
<p>I don&#8217;t use lisps of any sort at work, so it&#8217;s not a &#8220;practical&#8221; language to study like, say, C# is.  But that&#8217;s short-term thinking.  Learning lisp is plenty practical, if you&#8217;re looking longer-term &#8212; even if it&#8217;s only three years out.  Learning lisp teaches you about some of the core issues of programming.</p>
<p>I like learning lisp because it&#8217;s a window into the workings of programming languages, being so close to the AST.  I want to learn more about meta-programming with macros, hygienic or not.</p>
<p>And for the record, the functional techniques in <a href="http://www.amazon.com/gp/product/0262560992?ie=UTF8&amp;amp;tag=invisblock-20&amp;amp;linkCode=as2&amp;amp;camp=1789&amp;amp;creative=390957&amp;amp;creativeASIN=0262560992">The Little Schemer</a> have improved my Ruby, JavaScript, and C# in some really <a href="http://notes-on-haskell.blogspot.com/2007/02/whats-wrong-with-for-loop.html">solid</a> <a href="http://weblog.raganwald.com/2007/03/why-why-functional-programming-matters.html">ways</a>. Functional programming is making its way to the rest of the programming world, and lisp does functional programming.</p>
<p>If you&#8217;ll be at the conference, I&#8217;d love to hear from you.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/invisibleblocks.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/invisibleblocks.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/invisibleblocks.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/invisibleblocks.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/invisibleblocks.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/invisibleblocks.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/invisibleblocks.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/invisibleblocks.wordpress.com/225/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/invisibleblocks.wordpress.com/225/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/invisibleblocks.wordpress.com/225/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=225&subd=invisibleblocks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://invisibleblocks.wordpress.com/2009/02/24/heading-to-the-intl-lisp-conf-2009/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/360300f6962b973dae989991dc204f75?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bonsaigiant</media:title>
		</media:content>
	</item>
		<item>
		<title>Come to the Hartford Ruby Brigade&#8217;s January Meeting</title>
		<link>http://invisibleblocks.wordpress.com/2009/01/20/come-to-the-hartford-ruby-brigades-january-meeting/</link>
		<comments>http://invisibleblocks.wordpress.com/2009/01/20/come-to-the-hartford-ruby-brigades-january-meeting/#comments</comments>
		<pubDate>Tue, 20 Jan 2009 14:38:06 +0000</pubDate>
		<dc:creator>Daniel Bernier</dc:creator>
				<category><![CDATA[Hartford Ruby Brigade]]></category>
		<category><![CDATA[Hartford.rb]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://invisibleblocks.wordpress.com/?p=214</guid>
		<description><![CDATA[As we trudge back to our regular schedules after assorted holiday debaucheries, here&#8217;s a heartening thought:  Hartford.rb is meeting on Monday, 1/26!  Gary Wright will tell us the story of how he learned Git, and some lucky stiff will walk away with a free copy of The Rails Way.  We&#8217;ll even have [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=214&subd=invisibleblocks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>As we trudge back to our regular schedules after assorted holiday debaucheries, here&#8217;s a heartening thought:  Hartford.rb is meeting on Monday, 1/26!  Gary Wright will tell us the story of how he learned Git, and some lucky stiff will walk away with a free copy of <a href="http://www.amazon.com/gp/product/0321445619?ie=UTF8&amp;tag=invisblock-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0321445619">The Rails Way</a>.  We&#8217;ll even have pizza &#8212; though we&#8217;re now on the look-out for a pizza sponsor, so if you know anyone looking to feed us (for only ~$50 a month!), it might even be free.</p>
<p>The usual details:<br />
Monday, 1/26, 6 &#8211; 8 PM<br />
GeeZeo<br />
750 Main St, Hartford</p>
<p>See you soon.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/invisibleblocks.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/invisibleblocks.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/invisibleblocks.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/invisibleblocks.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/invisibleblocks.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/invisibleblocks.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/invisibleblocks.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/invisibleblocks.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/invisibleblocks.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/invisibleblocks.wordpress.com/214/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=214&subd=invisibleblocks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://invisibleblocks.wordpress.com/2009/01/20/come-to-the-hartford-ruby-brigades-january-meeting/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/360300f6962b973dae989991dc204f75?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bonsaigiant</media:title>
		</media:content>
	</item>
		<item>
		<title>Accuracy and Precision: Not the Same</title>
		<link>http://invisibleblocks.wordpress.com/2009/01/12/accuracy-and-precision-not-the-same/</link>
		<comments>http://invisibleblocks.wordpress.com/2009/01/12/accuracy-and-precision-not-the-same/#comments</comments>
		<pubDate>Mon, 12 Jan 2009 14:29:38 +0000</pubDate>
		<dc:creator>Daniel Bernier</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://invisibleblocks.wordpress.com/?p=211</guid>
		<description><![CDATA[I often hear people mis-use the words accuracy and precision, but they mean different things.
To say I&#8217;m 117 years, 93 days, 4 hours, 17 minutes, and 48.249 seconds old is very precise, but it&#8217;s inaccurate (right now, anyway).  To say I&#8217;m in my thirties is accurate, but imprecise.  Precision is about level of detail, accuracy [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=211&subd=invisibleblocks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I often hear people mis-use the words <em>accuracy</em> and <em>precision</em>, but they mean different things.</p>
<p>To say I&#8217;m 117 years, 93 days, 4 hours, 17 minutes, and 48.249 seconds old is very precise, but it&#8217;s inaccurate (right now, anyway).  To say I&#8217;m in my thirties is accurate, but imprecise.  Precision is about level of detail, accuracy is about truthiness.</p>
<p>They make a wonderful pair, working pretty much orthogonally to each other.</p>
<p>Wikipedia has <a title="Accuracy and Precision" href="http://en.wikipedia.org/wiki/Accuracy#Accuracy_versus_precision.3B_the_target_analogy">a nice analogy using a bulls-eye target</a>.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/invisibleblocks.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/invisibleblocks.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/invisibleblocks.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/invisibleblocks.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/invisibleblocks.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/invisibleblocks.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/invisibleblocks.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/invisibleblocks.wordpress.com/211/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/invisibleblocks.wordpress.com/211/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/invisibleblocks.wordpress.com/211/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=211&subd=invisibleblocks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://invisibleblocks.wordpress.com/2009/01/12/accuracy-and-precision-not-the-same/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/360300f6962b973dae989991dc204f75?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bonsaigiant</media:title>
		</media:content>
	</item>
		<item>
		<title>The Twelve Bugs of Christmas, via Ruby</title>
		<link>http://invisibleblocks.wordpress.com/2008/12/30/the-twelve-bugs-of-christmas-via-ruby/</link>
		<comments>http://invisibleblocks.wordpress.com/2008/12/30/the-twelve-bugs-of-christmas-via-ruby/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 13:46:48 +0000</pubDate>
		<dc:creator>Daniel Bernier</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://invisibleblocks.wordpress.com/?p=194</guid>
		<description><![CDATA[Here&#8217;s a little bit of post-Christmas fun, based on the Twelve Days of Christmas.


bugs = %{
     Tell them it&#039;s a feature
     Say it&#039;s not supported
     Change the documentation
     Blame it on the hardware
     Find a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=194&subd=invisibleblocks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Here&#8217;s a little bit of post-Christmas fun, based on the <a href="http://en.wikipedia.org/wiki/Twelve_Days_of_Christmas#In_song">Twelve Days of Christmas</a>.</p>
<pre name="code" class="ruby">

bugs = %{
     Tell them it&#039;s a feature
     Say it&#039;s not supported
     Change the documentation
     Blame it on the hardware
     Find a way around it
     Say they need an upgrade
     Reinstall the software
     Ask for a dump
     Run with the debugger
     Try to reproduce it
     Ask them how they did it and
     See if they can do it again.
}.strip.split(&quot;\n&quot;).map { |bug| bug.strip }.reverse

days = %w[first second third fourth fifth sixth seventh eighth ninth tenth eleventh twelfth]

0.upto(11) do |day_num|
  puts &quot;For the #{days[day_num]} bug of Christmas, my manager said to me&quot;
  day_num.downto(0) do |bug_num|
    puts bugs[bug_num]
  end
  puts
end
</pre>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/invisibleblocks.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/invisibleblocks.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/invisibleblocks.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/invisibleblocks.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/invisibleblocks.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/invisibleblocks.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/invisibleblocks.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/invisibleblocks.wordpress.com/194/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/invisibleblocks.wordpress.com/194/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/invisibleblocks.wordpress.com/194/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=194&subd=invisibleblocks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://invisibleblocks.wordpress.com/2008/12/30/the-twelve-bugs-of-christmas-via-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/360300f6962b973dae989991dc204f75?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bonsaigiant</media:title>
		</media:content>
	</item>
		<item>
		<title>Simplifying Boolean Expressions</title>
		<link>http://invisibleblocks.wordpress.com/2008/12/24/simplifying-boolean-expressions/</link>
		<comments>http://invisibleblocks.wordpress.com/2008/12/24/simplifying-boolean-expressions/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 16:43:25 +0000</pubDate>
		<dc:creator>Daniel Bernier</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://invisibleblocks.wordpress.com/?p=136</guid>
		<description><![CDATA[Many programmers can't simplify boolean expressions...or at least code like they can't.  Here are some basic rules for simplifying boolean expressions, and an example, with some pointers to more material on boolean algebra.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=136&subd=invisibleblocks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>(Apologies if this material seems elementary, but I&#8217;ve found enough code to make me think it should be talked about.)</p>
<p>I found some C# today that looked kind of like this.</p>
<pre name="code" class="csharp">

if (someCondition) {
    return true;
}
return false;
</pre>
<p>This code says the author missed some key points about booleans.  You can find it in almost any language.</p>
<p>Any code that fits into an <em>if</em> or <em>while</em> eventually becomes a boolean.  Using an <em>if</em> to &#8220;return true, else false&#8221; is redundant &#8212; just return the condition.  That code can be simplified down to this one-liner:</p>
<pre name="code" class="csharp">

return someCondition;
</pre>
<p>The same goes for assigning a variable.  This:</p>
<pre name="code" class="csharp">

bool result = false;
if (someCondition) {
    result = true;
}
</pre>
<p>&#8230;is the same as this:</p>
<pre name="code" class="csharp">

bool result = someCondition;
</pre>
<p>(If you feel the longer version is clearer, as <a href="http://c2.com/cgi/wiki?ReturnBooleanEvaluations">some do</a>, I respectfully disagree with you, but that&#8217;s a different point, and I&#8217;m not interested in debating preferences.  You can probably stop reading here, but thanks for stopping by.)</p>
<p>What if your boolean values are swapped?  You can invert your condition:</p>
<pre name="code" class="csharp">

if (someCondition) {
    return false;
}
return true;

// is the same as:
return !someCondition;
</pre>
<p>As the nesting gets deeper, it gets hairier, but it can still be tamed:</p>
<pre name="code" class="csharp">

if (condition1) {
    if (condition2) {
        return true;
    }
    return false;
}
return false;

// is basically:
return condition1 &amp;&amp; condition2;
</pre>
<p>And&#8230;</p>
<pre name="code" class="csharp">

if (condition1) {
    return true;
}
else if (condition2) {
   return true;
}
else {
   return false;
}

// is just:
return condition1 || condition2;
</pre>
<p>There are <a href="http://cs.wellesley.edu/~cs111/spring00/lectures/boolean-simplification.html">many other ways to tame a wild boolean</a> &#8212; follow that link, and check the first table.  It&#8217;s like simplifying an algebraic equation: <em>x</em>+0 is always <em>x</em>, and <em>y</em> &amp;&amp; true is always <em>y</em>.</p>
<h3>An Example</h3>
<p>Let&#8217;s work through a contrived, yet nasty, example, to see how some of this works.</p>
<pre name="code" class="jscript">

function contrivedYetNasty(hasYirmish, isNingle, amount) {
    var tooMuch = false;
    if (amount &gt; 100) {
        tooMuch = true;
    }

    var foo = false;
    if (hasYirmish == false) {
        if (!!tooMuch) {
          foo = true;
        }
        else {
          foo = false;
        }
    }
    else {
        foo = true;
    }

    if (isNingle == true) {
        if (foo == false) {
            return false;
        }
        else {
            return true;
        }
    }
    else {
        return false;
    }
}
</pre>
<p>I have no idea what this does, but it&#8217;s nasty (which is often the situation with legacy code).  These handy unit tests tell us how it behaves:</p>
<pre name="code" class="jscript">

assert(false, contrivedYetNasty(false, false, 0))
assert(true,  contrivedYetNasty(false, true, 110))
assert(false, contrivedYetNasty(true, false, 0))
assert(true,  contrivedYetNasty(true, true, 110))  

assert(false, contrivedYetNasty(false, false, 0))
assert(true,  contrivedYetNasty(false, true, 110))
assert(false, contrivedYetNasty(true, false, 0))
assert(true,  contrivedYetNasty(true, true, 110))
</pre>
<p>First, let&#8217;s tackle <code>tooMuch</code>.  It&#8217;s false, but if <code>amount</code> is over 100, then it&#8217;s true.  If it always has the same truthiness as <code>amount &gt; 100</code>, then it&#8217;s <em>equivalent</em> to <code>amount &gt; 100</code>.  Let&#8217;s write it that way.</p>
<pre name="code" class="jscript">

var tooMuch = amount &gt; 100;
</pre>
<p>The tests pass.</p>
<p>Next, let&#8217;s look inside the <code>if (hasYirmish == false)</code> block, lines 9 &#8211; 14 in the original.  First, <code>!!tooMuch</code> is a double-negative: the first <code>!</code> cancels out the second.  We can just say <code>if (tooMuch)</code>.  &#8220;If <code>tooMuch</code> is true, <code>foo</code> is true; else (if <code>tooMuch</code> is false), <code>foo</code> is false.&#8221;  So <code>foo</code> is the same as <code>tooMuch</code>, and we can rewrite the block like this:</p>
<pre name="code" class="jscript">

if (hasYirmish == false) {
    foo = tooMuch;
}
else {
    foo = true;
}
</pre>
<p>Tests pass.</p>
<p>&#8220;If <code>hasYirmish</code> is false, <code>foo</code> is <code>tooMuch</code>; else, <code>foo</code> is true.&#8221;  This is just like a boolean <em>OR</em> expression.  When <code>a || b</code> is evaluated, if <code>a</code> is true, the expression evaluates to true, without even checking <code>b</code>; but if <code>a</code> is false, then the expression evaluates to the value of <code>b</code>.  And that&#8217;s exactly what we want here.  That block just becomes:</p>
<pre name="code" class="jscript">

var foo = hasYirmish || tooMuch;
</pre>
<p>The tests still pass.  So far, we&#8217;re down to this:</p>
<pre name="code" class="jscript">

function contrivedYetNasty(hasYirmish, isNingle, amount) {
    var tooMuch = amount &gt; 100;
    var foo = hasYirmish || tooMuch;

    if (isNingle == true) {
        if (foo == false) {
            return false;
        }
        else {
            return true;
        }
    }
    else {
        return false;
    }
}
</pre>
<p>Not bad!</p>
<p>Inside the <code>isNingle == false</code> block, on lines 6 &#8211; 11 above, we have: &#8220;if <code>foo</code> is false, return false; else (if it&#8217;s true), return true.&#8221;  Again, we just want to return the value of <code>foo</code>.  Let&#8217;s re-write it that way, test it (they pass), and take a look at the <code>isNingle == true</code> block.</p>
<pre name="code" class="jscript">

if (isNingle == true) {
    return foo;
}
else {
    return false;
}
</pre>
<p>Now, we have a similar situation to when we introduced the <em>OR</em> expression, but it&#8217;s slightly different.  If <code>isNingle</code> is false, the whole thing is false; if it&#8217;s true, then it&#8217;s the value of <code>foo</code>.  Sounds like an <em>AND</em> expression.  Let&#8217;s try it.</p>
<pre name="code" class="jscript">

return isNingle &amp;&amp; foo;
</pre>
<p>The tests still pass.  Let&#8217;s step back and look at our progress:</p>
<pre name="code" class="jscript">

function contrivedYetNasty(hasYirmish, isNingle, amount) {
    var tooMuch = amount &gt; 100;
    var foo = hasYirmish || tooMuch;
    return isNingle &amp;&amp; foo;
}
</pre>
<p>From 31 lines down to five, and it&#8217;s actually readable.  We can in-line those variables, and it gets even clearer:</p>
<pre name="code" class="jscript">

function contrivedYetNasty(hasYirmish, isNingle, amount) {
    return isNingle &amp;&amp; (hasYirmish || amount &gt; 100);
}
</pre>
<p>It returns true &#8220;if isNingle, and either it hasYirmish, or amount is over 100.&#8221;  Much better.</p>
<h3>Beyond the Basics</h3>
<p>Once you&#8217;re comfortable with simplifying boolean expressions, there&#8217;s a number of rules you can employ to refactor nastier boolean expressions.  Most of them are easy to remember, and can be easily illustrated in real-life terms.  Meet <a href="http://en.wikipedia.org/wiki/De_Morgan%27s_laws">DeMorgan</a>:</p>
<ul>
<li><code>!(a || b) == !a &amp;&amp; !b</code>.  &#8220;It&#8217;s not red or green&#8221; is the same as &#8220;It&#8217;s not red, and it&#8217;s not green.&#8221;</li>
<li><code>!(a &amp;&amp; b) == !a || !b</code>.  &#8220;I&#8217;m not rich and handsome&#8221; is true if I&#8217;m not rich, OR if I&#8217;m not handsome.  (Or if I&#8217;m neither.)</li>
</ul>
<p>These rules are part of a larger topic called <a href="http://en.wikipedia.org/wiki/Boolean_algebra_(introduction)">Boolean algebra</a>, which is useful for <a href="http://www.allaboutcircuits.com/vol_4/chpt_7/5.html">simplifying circuits</a>, and (of course) programming.  At my university, Boolean algebra was taught in Discrete Math, which was required for CS majors.  Maybe programmers without a CS degree have a harder time with booleans because they missed this class, but the good news is, it&#8217;s easy enough to <a href="http://en.wikipedia.org/wiki/Boolean_algebra_(introduction)">pick up</a>.</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/invisibleblocks.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/invisibleblocks.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/invisibleblocks.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/invisibleblocks.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/invisibleblocks.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/invisibleblocks.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/invisibleblocks.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/invisibleblocks.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/invisibleblocks.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/invisibleblocks.wordpress.com/136/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=136&subd=invisibleblocks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://invisibleblocks.wordpress.com/2008/12/24/simplifying-boolean-expressions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/360300f6962b973dae989991dc204f75?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bonsaigiant</media:title>
		</media:content>
	</item>
		<item>
		<title>Next Hartford Ruby Group meeting: 11/24</title>
		<link>http://invisibleblocks.wordpress.com/2008/11/18/next-hartford-ruby-group-meeting-1124/</link>
		<comments>http://invisibleblocks.wordpress.com/2008/11/18/next-hartford-ruby-group-meeting-1124/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 13:25:26 +0000</pubDate>
		<dc:creator>Daniel Bernier</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Hartford.rb]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://invisibleblocks.wordpress.com/?p=151</guid>
		<description><![CDATA[The Hartford Ruby Group is meeting this Monday, 11/24, from 6 &#8211; 8 PM.
Flinn Mueller will be talking about rspec, and we&#8217;ll hopefully hear stories from the Voices that Matter attendees.
We&#8217;ll raffle off The Professional Ruby Collection: Mongrel, Rails Plugins, Rails Routing, Refactoring to REST, and Rubyisms CD.
GeeZeo offices
750 Main St, Hartford
Suite 1314
(next to the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=151&subd=invisibleblocks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The <a href="http://groups.google.com/group/hartford-ruby-brigade">Hartford Ruby Group</a> is meeting this Monday, 11/24, from 6 &#8211; 8 PM.</p>
<p><a href="http://www.actsasflinn.com/">Flinn Mueller</a> will be talking about <a href="http://rspec.info/">rspec</a>, and we&#8217;ll hopefully hear stories from the <a href="http://www.voicesthatmatter.com/ruby2008/">Voices that Matter</a> attendees.</p>
<p>We&#8217;ll raffle off <a href="http://www.amazon.com/gp/product/0132417995?ie=UTF8&amp;tag=invisblock-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0132417995">The Professional Ruby Collection: Mongrel, Rails Plugins, Rails Routing, Refactoring to REST, and Rubyisms CD</a>.</p>
<p>GeeZeo offices<br />
750 Main St, Hartford<br />
Suite 1314<br />
(next to the CVS)</p>
<p>Thanks to GeeZeo for hosting us, to Sun for feeding us, and to Addison Wesley for giving us books to raffle off.  Hope to see you there!</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/invisibleblocks.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/invisibleblocks.wordpress.com/151/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/invisibleblocks.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/invisibleblocks.wordpress.com/151/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/invisibleblocks.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/invisibleblocks.wordpress.com/151/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/invisibleblocks.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/invisibleblocks.wordpress.com/151/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/invisibleblocks.wordpress.com/151/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/invisibleblocks.wordpress.com/151/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=151&subd=invisibleblocks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://invisibleblocks.wordpress.com/2008/11/18/next-hartford-ruby-group-meeting-1124/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/360300f6962b973dae989991dc204f75?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bonsaigiant</media:title>
		</media:content>
	</item>
		<item>
		<title>Hartford Ruby Brigade next meeting: 10/27</title>
		<link>http://invisibleblocks.wordpress.com/2008/10/23/hartford-ruby-brigade-next-meeting-1027/</link>
		<comments>http://invisibleblocks.wordpress.com/2008/10/23/hartford-ruby-brigade-next-meeting-1027/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 17:27:58 +0000</pubDate>
		<dc:creator>Daniel Bernier</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Hartford.rb]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://invisibleblocks.wordpress.com/?p=148</guid>
		<description><![CDATA[The Hartford Ruby Group is meeting this Monday, 10/27, at 6:00 PM.
We&#8217;ll be tackling a Ruby Quiz problem, pairing up newcomers with veterans, and we&#8217;ll be raffling off a copy of Design Patterns in Ruby, by Russ Olsen.
GeeZeo offices
750 Main St, Hartford
Suite 1314
(next to the CVS)
Thanks to GeeZeo for hosting us, to Sun for feeding [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=148&subd=invisibleblocks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>The <a href="http://groups.google.com/group/hartford-ruby-brigade">Hartford Ruby Group</a> is meeting this Monday, 10/27, at 6:00 PM.</p>
<p>We&#8217;ll be tackling a <a href="http://rubyquiz.com">Ruby Quiz</a> problem, pairing up newcomers with veterans, and we&#8217;ll be raffling off a copy of <a href="http://www.amazon.com/Design-Patterns-Ruby-Addison-Wesley-Professional/dp/0321490452?tag=invisblock-20">Design Patterns in Ruby</a>, by Russ Olsen.</p>
<p>GeeZeo offices<br />
750 Main St, Hartford<br />
Suite 1314<br />
(next to the CVS)</p>
<p>Thanks to GeeZeo for hosting us, to Sun for feeding us, and to Addison Wesley for giving us books to raffle off.  Hope to see you there!</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/invisibleblocks.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/invisibleblocks.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/invisibleblocks.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/invisibleblocks.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/invisibleblocks.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/invisibleblocks.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/invisibleblocks.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/invisibleblocks.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/invisibleblocks.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/invisibleblocks.wordpress.com/148/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=148&subd=invisibleblocks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://invisibleblocks.wordpress.com/2008/10/23/hartford-ruby-brigade-next-meeting-1027/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/360300f6962b973dae989991dc204f75?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bonsaigiant</media:title>
		</media:content>
	</item>
		<item>
		<title>Hartford Ruby Brigade meeting tonight</title>
		<link>http://invisibleblocks.wordpress.com/2008/09/29/hartford-ruby-brigade-meeting-tonight/</link>
		<comments>http://invisibleblocks.wordpress.com/2008/09/29/hartford-ruby-brigade-meeting-tonight/#comments</comments>
		<pubDate>Mon, 29 Sep 2008 12:50:15 +0000</pubDate>
		<dc:creator>Daniel Bernier</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Hartford.rb]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://invisibleblocks.wordpress.com/?p=146</guid>
		<description><![CDATA[If you&#8217;re in the area, feel free to drop in:
The Hartford Ruby Brigade&#8217;s September meeting is tonight, from 6 to 8 PM, at GeeZeo&#8217;s offices on 750 Main St, Suite 1314, in Hartford (next to the CVS).
The big news is Addison Wesley has a ticket for the Voices That Matter: Professional Ruby Conference this November, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=146&subd=invisibleblocks&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>If you&#8217;re in the area, feel free to drop in:</p>
<p>The Hartford Ruby Brigade&#8217;s September meeting is tonight, from 6 to 8 PM, at <a href="https://www.geezeo.com/">GeeZeo&#8217;s</a> offices on <a href="http://maps.google.com/maps?f=q&amp;hl=en&amp;q=750+main+st+hartford+ct&amp;ie=UTF8">750 Main St, Suite 1314, in Hartford</a> (next to the CVS).</p>
<p>The big news is Addison Wesley has a ticket for the <a href="http://www.voicesthatmatter.com/ruby2008/">Voices That Matter: Professional Ruby Conference</a> this November, and we&#8217;ll raffle it off.  It looks like a sweet conference, go take a look.  You have to be there to win, so if you&#8217;ve been waiting for the right time to make an appearance, this is it.  And since only one of us can win the ticket, we&#8217;ll also raffle off a great Ruby book, Hal Fulton&#8217;s <a href="http://www.amazon.com/Ruby-Way-Second-Addison-Wesley-Professional/dp/0672328844">The Ruby Way</a>.  A big thanks to Addison Wesley!</p>
<p><a href="http://rubydoes.net/">Aaron</a> will give his long-promised IronRuby talk, and I&#8217;ll show a mini-project of mine that generates MSWord .docs from <a href="http://textism.com/tools/textile">Textile</a>, with <a href="http://redcloth.org">RedCloth</a>.  It&#8217;s flashy, I promise.</p>
<p>Remember, if you have a topic you&#8217;d like to talk about, or one you&#8217;d like to hear about, or even if you just have an idea for what to do for the evening, don&#8217;t be shy, speak up!  The group is what we make it, and it belongs to all of us.</p>
<p>As always, we&#8217;ll have free pizza and soda, courtesy of Sun Microsystems.</p>
<p>Hope to see you there!</p>
  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/invisibleblocks.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/invisibleblocks.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/invisibleblocks.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/invisibleblocks.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/invisibleblocks.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/invisibleblocks.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/invisibleblocks.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/invisibleblocks.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/invisibleblocks.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/invisibleblocks.wordpress.com/146/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=invisibleblocks.wordpress.com&blog=290283&post=146&subd=invisibleblocks&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://invisibleblocks.wordpress.com/2008/09/29/hartford-ruby-brigade-meeting-tonight/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/360300f6962b973dae989991dc204f75?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bonsaigiant</media:title>
		</media:content>
	</item>
	</channel>
</rss>