<?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>stacyprowell.com &#187; linkedin</title>
	<atom:link href="http://stacyprowell.com/blog/tag/linkedin/feed/" rel="self" type="application/rss+xml" />
	<link>http://stacyprowell.com/blog</link>
	<description>Ugh, Stacy&#039;s talking again...</description>
	<lastBuildDate>Sat, 04 Feb 2012 06:01:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Obama&#8217;s Long Form Birth Certificate&#8230; Again!</title>
		<link>http://stacyprowell.com/blog/2012/02/04/591/</link>
		<comments>http://stacyprowell.com/blog/2012/02/04/591/#comments</comments>
		<pubDate>Sat, 04 Feb 2012 06:00:15 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[obama]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=591</guid>
		<description><![CDATA[I just read this post on Facebook.  Fun! 1. Back in 1961 people of color were called &#8216;Negroes.&#8217; So how can the Obama &#8216;birth certificate&#8217; state he is &#8216;African-American&#8217; when the term wasn&#8217;t even used at that time? 2. The birth certificate that the White House released lists Obama&#8217;s birth as August 4, 1961. It [...]]]></description>
			<content:encoded><![CDATA[<p>I just read this post on Facebook.  Fun!</p>
<blockquote><p>1. Back in 1961 people of color were called &#8216;Negroes.&#8217; So how can the Obama &#8216;birth certificate&#8217; state he is &#8216;African-American&#8217; when the term wasn&#8217;t even used at that time?</p>
<p><span id="more-591"></span></p>
<p>2. The birth certificate that the White House released lists Obama&#8217;s birth as August 4, 1961. It also lists Barack Hussein Obama as his father. No big deal, right? At the time of Obama&#8217;s birth, it also shows that his father is aged 25 years old, and that Obama&#8217;s father was born in &#8221; Kenya , East Africa &#8220;. This wouldn&#8217;t seem like anything of concern, except the fact that Kenya did not even exist until 1963, two whole years after Obama&#8217;s birth, and 27 years after his father&#8217;s birth. How could Obama&#8217;s father have been born in a country that did not yet exist? Up and until Kenya was formed in 1963, it was known as the &#8221; British East Africa Protectorate&#8221;.</p>
<p>3. On the birth certificate released by the White House, the listed place of birth is &#8220;Kapi&#8217;olani Maternity &amp; Gynecological Hospital &#8220;. This cannot be, because the hospital(s) in question in 1961 were called &#8220;KauiKeolani Children&#8217;s Hospital&#8221; and &#8220;Kapi&#8217;olani Maternity Home&#8221;, respectively. The name did not change to Kapi&#8217;olani Maternity &amp; GynecologicalHospital until 1978, when these two hospitals merged. How can this particular name of the hospital be on a birth certificate dated 1961 if this name had not yet been applied to it until 1978?</p></blockquote>
<p>So let&#8217;s consider this, and take it one piece at a time.</p>
<p>1. Back in 1961 people of color were called &#8216;Negroes.&#8217; So how can the Obama &#8216;birth certificate&#8217; state he is &#8216;African-American&#8217; when the term wasn&#8217;t even used at that time?</p>
<p>Well, Obama&#8217;s &#8220;long form&#8221; birth certificate (released in April of 2011) does not state that Obama is an African-American.  It states that the father&#8217;s race is &#8220;African.&#8221;  The term &#8220;African&#8221; was used for native-born Africans, such as Obama&#8217;s father.  I really don&#8217;t think there is any reason to dig further here, given how obviously bad the next two claims apparently are.</p>
<p>2. Kenya did not even exist until 1963, two whole years after Obama&#8217;s birth, and 27 years after his father&#8217;s birth. How could Obama&#8217;s father have been born in a country that did not yet exist? Up and until Kenya was formed in 1963, it was known as the &#8221; British East Africa Protectorate&#8221;.</p>
<p>Wow.  This required a trip to Wikipedia.</p>
<blockquote><p>The <a title="British Empire" href="http://en.wikipedia.org/wiki/British_Empire">British Empire</a> established the <a title="East Africa Protectorate" href="http://en.wikipedia.org/wiki/East_Africa_Protectorate">East Africa Protectorate</a> in 1895, known from 1920 as the <a title="Kenya Colony" href="http://en.wikipedia.org/wiki/Kenya_Colony">Kenya Colony</a>. The independent Republic of Kenya was founded in December 1963.</p></blockquote>
<p>The East African Republic of Kenya has been called Kenya for a long time, apparently named after Mount Kenya.  (Aside: Have you watched <em>The Ghost and the Darkness</em>? Tasvo is in Kenya.)  While it didn&#8217;t get full independence from Britain until 1963, it was called Kenya, Kenya Colony, and The Colony of Kenya, and that is how Obama&#8217;s father would have identified it to the doctors, etc., especially since they were seeking independence at this time, to be achieved in 1963.  &#8220;Where are you from?&#8221;  &#8220;Kenya.&#8221;</p>
<p>Of course there is no reason to believe Wikipedia, but the conspiracy must be massive, because the name &#8220;Kenya&#8221; is everywhere prior to 1963.  A quick search on Google books turns up <a href="http://books.google.com/books?id=NTy942oX6f4C&amp;q=kenya#search_anchor">this book</a> (<em><span class="fn">The Akamba in British East Africa</span></em><span class="fn">, </span>by <span class="secondary">Gerhard Lindblom</span>) from 1920 that talks about Kenya as if it were&#8230; well&#8230; Kenya.</p>
<p>Finally, here&#8217;s <a href="http://www.ngmapcollection.com/product.aspx?cid=1561&amp;pid=15640">a map from 1950</a>.  Have a look.  You know where Kenya is, right?  About halfway down the eastern side of the continent.</p>
<p>3. On the birth certificate released by the White House, the listed place of birth is &#8220;Kapi&#8217;olani Maternity &amp; Gynecological Hospital &#8220;. This cannot be, because the hospital(s) in question in 1961 were called &#8220;KauiKeolani Children&#8217;s Hospital&#8221; and &#8220;Kapi&#8217;olani Maternity Home&#8221;, respectively. The name did not change to Kapi&#8217;olani Maternity &amp; Gynecological Hospital until 1978, when these two hospitals merged.</p>
<p>I visit Wikipedia again, and find this.</p>
<blockquote><p>In 1931, its name was changed to the Kapi?olani Maternity and Gynecological Hospital.</p></blockquote>
<p>Of course there is no reason to believe Wikipedia.  But then it didn&#8217;t take long to call the hospital and ask about the name.  I have Sprint&#8217;s &#8220;everything&#8221; plan, so it&#8217;s also (relatively) cheap to call.  Apparently they do get asked, because I got the answer pretty quickly.  1931.  <a href="http://nicedeb.wordpress.com/2011/04/28/yes-there-was-a-kapiolani-maternity-gynecological-hospital-in-1961/">Others</a> have been over this, too.  I won&#8217;t beat on this issue when <a href="http://nativeborncitizen.wordpress.com/2011/04/27/educating-the-confused-kapiolani-maternity-gynecological-hospital/">others have covered it so extensively</a>.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2012%2F02%2F04%2F591%2F&amp;title=Obama%26%238217%3Bs%20Long%20Form%20Birth%20Certificate%26%238230%3B%20Again%21" id="wpa2a_2"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2012/02/04/591/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My Ubuntu 11.10 Desktop</title>
		<link>http://stacyprowell.com/blog/2012/01/03/my-ubuntu-11-10-desktop/</link>
		<comments>http://stacyprowell.com/blog/2012/01/03/my-ubuntu-11-10-desktop/#comments</comments>
		<pubDate>Tue, 03 Jan 2012 05:18:07 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Nuisances]]></category>
		<category><![CDATA[linkedin]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=572</guid>
		<description><![CDATA[At the risk of drawing universal hatred, Unity isn&#8217;t so bad once you set the icon bar to autohide. Very uncluttered, quite stable, and even with no hardware acceleration it has good performance. That said&#8230; I don&#8217;t like it. So what to do? Everyone has been fleeing to Mint, but I&#8217;ve had stability problems, which [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_573" class="wp-caption alignright" style="width: 310px"><a href="http://stacyprowell.com/blog/wp-content/uploads/2012/01/Screenshot-at-2012-01-01-203146.png"><img class="size-medium wp-image-573" title="My Ubuntu 11.10 Desktop" src="http://stacyprowell.com/blog/wp-content/uploads/2012/01/Screenshot-at-2012-01-01-203146-300x168.png" alt="Desktop Image" width="300" height="168" /></a><p class="wp-caption-text">My Ubuntu 11.10 Desktop</p></div>
<p>At the risk of drawing universal hatred, Unity isn&#8217;t so bad once you set the icon bar to autohide. Very uncluttered, quite stable, and even with no hardware acceleration it has good performance. That said&#8230; I don&#8217;t like it.</p>
<p>So what to do? Everyone has been fleeing to Mint, but I&#8217;ve had stability problems, which is odd since it is basically just Ubuntu with some minor changes. I really like MintMenu, but Mint&#8217;s not an &#8220;approved&#8221; OS at work, so I need to use Ubuntu.</p>
<p>Here&#8217;s what I do to the stock Ubuntu 11.10 &#8220;Oneric&#8221; installation to make it habitable (for me &#8211; YMMV).<span id="more-572"></span></p>
<p>Most of this is taken from other web sites I&#8217;ve forgotten. A good resource is this one: <a href="http://www.webupd8.org/2011/10/things-to-tweak-after-installing-ubuntu.html" target="_blank">http://www.webupd8.org/2011/10/things-to-tweak-after-installing-ubuntu.html</a></p>
<p>You can install MintMenu or GnoMenu in the Cairo dock and dispense with Gnome shell&#8230; but I actually like the &#8220;expose&#8221; feature when I hit the Window key, so I&#8217;m keeping Gnome shell for now.</p>
<p>Install Ubuntu 11.10. Log in with the Unity interface. Open a terminal and let&#8217;s do the following.</p>
<ul>
<li>Install Synaptic.</li>
<ul>
<li><code>sudo apt-get install synaptic</code></li>
</ul>
<li>Fix the scroll bars. This will give you real scroll bars, but you&#8217;ll really need to reboot at some point to be sure the evil overlay library is gone.</li>
<ul>
<li><code>sudo apt-get remove overlay-scrollbar liboverlay-scrollbar3-0.2-0 liboverlay-scrollbar-0.2-0</code></li>
</ul>
<li>Install Sun Java instead of OpenJDK.</li>
<ul>
<li><code>sudo add-apt-repository ppa:ferramroberto/java</code></li>
<li><code>sudo apt-get update</code></li>
<li><code>sudo apt-get install sun-java6-jdk sun-java6-plugin</code></li>
<li><code>sudo update-alternatives --config java</code></li>
<li>Now select the Sun JDK. If for some reason you didn&#8217;t get OpenJDK during the install, you won&#8217;t have any other Java versions to select, and that&#8217;s okay.</li>
</ul>
<li>Mint uses the Gnome Shell, so let&#8217;s install that and some settings managers.</li>
<ul>
<li><code>sudo apt-get install gnome-shell gnome-tweak-tool compizconfig-settings-manager dconf-tools</code></li>
<li>Incidentally, at this point if you run <code>ccsm</code> you can actually turn off Unity! Don&#8217;t do that yet; let&#8217;s replace it with something else first.</li>
</ul>
<li>Make Gnome Shell the default for logging in.</li>
<ul>
<li><code>sudo /usr/lib/lightdm/lightdm-set-defaults -s gnome-shell<br />
</code></li>
</ul>
<li>Let&#8217;s install the old-style Gnome desktop, too.</li>
<ul>
<li><code>sudo apt-get install gnome-session-fallback</code></li>
</ul>
<li>Log out. Click the little gear and select &#8220;GNOME&#8221; and log back in. Now you&#8217;re running Gnome shell, and not Unity. Congratulations!  You can also select the Gnome with no fancy stuff, but then you have panels at the top and bottom (I&#8217;m going to add Cairo), and the default menu doesn&#8217;t allow typing names.</li>
<li>Install the Cairo dock. I like docks, and Cairo (so far) seems to be the most stable and best option.</li>
<ul>
<li><code>sudo apt-get install cairo-dock</code></li>
</ul>
<li>Now, I like Alt+F2 to bring up the &#8220;run a command&#8221; dialog. Let&#8217;s enable that.</li>
<ul>
<li>Click your name in the upper right and select System Settings.</li>
<li>Click Keyboard and then switch to the Shortcuts tab.<br />
Select System.</li>
<li>Double-click the &#8220;Show the run command prompt&#8221; item. It should change from &#8220;Disabled&#8221; to &#8220;New shortcut&#8230;&#8221;</li>
<li>Type Alt+F2.</li>
</ul>
</ul>
<p>Now, if you&#8217;re me, you want to install the following, with <code>apt-get install</code>.</p>
<ul>
<li>vim</li>
<li>emacs</li>
<li>eclipse</li>
<li>g++</li>
<li>cmake</li>
<li>ant</li>
<li>git</li>
<li>subversion</li>
</ul>
<p>I like to have Scala, but I don&#8217;t install it from the packages. They scatter it around a bit, and this confuses some software like the Idea IDE. Besides, you should keep it up to date with <code>sbaz</code>.</p>
<ul>
<li>To install Scala globally from a terminal do the following. Of course, you can install it for just you, and that might be what you want in some cases.</li>
<ul>
<li><code>wget http://www.scala-lang.org/downloads/distrib/files/scala-2.9.1.final.tgz</code></li>
<li><code>cd /usr/local</code></li>
<li><code>sudo tar xvzf ~/scala-2.9.1.final.tgz</code></li>
<li><code>sudo ln -s scala-2.9.1.final scala</code></li>
<li><code>cd bin; for file in ../scala/bin/* ; do if [ -x $file ] ; then sudo ln -s $file ; fi ; done</code></li>
<li>The last command makes all the scala stuff visible to both regular users and to root, which is useful, since this lets root run sbaz. You can avoid all this by just installing scala under your home area and then adding its bin folder to your path. I assume you are all capable of that.</li>
<li><code>sudo sbaz install scala-devel-docs scala-documentation</code></li>
</ul>
</ul>
<p>And that&#8217;s it!</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2012%2F01%2F03%2Fmy-ubuntu-11-10-desktop%2F&amp;title=My%20Ubuntu%2011.10%20Desktop" id="wpa2a_4"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2012/01/03/my-ubuntu-11-10-desktop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gosu</title>
		<link>http://stacyprowell.com/blog/2011/12/05/gosu/</link>
		<comments>http://stacyprowell.com/blog/2011/12/05/gosu/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 01:06:58 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Gosu]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Nuisances]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[linkedin]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=538</guid>
		<description><![CDATA[Deriving its name from the Korean term for a &#8220;highly skilled person,&#8221; Gosu is a computer language targeting the Java Virtual Machine.  It tries to be more expressive than Java (which isn&#8217;t really all that hard) while being conceptually simpler than Scala.  Of course, sometimes simplicity comes with other baggage.  &#8220;Type parameters are covariant. This [...]]]></description>
			<content:encoded><![CDATA[<p>Deriving its name from the Korean term for a &#8220;highly skilled person,&#8221; Gosu is a computer language targeting the Java Virtual Machine.  It tries to be more expressive than Java (which isn&#8217;t really all that hard) while being conceptually simpler than Scala.  <span id="more-538"></span>Of course, sometimes simplicity comes with other baggage.  &#8220;Type parameters are covariant. This is not sound, and that does not matter&#8221; (<a href="http://lazygosu.org/generics.html">http://lazygosu.org/generics.html</a>).  I&#8217;ve spent some time arguing about covariance, contravariance, and invariance, and I think it matters.</p>
<p>Ooh!  See <a href="http://channel9.msdn.com/shows/Going+Deep/E2E-Brian-Beckman-and-Erik-Meijer-CoContravariance-in-Physics-and-Programming-1-of-2/">this</a>.</p>
<p>The real issue with Gosu, however, is documentation.  A quick search for Gosu turns up <a href="http://gosu-lang.org/">http://gosu-lang.org/</a>.  From this you can find a quick start guide and comprehensive documentation.  Great!  I read the quick introduction and then download the distribution.  It&#8217;s nice!  It comes with a lightweight IDE and an interactive &#8220;REPL&#8221; mode.  That&#8217;s what I like to see.  Next, I dig into the comprehensive documentation&#8230; but examples from the documentation don&#8217;t work!</p>
<p>For instance, the documentation of DateTime (<a href="http://gosu-lang.org/doc/wwhelp/wwhimpl/js/html/wwhelp.htm#href=Gosu%20Reference%20Guide/datatypes.06.05.html">here</a>) says:</p>
<blockquote><p>Gosu implicitly coerces the Gosu DateTime object from String in most formats. For example:</p>
<pre>var date : DateTime = "2007-01-02"</pre>
</blockquote>
<p>Nifty!  I try that and I get:</p>
<blockquote>
<pre>program_.__Program__0

Errors: 

The type "java.lang.String" cannot be converted to "java.util.Date" [line:1 col:23] in
line 1: var date : DateTime = "2007-01-02"
Expected Type: Date
Line Number: 1  Column: 23</pre>
</blockquote>
<p>That doesn&#8217;t seem like automatic coercion to me.  Never one to give up without a fight, I post the following message to the Gosu forum:</p>
<blockquote><p>The following code:</p>
<p>var date : DateTime = &#8220;2007-01-02&#8243;</p>
<p>taken directly from the Gosu DateTime page fails to compile with the<br />
following errors:</p>
<p>Errors:</p>
<p>The type &#8220;java.lang.String&#8221; cannot be converted to<br />
&#8220;java.util.Date&#8221; [line:1 col:23] in<br />
line 1: var date : DateTime = &#8220;2007-01-02&#8243;<br />
Expected Type: Date<br />
Line Number: 1  Column: 23</p>
<p>This is also true if one tries &#8220;Jan 2, 2007&#8243; as DateTime.<br />
So&#8230; What am I doing wrong?</p>
<p>$ gosu -version<br />
0.8.6.1-C</p>
<p>$ javac -version<br />
javac 1.6.0_26</p>
<p>$ java -version<br />
java version &#8220;1.6.0_26&#8243;</p>
<p>Java(TM) SE Runtime Environment (build 1.6.0_26-b03)<br />
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)</p>
<p>$ uname -rm<br />
3.0.0-13-generic x86_64</p>
<div>Thanks in advance.</div>
</blockquote>
<p>I expect something along the lines of &#8220;wrong version of Java,&#8221; or &#8220;you&#8217;re an idiot because.&#8221;  Instead, I receive this:</p>
<blockquote>
<div>In the open source version of Gosu we don&#8217;t support the implicit string to<br />
date coercion.  You&#8217;ll have to use the .toDate() method on String instead:<br />
var date  = &#8220;2007-01-02&#8243;.toDate()</div>
<div>Cheers,<br />
Carson</div>
</blockquote>
<p>Okay, great.  Apparently there is an open source version of Gosu and a closed source version.  I google in vain for information on these, but none appears.  Searching the documentation for &#8220;open source&#8221; turns up nothing.  Everything I read says Gosu is released under the Apache license, and nothing seems to indicate otherwise.  So&#8230; Clearly the comprehensive documentation is either wrong or incomplete.  I&#8217;m not happy with either.</p>
<p>I did reply asking where the differences are documented.  I&#8217;ll post here whatever I hear back.  I&#8217;ve probably missed some obvious, glaring, probably blinking notice about the differences.</p>
<p>So no Gosu for us, at least for now.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2011%2F12%2F05%2Fgosu%2F&amp;title=Gosu" id="wpa2a_6"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2011/12/05/gosu/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Grammar Rant of the Day</title>
		<link>http://stacyprowell.com/blog/2011/10/02/grammar-rant-of-the-day/</link>
		<comments>http://stacyprowell.com/blog/2011/10/02/grammar-rant-of-the-day/#comments</comments>
		<pubDate>Sun, 02 Oct 2011 16:44:04 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Nuisances]]></category>
		<category><![CDATA[grammar]]></category>
		<category><![CDATA[linkedin]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=507</guid>
		<description><![CDATA[And now for something completely different. Here are the so-called grammar rules I personally encourage everyone, everywhere to ignore and break. Do it today. I said do it! (1) Use the singular they. Shakespeare did. It is far superior to the alternatives. Why should every unknown person be a he or a him? &#8220;If a [...]]]></description>
			<content:encoded><![CDATA[<p>And now for something completely different.  Here are the so-called  grammar rules I personally encourage everyone, everywhere to ignore and  break.  Do it today.  I said do it!</p>
<p>(1) Use the singular they.  Shakespeare did.  It is far superior to the alternatives.  Why should every unknown person be a <em>he</em> or a <em>him</em>?</p>
<blockquote><p>&#8220;If a person wants to use the singular they, they should.&#8221;</p></blockquote>
<p>(2)  Split infinitives with singe words.  This is an idiotic, idiotic rule  created by idiots because they are too idiotic to know better.  Idiots.   Avoid the temptation to perhaps understandably, perhaps even defensibly  and rationally, but still annoyingly, go crazy.</p>
<blockquote><p>&#8220;Sentence diagrams are hard to easily do when you split your infinitives!&#8221;</p></blockquote>
<p>(3)  End your clauses with prepositions whenever you feel like it.  See the  previous comment.  Note that the &#8220;prohibition&#8221; against this has never  existed, except in the small, closed minds of amateur prescriptivism.   Professional prescriptivisits know this.  &#8220;P-stranding?&#8221;  Having a fancy  name for it doesn&#8217;t make it right.</p>
<blockquote><p>&#8220;That&#8217;s where it&#8217;s at!&#8221;</p></blockquote>
<p>(4) Coordinating conjunctions can join anything to anything.  Syntactic importance be damned!</p>
<blockquote><p>&#8220;I  was going to have an example, and that&#8217;s when I thought, damn, I need  an example.  And so I wrote this.  And here it is.  Now what?&#8221;</p></blockquote>
<p>(5) Dangle hopefully.  We all know what you mean.</p>
<blockquote><p>&#8220;Hopefully no grammarian&#8217;s head will explode when they read this.&#8221;</p></blockquote>
<p>(6) Make up your own words.  &#8220;Graphic&#8221; isn&#8217;t graphic enough for you?  Try graphical!  It&#8217;s <em>twice</em> the adjective!</p>
<blockquote><p>&#8220;I admire the realisticism of your new poster.  It&#8217;s like I&#8217;m literally there!&#8221;</p></blockquote>
<p>Some  things actually do bother me, but they tend to be in the &#8220;poor  communication&#8221; category.  That is, if you write something and folks  cannot tell what you mean, you need help.</p>
<p>Don&#8217;t get me wrong.  It  is important to know the rules.  Then you can know when you are  breaking them, decide which ones to break, and still get your point  across.  Even with fragments.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2011%2F10%2F02%2Fgrammar-rant-of-the-day%2F&amp;title=Grammar%20Rant%20of%20the%20Day" id="wpa2a_8"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2011/10/02/grammar-rant-of-the-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scala Sudoku</title>
		<link>http://stacyprowell.com/blog/2010/12/28/scala-sudoku/</link>
		<comments>http://stacyprowell.com/blog/2010/12/28/scala-sudoku/#comments</comments>
		<pubDate>Tue, 28 Dec 2010 06:48:07 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[sudoku]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=484</guid>
		<description><![CDATA[A while back I needed to get back up to speed on Python, so I wrote a Sudoku solver using a simple algorithm.  This required me to think just a bit about the language and how to implement this&#8230; plus it was fun. Now I&#8217;m tinkering with Scala, and it occurred to me to write [...]]]></description>
			<content:encoded><![CDATA[<p>A while back I needed to get back up to speed on Python, so I wrote a Sudoku solver using a simple algorithm.  This required me to think just a bit about the language and how to implement this&#8230; plus it was fun.</p>
<p>Now I&#8217;m tinkering with Scala, and it occurred to me to write a Sudoku solver tonight.  So just over an hour later I have one, and I&#8217;m posting it here.<span id="more-484"></span>  I make no claims about this being the right way to do things, or even a good way to do things.  In particular, it uses a dumb algorithm to solve Sudoku, and probably isn&#8217;t done The Scala Way.  In fact, I&#8217;m actually posting this so you can tell me where I&#8217;ve gone wrong, should have done things differently, etc.</p>
<p>Plus, if you want a Scala Sudoku solver, well, here&#8217;s one.  Enjoy!</p>
<p>Use this in one of three ways.</p>
<ol>
<li>Compile it and run it from the prompt.  Enter the board one line per row.</li>
<li>Load it into a running Scala REPL and use Board.init to interactively enter the board, one line per row.  Use the search method to find a solution.</li>
<li>Put the board in a file and use Board.read from the Scala REPL to load the board.  Use the search method to find a solution.</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #00ff00; font-style: italic;">/**
 * Simple Sudoku solver.
 * 2010 by Stacy Prowell.
 * http://stacyprowell.com/
 */</span>
<span style="color: #0000ff; font-weight: bold;">package</span> sjp.<span style="color: #000000;">sudoku</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/**
 * An exception indicating that the board has no solution.
 * @param msg  A message.
 * @author sprowell
 */</span>
<span style="color: #0000ff; font-weight: bold;">class</span> BadBoardException<span style="color: #F78811;">&#40;</span>msg<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Exception<span style="color: #F78811;">&#40;</span>msg<span style="color: #F78811;">&#41;</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/**
 * Helper class for search data.
 * @param fails  Number of failures detected so far during the search.
 * @author sprowell
 */</span>
<span style="color: #0000ff; font-weight: bold;">protected</span> <span style="color: #0000ff; font-weight: bold;">class</span> SearchData<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">var</span> fails<span style="color: #000080;">:</span>Int <span style="color: #000080;">=</span> <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/**
 * A ''board'' holds two items.
 * - The currently known cell values
 * - The set of possible values for each unknown cell
 * 
 * @author sprowell
 */</span>
<span style="color: #0000ff; font-weight: bold;">class</span> Board <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/** Size of board. */</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> N <span style="color: #000080;">=</span> <span style="color: #F78811;">9</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/** Representation of an unknown cell. */</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> U <span style="color: #000080;">=</span> <span style="color: #6666FF;">' '</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/** Known values in the grid. */</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> known <span style="color: #000080;">=</span> Array.<span style="color: #000000;">ofDim</span><span style="color: #F78811;">&#91;</span>Char<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>N,N<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until N<span style="color: #000080;">;</span> j <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until N<span style="color: #F78811;">&#41;</span> known<span style="color: #F78811;">&#40;</span>i<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>j<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> U
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/** Allowed possibilities for each cell. */</span>
  <span style="color: #0000ff; font-weight: bold;">val</span> possible <span style="color: #000080;">=</span> Array.<span style="color: #000000;">ofDim</span><span style="color: #F78811;">&#91;</span>Set<span style="color: #F78811;">&#91;</span>Char<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>N,N<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until N<span style="color: #000080;">;</span> j <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until N<span style="color: #F78811;">&#41;</span> possible<span style="color: #F78811;">&#40;</span>i<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>j<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #6666FF;">'1'</span> to <span style="color: #6666FF;">'9'</span> toSet
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Generate a string representing the current state of a cell.
   * There are two possiblities.
   * - `(5,4)=9` if the cell value is known
   * - `(5,4) allows {3,9}` if the cell value is now known
   * @param row  The zero-based row.
   * @param col  The zero-based column.
   * @return  The cell representation.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> cellToString<span style="color: #F78811;">&#40;</span>row<span style="color: #000080;">:</span>Int, col<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    require<span style="color: #F78811;">&#40;</span>row <span style="color: #000080;">&gt;=</span> <span style="color: #F78811;">0</span> <span style="color: #000080;">&amp;&amp;</span> row <span style="color: #000080;">&lt;</span> N<span style="color: #F78811;">&#41;</span>
    require<span style="color: #F78811;">&#40;</span>col <span style="color: #000080;">&gt;=</span> <span style="color: #F78811;">0</span> <span style="color: #000080;">&amp;&amp;</span> col <span style="color: #000080;">&lt;</span> N<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#40;</span>row,col<span style="color: #F78811;">&#41;</span> +
      <span style="color: #F78811;">&#40;</span>known<span style="color: #F78811;">&#40;</span>row<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>col<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">case</span> U <span style="color: #000080;">=&gt;</span> <span style="color: #6666FF;">&quot; allows {&quot;</span> + possible<span style="color: #F78811;">&#40;</span>row<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>col<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">mkString</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;,&quot;</span><span style="color: #F78811;">&#41;</span> + <span style="color: #6666FF;">&quot;}&quot;</span>
        <span style="color: #0000ff; font-weight: bold;">case</span> c <span style="color: #000080;">=&gt;</span> <span style="color: #6666FF;">&quot;=&quot;</span> + c
      <span style="color: #F78811;">&#125;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Set a cell to the given value.  This also updates the set of possible
   * values for each cell.
   * @param row  The zero-based row.
   * @param col  The zero-based column.
   * @param ch  The character, 0-9.
   * @return  The updated board.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> set<span style="color: #F78811;">&#40;</span>row<span style="color: #000080;">:</span>Int, col<span style="color: #000080;">:</span>Int, ch<span style="color: #000080;">:</span>Char<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Board <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #00ff00; font-style: italic;">/**
     * Tiny local function to exclude a value from the possibilities for
     * a cell.
     * @param rr  The zero-based row.
     * @param cc  The zero-based column.
     * @return  True if the cell is solved, false if it is not.
     * @throws  BadBoardException  There is no longer any viable solution.
     */</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> exclude<span style="color: #F78811;">&#40;</span>rr<span style="color: #000080;">:</span>Int, cc<span style="color: #000080;">:</span>Int, ex<span style="color: #000080;">:</span>Char<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>possible<span style="color: #F78811;">&#40;</span>rr<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>cc<span style="color: #F78811;">&#41;</span> contains ex<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        possible<span style="color: #F78811;">&#40;</span>rr<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>cc<span style="color: #F78811;">&#41;</span> -<span style="color: #000080;">=</span> ex
        possible<span style="color: #F78811;">&#40;</span>rr<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>cc<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">size</span> <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
          <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">0</span> <span style="color: #000080;">=&gt;</span> <span style="color: #0000ff; font-weight: bold;">throw</span> <span style="color: #0000ff; font-weight: bold;">new</span> BadBoardException<span style="color: #F78811;">&#40;</span>cellToString<span style="color: #F78811;">&#40;</span>rr,cc<span style="color: #F78811;">&#41;</span> +
            <span style="color: #6666FF;">&quot; has no possibilities&quot;</span><span style="color: #F78811;">&#41;</span>
          <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">1</span> <span style="color: #000080;">=&gt;</span> <span style="color: #0000ff; font-weight: bold;">true</span>
          <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #000080;">_</span> <span style="color: #000080;">=&gt;</span> <span style="color: #0000ff; font-weight: bold;">false</span>
        <span style="color: #F78811;">&#125;</span>
      <span style="color: #F78811;">&#125;</span>
      <span style="color: #0000ff; font-weight: bold;">false</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// Check the arguments.</span>
    require<span style="color: #F78811;">&#40;</span>row <span style="color: #000080;">&gt;=</span> <span style="color: #F78811;">0</span> <span style="color: #000080;">&amp;&amp;</span> row <span style="color: #000080;">&lt;</span> N<span style="color: #F78811;">&#41;</span>
    require<span style="color: #F78811;">&#40;</span>col <span style="color: #000080;">&gt;=</span> <span style="color: #F78811;">0</span> <span style="color: #000080;">&amp;&amp;</span> col <span style="color: #000080;">&lt;</span> N<span style="color: #F78811;">&#41;</span>
    require<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">'1'</span> <span style="color: #000080;">&lt;=</span> ch <span style="color: #000080;">&amp;&amp;</span> ch <span style="color: #000080;">&lt;=</span> <span style="color: #6666FF;">'9'</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// The proposed value must be allowed for the cell.</span>
    <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>known<span style="color: #F78811;">&#40;</span>row<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>col<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">!=</span> U <span style="color: #000080;">&amp;&amp;</span> known<span style="color: #F78811;">&#40;</span>row<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>col<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">!=</span> ch<span style="color: #F78811;">&#41;</span>
      <span style="color: #0000ff; font-weight: bold;">throw</span> <span style="color: #0000ff; font-weight: bold;">new</span> BadBoardException<span style="color: #F78811;">&#40;</span>cellToString<span style="color: #F78811;">&#40;</span>row,col<span style="color: #F78811;">&#41;</span> + <span style="color: #6666FF;">&quot; cannot be &quot;</span> + ch<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">!</span>possible<span style="color: #F78811;">&#40;</span>row<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>col<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">contains</span><span style="color: #F78811;">&#40;</span>ch<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #0000ff; font-weight: bold;">throw</span> <span style="color: #0000ff; font-weight: bold;">new</span> BadBoardException<span style="color: #F78811;">&#40;</span>cellToString<span style="color: #F78811;">&#40;</span>row,col<span style="color: #F78811;">&#41;</span> + <span style="color: #6666FF;">&quot; cannot be &quot;</span> + ch<span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// Record any cells that get solved along the way.</span>
    <span style="color: #0000ff; font-weight: bold;">var</span> also <span style="color: #000080;">=</span> List<span style="color: #F78811;">&#91;</span><span style="color: #F78811;">&#40;</span>Int,Int<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// Cannot have same value in row or column.</span>
    <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until N<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>i <span style="color: #000080;">!=</span> col <span style="color: #000080;">&amp;&amp;</span> exclude<span style="color: #F78811;">&#40;</span>row,i,ch<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> also <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span>row,i<span style="color: #F78811;">&#41;</span><span style="color: #000080;">::</span>also
      <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>i <span style="color: #000080;">!=</span> row <span style="color: #000080;">&amp;&amp;</span> exclude<span style="color: #F78811;">&#40;</span>i,col,ch<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> also <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span>i,col<span style="color: #F78811;">&#41;</span><span style="color: #000080;">::</span>also
    <span style="color: #F78811;">&#125;</span> <span style="color: #008000; font-style: italic;">// Exclude in row and column.</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// Cannot have same value in block.</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> <span style="color: #F78811;">&#40;</span>brow,bcol<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span>row/<span style="color: #F78811;">3</span><span style="color: #000080;">*</span><span style="color: #F78811;">3</span>,col/<span style="color: #F78811;">3</span><span style="color: #000080;">*</span><span style="color: #F78811;">3</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> to <span style="color: #F78811;">2</span><span style="color: #000080;">;</span> j <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> to <span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> <span style="color: #F78811;">&#40;</span>rr,cc<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span>brow+i,bcol+j<span style="color: #F78811;">&#41;</span>
      <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>rr <span style="color: #000080;">!=</span> row <span style="color: #000080;">&amp;&amp;</span> cc <span style="color: #000080;">!=</span> col <span style="color: #000080;">&amp;&amp;</span> exclude<span style="color: #F78811;">&#40;</span>rr,cc,ch<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> also <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span>rr,cc<span style="color: #F78811;">&#41;</span><span style="color: #000080;">::</span>also
    <span style="color: #F78811;">&#125;</span> <span style="color: #008000; font-style: italic;">// Exclude in block.</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// Fix up the cell.  Set known and the set of possibilities.</span>
    known<span style="color: #F78811;">&#40;</span>row<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>col<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> ch <span style="color: #000080;">;</span> possible<span style="color: #F78811;">&#40;</span>row<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>col<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> Set<span style="color: #F78811;">&#40;</span>ch<span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// Done.  Now check to see if we discovered any other values.</span>
    <span style="color: #008000; font-style: italic;">// If so, then set those now, recursively.</span>
    also foreach <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">&#40;</span>rr,cc<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> set<span style="color: #F78811;">&#40;</span>rr,cc,possible<span style="color: #F78811;">&#40;</span>rr<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>cc<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">head</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span> <span style="color: #008000; font-style: italic;">// Set each discovered solved cell.</span>
    <span style="color: #0000ff; font-weight: bold;">this</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Create a duplicate of this board.
   * @return  An independent clone of this board.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> clone <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> cl <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Board<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until N<span style="color: #000080;">;</span> j <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until N<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      cl.<span style="color: #000000;">known</span><span style="color: #F78811;">&#40;</span>i<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>j<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> known<span style="color: #F78811;">&#40;</span>i<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>j<span style="color: #F78811;">&#41;</span>
      cl.<span style="color: #000000;">possible</span><span style="color: #F78811;">&#40;</span>i<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>j<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> possible<span style="color: #F78811;">&#40;</span>i<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>j<span style="color: #F78811;">&#41;</span> 
    <span style="color: #F78811;">&#125;</span> <span style="color: #008000; font-style: italic;">// Duplicate known and possible.</span>
    cl
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Initialize a row.  This is only intended to be used from the
   * companion object's init.  The provided string can be any length;
   * only the first N or fewer characters are used.
   * @param row  The zero-based row.
   * @param vals  A string of the values in that row.
   * @return  The updated board.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">def</span> init<span style="color: #F78811;">&#40;</span>row<span style="color: #000080;">:</span>Int, vals<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    require<span style="color: #F78811;">&#40;</span>row <span style="color: #000080;">&gt;=</span> <span style="color: #F78811;">0</span> <span style="color: #000080;">&amp;&amp;</span> row <span style="color: #000080;">&lt;</span> N<span style="color: #F78811;">&#41;</span>
    require<span style="color: #F78811;">&#40;</span>vals.<span style="color: #000000;">length</span> <span style="color: #000080;">&lt;=</span> N<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">var</span> col <span style="color: #000080;">=</span> <span style="color: #F78811;">0</span>
    vals.<span style="color: #000000;">foreach</span><span style="color: #F78811;">&#123;</span>ch <span style="color: #000080;">=&gt;</span> <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>ch <span style="color: #000080;">!=</span> U<span style="color: #F78811;">&#41;</span> set<span style="color: #F78811;">&#40;</span>row,col,ch<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">;</span> col +<span style="color: #000080;">=</span> <span style="color: #F78811;">1</span><span style="color: #F78811;">&#125;</span>
    <span style="color: #0000ff; font-weight: bold;">this</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #008000; font-style: italic;">// Import used below.</span>
  <span style="color: #0000ff; font-weight: bold;">import</span> java.<span style="color: #000000;">io</span>.<span style="color: #F78811;">&#123;</span>Writer,FileWriter<span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Write the board to the given writer.
   * @param writer  The writer to get the output.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> write<span style="color: #F78811;">&#40;</span>writer<span style="color: #000080;">:</span>Writer<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    writer.<span style="color: #000000;">write</span><span style="color: #F78811;">&#40;</span>known.<span style="color: #000000;">map</span><span style="color: #F78811;">&#40;</span>row <span style="color: #000080;">=&gt;</span> row.<span style="color: #000000;">mkString</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">mkString</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;<span style="color: #0000ff; font-weight: bold;">\n</span>&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
    writer.<span style="color: #000000;">flush</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Write the board to the given file.
   * @param file  The file name.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> write<span style="color: #F78811;">&#40;</span>file<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    write<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> FileWriter<span style="color: #F78811;">&#40;</span>file<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Pretty-print the board.
   * @return  String representing the current known state of the board.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> toString<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> rts<span style="color: #F78811;">&#40;</span>row<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span>
      known<span style="color: #F78811;">&#40;</span>row<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">slice</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span>, <span style="color: #F78811;">3</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">mkString</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;&quot;</span><span style="color: #F78811;">&#41;</span> + <span style="color: #6666FF;">&quot; | &quot;</span> +
      known<span style="color: #F78811;">&#40;</span>row<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">slice</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">3</span>, <span style="color: #F78811;">6</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">mkString</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;&quot;</span><span style="color: #F78811;">&#41;</span> + <span style="color: #6666FF;">&quot; | &quot;</span> +
      known<span style="color: #F78811;">&#40;</span>row<span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">slice</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">6</span>, <span style="color: #F78811;">9</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">mkString</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;&quot;</span><span style="color: #F78811;">&#41;</span> + <span style="color: #6666FF;">&quot;<span style="color: #0000ff; font-weight: bold;">\n</span>&quot;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">var</span> str <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;&quot;</span>
    <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>rblock <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until N by <span style="color: #F78811;">3</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>rblock <span style="color: #000080;">!=</span> <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> str +<span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;----+-----+----<span style="color: #0000ff; font-weight: bold;">\n</span>&quot;</span>
      <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>roff <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> to <span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        str +<span style="color: #000080;">=</span> rts<span style="color: #F78811;">&#40;</span>rblock+roff<span style="color: #F78811;">&#41;</span>
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span> <span style="color: #008000; font-style: italic;">// Format all blocks.</span>
    str
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Search for a solution.
   * @return  Solution, if one is found.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> search<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Option<span style="color: #F78811;">&#91;</span>Board<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> search<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> SearchData<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Search for a solution.
   * @param sd  Search data block.
   * @return  Solution, if one is found.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">private</span> <span style="color: #0000ff; font-weight: bold;">def</span> search<span style="color: #F78811;">&#40;</span>sd<span style="color: #000080;">:</span>SearchData<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Option<span style="color: #F78811;">&#91;</span>Board<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #008000; font-style: italic;">// Get the cells that are currently unknown, and get the possibilities</span>
    <span style="color: #008000; font-style: italic;">// for each.</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> unknowns <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until N<span style="color: #000080;">;</span> j <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until N <span style="color: #0000ff; font-weight: bold;">if</span> known<span style="color: #F78811;">&#40;</span>i<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>j<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">==</span> U<span style="color: #F78811;">&#41;</span>
        <span style="color: #0000ff; font-weight: bold;">yield</span> <span style="color: #F78811;">&#40;</span>i,j,possible<span style="color: #F78811;">&#40;</span>i<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#40;</span>j<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// If all cells are known... We are done!</span>
    <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>unknowns.<span style="color: #000000;">length</span> <span style="color: #000080;">==</span> <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      printf<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Solution found:  fails=%d<span style="color: #0000ff; font-weight: bold;">\n</span>&quot;</span>, sd.<span style="color: #000000;">fails</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #0000ff; font-weight: bold;">return</span> Option<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// Randomly choose an unknown</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> <span style="color: #F78811;">&#40;</span>row, col, choices<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> unknowns<span style="color: #F78811;">&#40;</span>scala.<span style="color: #000000;">util</span>.<span style="color: #000000;">Random</span>.<span style="color: #000000;">nextInt</span><span style="color: #F78811;">&#40;</span>unknowns.<span style="color: #000000;">length</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
    <span style="color: #008000; font-style: italic;">// Try each choice for the unknown cell.</span>
    choices foreach <span style="color: #F78811;">&#123;</span>
      ch <span style="color: #000080;">=&gt;</span>
      <span style="color: #008000; font-style: italic;">// Copy the board and set the value.  Then recursively seek a</span>
      <span style="color: #008000; font-style: italic;">// solution.  If we find one, we are done.</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> newboard <span style="color: #000080;">=</span> clone
      <span style="color: #0000ff; font-weight: bold;">try</span> <span style="color: #F78811;">&#123;</span>
        newboard.<span style="color: #000000;">set</span><span style="color: #F78811;">&#40;</span>row, col, ch<span style="color: #F78811;">&#41;</span>
        <span style="color: #0000ff; font-weight: bold;">val</span> soln <span style="color: #000080;">=</span> newboard.<span style="color: #000000;">search</span><span style="color: #F78811;">&#40;</span>sd<span style="color: #F78811;">&#41;</span>
        <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>soln.<span style="color: #000000;">isDefined</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">return</span> soln
      <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">catch</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">case</span> e<span style="color: #000080;">:</span>BadBoardException <span style="color: #000080;">=&gt;</span> sd.<span style="color: #000000;">fails</span> +<span style="color: #000080;">=</span> <span style="color: #F78811;">1</span>
      <span style="color: #F78811;">&#125;</span>
    <span style="color: #F78811;">&#125;</span> <span style="color: #008000; font-style: italic;">// Trying each choice.</span>
    None
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/**
 * Companion object to the board class.  Provides methods to make and
 * initialize a board.
 *
 * @author sprowell
 */</span>
<span style="color: #0000ff; font-weight: bold;">object</span> Board <span style="color: #F78811;">&#123;</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Convenience method to make a new board.
   * @return  A new board.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> apply<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Board<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #0000ff; font-weight: bold;">import</span> scala.<span style="color: #000000;">io</span>.<span style="color: #000000;">Source</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Read a file into a new board.  The file should consist of nine lines,
   * with each line a sequence of digits and unknown markers (typically a
   * space).  This designates the initial board.
   * @param file  The file path.
   * @return  The new board.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> read<span style="color: #F78811;">&#40;</span>file<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Board <span style="color: #000080;">=</span> read<span style="color: #F78811;">&#40;</span>Source.<span style="color: #000000;">fromFile</span><span style="color: #F78811;">&#40;</span>file<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Read a file into a new board.  The file should consist of nine lines,
   * with each line a sequence of digits and unknown markers (typically a
   * space).  This designates the initial board.
   * @param source  The source providing the data.
   * @return  The new board.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> read<span style="color: #F78811;">&#40;</span>source<span style="color: #000080;">:</span>Source<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span>Board <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> board <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Board<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">var</span> row <span style="color: #000080;">=</span> <span style="color: #F78811;">0</span>
    <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>line <span style="color: #000080;">&lt;</span>- source.<span style="color: #000000;">getLines</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>line.<span style="color: #000000;">length</span> <span style="color: #000080;">&gt;</span> <span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> board.<span style="color: #000000;">init</span><span style="color: #F78811;">&#40;</span>row, line<span style="color: #F78811;">&#41;</span>
      row +<span style="color: #000080;">=</span> <span style="color: #F78811;">1</span>
    <span style="color: #F78811;">&#125;</span> <span style="color: #008000; font-style: italic;">// Read all rows.</span>
    board
  <span style="color: #F78811;">&#125;</span>
&nbsp;
  <span style="color: #00ff00; font-style: italic;">/**
   * Interactively initialize a board by requesting nine lines of text
   * from the user.  Each line should consist of digits and unknown markers
   * (typically a space).  This is the initial board.
   * @return  The new board.
   */</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> init<span style="color: #000080;">:</span>Board <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> board <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Board
    <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>row <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">0</span> until board.<span style="color: #000000;">N</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">val</span> line <span style="color: #000080;">=</span> Console.<span style="color: #000000;">readLine</span><span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Row %d: &quot;</span>, row<span style="color: #F78811;">&#41;</span>
      board.<span style="color: #000000;">init</span><span style="color: #F78811;">&#40;</span>row, line<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span> <span style="color: #008000; font-style: italic;">// Read all rows.</span>
    board
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #00ff00; font-style: italic;">/**
 * Application entry point when run from the prompt.  Initialize a board
 * using user input, solve it, and print the solution.
 *
 * @author sprowell
 */</span>
<span style="color: #0000ff; font-weight: bold;">object</span> Sudoku <span style="color: #F78811;">&#123;</span>
  <span style="color: #00ff00; font-style: italic;">/**
   * Entry point when run from the prompt.
   * @param args  Command line arguments (ignored).
   */</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>args<span style="color: #000080;">:</span>Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    Board.<span style="color: #000000;">init</span>.<span style="color: #000000;">search</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">match</span> <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> None <span style="color: #000080;">=&gt;</span> println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;No solution.&quot;</span><span style="color: #F78811;">&#41;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> Some<span style="color: #F78811;">&#40;</span>b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> println<span style="color: #F78811;">&#40;</span>b<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2010%2F12%2F28%2Fscala-sudoku%2F&amp;title=Scala%20Sudoku" id="wpa2a_10"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2010/12/28/scala-sudoku/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Watched Properties in Scala</title>
		<link>http://stacyprowell.com/blog/2010/04/27/watched-properties-in-scala/</link>
		<comments>http://stacyprowell.com/blog/2010/04/27/watched-properties-in-scala/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 20:08:39 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[linkedin]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=471</guid>
		<description><![CDATA[Scala is pretty damn neat.  I&#8217;ve only just started learning the language (an earlier attempt had to be sidelined because &#8220;real&#8221; work interfered), but I&#8217;m back to it now. Properties Here&#8217;s a funny little class I&#8217;ve been playing with in some development work. class Property&#91;T&#93;&#40;var value:T&#41; &#123; def get:T = value def set&#40;newval:T&#41; = &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>Scala is pretty damn neat.  I&#8217;ve only just started learning the language (an earlier attempt had to be sidelined because &#8220;real&#8221; work interfered), but I&#8217;m back to it now.<span id="more-471"></span></p>
<h1>Properties</h1>
<p>Here&#8217;s a funny little class I&#8217;ve been playing with in some development work.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> Property<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">var</span> value<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> get<span style="color: #000080;">:</span>T <span style="color: #000080;">=</span> value
  <span style="color: #0000ff; font-weight: bold;">def</span> set<span style="color: #F78811;">&#40;</span>newval<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    value <span style="color: #000080;">=</span> newval
    <span style="color: #0000ff; font-weight: bold;">this</span>
  <span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> <span style="color: #000080;">:=</span><span style="color: #F78811;">&#40;</span>newval<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> set<span style="color: #F78811;">&#40;</span>newval<span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> toString <span style="color: #000080;">=</span> <span style="color: #6666FF;">&quot;Property(&quot;</span> + value + <span style="color: #6666FF;">&quot;)&quot;</span>
<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">implicit</span> <span style="color: #0000ff; font-weight: bold;">def</span> Property2value<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>prop<span style="color: #000080;">:</span>Property<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> prop.<span style="color: #000000;">get</span></pre></div></div>

<p>I think that&#8217;s just too neat.  The property can be set with the (admittedly PL/1-ish) := operator, and the value can be extracted with a cast or assignment.</p>
<p>If I could override the assignment then I could make this very, very C++-ish-ish.  Note that I could have written something like the following.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> `<span style="color: #000080;">=</span>`<span style="color: #F78811;">&#40;</span>newval<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> set<span style="color: #F78811;">&#40;</span>newval<span style="color: #F78811;">&#41;</span></pre></div></div>

<p>But then I&#8217;d have to use this as follows.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> p <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Property<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">12</span><span style="color: #F78811;">&#41;</span>
scala<span style="color: #000080;">&gt;</span> p `<span style="color: #000080;">=</span>` <span style="color: #F78811;">13</span></pre></div></div>

<p>That&#8217;s just not the same thing.  Scala does not allow overriding assignment; no copy constructor for you.</p>
<p>If you are willing to declare p to be a var, then you can do the following.  But this is really not what you want since it creates a new property, and does not modify the value of the existing property.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">def</span> ValueToProperty<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Property<span style="color: #F78811;">&#40;</span>value<span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">var</span> p <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Property<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">12</span><span style="color: #F78811;">&#41;</span>
p<span style="color: #000080;">:</span> Property<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Property<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">12</span><span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> p <span style="color: #000080;">=</span> <span style="color: #F78811;">171</span>
p<span style="color: #000080;">:</span> Property<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Property<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">171</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<h1>Watched Property</h1>
<p>So what&#8217;s the point of having this mutable property class?  Well, now that I have the above I can add code to watch the property.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> Watcher<span style="color: #F78811;">&#91;</span>T,U<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> notify<span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>T, data<span style="color: #000080;">:</span>U<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">trait</span> Watchable<span style="color: #F78811;">&#91;</span>T,U<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> watchers<span style="color: #000080;">:</span>Map<span style="color: #F78811;">&#91;</span>Watcher<span style="color: #F78811;">&#91;</span>T,U<span style="color: #F78811;">&#93;</span>,U<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Map<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> register<span style="color: #F78811;">&#40;</span>watch<span style="color: #000080;">:</span>Watcher<span style="color: #F78811;">&#91;</span>T,U<span style="color: #F78811;">&#93;</span>, data<span style="color: #000080;">:</span>U<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    watchers +<span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span>watch -<span style="color: #000080;">&gt;</span> data<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> notify<span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    watchers foreach <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">&#40;</span>watcher, data<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> watcher.<span style="color: #000000;">notify</span><span style="color: #F78811;">&#40;</span>value, data<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> WatchedProperty<span style="color: #F78811;">&#91;</span>T,U<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Property<span style="color: #F78811;">&#40;</span>value<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">with</span> Watchable<span style="color: #F78811;">&#91;</span>T,U<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> set<span style="color: #F78811;">&#40;</span>newval<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">super</span>.<span style="color: #000000;">set</span><span style="color: #F78811;">&#40;</span>newval<span style="color: #F78811;">&#41;</span>
    notify<span style="color: #F78811;">&#40;</span>newval<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">this</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Now I have a watcher and a watchable.  I mix in the watchable trait with the property, and presto!  I have a watched property.  It&#8217;s somewhat of a big deal for some work I&#8217;m doing now; this would have made my life <em>much</em> easier.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> See<span style="color: #F78811;">&#91;</span>T,U<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Watcher<span style="color: #F78811;">&#91;</span>T,U<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> notify<span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>T, data<span style="color: #000080;">:</span>U<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Value is now &quot;</span> + value + <span style="color: #6666FF;">&quot; and data is &quot;</span> + data + <span style="color: #6666FF;">&quot;.&quot;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Finally, we do some &#8220;work.&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> see <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> See<span style="color: #F78811;">&#91;</span>Int,String<span style="color: #F78811;">&#93;</span>
see<span style="color: #000080;">:</span> See<span style="color: #F78811;">&#91;</span>Int,String<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> See<span style="color: #000080;">@</span>fab91
&nbsp;
scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> watch <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> WatchedProperty<span style="color: #F78811;">&#91;</span>Int,String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span>
watch<span style="color: #000080;">:</span> WatchedProperty<span style="color: #F78811;">&#91;</span>Int,String<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Property<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> watch register <span style="color: #F78811;">&#40;</span>see,<span style="color: #6666FF;">&quot;watch&quot;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> watch <span style="color: #000080;">:=</span> <span style="color: #F78811;">12</span>
Value is now <span style="color: #F78811;">12</span> and data is watch.
<span style="color: #000000;">res22</span><span style="color: #000080;">:</span> Property<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Property<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">12</span><span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> watch <span style="color: #000080;">:=</span> <span style="color: #F78811;">31</span>
Value is now <span style="color: #F78811;">31</span> and data is watch.
<span style="color: #000000;">res23</span><span style="color: #000080;">:</span> Property<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Property<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">31</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>Of course, I can also avoid new by declaring the companion object for WatchedProperty and adding a factory method.  Life in Scala is pretty good (so far).</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> WatchedProperty <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> apply<span style="color: #F78811;">&#91;</span>T,U<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> WatchedProperty<span style="color: #F78811;">&#91;</span>T,U<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>value<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<h1>Cleaner Watched Property</h1>
<p>There is a problem with the implementation of watched property, and it is the type annotation U.  Why should the property have to know the type for the data?  The data only exists for the benefit of the watcher.  Worse, the watched property isn&#8217;t a drop-in replacement for property; it has two parameters instead of just one.  Finally, everyone who wants to register must use the same data type U (or a subclass).  Here are some alternatives.</p>
<ol>
<li>The watcher can maintain a map from watched property to the associated data.  The watched property can then send itself with the property change notification.  This is <em>bad</em>.  The watcher has to maintain the map.  If it is watching a lot of properties then it may have to maintain a big map, and there is lookup overhead.  We avoid that by having the property return the data with the notification.</li>
<li>We could treat the data as an Any.  Then it is the responsibility of the watcher to correctly cast the value during notification.  This is a nuisance because the compiler cannot catch type problems for us, but Java programmers should be used to that by now.</li>
</ol>
<p>Let&#8217;s try the second approach.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> Watcher<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> notify<span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>T, data<span style="color: #000080;">:</span>Any<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">trait</span> Watchable<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> watchers<span style="color: #000080;">:</span>Map<span style="color: #F78811;">&#91;</span>Watcher<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span>,Any<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Map<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> register<span style="color: #F78811;">&#40;</span>watch<span style="color: #000080;">:</span>Watcher<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span>, data<span style="color: #000080;">:</span>Any<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    watchers +<span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span>watch -<span style="color: #000080;">&gt;</span> data<span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> notify<span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    watchers foreach <span style="color: #F78811;">&#123;</span>
      <span style="color: #0000ff; font-weight: bold;">case</span> <span style="color: #F78811;">&#40;</span>watcher, data<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> watcher.<span style="color: #000000;">notify</span><span style="color: #F78811;">&#40;</span>value, data<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> WatchedProperty<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Property<span style="color: #F78811;">&#40;</span>value<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">with</span> Watchable<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> set<span style="color: #F78811;">&#40;</span>newval<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">super</span>.<span style="color: #000000;">set</span><span style="color: #F78811;">&#40;</span>newval<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span>
    notify<span style="color: #F78811;">&#40;</span>newval<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">this</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>That&#8217;s better, actually.  Watched property is again a referentially-transparent replacement for property, and watchers can use any data type they want.  What could be better than this?</p>
<h1>Better Watched Property</h1>
<p>Actually that whole &#8220;Any&#8221; value is the problem now.  This implementation cleans up the design a bit with better information hiding and referential transparency, but it also musses up type safety a bit.  The watcher knows the type of the data; why can&#8217;t it get back properly-typed data during notification?</p>
<p>Maybe we&#8217;ve been asking the wrong question.  The issue is, I think, the Java-esque nature of the solution.  Why does notify really have to be passed the data?  Why not do something very different?  In fact, why not get rid of the watcher trait altogether?  Why do we need it?  We really just want to invoke a function (or method) when the property is changed.  Let&#8217;s just directly register the callback, and forget the middleman.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">trait</span> Watchable<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">var</span> watchers<span style="color: #000080;">:</span>List<span style="color: #F78811;">&#91;</span><span style="color: #F78811;">&#40;</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> Unit<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> List<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> register<span style="color: #F78811;">&#40;</span>notify<span style="color: #000080;">:</span><span style="color: #F78811;">&#40;</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> Unit<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    watchers +<span style="color: #000080;">=</span> notify
  <span style="color: #F78811;">&#125;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> notify<span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    watchers foreach <span style="color: #F78811;">&#40;</span>notify <span style="color: #000080;">=&gt;</span> notify<span style="color: #F78811;">&#40;</span>value<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> WatchedProperty<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">extends</span> Property<span style="color: #F78811;">&#40;</span>value<span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">with</span> Watchable<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span> <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">override</span> <span style="color: #0000ff; font-weight: bold;">def</span> set<span style="color: #F78811;">&#40;</span>newval<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">super</span>.<span style="color: #000000;">set</span><span style="color: #F78811;">&#40;</span>newval<span style="color: #F78811;">&#41;</span>
    notify<span style="color: #F78811;">&#40;</span>newval<span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">this</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Watcher is gone.  The data item is gone.  The funny type problem is gone.  The code is simpler, and even more flexible, since anything can be a watcher, including lambdas.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> prop <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> WatchedProperty<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">21</span><span style="color: #F78811;">&#41;</span>
prop<span style="color: #000080;">:</span> WatchedProperty<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Property<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">21</span><span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">def</span> see<span style="color: #F78811;">&#40;</span>x<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Value changed to: &quot;</span> + x + <span style="color: #6666FF;">&quot;.&quot;</span><span style="color: #F78811;">&#41;</span>
see<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>Int<span style="color: #F78811;">&#41;</span>Unit
&nbsp;
scala<span style="color: #000080;">&gt;</span> prop.<span style="color: #000000;">register</span><span style="color: #F78811;">&#40;</span>see<span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> prop.<span style="color: #000000;">register</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>x<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Lambda sees value: &quot;</span> + x + <span style="color: #6666FF;">&quot;.&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> prop <span style="color: #000080;">:=</span> <span style="color: #F78811;">413</span>
Value changed to<span style="color: #000080;">:</span> <span style="color: #F78811;">413</span>.
<span style="color: #000000;">Lambda</span> sees value<span style="color: #000080;">:</span> <span style="color: #F78811;">413</span>.
<span style="color: #000000;">res10</span><span style="color: #000080;">:</span> Property<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Property<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">413</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<h1>Data Again</h1>
<p>Well, what about data?  The value for data can be captured in a closure.  Here&#8217;s an example, where the data is a name for the property being monitored.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> ChangeWatcher <span style="color: #F78811;">&#123;</span>
  <span style="color: #0000ff; font-weight: bold;">def</span> watch<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>prop<span style="color: #000080;">:</span>WatchedProperty<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span>, name<span style="color: #000080;">:</span>String<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    prop register <span style="color: #F78811;">&#123;</span>
      <span style="color: #F78811;">&#40;</span>value<span style="color: #000080;">:</span>T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> println<span style="color: #F78811;">&#40;</span>name + <span style="color: #6666FF;">&quot;: &quot;</span> + value<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
  <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>Did you get all that?  The above is a fully-generic watcher.  You pass it a property and a name for the property.  Whenever the property is changed, it notifies you.  This works for any kind of property.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> prop <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> WatchedProperty<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">21</span><span style="color: #F78811;">&#41;</span>
prop<span style="color: #000080;">:</span> WatchedProperty<span style="color: #F78811;">&#91;</span>Int<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Property<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">21</span><span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> status <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> WatchedProperty<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;okay&quot;</span><span style="color: #F78811;">&#41;</span>
status<span style="color: #000080;">:</span> WatchedProperty<span style="color: #F78811;">&#91;</span>java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">String</span><span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Property<span style="color: #F78811;">&#40;</span>okay<span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> temp <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> WatchedProperty<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">21.8</span><span style="color: #F78811;">&#41;</span>
temp<span style="color: #000080;">:</span> WatchedProperty<span style="color: #F78811;">&#91;</span>Double<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Property<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">21.8</span><span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> <span style="color: #0000ff; font-weight: bold;">val</span> cw <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> ChangeWatcher
cw<span style="color: #000080;">:</span> ChangeWatcher <span style="color: #000080;">=</span> ChangeWatcher<span style="color: #000080;">@</span>3da8bc
&nbsp;
scala<span style="color: #000080;">&gt;</span> cw.<span style="color: #000000;">watch</span><span style="color: #F78811;">&#40;</span>prop, <span style="color: #6666FF;">&quot;prop&quot;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> cw.<span style="color: #000000;">watch</span><span style="color: #F78811;">&#40;</span>status, <span style="color: #6666FF;">&quot;status&quot;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> cw.<span style="color: #000000;">watch</span><span style="color: #F78811;">&#40;</span>temp, <span style="color: #6666FF;">&quot;temperature&quot;</span><span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> status <span style="color: #000080;">:=</span> <span style="color: #6666FF;">&quot;Danger!&quot;</span>
status<span style="color: #000080;">:</span> Danger<span style="color: #000080;">!</span>
res18<span style="color: #000080;">:</span> Property<span style="color: #F78811;">&#91;</span>java.<span style="color: #000000;">lang</span>.<span style="color: #000000;">String</span><span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Property<span style="color: #F78811;">&#40;</span>Danger<span style="color: #000080;">!</span><span style="color: #F78811;">&#41;</span>
&nbsp;
scala<span style="color: #000080;">&gt;</span> temp <span style="color: #000080;">:=</span> <span style="color: #F78811;">73.2</span>
temperature<span style="color: #000080;">:</span> <span style="color: #F78811;">73.2</span>
res19<span style="color: #000080;">:</span> Property<span style="color: #F78811;">&#91;</span>Double<span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> Property<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">73.2</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>The important thing to note is that you don&#8217;t have to have data.  Or you can have lots of data.  Or whatever you want.  The responsibility for data is all back to the watcher, and off the shoulders of the watchable.  This seems (to me) to be the correct division of responsibility.</p>
<p>To sum up: Functional Programming + Objects = Good.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2010%2F04%2F27%2Fwatched-properties-in-scala%2F&amp;title=Watched%20Properties%20in%20Scala" id="wpa2a_12"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2010/04/27/watched-properties-in-scala/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Little Book</title>
		<link>http://stacyprowell.com/blog/2010/04/18/a-little-book/</link>
		<comments>http://stacyprowell.com/blog/2010/04/18/a-little-book/#comments</comments>
		<pubDate>Sun, 18 Apr 2010 22:03:23 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[linkedin]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=465</guid>
		<description><![CDATA[Our new book, Seven Deadliest Network Attacks, is finally out, and I have my copy.  It is thin, but chock full of network attack goodness (I hope).  Run out and get a copy and tell me what you think!  The process of creating this book was quite painful, since it coincided with my changing jobs and relocating [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://stacyprowell.com/blog/wp-content/uploads/2010/04/7deadliest.jpg"><img class="alignright size-full wp-image-467" title="Seven Deadliest Network Attacks" src="http://stacyprowell.com/blog/wp-content/uploads/2010/04/7deadliest.jpg" alt="Seven Deadliest Network Attacks" width="180" height="180" /></a>Our new book, <em><a href="http://www.amazon.com/Seven-Deadliest-Network-Attacks-Prowell/dp/1597495492" target="_blank">Seven Deadliest Network Attacks</a></em>, is finally out, and I have my copy.  It is thin, but chock full of network attack goodness (I hope).  Run out and get a copy and tell me what you think!  The process of creating this book was <em>quite</em> painful, since it coincided with my changing jobs and relocating in two (or three) painful steps to a job that absolutely consumes all available cycles.  I am grateful to my editors, co-authors, and family (who had to endure me doing most of the work at night) for not killing and/or replacing me.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2010%2F04%2F18%2Fa-little-book%2F&amp;title=A%20Little%20Book" id="wpa2a_14"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2010/04/18/a-little-book/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Obama&#8217;s Birth Certificate</title>
		<link>http://stacyprowell.com/blog/2009/11/24/obamas-birth-certificate/</link>
		<comments>http://stacyprowell.com/blog/2009/11/24/obamas-birth-certificate/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 20:53:43 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Politics]]></category>
		<category><![CDATA[birth certificate]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[obama]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=452</guid>
		<description><![CDATA[I haven&#8217;t written anything on the blog in a while, so it&#8217;s probably time.  This comes from Facebook comments (what doesn&#8217;t these days).  Social networking is fun!  I&#8217;ve paraphrased and added emphasis below because, hey, this is my blog, after all.  So, let&#8217;s visit that Obama birth certificate issue One More Time&#8230; The Argument: As [...]]]></description>
			<content:encoded><![CDATA[<p>I haven&#8217;t written anything on the blog in a while, so it&#8217;s probably time.  This comes from Facebook comments (what doesn&#8217;t these days).  Social networking is fun!  I&#8217;ve paraphrased and added emphasis below because, hey, this is <em>my</em> blog, after all.  So, let&#8217;s visit that Obama birth certificate issue One More Time&#8230;<span id="more-452"></span></p>
<p><strong>The Argument:</strong><br />
As far as the &#8220;birthers&#8221; go, common sense says if there wasn&#8217;t anything to hide, then nobody would be hiding anything, right?</p>
<p><strong>My Answer:</strong><br />
There is a fundamental principle of inquiry &#8211; that is assuming one cares about the truth &#8211; that assertions should be based on <em>evidence</em>. The absence of evidence does not constitute evidence. So far as the courts and I can tell, nobody is hiding anything. To accept assertions on insufficient evidence risks making oneself a tool for evil hands.</p>
<p><em>By the way</em>: The argument presented is a common <em>fallacy</em>.  The argument in the general form goes something like: <em>If you haven&#8217;t done anything wrong, then you have nothing to hide.</em> I assume everyone reading this can understand why this argument is not valid.  It always bugs me when I hear people say it, especially in this country where we have an enshrined right to privacy arising from the Fourth Amendment to the U.S. Constitution.</p>
<p><strong>The Counterpunch:<br />
</strong>But isn&#8217;t it a duty to <em>show</em> the evidence if a person is running for President?</p>
<p><strong>My Answer:</strong><br />
Yes, it is. And this was done to the satisfaction of the U.S. courts <em>more than once</em>.</p>
<p><strong>The Non-Sequitur:</strong><br />
And that is enough to satify a you?  Big difference between this and McCain, and you know it.</p>
<p><strong>My Answer:</strong><br />
Yes, there is a big difference.</p>
<p>McCain was born in the Panama Canal Zone, a tenuous and temporary extension of the US and <em>not</em> a state of the union. If elected, McCain would have been the first US President to have been born <em>outside</em> the states. This was a serious legal matter that ultimately ended in a (nonbinding) Senate resolution and required real legal investigation by a bipartisan legal review. This is because &#8220;natural born&#8221; is not defined in the Constitution. While McCain would be very, very likely to prevail in a legal challenge, it is not assured. In short, the courts have not ruled on this matter.</p>
<p>Obama was born in Hawaii, our 50th state, and a state of the union. He has provided documentation of his birth adequate for all legal purposes. In addition, his <em>original</em> birth certificate has been examined by the director of the Hawaii State Department of Health who attested to its authenticity under oath. The Republican governor of the state, Linda Lingle, has also attested to Obama&#8217;s status. The US courts have ruled on this matter.<span><span><a onclick="CSS.addClass($(&quot;text_expose_id_4b0c442343a233d90d5c5&quot;), &quot;text_exposed&quot;);"></a></span></span></p>
<p>So, yes, there is a big difference.  Obama&#8217;s status as a naturalized citizen is well established.  McCain&#8217;s is not.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F11%2F24%2Fobamas-birth-certificate%2F&amp;title=Obama%26%238217%3Bs%20Birth%20Certificate" id="wpa2a_16"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/11/24/obamas-birth-certificate/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Silly Scala Tricks</title>
		<link>http://stacyprowell.com/blog/2009/11/12/silly-scala-tricks/</link>
		<comments>http://stacyprowell.com/blog/2009/11/12/silly-scala-tricks/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 04:53:10 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[linkedin]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=449</guid>
		<description><![CDATA[One of the things I have in my Scala startup file is the following. def show&#91;T&#93;&#40;implicit thetype:scala.reflect.Manifest&#91;T&#93;&#41; &#123; thetype.erasure.getMethods&#40;&#41; foreach println &#125; def help&#40;x:AnyRef&#41; &#123; x.getClass&#40;&#41;.getMethods&#40;&#41; foreach println &#125; These silly little functions are useful &#8211; at least, to me. Now when I can&#8217;t remember the name of a method I can type something like [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things I have in my Scala startup file is the following.</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> show<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">implicit</span> thetype<span style="color: #000080;">:</span>scala.<span style="color: #000000;">reflect</span>.<span style="color: #000000;">Manifest</span><span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
  thetype.<span style="color: #000000;">erasure</span>.<span style="color: #000000;">getMethods</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> foreach println
<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">def</span> help<span style="color: #F78811;">&#40;</span>x<span style="color: #000080;">:</span>AnyRef<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
  x.<span style="color: #000000;">getClass</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>.<span style="color: #000000;">getMethods</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> foreach println
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>These silly little functions are useful &#8211; at least, to me. Now when I can&#8217;t remember the name of a method I can type something like the following at the Scala prompt.<span id="more-449"></span></p>
<pre>scala&gt; help(Map())
public scala.Option scala.collection.immutable.EmptyMap.get(java.lang.Object)
public int scala.collection.immutable.EmptyMap.hashCode()
public boolean scala.collection.immutable.EmptyMap.equals(java.lang.Object)
...</pre>
<p>The output isn&#8217;t very Scala-ish, but I could format it if I weren&#8217;t so lazy.</p>
<pre>scala&gt; help(scala.runtime.RichString)
public int scala.runtime.RichString$.$tag() throws java.rmi.RemoteException
public final boolean scala.runtime.RichString$.scala$runtime$RichString$$parseBoolean(java.lang.String)
public final char scala.runtime.RichString$.scala$runtime$RichString$$LF()
public final char scala.runtime.RichString$.scala$runtime$RichString$$CR()
public final char scala.runtime.RichString$.scala$runtime$RichString$$FF()
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
public final void java.lang.Object.wait() throws java.lang.InterruptedException
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
public native int java.lang.Object.hashCode()
public final native java.lang.Class java.lang.Object.getClass()
public boolean java.lang.Object.equals(java.lang.Object)
public java.lang.String java.lang.Object.toString()
public final native void java.lang.Object.notify()
public final native void java.lang.Object.notifyAll()</pre>
<p>These functions aren&#8217;t in any way brilliant, or even clever, but they are short and ridiculously trivial to write.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F11%2F12%2Fsilly-scala-tricks%2F&amp;title=Silly%20Scala%20Tricks" id="wpa2a_18"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/11/12/silly-scala-tricks/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Eight Character Passwords</title>
		<link>http://stacyprowell.com/blog/2009/10/19/eight-character-passwords/</link>
		<comments>http://stacyprowell.com/blog/2009/10/19/eight-character-passwords/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 21:49:13 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[crypt]]></category>
		<category><![CDATA[cyber]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[md5]]></category>
		<category><![CDATA[password]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=432</guid>
		<description><![CDATA[How long are your passwords?  Let&#8217;s say eight characters is the length.  How many possibilities are there?  Well, you can use any single-byte printable character (though I once used an escape key in an RS/6000 password; it worked, but isn&#8217;t a good idea everywhere), and any length from one to eight. Printable ASCII is roughly [...]]]></description>
			<content:encoded><![CDATA[<p>How long are your passwords?  Let&#8217;s say eight characters is the length.  How many possibilities are there?  Well, you can use any single-byte printable character (though I once used an escape key in an RS/6000 password; it worked, but isn&#8217;t a good idea everywhere), and any length from one to eight.<span id="more-432"></span></p>
<p>Printable ASCII is roughly codes 32 through 126, or 126-32+1 = 95 characters.  There are 95 passwords of length one, $95\times 95$ passwords of length two, $95^3$ passwords of length three, etc.  This gives a grand total of:</p>
<p>\[\sum_{i=1}^8 95^i = 6,704,780,954,517,120\]</p>
<p>This is a lot of passwords.  A lot.  That&#8217;s well over 6 <em>quadrillion</em> passwords.  It includes the passwords ~ and ~~~~~~~~.</p>
<p>Not all those are available for use in most cases.  First, you typically have to do the following.</p>
<ol>
<li>Choose a lower-case letter.  26 of those.</li>
<li>Choose an upper-case letter.  26 of those.</li>
<li>Choose a digit.  10 of those.</li>
<li>Choose punctuation.  Let&#8217;s call that &#8220;everything else&#8221; and say that space is punctuation.  Thus there are $95-26-26-10 = 33$ punctuation characters.</li>
</ol>
<p>So that limits our choice on four characters.  Let&#8217;s try again!  Now there is a grand total of:</p>
<p>\[26\times 26\times 10\times 33\times \sum_{i=0}^4 95^i = 223,080\times 82,317,121 = 18,363,303,352,680\]</p>
<p>That&#8217;s over 18 <em>trillion</em> passwords.  That&#8217;s actually way, way down from the full potential!</p>
<p>How much memory is required to store every single stinkin&#8217; password&#8217;s crypt, MD5, etc.  Well, if we do it stupidly (<em>i.e.</em>, ignore collisions, ignore smart data structures, etc.), then for MD5 we need 16 bytes for each.  That&#8217;s approximately $16\times 17~\mbox{Tib} \approx 272~\mbox{Tib}$.  That isn&#8217;t as much as you might think.  A single terabyte drive costs about \$100 (US) right now, and we&#8217;d need 272 of them.  Let&#8217;s say 300, just to be safe.  How much is that?  $300\times \$100 = \$30,000$.  Overhead, installation, maintenance, etc., all add to that, but not that much.  In short, it is very cost effective.</p>
<p>Computing them all?  We&#8217;d need to compute <em>a lot</em> of hashes.  Let&#8217;s pick on MD5 (even though it is almost cracked).  Suppose it takes us one millisecond to compute one hash.  We still need to compute 18.4 trillion hashes.  Serially, that&#8217;s $18.4\times 10^{12}~\mbox{passwords}\cdot 1\times 10^{-3}~\frac{\mbox{sec}}{\mbox{password}} = 1.84\times 10^{10}~\mbox{sec}$.  That&#8217;s just over 583 years.  Whew!  We&#8217;re all completely safe.</p>
<p>Not even close.  Suppose I have a 1024 node machine (small by today&#8217;s standards).  That cuts three orders of magnitude from the result, since every hash can be computed independently of the others (though smart storage would cause some serialization).  Now we&#8217;re down to under 6 years.  And how fast can we compute MD5 hashes???</p>
<p>Smart folks are making MD5 very fast.  See http://www.faqs.org/rfcs/rfc1810.html.  That&#8217;s from 1995.  Well, <code>md5 -t</code>, on my little 32-bit Intel MacBook, reports a speed of 313,406,912 bytes per second.  Eight bytes should take just about $2.55\times 10^{-8}~\mbox{seconds}$.  Yes, there&#8217;s overhead at the end of each hash, but I can counter with the following: I can hard code the eight byte limit to get a more efficient algorithm.  I can do a smart iteration with single-byte modifications and hash updates instead of a whole new hash computation.  Plus, there are much faster processors and, I feel confident, faster implementations.  I&#8217;ve got some confidence in my machine&#8217;s performance as an upper bound.  So, back to our 1024 node machine.</p>
<p>\[\frac{18.4\times 10^{12}~\mbox{passwords}}{1024~\mbox{node}} \cdot 2.55\times 10^{-8}~\frac{\mbox{seconds}}{\mbox{password}} = 458 \frac{\mbox{seconds}}{\mbox{node}}\]</p>
<p>That&#8217;s under eight minutes.  I can&#8217;t even manage to make coffee in that time.  Even assuming we add two orders of magnitude for storage and overhead, that&#8217;s just 800 minutes, or a few days.</p>
<p>I&#8217;d be surprised if there isn&#8217;t a database somewhere of all 8-byte (or shorter) passwords and their crypt, MD5, SHA1, and what-have-you.  That is, I expect 8-byte passwords have been <em>solved</em>.  Think about that next time you log in to a site on the web, secure in the knowledge that your password is being sent encrypted.</p>
<p>Who could do this?  If I were highly motivated, I could.  I&#8217;d do better data reduction (to save on data warehousing) and maybe even ship everything up to be computed on one of the &#8220;clouds&#8221; at Google, Dell, or Amazon.  If I can do this sitting in my house at my laptop&#8230;</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F10%2F19%2Feight-character-passwords%2F&amp;title=Eight%20Character%20Passwords" id="wpa2a_20"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/10/19/eight-character-passwords/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

