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

<channel>
	<title>eric the fruitbat &#187; Fun</title>
	<atom:link href="http://www.cogitolingua.net/blog/category/fun/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cogitolingua.net/blog</link>
	<description>Sounding out the Noosphere.</description>
	<lastBuildDate>Fri, 03 Feb 2012 23:40:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Merovingian Neckwear</title>
		<link>http://www.cogitolingua.net/blog/2012/01/21/merovingian-neckwear/</link>
		<comments>http://www.cogitolingua.net/blog/2012/01/21/merovingian-neckwear/#comments</comments>
		<pubDate>Sat, 21 Jan 2012 07:38:06 +0000</pubDate>
		<dc:creator>erich</dc:creator>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[Self]]></category>

		<guid isPermaLink="false">http://www.cogitolingua.net/blog/?p=1227</guid>
		<description><![CDATA[<p></p> <p>Somehow, I feel that I betray my cyberpunk heritage because I&#8217;ve recently found it entertaining to don extravagant neckwear. But, my recently conducted anthropological expedition has revealed evidence that I am in fact upholding a veritably geek tradition.</p> ]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cogitolingua.net/blog/wp-content/uploads/2012/01/atlantic.jpg"><img src="http://www.cogitolingua.net/blog/wp-content/uploads/2012/01/atlantic-300x225.jpg" alt="" title="Merovingian Knot" width="300" height="225" class="alignright size-medium wp-image-1228" /></a></p>
<p>Somehow, I feel that I betray my cyberpunk heritage because I&#8217;ve recently found it entertaining to don extravagant neckwear. But, my recently conducted anthropological expedition has revealed <a href="http://xirdal.lmu.de/cgi-bin/blosxom.cgi/2008/06/13">evidence</a> that I am in fact upholding a veritably geek tradition.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cogitolingua.net/blog/2012/01/21/merovingian-neckwear/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>KernCUE</title>
		<link>http://www.cogitolingua.net/blog/2011/10/24/kerncue/</link>
		<comments>http://www.cogitolingua.net/blog/2011/10/24/kerncue/#comments</comments>
		<pubDate>Mon, 24 Oct 2011 07:34:57 +0000</pubDate>
		<dc:creator>erich</dc:creator>
				<category><![CDATA[Education]]></category>
		<category><![CDATA[Fun]]></category>
		<category><![CDATA[People]]></category>
		<category><![CDATA[Self]]></category>

		<guid isPermaLink="false">http://www.cogitolingua.net/blog/?p=814</guid>
		<description><![CDATA[<p>This past weekend, I made the trip up to Bakersfield. It took far longer than necessary to drive there because, even at midnight, the 5 is clogged with traffic. I went to attend the KernCue conference. Although, it focused exclusively on the K-12 system, and I&#8217;m focused at the college level, I still had quite [...]]]></description>
			<content:encoded><![CDATA[<p>This past weekend, I made the trip up to Bakersfield. It took far longer than necessary to drive there because, even at midnight, the 5 is clogged with traffic. I went to attend the <a href="http://kerncue.org/">KernCue</a> conference. Although, it focused exclusively on the K-12 system, and I&#8217;m focused at the college level, I still had quite a bit of fun; and picked up a couple of interesting tips.</p>
<p>The conference was cheap ($30) and quite short: 8am to 3pm, lunch and breakfast included. (I paid $70 for the hotel, just so I wouldn&#8217;t have to wake up and drive). It had enough presenters for several tracks. I was only able to goto 3 of the talks. The ones that I chose to attend must not have been very popular, because each audience was only about 4 people, including myself. That&#8217;s alright though, because it really gives you the opportunity to ask more questions, and get more information.</p>
<p>One presenter, Craig Whitmore, has been working with Norris Middle School students, teaching them to program via <a href="http://scratch.mit.edu/">scratch</a>. A few of his student are there because they didn&#8217;t get the elective class they actually wanted. But that doesn&#8217;t deter him from showing them the magic of programming. He finds that everyone has fun, and the students tend to divide up among those that are really into the artistic aspect and those that enjoy more the computational aspect. Most students are engaged by the ability to author their own games. Some of the highlights can be found <a href="http://scratch.mit.edu/tags/view/norris%20middle%20school">here</a>. He also teaches the science class, and was able to use scratch to simulate organic compound mixing, and have students identify an unknown material via testing. Scratch was appropriate, because the school couldn&#8217;t afford the chemicals involved (and it lowers the danger). He also has a <a href="http://www.lulu.com/spotlight/craigwhitmore">book</a> about it.</p>
<p>Anything that can get students, no matter how young, creating with the computer is a great thing. So much the better if they get the programming bug.</p>
<p>Another presenter, <a href="http://catlintucker.com/">Catlin Tucker</a>, an English Teacher of Windsor School District, talked about how to &#8216;flip&#8217; your classroom. The idea of <a href="http://www.economist.com/node/21529062">flipping the classroom</a> is basically getting the student to do the boring, individual study at home; and do the engaging group stuff in the classroom. Some of her techniques including running a collaborative online discussion, usually kick-started by a video and open-ended question (spurs opinion and debate). She&#8217;s got students posting 3-5 full-length paragraphs with argument and analysis of their positions without specifying length as a requirement. But how do you get students to do the reading at home? By doing something fun enough in the classroom that those who aren&#8217;t prepared feel left out, and are sentenced to Cornell Notes or other individualized material review instead. It&#8217;s really cool that she&#8217;s able to engage the students, but I&#8217;m not quite sure how these techniques will scale to a college intro class of 150 students.</p>
<p>The keynote speaker, Jon Corippo, was fantastic. He wasn&#8217;t afraid to yell and shout, emulating the raw excitement that 3rd graders can feel when really interesting in something. He had audience interaction, by demoing some teaching techniques, and giving away a $1 to someone that could produce an appositive phrase (proving that almost nobody knows what that is, how sad our education didn&#8217;t stick, it must not have been engaging enough to remember) He showed some educational games that can engage a whole class. He also showed how you can teach plot device through youTube clips of commercials (as any good commercial is a 30-second drama). It works really well to give students scaffolding for a problem. Boxes and lines to fill in. After analyzing some work that way, you can then ask them to create their own works. The scaffolding, which identifies essential components, now operates in reverse, instead of breaking apart for analysis, it&#8217;s used to synthesize and gather pieces together.</p>
<p>He spouted out a large number of online and free resources for those working on a budget. For about $60 for an AppleTV, you can connect the iPad2 wirelessly to a projector: a walk-around tablet! Also, <a href="http://www.edubuntu.org/">edubuntu</a>, <a href="http://www.freetech4teachers.com/">freetech4teachers</a>, <a href="http://edutecher.net/">edutecher app</a>, <a href="goo.gl">goo.gl</a> which has analytics! and more that I didn&#8217;t write down.</p>
<p>Although the K12 isn&#8217;t my focus at all, it was interesting to see the problems they face, and the solutions they&#8217;re trying. The people there were wonderfully excited to be together and sharing. The only unfortunate part is, how small the representation, given the size of Kern County. How many teachers aren&#8217;t interested enough to improve themselves?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cogitolingua.net/blog/2011/10/24/kerncue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Progress Phallus</title>
		<link>http://www.cogitolingua.net/blog/2011/09/25/the-progress-phallus/</link>
		<comments>http://www.cogitolingua.net/blog/2011/09/25/the-progress-phallus/#comments</comments>
		<pubDate>Sun, 25 Sep 2011 23:31:04 +0000</pubDate>
		<dc:creator>erich</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://www.cogitolingua.net/blog/?p=775</guid>
		<description><![CDATA[<p>My friend Ray runs numerical simulations and wanted to have a decent progress bar for his code. A bit of web searching revealed some code, that takes care of this automatically. However, we thought that an ordinary progress bar is simply too conventional. We should gay it up with some ascii art into a beautiful [...]]]></description>
			<content:encoded><![CDATA[<p>My friend Ray runs numerical simulations and wanted to have a decent progress bar for his code. A bit of web searching revealed <a href="http://nakkaya.com/2009/11/08/command-line-progress-bar/">some code</a>, that takes care of this automatically. However, we thought that an ordinary progress bar is simply too conventional. We should gay it up with some ascii art into a beautiful progress phallus!</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;unistd.h&gt;</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">void</span> printProgBar<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> percent<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> bar<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">50</span><span style="color: #008080;">;</span> i<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">&lt;</span> <span style="color: #008000;">&#40;</span>percent<span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            bar.<span style="color: #007788;">replace</span><span style="color: #008000;">&#40;</span>i, <span style="color: #0000dd;">1</span>, <span style="color: #FF0000;">&quot;=&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>i <span style="color: #000080;">==</span> <span style="color: #008000;">&#40;</span>percent<span style="color: #000040;">/</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            bar.<span style="color: #007788;">replace</span><span style="color: #008000;">&#40;</span>i, <span style="color: #0000dd;">1</span>, <span style="color: #FF0000;">&quot;D&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
            bar.<span style="color: #007788;">replace</span><span style="color: #008000;">&#40;</span>i, <span style="color: #0000dd;">1</span>, <span style="color: #FF0000;">&quot; &quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>percent <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">100</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span> <span style="color: #FF0000;">&quot;8&quot;</span> <span style="color: #000080;">&lt;&lt;</span> bar <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;@ &quot;</span><span style="color: #008080;">;</span>
        std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span>.<span style="color: #007788;">width</span><span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">3</span> <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> percent <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;%     &quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">flush</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span>
        std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span> <span style="color: #FF0000;">&quot;8&quot;</span> <span style="color: #000080;">&lt;&lt;</span> bar<span style="color: #008080;">;</span>
        std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;D~~~~ ~~~ ~&quot;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">flush</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">95</span><span style="color: #008080;">;</span> i<span style="color: #000040;">+</span><span style="color: #000080;">=</span><span style="color: #0000dd;">10</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        sleep<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        printProgBar<span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #000080;">=</span><span style="color: #0000dd;">95</span><span style="color: #008080;">;</span> i<span style="color: #000080;">&lt;=</span><span style="color: #0000dd;">100</span><span style="color: #008080;">;</span> <span style="color: #000040;">++</span>i<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        sleep<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        printProgBar<span style="color: #008000;">&#40;</span>i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
    std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<pre>
8D                                                 @   0%
8=====D                                            @  10%
8==========D                                       @  20%
8===============D                                  @  30%
8====================D                             @  40%
8=========================D                        @  50%
8==============================D                   @  60%
8===================================D              @  70%
8========================================D         @  80%
8=============================================D    @  90%
8===============================================D  @  95%
8================================================D @  96%
8================================================D @  97%
8=================================================D@  98%
8=================================================D@  99%
8==================================================D~~~~ ~~~ ~
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.cogitolingua.net/blog/2011/09/25/the-progress-phallus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cool stuff to look at</title>
		<link>http://www.cogitolingua.net/blog/2011/08/06/cool-stuff-to-look-at/</link>
		<comments>http://www.cogitolingua.net/blog/2011/08/06/cool-stuff-to-look-at/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 23:08:10 +0000</pubDate>
		<dc:creator>erich</dc:creator>
				<category><![CDATA[Comp*]]></category>
		<category><![CDATA[Fun]]></category>
		<category><![CDATA[People]]></category>
		<category><![CDATA[Tech*]]></category>

		<guid isPermaLink="false">http://www.cogitolingua.net/blog/?p=625</guid>
		<description><![CDATA[<p>Steve Yegge gave a nice talk at OSCON, What would you do with your own Google?, compelling us all to focus on math, stats, machine learning, and the understanding the core fundamentals. We should be working to make the world a better place! Use our knowledge of scaling and systems, to solve big, important problems [...]]]></description>
			<content:encoded><![CDATA[<p>Steve Yegge gave a nice talk at OSCON, <a href="http://www.youtube.com/watch?v=vKmQW_Nkfk8">What would you do with your own Google?</a>, compelling us all to focus on math, stats, machine learning, and the understanding the core fundamentals. We should be working to make the world a better place! Use our knowledge of scaling and systems, to solve big, important problems (medicine, cancer, etc.) not on optimizing ads or cat pictures. From this talk, I think we should all spend an hour each day on learning and self-improvement!</p>
<p>Alan Kay has a nice talk, <a href="http://tele-task.de/archive/video/flash/14029/">Programming and Scaling</a> (<a href="http://lambda-the-ultimate.org/node/4325">also at LtU</a>), where he carefully, and patiently explains that our software today is tinker-toys. We haven&#8217;t raised our level of consciousness, we aren&#8217;t innovating ideas, and our languages aren&#8217;t powerful enough. I like this because, at the end of the talk he demonstrates how to stitch pieces together with greater effect: <a href="">OMeta</a>.</p>
<p>Ungar has a video given at Stanford called <a href="">Self and Self: Whys and Wherefores</a> where he shows clips demonstrating the Alternate Reality Kit . The ARK, in my opinion, represents alot of what Kay has been doing in Squeak. Ungar, uses these clips to argue that &#8220;simplicity begats uniformity begats malleability&#8221; (Randy Smith). When designing a system, we should try to keep it small and elegant. This is not just an aesthetic issue, but it&#8217;s an engineering complexity issue. By striving to keep the system small, we factor out the core ideas behind the system. This helps use to explicitly identify the underlying similarities of what we are trying to model. This uniformity make for a better system,  and gives it a cohesive framework. You&#8217;ll notice the same lesson is available from Unix, where Thompson and Ritchie credit the idea of &#8216;piping&#8217; with a fundamentally better means of program interactivity, and the &#8216;file object&#8217; as the enabler. (The file interface was later improved in the Plan 9 OS). The uniformity can also be brought to the fore, by language. A core language for expressing the core ideas. You should also not add to the design of the language by looking at clever examples that show off the feature. Instead, step back, and see if the new rules fit in with the principles of the system.</p>
<p>These demos of old systems (especially the <a href="http://en.wikipedia.org/wiki/The_Mother_of_All_Demos">Mother of All Demos</a> (<a href="http://www.youtube.com/watch?v=JfIgzSoTMOs">vid</a>)), really pack a punch. It kills me to think that we&#8217;ve been getting it wrong for so long. The mediocre triumphs because our brains are too slow. As Kay observes in the talk above: We can&#8217;t conceptualize our future except by baby-step-iteration of the present. This is a sad state of affairs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cogitolingua.net/blog/2011/08/06/cool-stuff-to-look-at/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Higher-Order Functions in Java</title>
		<link>http://www.cogitolingua.net/blog/2011/07/22/higher-order-functions-in-java/</link>
		<comments>http://www.cogitolingua.net/blog/2011/07/22/higher-order-functions-in-java/#comments</comments>
		<pubDate>Sat, 23 Jul 2011 04:21:01 +0000</pubDate>
		<dc:creator>erich</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[Comp*]]></category>
		<category><![CDATA[Education]]></category>
		<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://www.cogitolingua.net/blog/?p=543</guid>
		<description><![CDATA[<p> This Summer I&#8217;ve been teaching Intro to Computer Languages. It&#8217;s a really awesome class, with alot of good material in it. Last week, my students got back the results of their midterms, and were disappointed in their scores. Immediately, I heard some clamoring for an extra credit. Not only was the average midterm score [...]]]></description>
			<content:encoded><![CDATA[<p>
This Summer I&#8217;ve been teaching <a href="http://www.ics.uci.edu/~ehenniga/ics141/index.html">Intro to Computer Languages</a>.  It&#8217;s a really awesome class, with alot of good material in it.  Last week, my students got back the results of their midterms, and were disappointed in their scores.  Immediately, I heard some clamoring for an extra credit. Not only was the average midterm score pretty low, but also the class has been jumping rapidly from one topic to another. The schedule was originally planned for a 10 week course, but has been compressed for a 6 week Summer Session.  I don&#8217;t think that student learning rate improves by almost a factor of 2 just because it&#8217;s summer an the lecture is 3hrs instead of the normal 1hr 30min. Quite the contrary, I think retention is best achieved as a result of long-term exposure to material, precisely the opposite of our current situation.</p>
<p>
Prior to the midterm, in Project 2, we had done an exercise writing a in-order iterator over a binary search tree. This exercise had two important components: (1) The iterator allows pausing between elements. So, it must have a way to remember where it left off the last time an element was returned. (2) The binary search tree could be storing any type of data. So, the entire project had to satisify the typing constraints of Java&#8217;s generics system.</p>
<p>
Then, in the following week (I told you the class moves quick), We used Haskell in Project 3 to cover the concept of <a href="http://en.wikipedia.org/wiki/Higher-order_function">Higher Order Functions</a> (HOF). Now, most of my students have had their minds trained on Java, which doesn&#8217;t provide <a href="http://en.wikipedia.org/wiki/First-class_function">first class functions</a>. So, the concept that a function might take other functions as arguments or return a function as a result is really foreign.</p>
<p>
Since my students were clamoring for some extra credit, I tried to think up something that might tie together what has been done so far. My predecessor, <a href="http://alexthornton.net/Blog.aspx">Alex Thornton</a>, had done a Higher Order Function implementation in C# as a fun and interesting demo for his last lecture. Now, I don&#8217;t know C#, but I&#8217;m pretty sure that I could do the same in Java.  It turns out you can create for yourself a java Framework that provides this higher order functions, with a bit of work.  Although, as we shall see, it&#8217;s verbose and clunky.</p>
<p>
Let&#8217;s begin by defining what a function looks like. Since Java doesn&#8217;t support first-class functions, we start by defining a <a href="http://en.wikipedia.org/wiki/Function_object">Function Object</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> IFunction<span style="color: #339933;">&lt;</span>A, B<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
	B call<span style="color: #009900;">&#40;</span>A arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Basically, any object that wishes to act as a function from A &rarr; B, implements the IFunction interface, so that it has a <code>call</code> method.</p>
<p>
Next, I try and think of some task that would use some HOFs. So, let&#8217;s first create a list of <code>String</code>s, and print them out.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> words <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    words.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;File&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    words.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Edit&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    words.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Source&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    words.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Refactor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    words.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Navigate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    words.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Search&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    words.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Project&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    words.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Run&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    words.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Window&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    words.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Help&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Starting With: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    FunctionLib.<span style="color: #006633;">map</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PrintFunction<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, words<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
Instead of using the normal for-each loop to I print out all the elements in a list, I map a <code>PrintFunction</code> (specialized on <code>String</code>) onto the list. Let&#8217;s look at the HOF <code>map</code> first. We know from using Haskell that <code>map</code> takes a function and a list, and applies that function to every element in the list.  So, <code>map</code> takes two arguments: (1) a function from A &rarr; B, and (2) a list of A. Finally, after applying the function to every element, it returns a list of B. We collect all the HOFs we&#8217;ll implement into a <code>FunctionLib</code> class.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FunctionLib <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// map :: (a -&gt; b) -&gt; [a] -&gt; [b]</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>A,B<span style="color: #339933;">&gt;</span> List<span style="color: #339933;">&lt;</span>B<span style="color: #339933;">&gt;</span> map<span style="color: #009900;">&#40;</span>IFunction<span style="color: #339933;">&lt;</span>A, B<span style="color: #339933;">&gt;</span> fun, List<span style="color: #339933;">&lt;</span>A<span style="color: #339933;">&gt;</span> args<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        List<span style="color: #339933;">&lt;</span>B<span style="color: #339933;">&gt;</span> results <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>B<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>A a <span style="color: #339933;">:</span> args<span style="color: #009900;">&#41;</span>
            results.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>fun.<span style="color: #006633;">call</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> results<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
I&#8217;m mappping a &lt;code&gt;PrintFunction&lt;/code&gt; that has the side-effect of printing each element it is called on. The primary difficulty with doing this is that according to my <code>IFunction</code> interface, I have to return something that fits in Java&#8217;s generics system. So, I specialize the return type parameter to <code>Object</code> and return <code>null</code>. Technically, the <code>map</code> method will remember the resulting <code>null</code>s, and build a list of them, which then gets discarded. A bit wasteful, but it&#8217;s an <a href="http://en.wikipedia.org/wiki/Eager_evaluation">eager language</a>, so there&#8217;s not much we can do to avoid that.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PrintFunction<span style="color: #339933;">&lt;</span>E<span style="color: #339933;">&gt;</span> <span style="color: #000000; font-weight: bold;">implements</span> IFunction<span style="color: #339933;">&lt;</span>E, Object<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> call<span style="color: #009900;">&#40;</span>E arg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>arg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
I don&#8217;t especially like creating a whole class file for each function I might want to pass into an HOF. Fortunately, Java supports <a href="http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm">anonymous classes</a>. Let&#8217;s create one for getting the length of a string, call it <code>lengthFunction</code>, and pass it into <code>map</code>, so that we get back an array of <code>Integer</code>s representing the lengths.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The length of each String is: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    IFunction<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span> lengthFunction <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> IFunction<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Integer</span> call<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> arg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> arg.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    List<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> lengths<span style="color: #339933;">=</span> FunctionLib.<span style="color: #006633;">map</span><span style="color: #009900;">&#40;</span>lengthFunction, words<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    FunctionLib.<span style="color: #006633;">map</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PrintFunction<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, lengths<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Although, not creating a class file for each new function is nice, it&#8217;s still hideously verbose to create a whole class. Oh, do I wish that Java provided syntax for <a href="http://en.wikipedia.org/wiki/Lambda_(programming)">lambda</a>&#8216;s.</p>
<p>
Now that I have two lists, one of words and another of their lengths, I should be able to stitch them together. Haskell provides another HOF for precisely this task: <code>zip</code> is a function that takes two lists and returns a pair of their elements. The resulting list of pairs is the same length as the shortest input list.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FunctionLib <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// zip :: [a] -&gt; [b] -&gt; [(a, b)]</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>A,B<span style="color: #339933;">&gt;</span> List<span style="color: #339933;">&lt;</span>Pair<span style="color: #339933;">&lt;</span>A,B<span style="color: #339933;">&gt;&gt;</span> zip<span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>A<span style="color: #339933;">&gt;</span> listA, List<span style="color: #339933;">&lt;</span>B<span style="color: #339933;">&amp;&gt;</span> listB<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        List<span style="color: #339933;">&lt;</span>Pair<span style="color: #339933;">&lt;</span>A,B<span style="color: #339933;">&gt;</span> results <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>Pair<span style="color: #339933;">&lt;</span>A,B<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> len <span style="color: #339933;">=</span> <span style="color: #003399;">Math</span>.<span style="color: #006633;">min</span><span style="color: #009900;">&#40;</span>listA.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, listB.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>len<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            results.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Pair<span style="color: #339933;">&lt;</span>A,B<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>listA.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span>, listB.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> results<span style="color: #339933;">;</span>    
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
Java is so hideously dysfunctional that it doesn&#8217;t come with a native <code>Pair</code> class. However, it&#8217;s easy to make one.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Pair<span style="color: #339933;">&lt;</span>A,B<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> A first<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> B second<span style="color: #339933;">;</span>
&nbsp;
    Pair<span style="color: #009900;">&#40;</span>A a, B b<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        first <span style="color: #339933;">=</span> a<span style="color: #339933;">;</span>
        second <span style="color: #339933;">=</span> b<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> firstStr <span style="color: #339933;">=</span> first <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> first.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;null&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> secondStr <span style="color: #339933;">=</span> second <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> second.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;null&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;(&quot;</span> <span style="color: #339933;">+</span> firstStr <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, &quot;</span> <span style="color: #339933;">+</span> secondStr <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
After all that monstrous cruft, we can finally get the list of pairs containing each word and its length.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    List<span style="color: #339933;">&lt;</span>Pair<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;&gt;</span> associations <span style="color: #339933;">=</span> FunctionLib.<span style="color: #006633;">zip</span><span style="color: #009900;">&#40;</span>words, lengths<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
But we&#8217;ve yet to do anything really interesting with that. So, let&#8217;s filter the list for words that are greater than 5 letters long. Haskell also provides an HOF <code>filter</code> which takes a function that is applied to every element in the list, and a list on which to apply it, returning a list of those elements for which the function evaluated true.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FunctionLib <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// filter :: (a -&gt; bool) -&gt; [a] -&gt; [a]</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>A<span style="color: #339933;">&gt;</span> List<span style="color: #339933;">&lt;</span>A<span style="color: #339933;">&gt;</span> filter<span style="color: #009900;">&#40;</span>IFunction<span style="color: #339933;">&lt;</span>A, Boolean<span style="color: #339933;">&gt;</span> pred, List<span style="color: #339933;">&lt;</span>A<span style="color: #339933;">&gt;</span> listA<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        List<span style="color: #339933;">&lt;</span>A<span style="color: #339933;">&gt;</span> results <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>A<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>A a <span style="color: #339933;">:</span> listA<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>pred.<span style="color: #006633;">call</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                results.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> results<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
I use this function to pull out the pairs which have a string longer than 5 characters. Again, I again use the verbose anonymous class.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The words with length bigger than 5 are:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    IFunction<span style="color: #339933;">&lt;</span>Pair<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span>, Boolean<span style="color: #339933;">&gt;</span> biggerThanFive <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> IFunction<span style="color: #339933;">&lt;</span>Pair<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span>, Boolean<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Boolean</span> call<span style="color: #009900;">&#40;</span>Pair<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;</span> arg<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> arg.<span style="color: #006633;">second</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    List<span style="color: #339933;">&lt;</span>Pair<span style="color: #339933;">&lt;</span>String, Integer<span style="color: #339933;">&gt;&gt;</span> longerThanFive <span style="color: #339933;">=</span> FunctionLib.<span style="color: #006633;">filter</span><span style="color: #009900;">&#40;</span>biggerThanFive, associations<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
But, I&#8217;m really only interested in the strings, not the pairs. So, now I want a way to pull out only the first element of each pair.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> stringsLongerThanFive <span style="color: #339933;">=</span> FunctionLib.<span style="color: #006633;">map</span><span style="color: #009900;">&#40;</span>Pair.<span style="color: #006633;">firstFunction</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #003399;">Integer</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>, longerThanFive<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    FunctionLib.<span style="color: #006633;">map</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PrintFunction<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, stringsLongerThanFive<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>
Notice, that this time, I&#8217;ve given myself a convenience method, <code>Pair.firstFunction</code>. I&#8217;d really have preferred something like C++&#8217;s syntax here, but Java&#8217;s generic system has some pretty fundamental brokenness. First, as a result of <a href="http://download.oracle.com/javase/tutorial/java/generics/erasure.html">type-erasure</a>, the system doesn&#8217;t know what the type parameters into the function are at runtime. That makes an expression like <code>Pair&lt;String,Integer&gt;.firstFunction</code> invalid, because the type parameters are thrown away way before the method call takes place. Second, the type parameters to a generic method are determined via inspection on the types of the provided actual arguments. That makes an expression like <code>Pair.firstFunction&lt;String,Integer&gt;</code> invalid. You don&#8217;t pass type parameters at the function call site (as you would when you instantiate a generic object). Instead, as a workaround, you have to provide dummy actual arguments, that the typing system uses to specialize the call.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Pair<span style="color: #339933;">&lt;</span>A,B<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    ...
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>A,B<span style="color: #339933;">&gt;</span> IFunction<span style="color: #339933;">&lt;</span>Pair<span style="color: #339933;">&lt;</span>A, B<span style="color: #339933;">&gt;</span>, A<span style="color: #339933;">&gt;</span> firstFunction<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>A<span style="color: #339933;">&gt;</span> a, Class<span style="color: #339933;">&lt;</span>B<span style="color: #339933;">&gt;</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> IFunction<span style="color: #339933;">&lt;</span>Pair<span style="color: #339933;">&lt;</span>A, B<span style="color: #339933;">&gt;</span>, A<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">public</span> A call<span style="color: #009900;">&#40;</span>Pair<span style="color: #339933;">&lt;</span>A, B<span style="color: #339933;">&gt;</span> arg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> arg.<span style="color: #006633;">first</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>A,B<span style="color: #339933;">&gt;</span> IFunction<span style="color: #339933;">&lt;</span>Pair<span style="color: #339933;">&lt;</span>A,B<span style="color: #339933;">&gt;</span>, B<span style="color: #339933;">&gt;</span> secondFunction<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>A<span style="color: #339933;">&gt;</span> a, Class<span style="color: #339933;">&lt;</span>B<span style="color: #339933;">&gt;</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> IFunction<span style="color: #339933;">&lt;</span>Pair<span style="color: #339933;">&lt;</span>A, B<span style="color: #339933;">&gt;</span>, B<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">public</span> B call<span style="color: #009900;">&#40;</span>Pair<span style="color: #339933;">&lt;</span>A, B<span style="color: #339933;">&gt;</span> arg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> arg.<span style="color: #006633;">second</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
Things are a little bit different here than the <code>PrintFunction</code> we implemented earlier. We should like to keep the function that pulls out the first element of a pair in the <code>Pair</code> class. Doing this is not only trick because of Java&#8217;s generic type system deficiencies, but also because we can&#8217;t simply name a function as a reference (as you can in C). Instead, we create a <code>firstFunction</code> method, that when called with type parameters as actual arguments, returns an appropriately specialized <code>IFunction</code> which returns the first element when you pass it a pair in the <code>call</code>. Creation of the returned IFunction is also achieved via an anonymous class.</p>
<p>
We now turn to the coup de gr&acirc;ce. We have a list of all the strings with length bigger than 5, so let&#8217;s use an HOF to concatenate them. Haskell again provides us with a function, <code>foldl1</code> that will start at the beginning of a list and inductively apply a two argument function onto it, returning a single result. Essentially, if <code>+</code> were my string concatenation operator and my list were <code>[a, b, c, d, ...]</code> I&#8217;d be calculating <code>(..((a + b) + c) + d) + ..)</code>. It&#8217;s a bit tricky in our implementation though. We have to pass into the HOF a function that takes two arguments, but my <code>IFunction</code> interface is only able to represent functions of a single argument. What shall we do?</p>
<p>
<a href="http://en.wikipedia.org/wiki/Currying">Currying</a> to the rescue! Haskell types the first argument to <code>foldl1</code> as (a -&gt; a -&gt; a). Which you might na&iuml;vely think is an function of two arguments, but the arrow operator is right-associative. So, it&#8217;s actually parsed as (a -&gt; (a -&gt; a)), which is a function of one argument that returns a function of one argument. So, a function of two arguments is equivalent to a nested chain of functions, each accepting one argument. Which means, the <code>IFunction</code> interface is enough!</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FunctionLib <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// foldl1 :: (a -&gt; a -&gt; a) -&gt; [a] -&gt; a</span>
    <span style="color: #666666; font-style: italic;">// In Haskell the function type operator -&gt; is right associative,</span>
    <span style="color: #666666; font-style: italic;">// So, the function description &quot;a -&gt; a -&gt; a&quot; is parsed as &quot;a -&gt; (a -&gt; a)&quot;</span>
    <span style="color: #666666; font-style: italic;">// This order of application is represented in the nesting of IFunction's below</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>A<span style="color: #339933;">&gt;</span> A foldl1<span style="color: #009900;">&#40;</span>IFunction<span style="color: #339933;">&lt;</span>A, IFunction<span style="color: #339933;">&lt;</span>A,A<span style="color: #339933;">&gt;&gt;</span> fn, List<span style="color: #339933;">&lt;</span>A<span style="color: #339933;">&gt;</span> listA<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        A accumulator <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>listA.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">return</span> accumulator<span style="color: #339933;">;</span>
&nbsp;
        accumulator <span style="color: #339933;">=</span> listA.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>listA.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">return</span> accumulator<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>listA.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// currying means two .call invocations</span>
            accumulator <span style="color: #339933;">=</span> fn.<span style="color: #006633;">call</span><span style="color: #009900;">&#40;</span>accumulator<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">call</span><span style="color: #009900;">&#40;</span>listA.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> accumulator<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
Finally, I can wrap up this exercise by applying a concatenation operator to a call to <code>foldl1</code>. This ends up being some of the most ugly code that can be written. Unlike the previous calls to <code>map</code> and <code>filter</code>, I did not want to save the temporary function into a variable. Rather, I&#8217;ve written it inline. Unfortunately, because of the currying, we have to nest anonymous functions!</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;When we concatenate these we get:&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">String</span> concat <span style="color: #339933;">=</span> FunctionLib.<span style="color: #006633;">foldl1</span><span style="color: #009900;">&#40;</span>
        <span style="color: #000000; font-weight: bold;">new</span> IFunction<span style="color: #339933;">&lt;</span>String, IFunction<span style="color: #339933;">&gt;</span>String, String<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">public</span> IFunction<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span> call<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> arg1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> IFunction<span style="color: #339933;">&lt;</span>String, String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> call<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> arg2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000000; font-weight: bold;">return</span> arg1 <span style="color: #339933;">+</span> arg2<span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>,
        stringsLongerThanFive<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>concat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>
What&#8217;s going on here? Remember, we are representing a function of two arguments as nested functions of a single argument. So for example, I want to concatenate the list <code>[a, b, c, d]</code>, I would pass in the concatenation operator, <code>(+)</code>. Inside of <code>foldl1</code>, we take out the first element, <code>a</code> and invoke the <code>call</code> method with the first argument. This returns <code>(a+)</code>, which is the concatenation operator with the first argument filled in. A second invocation is used to pass in the second argument, and that returns a string, which is saved in an accumulator.</p>
<p>
This level of indirection means that I have to have some way of returning a function with the first argument filled in.  I accomplish this by nesting anonymous classes and using a <a href="http://en.wikipedia.org/wiki/Closure_(computer_science)">closure</a>. The outer <code>call</code> method takes <code>final String arg1</code> as an argument. <code>final</code> is necessary, because I have to tell Java that it is OK to &#8216;save&#8217; or &#8216;cache&#8217; this argument. I&#8217;m guaranteeing that I don&#8217;t have any code within this method that might change what <code>arg1</code> referrs to. Once I&#8217;ve made this guarantee, I&#8217;m able to create and return a <code>new IFunction</code> with its own <code>call(String arg2)</code> that uses the outer <code>arg1</code>.</p>
<p>
After going through all this awesome Computer Science (weird rules about the generics system, anonymous inner classes, currying, and closures), I decided that this is not something I could reasonably expect my students to do as extra credit. First, I would want extra credit to assist my student&#8217;s absorption of concepts, and yet still not be so difficult that it becomes a distraction to actual coursework. Second, it should be easy enough to help the students that need it to improve their grade. Although this example does hit the generics and higher-order functions topics, I think it has too many complex issues rolled together. For example, I&#8217;ve never actually had to use closures and anonymous inner classes before. Now, suddenly, I use them both in a single project!</p>
<p>UPDATE Mon Aug  8 19:56:37 PDT 2011: Jim Duey has a talk <a href="http://www.infoq.com/presentations/Functional-Programming-A-Pragmatic-Introduction">Functional Programming: A Pragmatic Introduction</a>. At around 30mins he shows how Laziness can be used to implement a message queue for concurrent programs.</p>
<p>UPDATE Tue Aug  9 11:30:16 PDT 2011: You can download the project source <a href="http://www.cogitolingua.net/blog/wp-content/uploads/2011/08/FunctionalJava.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cogitolingua.net/blog/2011/07/22/higher-order-functions-in-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arrow&#8217;s voting theorem of Economics</title>
		<link>http://www.cogitolingua.net/blog/2011/03/08/arrows-voting-theorem-of-economics/</link>
		<comments>http://www.cogitolingua.net/blog/2011/03/08/arrows-voting-theorem-of-economics/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 06:11:58 +0000</pubDate>
		<dc:creator>erich</dc:creator>
				<category><![CDATA[Economics]]></category>
		<category><![CDATA[Fun]]></category>
		<category><![CDATA[Ideas]]></category>
		<category><![CDATA[People]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.cogitolingua.net/blog/?p=465</guid>
		<description><![CDATA[<p>I was in the dining hall, and the TV there had some talking heads babbling about the stock market. I couldn&#8217;t really make out what they were saying, but it set the stage in my mind for some other thoughts. I was informed recently that the High Frequency Traders, are really just a natural response [...]]]></description>
			<content:encoded><![CDATA[<p>I was in the dining hall, and the TV there had some talking heads babbling about the stock market. I couldn&#8217;t really make out what they were saying, but it set the stage in my mind for some other thoughts. I was informed recently that the High Frequency Traders, are really just a natural response to the exchange incentives (HTF&#8217;ers get paid for volume created, so all they have to do is make sure they trade alot every day, and break even on any price differential). This really quite perturbed be, as I want a market that is more easily accessible by someone with my income level (slave-wage grad student). I also want a market that is more fair (whatever that might mean).</p>
<p>Deep in the recesses of my mind, I knew something about fairness. It is not always to be had. For example, there is <a href="http://en.wikipedia.org/wiki/Arrow%27s_impossibility_theorem">Arrow&#8217;s Impossibility Theorem</a>:</p>
<blockquote><p>
In short, the theorem proves that no voting system can be designed that satisfies these three &#8220;fairness&#8221; criteria:</p>
<ul>
<li> If every voter prefers alternative X over alternative Y, then the group prefers X over Y.</li>
<li> If every voter&#8217;s preference between X and Y remains unchanged, then the group&#8217;s preference between X and Y will also remain unchanged (even if voters&#8217; preferences between other pairs like X and Z, Y and Z, or Z and W change).</li>
<li>There is no &#8220;dictator&#8221;: no single voter possesses the power to always determine the group&#8217;s preference.</li>
</ul>
</blockquote>
<p>So, sometimes fairness isn&#8217;t achievable. In particular, Ken Arrow has proven it unattainable in a voting system. But, isn&#8217;t the stock market, really just a giant online voting system? where people casting their bets as dollars can be seen as people casting their votes as ballots? Doesn&#8217;t the market clearing algorithm have to ensure some fairness criteria akin to that listed above? I don&#8217;t really have time to look into the issue too deeply, but since Ken Arrow is a very esteemed economist, he may have already published something on the topic. At least, I would expect results concerning market behavior, though my idea concerning a proof of impossibility for a fair market clearing algorithm might be a bit too specific.</p>
<p>Then, if we accept the hypothesis that no fair market clearing algorithm exists, it is simply a natural state of affairs that some companies with inevitably &#8216;game&#8217; the algorithms which are implemented. Perhaps the only &#8216;fair&#8217; algorithm is to rotate among a collection of different clearing algorithms, so that the unfairness is amortized across each round of clearing. (This strategy might be problematic though, as I remember it was once possible to make a guaranteed winning at poker if the house rotated among different rules (5 card stud, Texan hold-em, etc) and you changed your betting strategy appropriately. This is actually a specific instance of a more general game theory result that it is sometimes possible, to make a guaranteed win out of two games of chance which, when either is played alone, are a guaranteed loss. I cannot at the moment remember what such a paring of games is called.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cogitolingua.net/blog/2011/03/08/arrows-voting-theorem-of-economics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Type-dispatch with Exceptions</title>
		<link>http://www.cogitolingua.net/blog/2011/01/08/type-dispatch-with-exceptions/</link>
		<comments>http://www.cogitolingua.net/blog/2011/01/08/type-dispatch-with-exceptions/#comments</comments>
		<pubDate>Sat, 08 Jan 2011 09:00:03 +0000</pubDate>
		<dc:creator>erich</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[Comp*]]></category>
		<category><![CDATA[Fun]]></category>
		<category><![CDATA[Ideas]]></category>

		<guid isPermaLink="false">http://www.cogitolingua.net/blog/?p=436</guid>
		<description><![CDATA[<p>I&#8217;m one of those Object Oriented programmers that consider type-inspection to be a design flaw. Much to my chagrin, I&#8217;ve found myself making occasional use of the Java instanceof keyword. In my most recent class lecture, we discussed Exceptions, and I later realized that one could essentially type-dispatch using the exception mechanism, because the catch [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m one of those Object Oriented programmers that consider type-inspection to be a design flaw. Much to my chagrin, I&#8217;ve found myself making occasional use of the Java  <code>instanceof</code> keyword. In my most recent class lecture, we discussed Exceptions, and I later realized that one could essentially type-dispatch using the exception mechanism, because the <code>catch</code> blocks will trigger on the exception type. I experimented a bit, and discovered that this trigger behaves polymorphically (as I expected). I don&#8217;t know what use this is, other than a bit of craziness and verbosity.</p>
<p>First, we&#8217;ll create a hierarchy of messages:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> Message <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">Throwable</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #003399;">String</span> contents<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PrivateMessage <span style="color: #000000; font-weight: bold;">extends</span> Message
<span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> msg <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>	
    <span style="color: #000000; font-weight: bold;">public</span> PrivateMessage<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">msg</span> <span style="color: #339933;">=</span> msg<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> contents<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;(private)&quot;</span> <span style="color: #339933;">+</span> msg<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PrivateMessage <span style="color: #000000; font-weight: bold;">extends</span> Message
<span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">String</span> msg <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>	
        <span style="color: #000000; font-weight: bold;">public</span> PrivateMessage<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">msg</span> <span style="color: #339933;">=</span> msg<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> contents<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;(private)&quot;</span> <span style="color: #339933;">+</span> msg<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then we&#8217;ll create a Sender and Receiver to test out a message pass.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Sender
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">Receiver</span> r <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Receiver</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        r.<span style="color: #006633;">message</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PrivateMessage<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hello&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        r.<span style="color: #006633;">message</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> PublicMessage<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;world&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Receiver</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> message<span style="color: #009900;">&#40;</span>Message msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> msg<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>PrivateMessage pm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;got a private message: &quot;</span> <span style="color: #339933;">+</span> pm.<span style="color: #006633;">contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>PublicMessage pm<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;got a public message: &quot;</span> <span style="color: #339933;">+</span> pm.<span style="color: #006633;">contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>Message m<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unknown message:&quot;</span> <span style="color: #339933;">+</span> m.<span style="color: #006633;">contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Lo, and Behold! when the program is run, we get the expected output:</p>
<pre lang="">
got a private message: (private)hello
got a public message: (public)world
</pre>
<p>I really have no idea if this feature is useful or just crazy verbiage for what could easily have been a daisy-chain of <code>instanceof</code> tests. Java makes it especially complicated because you must extend the <code>Throwable</code> class, in order to implement the messages as exceptions. Perhaps in languages without runtime type inspection this pattern might come in useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cogitolingua.net/blog/2011/01/08/type-dispatch-with-exceptions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Trip to Austria</title>
		<link>http://www.cogitolingua.net/blog/2010/08/06/trip-to-austria/</link>
		<comments>http://www.cogitolingua.net/blog/2010/08/06/trip-to-austria/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 17:36:04 +0000</pubDate>
		<dc:creator>erich</dc:creator>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[Self]]></category>

		<guid isPermaLink="false">http://www.cogitolingua.net/blog/?p=368</guid>
		<description><![CDATA[<p>Recently, I made a trip to Austria, visiting my good friend and partner in crimelab, Christoph.</p> <p>On my first day, I walked around Graz. I saw the alien, though I wasn&#8217;t impressed with the art inside. I saw the Armory, which I enjoyed for about 2 hours. Graz is a really tiny city, and everything [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I made a trip to Austria, visiting my good friend and partner in <del>crime</del>lab, <a href="http://christophkerschbaumer.com/">Christoph</a>.</p>
<p>On my first day, I walked around <a href="http://en.wikipedia.org/wiki/Graz">Graz</a>. I saw the <a href="http://www.museum-joanneum.at/de/kunsthaus">alien</a>, though I wasn&#8217;t impressed with the art inside. I saw the <a href="http://www.zeughaus.at/">Armory</a>, which I enjoyed for about 2 hours. Graz is a really tiny city, and everything is within walking distance. The age of the city means that almost all of the buildings are not perfectly rectangular, that there&#8217;s a very large number of publicly accessible courtyards with shops and cafe&#8217;s; though you wouldn&#8217;t know they exist unless you&#8217;d been wandering around lost or already knew they existed. The city obviously grew up organically, without any central planning, so the streets do not form a rectangular grid making it quite easy to get lost. My last activity for the day was walking up to the clock tower and looking back down at the city. It&#8217;s interesting to note the architectural contrast between the ancient buildings and the modern ones.</p>
<p>I spent most of my vacation with Christoph&#8217;s family in Liezen. His family was very supportive, and made sure that throughout my entire vacation, I didn&#8217;t have to lift a finger to do anything myself. Even though I understood almost none of the German, we ate as a family everyday, with delicious food. His family has a cabin up in the mountains, and we spent one night there drinking schnapps and playing cards. We also hiked a bit (2hrs) which left me rather exhausted. We also went to see the Riegersburg castle, attended a local tent-fest, and visited the <a href="http://www.stiftadmont.at/english/museum/bibliothek/stiftsbibliothek.php">Admont monastic library</a>.</p>
<p>During the middle of my vacation I spent a couple days in Vienna, with Christoph&#8217;s friend Florian. We climbed the steps of the impressive <a href="http://en.wikipedia.org/wiki/St._Stephen%27s_Cathedral,_Vienna">Stephensdom</a>. We saw the <a href="http://www.wienerriesenrad.com/index.php">riesenrad</a>, and a number of the older buildings. We also visited <a href="http://www.schoenbrunn.at/en/">Schoenbrunn</a> and walked through the gardens (astoundingly large) and hedge maze.</p>
<p>I questioned my hosts about who uses all the beautiful cathedrals and churches, because it was pretty evident that the majority of people in my generation are fully secular. It appears that there will be not enough attendees to pay for the maintenance of these buildings, and I didn&#8217;t hear of any plans for turning them into community centers, or renting them out for raves.</p>
<p>Even though I was in Austria for 10 days exploring the cities and gorgeous countryside, I didn&#8217;t see a single kangaroo.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cogitolingua.net/blog/2010/08/06/trip-to-austria/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>High-Tech Life</title>
		<link>http://www.cogitolingua.net/blog/2009/07/27/high-tech-life/</link>
		<comments>http://www.cogitolingua.net/blog/2009/07/27/high-tech-life/#comments</comments>
		<pubDate>Tue, 28 Jul 2009 02:07:59 +0000</pubDate>
		<dc:creator>erich</dc:creator>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[Tech*]]></category>

		<guid isPermaLink="false">http://www.cogitolingua.net/blog/?p=239</guid>
		<description><![CDATA[<p>There are many things associated with a high-tech lifestyle. Most of these things are electronic: cellphone, kindle, PDA, laptop, video-conferencing, etc. I think it&#8217;s the more subtle things that actually matter more. The things that aren&#8217;t visible, that are often overlooked, but which actually have a large impact.</p> <p>Today an article came out on slashdot [...]]]></description>
			<content:encoded><![CDATA[<p>There are many things associated with a high-tech lifestyle. Most of these things are electronic: cellphone, kindle, PDA, laptop, video-conferencing, etc. I think it&#8217;s the more subtle things that actually matter more. The things that aren&#8217;t visible, that are often overlooked, but which actually have a large impact.</p>
<p>Today an article came out on slashdot about a <a href="http://science.slashdot.org/story/09/07/25/2115255/Bacterial-Computer-Solves-Hamiltonian-Path-Problem?from=rss">Bacterial Computer that can Solves the Hamiltonian Path Problem</a> This article, of course, led me awander on the internet, whereupon I read about <a href="http://en.wikipedia.org/wiki/Antibubble">Antibubbles!</a> and a company called <a href="http://www.zubbles.com/">Zubbles</a>.</p>
<p>Zubbles are <em>fascinating</em>. Colored bubbles. The idea is deceptively simple, but let&#8217;s think about how color in bubbles is formed. Bubble typically have a rainbow shine to them because of variations in the bubble film, that resonate with different frequencies of light. Suppose that you wanted only a single color instead of an ever-changing rainbow.</p>
<p>There are two options:</p>
<ol>
<li>produce a film that has a specific thickness that&#8217;s the color you want.</li>
<li>add a colored dye to the soap solution.</li>
</ol>
<p>The Zubbles company chose option (2), but still had a great deal of technical details to work out. The chosen dye must be soluble, non-toxic, can&#8217;t interfere with surface-tension properties necessary for bubble formation, and shouldn&#8217;t get colored dye all over everything the bubbles might land and pop on. Remarkably, bright chemists were able to <a href="http://en.wikipedia.org/wiki/Zubbles">solve</a> these problems. The found a set of dye&#8217;s that remain in-tact until the bubble pops. The popping has enough energy that it causes a carbon ring within the dye to open up. When the ring is broken the dye loses its color.</p>
<p>About ten years of experimenting, for a deceptively simple children&#8217;s toy. Science is awesome!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cogitolingua.net/blog/2009/07/27/high-tech-life/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

