<?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's talking again...</description>
	<lastBuildDate>Tue, 27 Apr 2010 20:09:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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> 413.
<span style="color: #000000;">Lambda</span> sees value<span style="color: #000080;">:</span> 413.
<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>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2010%2F04%2F27%2Fwatched-properties-in-scala%2F&amp;linkname=Watched%20Properties%20in%20Scala"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></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>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2010%2F04%2F18%2Fa-little-book%2F&amp;linkname=A%20Little%20Book"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></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 far 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>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F11%2F24%2Fobamas-birth-certificate%2F&amp;linkname=Obama%26%238217%3Bs%20Birth%20Certificate"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></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 the following at [...]]]></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>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F11%2F12%2Fsilly-scala-tricks%2F&amp;linkname=Silly%20Scala%20Tricks"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></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 codes [...]]]></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>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F10%2F19%2Feight-character-passwords%2F&amp;linkname=Eight%20Character%20Passwords"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/10/19/eight-character-passwords/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>And Another Thing That Bugs Me About Java&#8230;</title>
		<link>http://stacyprowell.com/blog/2009/09/30/and-another-thing-that-bugs-me-about-java/</link>
		<comments>http://stacyprowell.com/blog/2009/09/30/and-another-thing-that-bugs-me-about-java/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 03:05:15 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Nuisances]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[const]]></category>
		<category><![CDATA[immutable]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[reference]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=415</guid>
		<description><![CDATA[Just a short note on an item that bugs me about Java.  In C++, I tend to use exactly three kinds of method parameters.

A const reference.  I don&#8217;t want to copy it, but I promise not to modify it, either.
A reference.  I might modify it.
A copy.  I might modify my local copy, but not the [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-thumbnail wp-image-116" title="java_logo_2" src="http://stacyprowell.com/blog/wp-content/uploads/2008/07/java_logo_2-150x150.png" alt="java_logo_2" width="120" height="120" />Just a short note on an item that bugs me about Java.  In C++, I tend to use exactly three kinds of method parameters.</p>
<ul>
<li>A const reference.  I don&#8217;t want to copy it, but I promise not to modify it, either.</li>
<li>A reference.  I might modify it.</li>
<li>A copy.  I might modify my local copy, but not the original.  I&#8217;m getting a copy, after all.</li>
</ul>
<p>Easy peasy in C++.  In Java?  Uh, I pass all objects by reference.<span id="more-415"></span> The interface says nothing about modification.  Pass me a Map and maybe I&#8217;ll modify it, maybe not.  You don&#8217;t know, and you can&#8217;t enforce it.  Unless you pass me a Collections.unmodifiableMap().  Then my code might break!  There might be some odd little case left over where I do modify it, in spite of documentation comments to the contrary.  C++?  I declare it to be a const reference, and now the compiler will prevent me from modifying it.  I like that quite a bit.</p>
<p>I also like const methods, and you really can&#8217;t have const without const methods.  The Java way it to throw exceptions.  I&#8217;d rather have the compiler tell me something at compile-time than have the runtime system fail at runtime.  But I&#8217;m weird, I suppose.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F09%2F30%2Fand-another-thing-that-bugs-me-about-java%2F&amp;linkname=And%20Another%20Thing%20That%20Bugs%20Me%20About%20Java%26%238230%3B"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/09/30/and-another-thing-that-bugs-me-about-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Good Idea / Bad Idea</title>
		<link>http://stacyprowell.com/blog/2009/08/21/good-idea-bad-idea/</link>
		<comments>http://stacyprowell.com/blog/2009/08/21/good-idea-bad-idea/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 17:52:04 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Nuisances]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Computers]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[linkedin]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=350</guid>
		<description><![CDATA[I spend my time (recently) writing a mishmash of Python, C++, and Java.  It&#8217;s interesting to switch back and forth.
What&#8217;s a good idea and what&#8217;s a bad (or dangerous) idea in computer language design?  We&#8217;ve got a lot of candidates, and a lot of opinions.
I&#8217;ll list a few here, along with a few [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_235" class="wp-caption alignright" style="width: 160px"><img class="size-full wp-image-235" title="programming" src="http://stacyprowell.com/blog/wp-content/uploads/2009/05/1ebeb1d2f212046a4e47fcd414dbad9b1-150x150.jpg" alt="Programming" width="150" height="150" /><p class="wp-caption-text">Programming</p></div>
<p>I spend my time (recently) writing a mishmash of Python, C++, and Java.  It&#8217;s interesting to switch back and forth.</p>
<p>What&#8217;s a good idea and what&#8217;s a bad (or dangerous) idea in computer language design?  We&#8217;ve got a lot of candidates, and a lot of opinions.</p>
<p>I&#8217;ll list a few here, along with a few places where they show up.<span id="more-350"></span> I&#8217;m not saying C macros are the same as, or even similar to, Lisp macros.  Just trying to clarify the spectrum.</p>
<p>I&#8217;ve heard someone, at some point, say each one of the items below is a good idea / bad idea.  Dangerous in the hands of a novice?  Yes, but then so is any computer with a network connection.</p>
<ul>
<li>Preprocessors (C, C++)</li>
<li>Macros (C, Lisp)</li>
<li>Multiple Inheritance (C++, Smalltalk)</li>
<li>Operator Overloading (C++, Python)</li>
<li>Closures (Lisp, Haskell)</li>
<li>Generics (Java, Ada)</li>
<li>Template Metaprogramming (C++)</li>
<li>Casts (C, C++)</li>
<li>Strong or Weak Typing (Scala, Lisp, Python, ML)</li>
<li>Fixed-Point Math (C# and COBOL)</li>
<li>Fixed Integer Types vs. &#8220;Numbers&#8221; (C, Lisp, Python)</li>
<li>Garbage Collection (Java, Lisp, ML)</li>
<li>Manual Allocation / Deallocation (C, C++)</li>
<li>Everything is an Object / Some Things are Primitives (Smalltalk, Java)</li>
<li>Auto-(Un)Boxing (Java, C#)</li>
<li>Indentation is Significant (Python)</li>
<li>First-Order Functions (ML, Lisp)</li>
<li>Lambdas (ML, Lisp, Python)</li>
<li>Type Erasure (Java)</li>
</ul>
<p>Did I leave out your favorite / most reviled language feature?  Complain below.  I included type erasure at the end of the list.  Does <em>anyone</em> think this was a good idea for Java???</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F08%2F21%2Fgood-idea-bad-idea%2F&amp;linkname=Good%20Idea%20%2F%20Bad%20Idea"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/08/21/good-idea-bad-idea/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Palin&#8217;s Speech</title>
		<link>http://stacyprowell.com/blog/2009/07/30/palins-speech/</link>
		<comments>http://stacyprowell.com/blog/2009/07/30/palins-speech/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 18:20:08 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Politics]]></category>
		<category><![CDATA[Society]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[palin]]></category>
		<category><![CDATA[republicans]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=337</guid>
		<description><![CDATA[The Palin speech really is poetry.  Read below.  It makes me want to go to Alaska.
And getting up here I say:
It is the best road trip in America.
Soaring through nature&#8217;s finest show.
Denali, the great one,
Soaring under the midnight sun.
And then the extremes.
In the winter time it&#8217;s the frozen road
That is competing with the [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_343" class="wp-caption alignright" style="width: 160px"><img class="size-thumbnail wp-image-343" title="palin1" src="http://stacyprowell.com/blog/wp-content/uploads/2009/07/palin1-150x150.jpg" alt="Sarah Palin" width="150" height="150" /><p class="wp-caption-text">Sarah Palin</p></div>
<p>The Palin speech really <em>is</em> poetry.  Read below.  It makes me want to go to Alaska.</p>
<blockquote><p>And getting up here I say:<br />
It is the best road trip in America.<br />
Soaring through nature&#8217;s finest show.<br />
Denali, the great one,<br />
Soaring under the midnight sun.<br />
And then the extremes.</p>
<p><span id="more-337"></span>In the winter time it&#8217;s the frozen road<br />
That is competing with the view<br />
Of ice fogged frigid beauty.<br />
The cold though.<br />
Doesn&#8217;t it split the Cheechakos<br />
From the Sourdoughs?</p>
<p>And then in the summertime,<br />
Such extreme summertime.<br />
About a hundred and fifty degrees<br />
Hotter than just some months ago,<br />
Than just some months from now,<br />
With fireweed blooming<br />
Along the frost heaves<br />
And merciless rivers that<br />
&nbsp;&nbsp;are rushing<br />
&nbsp;&nbsp;&nbsp;&nbsp;and carving<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and reminding us that here, Mother Nature wins.</p>
<p>It is as throughout all Alaska<br />
That big wild good life teeming<br />
Along the road that is north<br />
To the future.</p></blockquote>
<p>I wonder who wrote it?  It isn&#8217;t crazy-talk.  &#8220;The cold though, doesn&#8217;t it split the Cheechakos from the Sourdoughs?&#8221;  A <em>cheechako</em> is a newcomer to Alaska, while a <em>sourdough</em> is a long-time resident of Alaska.  And &#8220;north, to the future?&#8221;  That&#8217;s Alaska&#8217;s state motto.  Denali?  That&#8217;s the local Athabaskan name and official Alaskan name for Mount McKinley, and it means &#8220;The Great One.&#8221;  It has greater bulk and rise that Everest, so it&#8217;s a good name.</p>
<p>Whatever else you might say about Palin, this part of the speech was directed to Alaskans, her constituency, and while people down here in the lower 48 might chuckle at it, it was actually a good political speech.  It has nothing to do with the issue at hand &#8212; her resignation with a year and a half remaining &#8212; but then that is one of the hallmarks of a good political speech.</p>
<p>In short, don&#8217;t count Sarah Palin out.  She may not get my vote, but if she learns to speak to the rest of America in this way then we can expect quite a bit from her in the coming years.  After all, we&#8217;re suckers for poetry.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F07%2F30%2Fpalins-speech%2F&amp;linkname=Palin%26%238217%3Bs%20Speech"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/07/30/palins-speech/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Car Talk and the Longest Word</title>
		<link>http://stacyprowell.com/blog/2009/06/14/car-talk-and-the-longest-word/</link>
		<comments>http://stacyprowell.com/blog/2009/06/14/car-talk-and-the-longest-word/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 02:50:47 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[puzzler]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=284</guid>
		<description><![CDATA[I recently heard about a Car Talk puzzler.  I don&#8217;t listen to Car Talk as much as I used to.  Anyway, you can read about the puzzler and the fellow who solved it here.  This is an excerpt.
During Christmas week on the popular National Public Radio show Car Talk, the weekly puzzler required listeners to [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_235" class="wp-caption alignright" style="width: 160px"><img class="size-full wp-image-235" title="programming" src="http://stacyprowell.com/blog/wp-content/uploads/2009/05/1ebeb1d2f212046a4e47fcd414dbad9b1-150x150.jpg" alt="Programming" width="150" height="150" /><p class="wp-caption-text">Programming</p></div>
<p>I recently heard about a <a href="http://www.cartalk.com/" target="_blank">Car Talk</a> puzzler.  I don&#8217;t listen to Car Talk as much as I used to.  Anyway, you can read about the puzzler and the fellow who solved it <a href="http://www.hmc.edu/newsandevents/thomasbarr.html" target="_blank">here</a>.  This is an excerpt.</p>
<blockquote><p>During Christmas week on the popular National Public Radio show Car Talk, the weekly puzzler required listeners to find the longest English word that remains a valid English word as you remove its letters one at a time, but without rearranging any of the letters. For example: sprite, spit, pit, it, I. There are many such words, but, as Barr discovered, only one with 11 letters.</p></blockquote>
<p>So, only one word with eleven letters: <em>complecting</em>.  Maybe.  I&#8217;m not convinced, but finding out is easy.<br />
<span id="more-284"></span></p>
<h2>Recursion</h2>
<p>Okay, so how would you know?  Well, suppose you had a list of valid words.  The statement of the problem suggests a simple recursive algorithm to find a solution.</p>
<ul>
<li>Put the words in a heap, ordered longest to shortest.</li>
<li>Iterate until the heap is empty.
<ul>
<li>Take the next word $w$ from the heap, and perform $\mbox{search}(w)$</li>
</ul>
</li>
</ul>
<p>The procedure $\mbox{search}(w)$ works as follows.  It returns true if it finds a solution.</p>
<ul>
<li>If $w$ is the empty string, then return true.</li>
<li>For each letter $l$ of $w$, do the following.
<ul>
<li>Drop $l$ from $w$, generating the candidate word $v$.</li>
<li>If $v$ is in the dictionary, then execute $r=\mbox{search}(v)$.
<ul>
<li>If $r$ is true, then write out $w$ and return true.</li>
</ul>
</li>
</ul>
</li>
<li>The word $w$ is a dead end; drop it from the dictionary and return false.</li>
</ul>
<p>Dropping the words from the dictionary, or otherwise marking them as dead ends in some other way is necessary to avoid re-searching them later.  The time complexity is roughly $O(n)$, where $n$ is the size of the dictionary to search.  If the average length of a word is $k$, then we can be more specific, and say the complexity is $O(kn)$, but $k$ is very small with respect to $n$, and does not vary much with the data size, so it can be safely ignored.</p>
<h2>Iteration</h2>
<p>The problem was explained to me in the reverse fashion.  Given a letter, generate words by adding a single letter at a time.  Every word must be in the dictionary.  Find the longest word you can generate from a single letter.  This suggests a different algorithm.</p>
<ul>
<li>Put the words in a heap, ordered shortest to longest.</li>
<li>Mark the empty string as reachable and set $long$ to the empty string.</li>
<li>Iterate through the heap, considering each word $w$.
<ul>
<li>For each position $i$ of $w$, do the following.
<ul>
<li>Drop the $i$th letter from $w$, generating the candidate word $v$.
<ul>
<li>If $v$ is marked as reachable, then mark $w$ as reachable by omitting $i$, and set $long=w$.</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>While $long$ is not the empty string, do the following.
<ul>
<li>Write $long$.</li>
<li>Omit the character $i$ by which $long$ was reached, and set $long$ equal to the new word.</li>
</ul>
</li>
</ul>
<p>We can improve this by checking against the length of the longest word reached.  If we ever have $|w|&gt;|longest|+1$, then we can stop the loop immediately, since we cannot reach any further words.  We can&#8217;t do that with the prior algorithm.</p>
<h2>So&#8230; What&#8217;s the Longest Word?</h2>
<p>The solution given was <em>complecting</em>, at 11 letters.  When I download the <a href="http://aspell.net/" target="_blank">aspell</a> dictionaries, and run the algorithm (the iterative one), I find the following derivation.</p>
<p>-&gt; a<br />
-&gt; ah<br />
-&gt; ach<br />
-&gt; bach<br />
-&gt; banch<br />
-&gt; banchi<br />
-&gt; branchi<br />
-&gt; branchia<br />
-&gt; branchiae<br />
-&gt; branchiate<br />
-&gt; abranchiate<br />
-&gt; abranchiates</p>
<p>Ooh.  12 letters.  But, it&#8217;s a plural.  Does that count?  Well, complecting is a participle, so it seems it should.  Time to send a note to Tom and Ray?</p>
<h2>So&#8230; Which Algorithm?</h2>
<p>Well, I coded up the iterative one in Java, since originally I was asked how I would code it in Java.  Again, the question suggesting the solution.  I suspect there are a lot of long words that are not reachable, so it seemed better to use the trick to terminate the search when the current word is longer than the longest found (so far) plus one.  Plus, it isn&#8217;t recursive, and I don&#8217;t have to modify the data structure by deleting an item from the heap.</p>
<h2>The Java</h2>
<p>So&#8230; here&#8217;s the Java source.  Enjoy!</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/**
 * Given some iterable object that provides strings, find the longest
 * string in the collection that can be reached from the empty string
 * by adding one character at a time such that every intermediate step
 * is also in the collection.
 *
&nbsp;
 * For example, if the collection is {@code {@literal {}a,
 * at, cat, chat, chart, cart, car{@literal }}},
 * then the longst word is {@code chart}, and it's derivation is:
 * {@code a =&amp;gt; at =&amp;gt; cat =&amp;gt; cart =&amp;gt; chart}.  If there are multiple
 * longest words, then only one is detected.
 *
&nbsp;
 * Note that if single letters are not in the collection, then no words
 * can be derived, and only the empty derivation is returned.
 *
 * @param strings    An iterable providing the strings.
 * @return    The derivation of the longest, from the shortest word to the
 *             longest.  If there is no longest word, then the empty
 *             derivation is returned.
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span> search<span style="color: #009900;">&#40;</span>Iterable strings<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>strings <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">NullPointerException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The string iterator is null.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Read each string from the iterable.  The strings assumed to be</span>
    <span style="color: #666666; font-style: italic;">// single words, and the words are not assumed to occur in any</span>
    <span style="color: #666666; font-style: italic;">// particular order.  We put the words we read into a heap, ordered</span>
    <span style="color: #666666; font-style: italic;">// by length.  Our &quot;heap&quot; is a sorted map.</span>
    <span style="color: #666666; font-style: italic;">//</span>
    <span style="color: #666666; font-style: italic;">// Every entry in the map has an associated integer value, which</span>
    <span style="color: #666666; font-style: italic;">// tells us the position of the last inserted character.  This can</span>
    <span style="color: #666666; font-style: italic;">// be used to &quot;unwind&quot; the derivation of the word.  If there is not</span>
    <span style="color: #666666; font-style: italic;">// (yet) any way to reach the word, then the word is mapped to -1.</span>
    <span style="color: #003399;">TreeMap</span> heap <span style="color: #339933;">=</span>
        <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">TreeMap</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Comparator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> compare<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> first, <span style="color: #003399;">String</span> second<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// Okay, we sort first by length, and then alphabetically.</span>
                <span style="color: #666666; font-style: italic;">// This is necessary because of the way TreeMap tests</span>
                <span style="color: #666666; font-style: italic;">// equality; it uses the comparator.  Thus we must only</span>
                <span style="color: #666666; font-style: italic;">// return zero if the two are actually equal.  So, we</span>
                <span style="color: #666666; font-style: italic;">// first order by length, and then for items of the same</span>
                <span style="color: #666666; font-style: italic;">// length, we order using the natural comparator for</span>
                <span style="color: #666666; font-style: italic;">// strings.</span>
                <span style="color: #000066; font-weight: bold;">int</span> order <span style="color: #339933;">=</span> first.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> second.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>order <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    order <span style="color: #339933;">=</span> first.<span style="color: #006633;">compareToIgnoreCase</span><span style="color: #009900;">&#40;</span>second<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000000; font-weight: bold;">return</span> order<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> str <span style="color: #339933;">:</span> strings<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        heap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>str,<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Add all strings to the heap.</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Prime the pump by adding the empty string as &quot;reachable.&quot;</span>
    <span style="color: #003399;">String</span> longest <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">int</span> length <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    heap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// At this point the heap is constructed.  We now iterate over the</span>
    <span style="color: #666666; font-style: italic;">// heap looking for connected words.  If we find one, we mark it.</span>
    <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> str <span style="color: #339933;">:</span> heap.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Watch for a case where we skip a length.  If we do, we're done.</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>str.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> length<span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        length <span style="color: #339933;">=</span> str.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Now we have a string, we need to find out if the string is</span>
        <span style="color: #666666; font-style: italic;">// reachable from a previously-reached string.  We drop each</span>
        <span style="color: #666666; font-style: italic;">// character, and then check if the resulting string is</span>
        <span style="color: #666666; font-style: italic;">// present in the heap and is marked.</span>
        <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> index <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> index <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> str.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> index<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// Omit the character at position index.</span>
            <span style="color: #003399;">String</span> test <span style="color: #339933;">=</span> str.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>,index<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> str.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>index<span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// See if the resulting test string is in the heap.</span>
            <span style="color: #003399;">Integer</span> pos <span style="color: #339933;">=</span> heap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Now there are three cases:</span>
            <span style="color: #666666; font-style: italic;">// (1) The test string is not in the heap.</span>
            <span style="color: #666666; font-style: italic;">// (2) The test string is in the heap, but not reachable.</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>pos <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> pos <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">continue</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #666666; font-style: italic;">// (3) The test string is in the heap, and is reachable.</span>
            <span style="color: #666666; font-style: italic;">// In this last case we know we can reach the current string</span>
            <span style="color: #666666; font-style: italic;">// from the test string by adding the character at position</span>
            <span style="color: #666666; font-style: italic;">// index, so put this in the heap.</span>
            heap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>str, index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// Note that whenever this happens, we might have just found</span>
            <span style="color: #666666; font-style: italic;">// (one of) the longest words, so save it.</span>
            longest <span style="color: #339933;">=</span> str<span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">// We don't need to test any other positions.</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Construct new words by omitting each character.</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Search the heap for words.</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Now we have (hopefully) found a long word.  We need to generate</span>
    <span style="color: #666666; font-style: italic;">// the derivation and return it.</span>
    <span style="color: #003399;">List</span> derivation <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">LinkedList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>longest.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Get the insertion position from the heap.</span>
        <span style="color: #000066; font-weight: bold;">int</span> pos <span style="color: #339933;">=</span> heap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>longest<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Add the word to the derivation.</span>
        derivation.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, longest<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Generate the prior string in the derivation.</span>
        longest <span style="color: #339933;">=</span> longest.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, pos<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> longest.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>pos<span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// Build the derivation.</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Done!  Return the derivation.</span>
    <span style="color: #000000; font-weight: bold;">return</span> derivation<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The code isn&#8217;t optimal, but it is commented.  I assume you are all capable of putting this in a class and feeding it a dictionary.  Have fun!</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F06%2F14%2Fcar-talk-and-the-longest-word%2F&amp;linkname=Car%20Talk%20and%20the%20Longest%20Word"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/06/14/car-talk-and-the-longest-word/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mac MAC Address</title>
		<link>http://stacyprowell.com/blog/2009/06/05/mac-mac-address/</link>
		<comments>http://stacyprowell.com/blog/2009/06/05/mac-mac-address/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 18:01:12 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Macintosh]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[leopard]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[mac address]]></category>
		<category><![CDATA[os x]]></category>

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=12</guid>
		<description><![CDATA[For the record, here&#8217;s how I set the MAC address on my MacBook Pro.  As I write this it is running Leopard 10.5.6 and this has been working successfully with every version of Leopard.  In fact, it has worked so well I&#8217;d forgotten how I did it, so I&#8217;m basically writing this post [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_211" class="wp-caption alignright" style="width: 160px"><img class="size-thumbnail wp-image-211" title="3d_apple_logo_102" src="http://stacyprowell.com/blog/wp-content/uploads/2009/05/3d_apple_logo_102-150x150.jpg" alt="Apple" width="150" height="150" /><p class="wp-caption-text">Apple</p></div>
<p>For the record, here&#8217;s how I set the MAC address on my MacBook Pro.  As I write this it is running Leopard 10.5.6 and this has been working successfully with every version of Leopard.  In fact, it has worked so well I&#8217;d forgotten how I did it, so I&#8217;m basically writing this post so, if it ever stops working, I will know how to fix it. <img src='http://stacyprowell.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   If it helps you, all the better.  If it hurts you&#8230; well&#8230; don&#8217;t blame me.<span id="more-12"></span></p>
<p>Note that, for reasons obscure, I want to <em>permanently</em> set the MAC address to something other than the hardwired address.  If that&#8217;s not what you want, this recipe might not be for you.</p>
<p>For the record I found the information on creating the startup item <a href="http://developer.apple.com/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/StartupItems.html">here</a>.</p>
<p>First create a directory under the <tt>/Library/StartupItems</tt> folder.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>StartupItems<span style="color: #000000; font-weight: bold;">/</span>MACADDR</pre></div></div>

<p>That&#8217;s <tt>MACADDR</tt> for MAC ADDRess, in case you&#8217;re wondering.  I made it all caps so it would be more obvious to me in listings.</p>
<p>Next create the file <tt>/Library/StartupItems/MACADDR/StartupParameters.plist</tt>.  I used VI, but that&#8217;s me.  Use whatever editor you want.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">vi</span> <span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>StartupItems<span style="color: #000000; font-weight: bold;">/</span>MACADDR<span style="color: #000000; font-weight: bold;">/</span>StartupParameters.plist</pre></div></div>

<p>This file needs to have the following content.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
  <span style="color: #00bbdd;">&lt;!DOCTYPE plist SYSTEM &quot;file://localhost/System/Library/DTDs/PropertyList.dtd&quot;&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plist</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Description<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Set the MAC address for specified interfaces.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>OrderPreference<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>None<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Provides<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MACADDR<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Requires<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Network Configuration<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>I think the <tt>Requires</tt> part is correct; I stole that from looking at the configuration files for tun and tap, since this needs to happen before the network is brought up. It might be a better idea to specify an order, but I have not had trouble leaving it as <tt>None</tt>.</p>
<p>Next create the file <tt>/Library/StartupItems/MACADDR/MACADDR</tt>.  I think this file needs to have the same name as the directory, so if you don&#8217;t like <tt>MACADDR</tt> be sure to use the same name for both the directory and this file.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">vi</span> <span style="color: #000000; font-weight: bold;">/</span>Library<span style="color: #000000; font-weight: bold;">/</span>StartupItems<span style="color: #000000; font-weight: bold;">/</span>MACADDR<span style="color: #000000; font-weight: bold;">/</span>MACADDR</pre></div></div>

<p>This file needs to have the following content, where <tt>ADDRESS</tt> is replaced with the MAC address you want.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
. <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>rc.common
&nbsp;
<span style="color: #666666; font-style: italic;">##</span>
<span style="color: #666666; font-style: italic;"># Configure the MAC addresses of network interfaces.</span>
<span style="color: #666666; font-style: italic;">##</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># This script will set the hardware MAC address for the specified</span>
<span style="color: #666666; font-style: italic;"># interface(s).  The name of the interface (ex. en0) must be edited</span>
<span style="color: #666666; font-style: italic;"># to match the interface.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;"># Typically the built-in ethernet card is en0, and the built-in</span>
<span style="color: #666666; font-style: italic;"># wifi card is en1.</span>
<span style="color: #666666; font-style: italic;">#</span>
<span style="color: #666666; font-style: italic;">##</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># The start subroutine.</span>
StartService<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  ConsoleMessage <span style="color: #ff0000;">&quot;Configuring MAC address&quot;</span>
  <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">${MACADDR:=-NO-}</span>&quot;</span> = <span style="color: #ff0000;">&quot;-YES-&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #666666; font-style: italic;"># Make sure the wifi card has the correct MAC address.</span>
    <span style="color: #000000; font-weight: bold;">/</span>sbin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">ifconfig</span> en1 lladdr ADDRESS
  <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># The stop subroutine.</span>
StopService<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># The restart subroutine.</span>
RestartService<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># It might be nice to make this reset the address, but it's never come up.</span>
  <span style="color: #7a0874; font-weight: bold;">return</span> <span style="color: #000000;">0</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
RunService <span style="color: #ff0000;">&quot;$1&quot;</span></pre></div></div>

<p>Again, if you change the name from <tt>MACADDR</tt> make sure to also change it in this file.  Also, do not forget to replace <tt>ADDRESS</tt> with the MAC address you want to use.</p>
<p>These two files create a service that gets started when the Mac boots up.  This &#8220;service&#8221; just sets the MAC address of the wireless card.  Note that if you are running on Tiger you will probably need to use <tt>ether</tt> instead of <tt>lladdr</tt> in the <tt>ifconfig</tt> line.</p>
<p>If you find problems with new versions of OS X, or if I&#8217;ve done something boneheaded above, please add a comment below.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F06%2F05%2Fmac-mac-address%2F&amp;linkname=Mac%20MAC%20Address"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/06/05/mac-mac-address/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
