<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Foognostic blogs</title>
	
	<link>http://blogs.foognostic.net</link>
	<description>Seeking knowledge of foo</description>
	<lastBuildDate>Fri, 13 Nov 2009 15:18:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<creativeCommons:license>http://creativecommons.org/licenses/by-sa/2.0/</creativeCommons:license><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/foognostic" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
		<title>More poker with Clojure: what’s in a hand?</title>
		<link>http://feedproxy.google.com/~r/foognostic/~3/Fc4e9FaHy84/</link>
		<comments>http://blogs.foognostic.net/2009/11/more-poker-with-clojure-whats-in-a-hand/#comments</comments>
		<pubDate>Fri, 13 Nov 2009 15:18:37 +0000</pubDate>
		<dc:creator>Seth Schroeder</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://blogs.foognostic.net/?p=245</guid>
		<description><![CDATA[In a previous post I wrote some code to create and shuffle a deck of cards. This post demonstrates one way to figure out what a hand is worth according to basic 5 card draw rules.

The implementation is basically one switch statement from a C/Java/Ruby/Groovy/Python perspective. Ruby and Groovy coders will recognize that the return [...]]]></description>
			<content:encoded><![CDATA[<p>In a <a href="http://blogs.foognostic.net/2009/11/shuffling-cards-with-clojure/">previous post</a> I wrote some code to create and shuffle a deck of cards. This post demonstrates one way to figure out what a hand is worth according to basic 5 card draw rules.</p>

<p>The implementation is basically one switch statement from a C/Java/Ruby/Groovy/Python perspective. Ruby and Groovy coders will recognize that the return value of the function is the value of the last expression evaluated. So an if statement returns either the value of the true expression or the value of the false expression.</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>&lt; <span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;good, 1 &lt; 2 &lt; 3&quot;</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">&quot;what? someone file a bug report!&quot;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">;; &quot;good, 1 &lt; 2 &lt; 3&quot;</span></div></div>

<p>When conditional logic returns values like this, there is no need to use a stack variable to store a return value.</p>

<p>A quick introduction to some of the functions used in the code:</p>

<ul>
  <li><a href="http://clojure.org/special_forms#if">(if)</a>: Aside from returning its true/false expression result, no major difference than Java.</li>
  <li><a href="http://clojure.org/api#toc161">(cond)</a>: Basically a switch statement, returning a value just like (if)</li>
  <li><a href="http://clojure.org/api#toc330">(let)</a>: Defines and initializes &quot;variables&quot; which are immutable.
  <li>#(): This defines an anonymous function. Believe this is a macro for <a href="http://clojure.org/api#toc260">(fn)</a>.</li>
  <li>#{}: This defines a set <a href="http://clojure.org/data_structures">data structure</a>; unordered and no duplicates. Also see the <a href="http://clojure.org/api#toc519">(set)</a>. function</li>
  <li>{}: This defines a map data structure, populated with unordered key/value pairs. Bonus: (map key) <b>AND</b> (key map) both return the value for key in a map.</li>
</ul>

<p>With some minimal descriptions listed, let's dig into the code:</p>

<p>When judging a hand it's critical to know how many of each suit are present AND how many of each value are present. One way to model this is to build histograms for suit and value.</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>build-property-histogram<br />
&nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">take</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&#40;</span>shuffle deck<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp;:suit<br />
&nbsp;suits<span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">;; {:clubs 2, :diamonds 2, :spades 0, :hearts 1}</span><br />
<br />
<span style="color: #66cc66;">&#40;</span>build-property-histogram<br />
&nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">take</span> 5 <span style="color: #66cc66;">&#40;</span>shuffle deck<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp;:<span style="color: #b1b100;">name</span><br />
&nbsp;names<span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">;; {:queen 2, :king 1, :jack 0, :seven 1, :eight 0, </span><br />
<span style="color: #808080; font-style: italic;">;; &nbsp;:six 0, :nine 0, :five 0, :ace 0, :ten 0, :three 1,</span><br />
<span style="color: #808080; font-style: italic;">;; &nbsp;:two 0, :four 0}</span></div></div>

<p>The implementation behind that is not an easy read (probably because it's not great Clojure). Basically this code iterates over the pool of attributes (suits or values) and uses (conj) to aggregate maps into one big map. In the middle a filter function finds all cards matching the current property and then the total number is counted up.</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> build-property-histogram <span style="color: #66cc66;">&#91;</span>cards property-<span style="color: #b1b100;">name</span> property-pool<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">reduce</span><br />
&nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">fn</span> <span style="color: #66cc66;">&#91;</span>sieve value<span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">conj</span> sieve<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#123;</span> value<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">count</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">filter</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">fn</span> <span style="color: #66cc66;">&#91;</span>card<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>= value <span style="color: #66cc66;">&#40;</span>card property-<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cards<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp;property-pool<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>Only one more helper method to go. This method determines whether the hand is a straight, whether all of its cards have consecutive values. The approach taken was a little odd. An array of all possible straight values is built and then segmented into overlapping hands. For example, the first two arrays would be [ace, two, three, four, five] and [two, three, four, five, six]. Each of these possibilities is matched against the hand, using Clojure's facility to compare sets easily.</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> straight? <span style="color: #66cc66;">&#91;</span>hand<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">some</span><br />
&nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">fn</span> <span style="color: #66cc66;">&#91;</span>straight<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>= <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span> :<span style="color: #b1b100;">name</span> hand<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> straight<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">partition</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#91;</span>:ace, :two, :three, :four, :five, :six, :seven, :eight,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:nine, :ten, :jack, :queen, :king, :ace<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>(some) is a function which returns true when its filter function returns true for any of the collection items it examines.</p>

<p>After all that, here is the hand judging function. It's basically a few local &quot;variables&quot; and a switch statement. Some additional conditional logic appears inside the switch statement as well. This is required to determine whether a hand with three of a kind is a full house (are the remaining cards a pair?).</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:300px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> rank-cards <span style="color: #66cc66;">&#91;</span>cards<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>suit-histo <span style="color: #66cc66;">&#40;</span>build-property-histogram cards :suit suits<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; value-histo <span style="color: #66cc66;">&#40;</span>build-property-histogram cards :<span style="color: #b1b100;">name</span> names<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; max-per-suit <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> <span style="color: #b1b100;">max</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">vals</span> suit-histo<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; max-per-value <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> <span style="color: #b1b100;">max</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">vals</span> value-histo<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>= 4 max-per-value<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :quads<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>= 3 max-per-value<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">some</span> #<span style="color: #66cc66;">&#40;</span>= 2 %<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">vals</span> value-histo<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :full-house<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :trips<span style="color: #66cc66;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>= 2 max-per-value<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>&lt; 1 <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">count</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">filter</span> #<span style="color: #66cc66;">&#40;</span>= 2 %<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">vals</span> value-histo<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :two-pair<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :pair<span style="color: #66cc66;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">and</span> <span style="color: #66cc66;">&#40;</span>straight? cards<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>&gt; 5 max-per-suit<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :straight<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">and</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">not</span> <span style="color: #66cc66;">&#40;</span>straight? cards<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>= 5 max-per-suit<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :flush<br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>= 5 max-per-suit<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>= #<span style="color: #66cc66;">&#123;</span>:ace, :king, :queen, :jack, :ten<span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span> :<span style="color: #b1b100;">name</span> cards<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :royal-flush<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :straight-flush<span style="color: #66cc66;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; true<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :high-card<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>In the near future all this code will be cleaned up and moved out to BitBucket. Ideally some basic chip tracking and logic would be added so you could actually play poker from a REPL. That's probably much more difficult than it seems, but hopefully this has been useful so far. Please don't hesitate to contact me (goof at foognostic dot net) with any questions!</p>
<img src="http://feeds.feedburner.com/~r/foognostic/~4/Fc4e9FaHy84" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.foognostic.net/2009/11/more-poker-with-clojure-whats-in-a-hand/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.foognostic.net/2009/11/more-poker-with-clojure-whats-in-a-hand/</feedburner:origLink></item>
		<item>
		<title>Shuffling cards with Clojure</title>
		<link>http://feedproxy.google.com/~r/foognostic/~3/MTcGMhpR2cE/</link>
		<comments>http://blogs.foognostic.net/2009/11/shuffling-cards-with-clojure/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 13:59:56 +0000</pubDate>
		<dc:creator>Seth Schroeder</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://blogs.foognostic.net/?p=238</guid>
		<description><![CDATA[Using Clojure to generate a poker deck and shuffle it.]]></description>
			<content:encoded><![CDATA[<p>One of my friends and coworkers has been gently cluesticking me into learning poker. Along the way he suggested I write a very simple hand generator to get a feel for how the number of players changes the relative strength of your hole cards. Shuffling cards is one small step along the way, and that seemed like a good place to start.</p>

<p>First, let's create a poker namespace to work in and then define some basic constants as keywords in sets.</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">ns</span> poker<span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> suits #<span style="color: #66cc66;">&#123;</span>:hearts, :diamonds, :clubs, :spades<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> names #<span style="color: #66cc66;">&#123;</span>:two, :three, :four, :five, :six,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:seven, :eight, :nine, :ten, :jack,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:queen, :king, :ace<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>Next, let's &quot;define a class.&quot; I'm quoting that because a struct in Clojure is just a map with some keys to be expected.</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defstruct</span> card :suit :<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>Next we actually need, you know, a full deck of cards to shuffle. This was harder than I expected. At first I tried two loops, first iterating over suits and then values. This resulted in a list of ... four lists of... thirteen cards.</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> sequence-of-suits<br />
&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">fn</span> <span style="color: #66cc66;">&#91;</span>suit<span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">fn</span> <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">struct</span> card suit <span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;names<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; suits<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">count</span> sequence-of-suits<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; 4</span><br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">count</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">first</span> sequence-of-suits<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">;; 13</span></div></div>

<p>Unfortunately (map) didn't work as hoped. I decided to try a list comprehension. This was something I remembered from using once in Python. It turned out to be exactly what I needed:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">def</span> deck<br />
&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#91;</span>suit suits, <span style="color: #b1b100;">name</span> names<span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">struct-map</span> card :suit suit :<span style="color: #b1b100;">name</span> <span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>This yields a lot of text, so let's inspect the output programmatically. (filter) is a function which takes a function and a list; the output consists of the list items for which the function inspects and returns true.</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;height:300px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">count</span> deck<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">;; 52</span><br />
<br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">first</span> deck<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">;; {:suit :hearts, :name :queen}</span><br />
<br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">last</span> deck<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">;; {:suit :clubs, :name :four}</span><br />
<br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> heart? <span style="color: #66cc66;">&#91;</span>card<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span>= :hearts <span style="color: #66cc66;">&#40;</span>card :suit<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">count</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">filter</span> heart? deck<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">;; 13</span><br />
<br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> king? <span style="color: #66cc66;">&#91;</span>card<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span>= :king <span style="color: #66cc66;">&#40;</span>card :<span style="color: #b1b100;">name</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">count</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">filter</span> king? deck<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">;; 4</span><br />
<br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> king-of-hearts? <span style="color: #66cc66;">&#91;</span>card<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">and</span> <span style="color: #66cc66;">&#40;</span>king? card<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>heart? card<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">filter</span> king-of-hearts? deck<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">;; ({:suit :hearts, :name :king})</span></div></div>

<p>Now that we have the deck straightened out, it's time to shuffle it. I'm getting the feeling there is a beautiful mathmatical way to sort 52 cards involving permutations which would be easy to code functionally. Unfortunately I'm not there yet on either side. This implementation is really crude.</p>

<p>For example, this random-card implementation instantiates java.util.Random each time it is called (and without a seed value). (nth) returns the nth item of a sequence; (seq) is being used here because deck is a hashmap, which doesn't support ordered extraction.</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> random-card <span style="color: #66cc66;">&#91;</span>deck<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nth</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">seq</span> deck<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span>. <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">new</span> java.util.Random<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextInt<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">count</span> deck<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>Continuing on with the vulgar display of crude code... here is the shuffle method:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> shuffle<br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>deck<span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span>shuffle <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> deck<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>clean dirty<span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">empty?</span> dirty<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;clean<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>pick <span style="color: #66cc66;">&#40;</span>random-card dirty<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span>shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">conj</span> clean pick<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">disj</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> dirty<span style="color: #66cc66;">&#41;</span> pick<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>Let's ignore the lack of comments of any sort here. This implementation uses variable arity. That means when the function is passed one parameter the following code will run:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>deck<span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span>shuffle <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> deck<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>And when two parameters are passed the following code runs:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>clean dirty<span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">empty?</span> dirty<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;clean<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>pick <span style="color: #66cc66;">&#40;</span>random-card dirty<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span>shuffle <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">conj</span> clean pick<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">disj</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">set</span> dirty<span style="color: #66cc66;">&#41;</span> pick<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>This is a feature of Clojure. I kind of like it. The way recursion was used here the variable arity made the code simple to read. I probably could have used (recur) for a very similar amount of code, but the <a href="http://dogs.foognostic.net/">dogs</a> had to go out.</p>

<p>So the second chunk of (shuffle) picks a card at random from the unsorted pile, uses (conj) to include it in the sorted pile, uses (disj) to exclude the picked card from the unsorted pile, and then GOTO 10.</p>

<p>And now, with lots of gory code I'm not proud of, here is my first hand of 5 card draw:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">take</span> 5 <span style="color: #66cc66;">&#40;</span>shuffle deck<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>:suit :diamonds, :<span style="color: #b1b100;">name</span> :three<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#123;</span>:suit :clubs, :<span style="color: #b1b100;">name</span> :ten<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#123;</span>:suit :diamonds, :<span style="color: #b1b100;">name</span> :four<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#123;</span>:suit :spades, :<span style="color: #b1b100;">name</span> :three<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#123;</span>:suit :hearts, :<span style="color: #b1b100;">name</span> :three<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>Wow, three of a kind. It's almost as if I programmed in a "be nice to Seth" bit somewhere...</p>
<img src="http://feeds.feedburner.com/~r/foognostic/~4/MTcGMhpR2cE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.foognostic.net/2009/11/shuffling-cards-with-clojure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.foognostic.net/2009/11/shuffling-cards-with-clojure/</feedburner:origLink></item>
		<item>
		<title>Macros make Elvis better</title>
		<link>http://feedproxy.google.com/~r/foognostic/~3/8Zn0_f8lqeI/</link>
		<comments>http://blogs.foognostic.net/2009/11/macros-make-elvis-better/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 15:33:36 +0000</pubDate>
		<dc:creator>Seth Schroeder</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://blogs.foognostic.net/?p=222</guid>
		<description><![CDATA[Implementing Groovy's Elvis operator in Clojure, using a macro to avoid unnecessary evaluation.]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://cm.bell-labs.com/cm/cs/cbook/">C</a> programming language provides a ternary operator. It's a terse if/else expression:</p>

<div class="codecolorer-container c vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">void</span> print_something<span style="color: #009900;">&#40;</span><span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>str<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>text <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>str <span style="color: #339933;">!=</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> str <span style="color: #339933;">:</span> <span style="color: #ff0000;">&quot;null&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></div>

<p>The team behind the <a href="http://groovy.codehaus.org/">Groovy</a> programming language took a look at the ternary operator and realized something. Developers frequently use it during a variable assignment to take the value of the test expression when not null, otherwise take the value of the "else" expression. A new operator was added to the language to support exactly this usage and was dubbed the <a href="http://groovy.codehaus.org/Operators#Operators-ElvisOperator%28%3F%3A%29">Elvis operator</a>.</p>

<div class="codecolorer-container groovy vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="groovy codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> print_something<span style="color: #66cc66;">&#40;</span>str<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20def"><span style="color: #000000; font-weight: bold;">def</span></a> text <span style="color: #66cc66;">=</span> str <span style="color: #66cc66;">?</span>: <span style="color: #ff0000;">&quot;null&quot;</span><br />
&nbsp; &nbsp; <a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20println"><span style="color: #993399;">println</span></a> text<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
print_something<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;foo&quot;</span><span style="color: #66cc66;">&#41;</span><br />
print_something<span style="color: #66cc66;">&#40;</span><a href="http://www.google.de/search?q=site%3Adocs.codehaus.org/%20null"><span style="color: #000000; font-weight: bold;">null</span></a><span style="color: #66cc66;">&#41;</span></div></div>

<p>The <a href="http://www.scala-lang.org/">Scala</a> programming language does not have an Elvis operator, but Daniel Spiewak posted an <a href="http://www.codecommit.com/blog/scala/implementing-groovys-elvis-operator-in-scala">impressive implementation</a>.</p>

<p>This led me to thinking about defining the Elvis operator in <a href="http://clojure.org/">Clojure</a>, my current spare-time language. Clojure is a Lisp hosted on the JVM (with some plans for the CLR). This was a fun exercise but I make no claims that this is a good way to be Elvis-ish in Clojure.</p>

<p>First, here's a naïve implementation:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> elvis_fn <span style="color: #66cc66;">&#91;</span>a b<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if-not</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil?</span> a<span style="color: #66cc66;">&#41;</span> a b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>It works:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">user&gt; <span style="color: #66cc66;">&#40;</span>elvis_fn <span style="color: #cc66cc;">123</span> <span style="color: #cc66cc;">456</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #cc66cc;">123</span><br />
user&gt; <span style="color: #66cc66;">&#40;</span>elvis_fn nil <span style="color: #cc66cc;">456</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #cc66cc;">456</span></div></div>

<p>But it always evaluates the "else" expression. FALE.</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> nap <span style="color: #66cc66;">&#91;</span>duration<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span>println <span style="color: #66cc66;">&#40;</span>format <span style="color: #ff0000;">&quot;Sleeping for %d ms&quot;</span> duration<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span>Thread/sleep duration<span style="color: #66cc66;">&#41;</span><br />
&nbsp; duration<span style="color: #66cc66;">&#41;</span><br />
user&gt; <span style="color: #66cc66;">&#40;</span>elvis_fn <span style="color: #66cc66;">&#40;</span>nap 123<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>nap 456<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
Sleeping <span style="color: #b1b100;">for</span> 123 ms<br />
Sleeping <span style="color: #b1b100;">for</span> <span style="color: #cc66cc;">456</span> ms<br />
<span style="color: #cc66cc;">123</span></div></div>

<p>So that's bad. Let's make a macro version. This should prevent the expressions from being evaluated before Elvis gets a chance to do his thing:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defmacro</span> elvis_macro <span style="color: #66cc66;">&#91;</span>a b<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if-not</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil?</span> a<span style="color: #66cc66;">&#41;</span> a b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
user&gt; <span style="color: #66cc66;">&#40;</span>elvis_macro <span style="color: #66cc66;">&#40;</span>nap 123<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>nap 456<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
Sleeping <span style="color: #b1b100;">for</span> <span style="color: #cc66cc;">123</span> ms<br />
<span style="color: #cc66cc;">123</span></div></div>

<p>To verify this, I added</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span>println <span style="color: #ff0000;">&quot;Blue suede shoes&quot;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>before the (if-not) form and called the method and the macro again:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">user&gt; <span style="color: #66cc66;">&#40;</span>elvis_fn <span style="color: #66cc66;">&#40;</span>nap 123<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>nap 456<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
Sleeping <span style="color: #b1b100;">for</span> 123 ms<br />
Sleeping <span style="color: #b1b100;">for</span> 456 ms<br />
Blue suede shoes<br />
123<br />
user&gt; <span style="color: #66cc66;">&#40;</span>elvis_macro <span style="color: #66cc66;">&#40;</span>nap 123<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>nap 456<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
Blue suede shoes<br />
Sleeping <span style="color: #b1b100;">for</span> <span style="color: #cc66cc;">123</span> ms<br />
<span style="color: #cc66cc;">123</span></div></div>

<p>So it was easy to avoid evaluating things by simply making the function into a macro. I'm sure there are many things above and beyond what I am doing here, but it was a nice experiment.</p>
<img src="http://feeds.feedburner.com/~r/foognostic/~4/8Zn0_f8lqeI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.foognostic.net/2009/11/macros-make-elvis-better/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.foognostic.net/2009/11/macros-make-elvis-better/</feedburner:origLink></item>
		<item>
		<title>Clojure training!</title>
		<link>http://feedproxy.google.com/~r/foognostic/~3/-v6U0ESjk44/</link>
		<comments>http://blogs.foognostic.net/2009/10/clojure-training/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 01:18:17 +0000</pubDate>
		<dc:creator>Seth Schroeder</dc:creator>
				<category><![CDATA[clojure]]></category>

		<guid isPermaLink="false">http://blogs.foognostic.net/?p=219</guid>
		<description><![CDATA[Very exciting -- Rich Hickey and Stu Halloway will be giving Clojure training in early 2010 in Northern Virginia. Sign up here for news as soon as it happens: http://pragmaticstudio.com/clojure
]]></description>
			<content:encoded><![CDATA[<p>Very exciting -- Rich Hickey and Stu Halloway will be giving Clojure training in early 2010 <b>in Northern Virginia</b>. Sign up here for news as soon as it happens: <a href="http://pragmaticstudio.com/clojure">http://pragmaticstudio.com/clojure</a></p>
<img src="http://feeds.feedburner.com/~r/foognostic/~4/-v6U0ESjk44" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.foognostic.net/2009/10/clojure-training/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.foognostic.net/2009/10/clojure-training/</feedburner:origLink></item>
		<item>
		<title>I wanted a macro, but needed recursion</title>
		<link>http://feedproxy.google.com/~r/foognostic/~3/Y8cChwENmH4/</link>
		<comments>http://blogs.foognostic.net/2009/07/i-wanted-a-macro-but-needed-recursion/#comments</comments>
		<pubDate>Sat, 04 Jul 2009 15:09:31 +0000</pubDate>
		<dc:creator>Seth Schroeder</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://blogs.foognostic.net/?p=179</guid>
		<description><![CDATA[It's easier for me to start things than complete them. That seemed like bad news when I just couldn't figure out how to generalize a small bit of Clojure code. One long week went by as I repeatedly got very close but couldn't quite solve the problem. While eventually I figured it out, the process of getting there made me look forward to writing more Clojure.]]></description>
			<content:encoded><![CDATA[<p>It's easier for me to start things than complete them. That seemed like bad news when I just couldn't figure out how to generalize a small bit of Clojure code. One long week went by as I repeatedly got very close but couldn't quite solve the problem. While eventually I figured it out, the process of getting there made me look forward to writing more Clojure.</p>

<p>My last exploration of Clojure taught me to <a href="http://blogs.foognostic.net/2009/06/put-that-for-loop-down/">avoid for loops</a> while splitting a string into overlapping character pairs. This exploration into Clojure was going to generalize that code to emit any width of overlapping character chunks.</p>

<p>Here is the old, inflexible code:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> split-bigrams <span style="color: #66cc66;">&#91;</span>text<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">fn</span> <span style="color: #66cc66;">&#91;</span>l r<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">str</span> l r<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">seq</span> text<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">rest</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">seq</span> text<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>A quick summary of that:</p>

<ul>
<li>Defines a function named split-bigrams</li>
<li>It accepts one untyped parameter, here named <code class="codecolorer clojure dawn"><span class="clojure">text</span></code></li>
<li>The method body contains one function call to <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">map</span></span></code>, which takes three arguments: one anonymous function taking two arguments, and two sequences.
<li><code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">map</span></span></code> pulls one character from each sequence and passes them to the anonymous function, which turns them into a string
</ul>

<p>The trick? Two iterators over one string, with the second iterator starting at the second character. Then just iterate over the lists and mash the items together. Problem is, to get character triplets I would need a third iterator starting at the third character. That's no way to live! Just a big fat FALE WHALE.</p>

<p>I desperately hoped that macros could somehow magically generate all the lists for <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">map</span></span></code>. I like <a href="http://bitbucket.org/seths/bitunwise/src/ad2b47eb028d/bitunwise.c#cl-74">macros</a>. Clojure <a href="http://clojure.org/macros">likes</a> <a href="http://blog.n01se.net/?p=33">macros</a>! My poor little train of thought couldn't scale this mountain. I thought I could, I thought I could, but between tucking in kids and dog walks I was too tired/busy. So I gave up and looked for another option.</p>

<p>Howzabout recursion? I spent several days rewriting variants of this:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> take-chunk <span style="color: #66cc66;">&#91;</span>n <span style="color: #b1b100;">seq</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when</span> <span style="color: #66cc66;">&#40;</span>nthnext <span style="color: #b1b100;">seq</span> <span style="color: #66cc66;">&#40;</span>- n 1<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">take</span> n <span style="color: #b1b100;">seq</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> nice-<span style="color: #b1b100;">try</span> <span style="color: #66cc66;">&#91;</span>n <span style="color: #b1b100;">str</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">when-let</span> <span style="color: #66cc66;">&#91;</span>chars <span style="color: #66cc66;">&#40;</span>take-chunk n <span style="color: #b1b100;">str</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>prn chars<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">recur</span> n <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">rest</span> <span style="color: #b1b100;">str</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>This prints the values I want... it just doesn't put them into a collection! I feebly tried incorporating <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">map</span></span></code> and <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">reduce</span></span></code>. Not sure how many infinite loops were created and cancelled...</p>

<p>Finally, I decided to read the source code of Clojure to see how they do things like this. That was a great decision. <a href="http://github.com/richhickey/clojure/blob/c4a5cd208aef54ae5b292fa41c4880017315e553/src/clj/clojure/core.clj">core.clj</a> is clean, clear, and full of goodness. Somewhere in there I found <a href="http://clojure.org/special_forms#toc10"><code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">recur</span></span></code></a> being used how I needed it: do something to a collection, call itself with the output until the input was exhausted, and return the aggregated output:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> ngrams <span style="color: #66cc66;">&#91;</span>n chars ngrams<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>ngram <span style="color: #66cc66;">&#40;</span>take-chunk n chars<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">if</span> ngram<br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">recur</span> n <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">rest</span> chars<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> ngram ngrams<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; ngrams<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>This is probably a basic recursion pattern. I feel a little silly taking so long to figure it out. Just a little silly though, as the coding I do for a living doesn't encourage recursion.</p>

<p>At this point I am almost convinced that Clojure is the optimal language for me to grow as a programmer. Lisp itself is a low-level high-level language; you have to really understand how things are going to work, but not worry about things like garbage collection. The "homegrown VM" and "no library" problems just don't exist in Clojure. The APIs are clean and clear. Best of all the dogma is minimal and pragmatic. HUZZAH!</p>
<img src="http://feeds.feedburner.com/~r/foognostic/~4/Y8cChwENmH4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.foognostic.net/2009/07/i-wanted-a-macro-but-needed-recursion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.foognostic.net/2009/07/i-wanted-a-macro-but-needed-recursion/</feedburner:origLink></item>
		<item>
		<title>OBE</title>
		<link>http://feedproxy.google.com/~r/foognostic/~3/y7sLuzbzBl8/</link>
		<comments>http://blogs.foognostic.net/2009/06/obe/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 13:22:34 +0000</pubDate>
		<dc:creator>Seth Schroeder</dc:creator>
				<category><![CDATA[icfp]]></category>

		<guid isPermaLink="false">http://blogs.foognostic.net/?p=177</guid>
		<description><![CDATA[Overcome By Events -- this is my excuse for NOT submitting an entry for the ICFP contest this year. It's Sunday, 0900 EDT as I write this and not a single line of code has been written. On top of that, today is going to be busy. Maybe next year.
]]></description>
			<content:encoded><![CDATA[<p>Overcome By Events -- this is my excuse for NOT submitting an entry for the ICFP contest this year. It's Sunday, 0900 EDT as I write this and not a single line of code has been written. On top of that, today is going to be busy. Maybe next year.</p>
<img src="http://feeds.feedburner.com/~r/foognostic/~4/y7sLuzbzBl8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.foognostic.net/2009/06/obe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.foognostic.net/2009/06/obe/</feedburner:origLink></item>
		<item>
		<title>ICFP 2009 is off and running!</title>
		<link>http://feedproxy.google.com/~r/foognostic/~3/xs4BEyocVG4/</link>
		<comments>http://blogs.foognostic.net/2009/06/icfp-2009-is-off-and-running/#comments</comments>
		<pubDate>Fri, 26 Jun 2009 23:19:47 +0000</pubDate>
		<dc:creator>Seth Schroeder</dc:creator>
				<category><![CDATA[icfp]]></category>

		<guid isPermaLink="false">http://blogs.foognostic.net/?p=175</guid>
		<description><![CDATA[Well, the ICFP 2009 programming challenge started today. Team Foognostic has signed up (so far includes: me) and has skimmed through the problem description. It's a little premature to judge it, but the task kind of feels like a MMIXed up version of last year's contest.

Unless something awesome happens the physics portion of the challenge [...]]]></description>
			<content:encoded><![CDATA[<p>Well, the ICFP <a href="http://www.icfpcontest.org/">2009 programming challenge</a> started today. Team Foognostic has signed up (so far includes: me) and has skimmed through the problem description. It's a little premature to judge it, but the task kind of feels like a <a href="http://www-cs-faculty.stanford.edu/~knuth/mmix.html">MMIX</a>ed up version of last year's contest.</p>

<p>Unless something awesome happens the physics portion of the challenge will be tackled last, if at all. And I still intend to use Clojure.. I have a little ant based environment to assist.</p>

<p>If anyone is interested in joining team Foognostic, shoot me a mail at tbbs@sbbtabfgvp.arg, unless <a href="http://www.rot13.com/index.php">rot13</a> ain't your thing. Don't wait though, the contest ends June 29th at <a href="http://www08.wolframalpha.com/input/?i=14%3A00+CDT">14:00 CDT</a>!</p>
<img src="http://feeds.feedburner.com/~r/foognostic/~4/xs4BEyocVG4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.foognostic.net/2009/06/icfp-2009-is-off-and-running/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.foognostic.net/2009/06/icfp-2009-is-off-and-running/</feedburner:origLink></item>
		<item>
		<title>Reduce your way into good Clojure</title>
		<link>http://feedproxy.google.com/~r/foognostic/~3/W5RoOx4B3zQ/</link>
		<comments>http://blogs.foognostic.net/2009/06/reduce-your-way-into-good-clojure/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 00:17:33 +0000</pubDate>
		<dc:creator>Seth Schroeder</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://blogs.foognostic.net/?p=134</guid>
		<description><![CDATA[In my previous <a href="http://blogs.foognostic.net/2009/06/put-that-for-loop-down/">post</a> I discussed replacing for loops with the <code>(map)</code> function. That works, until it doesn't.]]></description>
			<content:encoded><![CDATA[<p>In my previous <a href="http://blogs.foognostic.net/2009/06/put-that-for-loop-down/">post</a> I discussed replacing for loops with the <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span><span style="color: #66cc66;">&#41;</span></span></code> function. That works, until it doesn't.</p>

<p>Another step needed to generate the cosine similarity for two strings is to create a frequency histogram, or how many times each character pair occurs in a string. This is a pretty good fit for a hash map, where the keys are the character pairs, and the values the occurrences.</p>

<p>Here was my initial try. This code meant to do well... but went far, far away from where I wanted. Let's take a detailed look at my failings:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">user=&gt; <br />
<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">let</span> <span style="color: #66cc66;">&#91;</span>hm <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">hash-map</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">fn</span> <span style="color: #66cc66;">&#91;</span><span style="color: #b1b100;">key</span> <span style="color: #b1b100;">val</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">assoc</span> hm <span style="color: #b1b100;">key</span> <span style="color: #b1b100;">val</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #ff0000;">&quot;b&quot;</span> <span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>What I wanted: a map like this => <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;b&quot;</span> <span style="color: #cc66cc;">2</span>, <span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#125;</span></span></code>. What I <b>got</b> was three hash maps, each with one key/value pair => <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;b&quot;</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#125;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span></span></code>. The intent was to use <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">map</span></span></code> to iterate over the sequences, and use <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">assoc</span></span></code> to put the key and value into one hash map. And now, for the parade of errors...</p>

<ol>
<li>Use the <a href="http://clojure.org/api#zipmap">zipmap</a> function to build hashmaps like this: <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">zipmap</span> <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #ff0000;">&quot;b&quot;</span> <span style="color: #ff0000;">&quot;a&quot;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span> <span style="color: #cc66cc;">2</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span></span></code> => <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;b&quot;</span> <span style="color: #cc66cc;">2</span>, <span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#125;</span></span></code>
<li>Variables in the let block cannot be changed...
<li>... but I really needed to update the hash map defined in the let block.
</ol>

<p>Rather than continue to stew uselessly I used Emacs to hop into the <a href="irc://irc.freenode.net/#clojure">#clojure IRC</a> channel. A wonderful person suggested the <a href="http://clojure.org/api#reduce"><code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">reduce</span></span></code></a> function; I'd used it once in Ruby where it is best known as <a href="http://railspikes.com/2008/8/11/understanding-map-and-reduce">inject</a>. I'm going to skimp on my description of reduce a little since that article is <b>so</b> well done.</p>

<p><code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">reduce</span></span></code> iterates over a collection like <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">map</span></span></code>, but it passes a mutable context to each callback. The return value of <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">reduce</span></span></code> is the final value of the context... essentially. I am still coming up to speed on it obviously.</p>

<p>Anyhow, enough yammering. Here is <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #b1b100;">reduce</span></span></code> in action:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> dot-product <span style="color: #66cc66;">&#91;</span>l_histo r_histo<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">reduce</span><br />
&nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">fn</span> <span style="color: #66cc66;">&#91;</span>product <span style="color: #b1b100;">key</span><span style="color: #66cc66;">&#93;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span>+ product<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>* <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">get</span> l_histo <span style="color: #b1b100;">key</span> 0<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">get</span> r_histo <span style="color: #b1b100;">key</span> 0<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;0<br />
&nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">keys</span> l_histo<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>Key points:</p>

<ul>
<li><code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#91;</span>l_histo r_histo<span style="color: #66cc66;">&#93;</span></span></code> -- these are the arguments to the dot-product function.
<li><code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">fn</span></span></code> defines an anonymous function.
<li><code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">get</span></span></code> gets the value for the specified key from the specified map, returning the final argument when the key is absent.
<li><code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #cc66cc;">0</span></span></code> is the default value for <code class="codecolorer clojure dawn"><span class="clojure">product</span></code>
<li><code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">keys</span></span></code> returns the keys of the specified hash map
<li>Clojure really wins a lot by delegating to Java. This happened for free: <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#40;</span>Math/sqrt <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span></span></code> => <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #cc66cc;">10.0</span></span></code>
</ul>

<p>So, reduce is a critical step for people coming from imperative programming languages looking to do basic things with collections.</p>
<img src="http://feeds.feedburner.com/~r/foognostic/~4/W5RoOx4B3zQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.foognostic.net/2009/06/reduce-your-way-into-good-clojure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.foognostic.net/2009/06/reduce-your-way-into-good-clojure/</feedburner:origLink></item>
		<item>
		<title>Put that for loop down</title>
		<link>http://feedproxy.google.com/~r/foognostic/~3/utk-Sw-lWM4/</link>
		<comments>http://blogs.foognostic.net/2009/06/put-that-for-loop-down/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 19:22:49 +0000</pubDate>
		<dc:creator>Seth Schroeder</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[lisp]]></category>

		<guid isPermaLink="false">http://blogs.foognostic.net/?p=124</guid>
		<description><![CDATA[I've been spending a lot of time with Clojure recently to prepare for the ICFP 2009 contest. It hasn't been the easiest thing, but the difficulty has come from trying to grasp many new concepts at once. The documentation has been pretty good, and the IRC channel has been pretty worthwhile.

I'm trying to settle on [...]]]></description>
			<content:encoded><![CDATA[<p>I've been spending a lot of time with Clojure recently to prepare for the ICFP 2009 contest. It hasn't been the easiest thing, but the difficulty has come from trying to grasp many new concepts at once. The documentation has been pretty good, and the IRC channel has been pretty worthwhile.</p>

<p>I'm trying to settle on a specific task to accomplish when learning a new language. Something concrete but still a little academic... implementing cosine similarity using character bigrams isn't much code, but it covers enough bases to be useful.</p>

<p>Part of the process involves splitting a string into overlapping character pairs.</p>

<p>Bad old me instinctively thinks "for loop".</p>

<p>Good new me thinks... bad old me probably has it right. But it doesn't feel very Clojure-y. This solution is a little more Clojuriffic, probably still a long shot from idiomatic:</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defn</span> split-bigrams <span style="color: #66cc66;">&#91;</span>text<span style="color: #66cc66;">&#93;</span><br />
&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span> <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">fn</span> <span style="color: #66cc66;">&#91;</span>l r<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">str</span> l r<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">seq</span> text<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">rest</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">seq</span> text<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>Bad old me (BOM) would have iterated over the string using a for loop with an index variable, cycling length - 1 times.</p>

<p>Good new me (GNM) had to take a different approach. I converted the string into two sequences; the first was just the string, but the second was the string minus the first character.</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">sequence A : <span style="color: #66cc66;">&#91;</span>A B C D E F<span style="color: #66cc66;">&#93;</span><br />
sequence A': <span style="color: #66cc66;">&#91;</span>B C D E F<span style="color: #66cc66;">&#93;</span></div></div>

<p>The <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span><span style="color: #66cc66;">&#41;</span></span></code> function takes a callback and a number of sequences. The callback function gets the nth element from each sequence, and the output from the callback is collected and returned by <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span><span style="color: #66cc66;">&#41;</span></span></code>. I don't have to specify a length or repetition count, as <code class="codecolorer clojure dawn"><span class="clojure"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">map</span><span style="color: #66cc66;">&#41;</span></span></code> stops processing when any of the collections run out of data.</p>

<div class="codecolorer-container clojure vibrant" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="clojure codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">user=&gt; <span style="color: #66cc66;">&#40;</span>split-bigrams <span style="color: #ff0000;">&quot;ABCDEF&quot;</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; <br />
<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;AB&quot;</span> <span style="color: #ff0000;">&quot;BC&quot;</span> <span style="color: #ff0000;">&quot;CD&quot;</span> <span style="color: #ff0000;">&quot;DE&quot;</span> <span style="color: #ff0000;">&quot;EF&quot;</span><span style="color: #66cc66;">&#41;</span></div></div>

<p>This hasn't taken a lot of time, but more than I like. It has taken <b>A LOT</b> of patience, energy, and focus during that time. But I like finding new ways to solve problems. Now I have a new tool in my nerdbelt, one that pushes the for loop back a little bit.</p>
<img src="http://feeds.feedburner.com/~r/foognostic/~4/utk-Sw-lWM4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.foognostic.net/2009/06/put-that-for-loop-down/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.foognostic.net/2009/06/put-that-for-loop-down/</feedburner:origLink></item>
		<item>
		<title>Clojure for the ICFP 2009 contest?</title>
		<link>http://feedproxy.google.com/~r/foognostic/~3/kYBVbi1_DJU/</link>
		<comments>http://blogs.foognostic.net/2009/06/clojure-for-the-icfp-2009-contest/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 12:56:32 +0000</pubDate>
		<dc:creator>Seth Schroeder</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[icfp]]></category>

		<guid isPermaLink="false">http://blogs.foognostic.net/?p=120</guid>
		<description><![CDATA[So the programming contest for the 12th International Conference on Functional Programming is coming up soon -- June 26th - 29th. Last year team Foognostic submitted a Ruby-based solution which made it to the second round. This year we will take another stab at it, this time with <a href="clojure.org/">Clojure</a>. Anyone else interested?]]></description>
			<content:encoded><![CDATA[<p>So the programming contest for the 12th International Conference on Functional Programming is coming up soon -- June 26th - 29th. Last year team Foognostic submitted a Ruby-based solution which made it to the second round. This year we will take another stab at it, this time with <a href="clojure.org/">Clojure</a>. Anyone else interested?</p>

<p>Last year I installed <a href="http://trac.edgewall.org/">Trac</a> to help organize things and that was fun, but this year I think <a href="http://bitbucket.org">BitBucket</a> is the way to go. More features that someone else maintains -- woo!</p>

<p>This year another Foognostic solution will be submitted, barring unexpected events that weekend. For the sake of trying to look cool I will be using Clojure, mostly as an exercise to really explore a Lisp. I'm more familiar with Emacs Lisp, but Clojure is sitting right on top of all those featuriffic Java APIs.</p>

<p>The countdown timer stands at a little more than two weeks. That should be enough time to come up to speed, right? ... or have I just uttered famous last words?</p>

<p>In any event, watch this space for updates!</p>

<ul>
<li>The ICFP 2009 <a href="http://www.ittc.ku.edu/icfp-contest/">contest site</a>.
<li>The ICFP 2008 <a href="http://smlnj.org/icfp08-contest/task.html">task description</a>.
</ul>
<img src="http://feeds.feedburner.com/~r/foognostic/~4/kYBVbi1_DJU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blogs.foognostic.net/2009/06/clojure-for-the-icfp-2009-contest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blogs.foognostic.net/2009/06/clojure-for-the-icfp-2009-contest/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 1.752 seconds. --><!-- Cached page generated by WP-Super-Cache on 2009-11-13 21:12:09 --><!-- Compression = gzip -->
