<?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; Scala</title>
	<atom:link href="http://stacyprowell.com/blog/category/programming/scala/feed/" rel="self" type="application/rss+xml" />
	<link>http://stacyprowell.com/blog</link>
	<description>Ugh, Stacy&#039;s talking again...</description>
	<lastBuildDate>Sat, 04 Feb 2012 06:01:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Scala Sudoku</title>
		<link>http://stacyprowell.com/blog/2010/12/28/scala-sudoku/</link>
		<comments>http://stacyprowell.com/blog/2010/12/28/scala-sudoku/#comments</comments>
		<pubDate>Tue, 28 Dec 2010 06:48:07 +0000</pubDate>
		<dc:creator>stacy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[sudoku]]></category>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

		<guid isPermaLink="false">http://stacyprowell.com/blog/?p=6</guid>
		<description><![CDATA[As usual, I&#8217;m learning Yet Another Programming Language.  Every time I do I see some new ideas and experience new frustrations.  This time the language is Scala http://www.scala-lang.org/.  There is a lot to like about it. First, it&#8217;s a functional langauge, and I really like functional languages.  Second, it is statically typed with type inference, [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;"><img class="aligncenter size-full wp-image-110" title="newsflash_logo2" src="http://stacyprowell.com/blog/wp-content/uploads/2009/01/newsflash_logo2.png" alt="newsflash_logo2" width="148" height="44" />As usual, I&#8217;m learning Yet Another Programming Language.  Every time I do I see some new ideas and experience new frustrations.  This time the language is Scala <a title="Scala" href="http://www.scala-lang.org/" target="_blank">http://www.scala-lang.org/</a>.  There is a lot to like about it.</p>
<p><span id="more-6"></span>First, it&#8217;s a functional langauge, and I really like functional languages.  Second, it is statically typed with type inference, and I like that.  Third, it is object-oriented in a nice way (everything really is an object).  Fourth it runs on the Java VM.  So far, so good.</p>
<p>The down side to it is that the learning curve seems quite steep.  I am occasionally surprised by the solutions given in the text I&#8217;m reading (the Scala book), and I think that&#8217;s <em>bad</em>.  After reading half the book I should be able to predict the form of a solution.  I&#8217;ve been doing quite a bit of functional programming recently, and so I&#8217;m not shocked by that aspect.  I think it is actually the exposition, which is otherwise excellent.  Too many ideas are being introduced for the first time in code snippets.</p>
<p>I am also frustrated that Scala uses type erasure.  It&#8217;s not as much of a problem as with Java, but it is still a bit frustrating.</p>
<p>On the plus side I <em>really</em> want to write some code in Scala.  I&#8217;m currently developing a class library for sequence enumeration.  It&#8217;s relatively compact, but mixins would really help the library, and the Scala prompt would make testing much nicer.  Right now I&#8217;m testing using Jython.  The temptation is strong to re-write the library in Scala.</p>
<p>If anyone else has experience with Scala, I&#8217;d love to hear about it.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fstacyprowell.com%2Fblog%2F2009%2F01%2F01%2Fscala%2F&amp;title=Scala" id="wpa2a_8"><img src="http://stacyprowell.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://stacyprowell.com/blog/2009/01/01/scala/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

