<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" 
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
    xmlns:admin="http://webns.net/mvcb/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
	<channel>
<title>LouFranco.com - RSS</title><link>http://www.loufranco.com/index.html</link><description>Entries from Lou Franco&#x27;s Blog</description><dc:language>en</dc:language><dc:creator>Lou Franco</dc:creator><dc:rights>Copyright 2008 Louis Franco</dc:rights><dc:date>2008-04-30T21:09:38-04:00</dc:date><admin:generatorAgent rdf:resource="http://www.realmacsoftware.com/" />
<admin:errorReportsTo rdf:resource="mailto:Lou Franco" /><sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<sy:updateBase>2000-01-01T12:00+00:00</sy:updateBase>
<lastBuildDate>Wed, 30 Apr 2008 21:16:49 -0400</lastBuildDate><item><title>Ant Colony in F#</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-04-30T21:09:38-04:00</dc:date><link>http://www.loufranco.com/blog/files/ad21adc38f8d971de4ee96a20cffab5f-59.html#unique-entry-id-59</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/ad21adc38f8d971de4ee96a20cffab5f-59.html#unique-entry-id-59</guid><content:encoded><![CDATA[My colleague, Rick, is playing around with F#. He made this <a href="http://www.atalasoft.com/cs/blogs/rickm/archive/2008/04/25/10-hours-in-fsharp-exploring-concurrency-through-an-ant-colony-simulation.aspx" rel="self">ant colony in F#</a> that's based on <a href="http://blip.tv/file/812787/" rel="self">Rich's clojure one</a>. If you know clojure, and you want to see what F# is all about, you might want to take a look.]]></content:encoded></item><item><title>Clojure Day Blog Roundup</title><dc:creator>Lou Franco</dc:creator><category>20 Days of Clojure</category><dc:date>2008-03-21T13:36:15-04:00</dc:date><link>http://www.loufranco.com/blog/files/c2702ef9e5a9c35e8cffcef1f4a62802-58.html#unique-entry-id-58</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/c2702ef9e5a9c35e8cffcef1f4a62802-58.html#unique-entry-id-58</guid><content:encoded><![CDATA[If you were there and blogged it, let me know:<br /><br />Here's what I found so far:<br /><br /><ul class="disc"><li><a href="http://www.atalasoft.com/cs/blogs/insertqualityhere/archive/2008/03/21/clojure.aspx" rel="self">Adam Scarborough</a></li><li><a href="http://blog.snowtide.com/2008/03/21/western-mass-developers-group-and-snowtide-host-rich-hickey-and-clojure" rel="self">Chas Emerick</a></li><li><a href="http://news.e-scribe.com/411" rel="self">Paul Bissex</a></li><li><a href="http://www.atalasoft.com/cs/blogs/rickm/archive/2008/03/21/clojure-impressions.aspx" rel="self">Rick Minerich</a></li></ul>]]></content:encoded></item><item><title>20 Days of Clojure: Day 20</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-21T07:47:30-04:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-20.html#unique-entry-id-57</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-20.html#unique-entry-id-57</guid><content:encoded><![CDATA[Last night was our Clojure event with Rich Hickey. BTW, if you were there, regular meeting of the <a href="http://wmassdevs.org" rel="self">Western Mass Developers Group</a> are every other Thursday at Panera Bread in Hadley (7pm) --- most of the time, we just sit around and talk -- very informal -- hope to see you there.<br /><br />Back to the presentation -- hopefully the slides/video/etc will be up somewhere soon -- Rich focussed on the concurrency features of Clojure (Vars, Refs, and Agents). First he showed us the state of concurrency support in Java/C# today (locking) and identified the problem as having direct references to mutable objects. Clojure offers only immutable atoms and datastructures so that's how he addresses one part of the problem. However, since the mutability is how the world works, Clojure models change as a change to what a reference points to. So, if you view the world through refs, it will appear to change. Then he introduced the controlled ways in which refs can change.<br /><br />1. Vars are essentially thread local variables. They are a reference whose changes can only be seen in a single thread -- each thread gets an independent and isolated view of the variable. Like thread-local variables -- they are a very simple way to take code that looks like it's manipulating global state and give each thread its own isolated copy.<br /><br />2. Refs are mutable references to immutable objects. They can only be changed inside a transaction and all changes to all refs in a transaction appear to have happened at the time the transaction ends (to other threads). Inside the transaction, the changes are immediate. When two transactions are open and change the same ref, the second one to finish is automatically retried (so you should have no side-effects inside of transactions). All Refs read inside of a transaction will return the value that they had at the start of the transaction (so are consistent with each other).<br /><br />3. Agents (which I have explained <a href="files/20-Days-of-Clojure-Day-11.html" rel="self" title="Blog:20 Days of Clojure: Day 11">before</a>) are for asynchronous changes to a reference.  Each agent hold a value and you send actions to it to change the value. Only one action will happen to an agent at a time and you can read the current state of the agent any time. Rich spent a little time to caution us against any comparison to Erlang's actors. They are a different concept with different tradeoffs. Agents have the advantage that they can be read from any thread conveniently (just dereference) whereas actors require sending a message to read (which is asynchronous). Clearly, Erlang's benefit is location transparency (for distributed code) -- which is what it was designed for.  Rich hinted that Clojure might have an Actor concept, but it would not be unified with Agents.<br /><br />What was new to me with agents is that there are two types of message sending mechanisms -- send and send-off (<a href="http://clojure.sourceforge.net/reference/agents.html" rel="self">which appear to be undocumented right now</a>) -- Rich used send-off which dedicates a thread to the agent (rather than run the action from a thread-pool). This is how you have to do it if agents block at all (which ants do because they have a small sleep).<br /><br />Then, he showed us an ant simulation -- I think he will make the code available. In short, there is a square world of cells, each one holds a Ref to what is in the cell (food, ant, pheromones, home base). Ants are represented by agents, which are sent a behave message. Behave picks a random action for the ant to do (move, turn, pick up food, drop food) and then mutates the world of cells in a transaction, then it sends itself another behave action. There is an agent responsible for painting the world, and another which evaporates pheromones in the background.<br /><br />Anyway, the demo was impressive -- since painting makes a copy of the world in a transaction, it has a completely consistent view of the world. Refs make sure that all changes to them are in transactions, so you have language support for where you need cooperation (contrasted to locking, which is not enforced). Agents also help you model the real world in a way that a coordinated single-looped simulation (calling behave on ants in a loop, then paint over and over again) could not. <br /><br />And clojure's agent mutating mechanism (sending a function to it), means that agents don't have to have any knowledge of the messages that might be sent to it (again contrasted to Erlang Actors).  Finally, various messages can take different times to complete and that would be represented in the simulation --- some ants might complete several small actions in the time it took another to complete one (which would not be the case in a behave loop).<br /><br />I'll have more on this when the slides, code, etc are available.]]></content:encoded></item><item><title>20 Days of Clojure: Day 19</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-19T07:41:01-04:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-19.html#unique-entry-id-56</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-19.html#unique-entry-id-56</guid><content:encoded><![CDATA[Ok, <a href="files/20-Days-of-Clojure-Day-18.html" rel="self" title="Blog:20 Days of Clojure: Day 18">yesterday</a> I decided to try to implement a defclass macro which would hobble multimethods and force you to have some kind of class inheritance for polymorphism. Today, I'll try to implement that macro.<br /><br />First, I made this macro, which I think will be useful<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defmacro sym2key [s]<br />&nbsp;&nbsp;&nbsp;&nbsp;`(keyword (name (quote ~s)))&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;)</span><br /><br />Works like this:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;user=&gt; (sym2key rect)<br />&nbsp;&nbsp;:rect</span><br /><br />(I wrote the above this morning, and after several hours, I didn't get too far)<br /><br />I had to cheat a lot, but I finally got something -- here is my final OO minilanguage<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defclass shape <br />&nbsp;&nbsp;&nbsp;&nbsp;(defabstractmeth area) <br />&nbsp;&nbsp;)<br />&nbsp;&nbsp;(defclass rect <br />&nbsp;&nbsp;&nbsp;&nbsp;(defctor (fn [w h] (setprop w) (setprop h) )) <br />&nbsp;&nbsp;&nbsp;&nbsp;(defmeth area (fn [] (* (:w this) (:h this))))<br />&nbsp;&nbsp;)<br />&nbsp;&nbsp;(defclass circle <br />&nbsp;&nbsp;&nbsp;&nbsp;(defctor (fn [r] (setprop r)  )) <br />&nbsp;&nbsp;&nbsp;&nbsp;(defmeth area (fn [] (* (:r this) (:r this) (. Math PI))))<br />&nbsp;&nbsp;)</span><br /><br />I got that to be processed by these macros:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defmacro sym2key [s]<br />&nbsp;&nbsp;&nbsp;&nbsp;`(keyword (name (quote ~s)))&nbsp;&nbsp;<br />&nbsp;&nbsp;)<br /><br />&nbsp;&nbsp;(defmacro defclass [name & body]<br />&nbsp;&nbsp;&nbsp;&nbsp;(cons 'do<br />&nbsp;&nbsp;&nbsp;&nbsp;(loop [classbody# body parts# nil] <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if classbody#<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(recur (rest classbody#) (cons (concat (first classbody#) `(~name)) parts#))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parts#)))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;)<br /><br />&nbsp;&nbsp;(defmacro defabstractmeth [name class] `(defmulti ~name :type) )<br /><br />&nbsp;&nbsp;(defmacro setprop [x] { (sym2key x) x })<br /><br />&nbsp;&nbsp;(defmacro defctor [fndef name]<br />&nbsp;&nbsp;&nbsp;&nbsp;(let [&nbsp;&nbsp;arglist# (second fndef) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fnbody# (map second (nthrest fndef 2) ) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;obj# (reduce merge <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{:type `(sym2key ~name)} <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(map assoc (repeat {}) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(map eval <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(map sym2key fnbody#)) fnbody#))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`(defn ~name ~arglist# ~obj#)<br />&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;)<br /><br />&nbsp;&nbsp;(defmacro defmeth [meth fndef name]<br />&nbsp;&nbsp;&nbsp;&nbsp;(let [&nbsp;&nbsp;arglist# (conj (second fndef) 'this)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fnbody# (first (nthrest fndef 2)) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;namesym# (eval `(sym2key ~name))<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`(defmethod ~meth ~namesym# ~arglist# ~fnbody#)<br />&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;)<br /><br /></span>This code shows it in action:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(prn (rect 10 20))<br />&nbsp;&nbsp;(prn (area (rect 10 20)))<br />&nbsp;&nbsp;(prn (circle 10))<br />&nbsp;&nbsp;(prn (area (circle 10)))<br /></span><br />Outputs:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;{:type :rect, :h 20, :w 10}<br />&nbsp;&nbsp;200<br />&nbsp;&nbsp;{:type :circle, :r 10}<br />&nbsp;&nbsp;314.1592653589793<br /><br /></span>I am way too tired to explain this -- suffice to say, this is kind a crazy way to make something, but it sure beats not being able to make it.  My ctor implementation is so crazy, that you should just ignore it -- defclass and defmeth are worth looking at (ctor is a major hack -- assumes only setprop calls and turns them into a map).]]></content:encoded></item><item><title>20 Days of Clojure: Day 18</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-18T21:06:44-04:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-18.html#unique-entry-id-55</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-18.html#unique-entry-id-55</guid><content:encoded><![CDATA[I just got around to watching Clojure TV on sequences, which I guess I should have done earlier, but I guess I was too lazy --- thanks, I'm here all week.<br /><br />Ok -- since clearly, day 20 is going to be about <a href="http://wmassdevs.com/wordpress/2008/02/28/clojure-talk-with-rich-hickey-on-march-20/" rel="self">Clojure Day in Northampton</a>, I only have two days to explore more of clojure.  I haven't even gotten to <a href="http://clojure.sourceforge.net/reference/multimethods.html" rel="self">multimethods</a>, which are very cool, but pretty simple to understand. I want to keep working with macros.<br /><br />The hardest thing about thinking about macros is that not using a language that has one, I haven't started to think in macros yet. Kind of reminds me of <a href="http://www.imdb.com/title/tt0083943/" rel="self">Clint Eastwood in Firefox</a> who has to keep remembering to think in Russian in order to fly his stolen super-secret Soviet jet.<br /><br />Ok, since I think in OO, let me try to do some OO in clojure via macros -- maybe I get to use multimethods after all (let's hobble them with conventional OO polymorphism)<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defclass rect <br />&nbsp;&nbsp;&nbsp;&nbsp;(defctor (fn [w h] (setprop w) (setprop h) )) <br />&nbsp;&nbsp;&nbsp;&nbsp;(defmeth area (fn [] (* w h))) <br />&nbsp;&nbsp;)<br /><br />&nbsp;&nbsp;(defclass circle <br />&nbsp;&nbsp;&nbsp;&nbsp;(defctor (fn [r] (setprop r)))<br />&nbsp;&nbsp;&nbsp;&nbsp;(defmeth area (fn [] (* r r (. Math PI))))<br />&nbsp;&nbsp;)<br /></span><br />I want that to turn into<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defn rect [w h] {:type :rect :w w :h h})<br />&nbsp;&nbsp;(defmulti area :type)<br />&nbsp;&nbsp;(defmethod area :rect [r]<br />&nbsp;&nbsp;&nbsp;&nbsp;(* (:w r) (:h r)))<br /><br />&nbsp;&nbsp;(defn circle [r] {:type :circle :r r})<br />&nbsp;&nbsp;(defmulti area :type)<br />&nbsp;&nbsp;(defmethod area :circle [c]<br />&nbsp;&nbsp;&nbsp;&nbsp;(* (. Math PI) (* (:r c) (:r c))))</span><br /><br />I'm not even sure this is possible -- I just checked to see if it's legal to defmulti the same name, and it's not really because it kills the definition for rect.  Luckily, I have a precedent to fall back on -- in Java/C# style OO, area is only polymorphic if it came from a superclass. I can change the class structure to this:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defclass shape <br />&nbsp;&nbsp;&nbsp;&nbsp;(defabstractmeth area) <br />&nbsp;&nbsp;)<br /></span><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defclass rect :extends shape<br />&nbsp;&nbsp;&nbsp;&nbsp;(defctor (fn [w h] (setprop w) (setprop h) )) <br />&nbsp;&nbsp;&nbsp;&nbsp;(defmeth area (fn [] (* w h))) <br />&nbsp;&nbsp;)<br /><br />&nbsp;&nbsp;(defclass circle :extends shape<br />&nbsp;&nbsp;&nbsp;&nbsp;(defctor (fn [r] (setprop r)))<br />&nbsp;&nbsp;&nbsp;&nbsp;(defmeth area (fn [] (* r r (. Math PI))))<br />&nbsp;&nbsp;)<br /><br /></span>This is so ugly, I don't even know if I want to go through with this. I'll try for tomorrow.]]></content:encoded></item><item><title>20 Days of Clojure: Day 17</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-17T19:02:52-04:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-17.html#unique-entry-id-54</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-17.html#unique-entry-id-54</guid><content:encoded><![CDATA[(<a href="http://wmassdevs.com/wordpress/2008/02/28/clojure-talk-with-rich-hickey-on-march-20/" rel="self" title="Rich Hickey in Northampton">clojure day in Northampton, MA is Thursday, March 20th</a>)<br /><br /><div class="image-left"><img class="imageStyle" alt="viman" src="http://www.loufranco.com/blog/files//page2_blog_entry54_1.gif" width="200" height="144"/></div>I know I'll reveal myself as being tragically unhip for not using emacs or aquamacs or whatever (I'm a VI man after all), but I started this <a href="assets/clojure.zip" rel="self">TextMate Bundle for clojure</a>. I'll grow it as time goes by and probably move it to its own page or some repository for bundles once I figure this bundling stuff out.<br /><br />I do some rudimentary syntax coloring and automatically expand <strong>defn</strong>[Tab], <strong>def</strong>[Tab], <strong>let</strong>[Tab], and <strong>if</strong>[Tab].<br /><br />If you use TextMate and have some ideas for things to add, please send me a note.]]></content:encoded></item><item><title>20 Days of Clojure: Day 16</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-16T14:06:02-04:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-16.html#unique-entry-id-53</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-16.html#unique-entry-id-53</guid><content:encoded><![CDATA[Ok, time to look at macros. Being new to lisps, I'm especially new to macros. I have been meaning to read <a href="http://www.paulgraham.com/onlisptext.html" rel="self">On Lisp</a> for a few months, so like the SICP, I'll show you stuff from the macros chapter and then try to write them in clojure.<br /><br />In, On Lisp (Chapter 7), Paul Graham writes:<br /><blockquote><p>The expression generated by the macro will be evaluated in place of the original macro call. A macro call is a list whose first element is the name of the macro. What happens when we type the macro call into the toplevel? Lisp notices that [it] is the name of a macro, and<ol><li>builds the expression specified by the definition, then</li><li>evaluates that expression in place of the original macro call</li></ol></p></blockquote>Macros simply allow you to write code that gets executed at compile time to generate code, which is then run. There really isn't anything quite like them in popular non-lisp languages, though <a href="http://www.atalasoft.com/cs/blogs/loufranco/archive/2006/06/27/10323.aspx" rel="self">there are a couple of .NET languages with language altering macros</a>. They are much more powerful than the preprocessor macros in C, and C++ template meta-programming can sometimes get you the effect, but templates were not designed for this, so it is too cumbersome most of the time.<br /><br />We have been using macros in clojure throughout this series. In fact, since Lisps evaluate all of their arguments before calling a function, whenever you see code where all of the arguments are not evaluated, you are either looking at something built-in to the language (e.g. if) or a macro (e.g. cond). You can look at boot.clj to see the definitions of them. For example, here's lazy-cons:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defmacro lazy-cons [x & body]<br />&nbsp;&nbsp;&nbsp;&nbsp;(list 'fnseq x (list* `fn [] body)))</span><br /><br />Remember, we used lazy-cons to make lazy sequences. lazy-cons acts like cons, except it does not evaluate the rest-expr until rest is called on the resulting list. We can use macroexpand-1 (which returns the unevaluated macro expansion of a macro call) to see how this works:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;user=&gt; (defn rfn [] (prn "Called") '(3))<br />&nbsp;&nbsp;#&lt;Var: user/rfn&gt;<br />&nbsp;&nbsp;user=&gt; (rfn)<br />&nbsp;&nbsp;"Called"<br />&nbsp;&nbsp;(3)<br />&nbsp;&nbsp;user=&gt; (def lazylist (lazy-cons 1 (rfn)))<br />&nbsp;&nbsp;#&lt;Var: user/lazylist&gt;<br />&nbsp;&nbsp;user=&gt; (first lazylist)<br />&nbsp;&nbsp;1<br />&nbsp;&nbsp;user=&gt; (rest lazylist)<br />&nbsp;&nbsp;"Called"<br />&nbsp;&nbsp;(3)<br />&nbsp;&nbsp;user=&gt; (rest lazylist)<br />&nbsp;&nbsp;(3)<br />&nbsp;&nbsp;user=&gt; (macroexpand-1 '(lazy-cons 1 (rfn)))       <br />&nbsp;&nbsp;(fnseq 1 (clojure/fn [] (rfn)))<br />&nbsp;&nbsp;user=&gt; (first (eval (macroexpand-1 '(lazy-cons 1 (rfn)))))<br />&nbsp;&nbsp;1<br />&nbsp;&nbsp;user=> (rest (eval (macroexpand-1 '(lazy-cons 1 (rfn)))))<br />&nbsp;&nbsp;"Called"<br />&nbsp;&nbsp;(3)<br /></span><br />So, a lazy-cons is just a short hand to using a fnseq and wrapping the rest in a function that will be called whenever rest is called on the fnseq. That's a typical use of a macro -- codifying common uses to make your programs shorter. And, lazy-cons cannot be implemented as a function, because if it were, its arguments would need to be evaluated, which defeats the purpose.<br /><br />Ok, let's write some macros (Documentation on the <a href="http://clojure.sourceforge.net/reference/macros.html" rel="self">clojure macro page</a> and the <a href="http://clojure.sourceforge.net/reference/reader.html" rel="self">clojure reader page</a>). On Lisp, section 7.3 (Defining Simple Macros) presents this one for while.<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defmacro while (test &body body)<br />&nbsp;&nbsp;&nbsp;&nbsp;`(do () ((not ,test)) ,@body))<br /></span><br />Here it is in clojure:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defmacro while [test & body]<br />&nbsp;&nbsp;&nbsp;&nbsp;`(loop [] (if ~test (do ~@body (recur)))))<br /></span><br />In clojure, you don't have many mutable objects -- here's how you use while with a thread-local var binding:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(def x 10)<br />&nbsp;&nbsp;(prn (macroexpand '(while (> x 0) (prn x) (set! x (dec x)))))<br />&nbsp;&nbsp;(binding [x 10] (while (> x 0) (prn x) (set! x (dec x))))<br /><br /></span>Here's the output:<span style="font:12px Courier, mono; "><br /><br />&nbsp;&nbsp;(loop [] (if (> x 0) (do (prn x) (set! x (dec x)) (recur))))<br />&nbsp;&nbsp;10<br />&nbsp;&nbsp;9<br />&nbsp;&nbsp;8<br />&nbsp;&nbsp;7<br />&nbsp;&nbsp;6<br />&nbsp;&nbsp;5<br />&nbsp;&nbsp;4<br />&nbsp;&nbsp;3<br />&nbsp;&nbsp;2<br />&nbsp;&nbsp;1</span>]]></content:encoded></item><item><title>20 Days of Clojure: Day 15</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-15T13:43:40-04:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-15.html#unique-entry-id-52</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-15.html#unique-entry-id-52</guid><content:encoded><![CDATA[<a href="http://www.cgrand.net" rel="self">Christophe Grand</a> made two observations about <a href="files/20-Days-of-Clojure-Day-14.html" rel="self" title="Blog:20 Days of Clojure: Day 14">my parallel prime code</a>, which improve it quite a bit.<br /><br />First, since primes-in-list is lazy, you have to (touch) it to get it to be completely created in the agent thread. That leaves you with this:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defn par-primes-in-list [potential-primes known-primes]<br />&nbsp;&nbsp;&nbsp;&nbsp;(let [ x 10 x-times-2 (* x 2)]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(! w1 (fn[w pp kp] (</span><span style="font:12px Courier, mono; font-weight:bold; ">touch</span><span style="font:12px Courier, mono; "> (primes-in-list pp kp))) (take x potential-primes) known-primes) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(! w2 (fn[w pp kp] (</span><span style="font:12px Courier, mono; font-weight:bold; ">touch</span><span style="font:12px Courier, mono; "> (primes-in-list pp kp))) (take x (drop x potential-primes)) known-primes) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(await w1 w2)<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let [new-primes (concat @w1 @w2) ]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (= (count new-primes) 0) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(recur (drop x-times-2 potential-primes) known-primes)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let [real-primes <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (> (first new-primes) x-times-2) new-primes (primes-in-list new-primes []))]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lazy-cat real-primes (par-primes-in-list <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(drop x-times-2 potential-primes) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(into known-primes real-primes))))))))<br /></span><br />But, the bigger difference is picking the number of potential-primes to evaluate intelligently. I had experimented with trying to do this dynamically, but without the fix above, anything you do will probably result in primes being found lazily in the main thread. Here's Christophe's code:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;&nbsp;&nbsp;(defn par-primes-in-list [potential-primes known-primes]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let [p (first potential-primes) x (quot p 2)]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(! w1 (fn[w pp kp] (touch (lazy-primes-in-list pp kp))) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(take x potential-primes) known-primes)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(! w2 (fn[w pp kp] (touch (lazy-primes-in-list pp kp))) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(take (- p x) (drop x potential-primes)) known-primes)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(await w1 w2)<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let [new-primes (concat @w1 @w2) ]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lazy-cat new-primes <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(par-primes-in-list<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(drop p potential-primes)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(into known-primes new-primes))))))<br /></span><br />Christophe's observation:<br /><blockquote><p>I use the property that no number in the interval [n .. 2*n-1] is divisible by n (except n itself) so it's safe to process the whole interval with the current set of known primes. Hence this interval can be divided into m intervals (where m is the number of cores) for parallel processing.</p></blockquote>The one issue with Christophe's code it part lazy and part eager, so it's applicability depends on whether you are trying to get to a specific number the fastest or just generate primes as fast as possible. Since the number of primes to try grows as it searches, it lazily generates bigger and bigger lists of primes. I tried to find a good value to ask for where it would perform well, and chose 20390 which is big enough to show a difference and doesn't make Christophe's code do too much extra.  Here are the results:<br /><br />Christophe's: "Elapsed time: 87266.119 msecs"<br />Mine (with a touch added): "Elapsed time: 108841.354 msecs"<br />Sequential: "Elapsed time: 151075.292 msecs"]]></content:encoded></item><item><title>20 Days of Clojure: Day 14</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-14T21:37:44-04:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-14.html#unique-entry-id-51</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-14.html#unique-entry-id-51</guid><content:encoded><![CDATA[Ok, I'm finally getting somewhere. To start with, Rich pointed out that I could use recur to make my last prime finder stack safe and faster. Here it is:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defn primes-in-list [potential-primes known-primes]<br />&nbsp;&nbsp;&nbsp;&nbsp;(let [p (first potential-primes)] <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (nil? p) '()<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (is-prime? p known-primes) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lazy-cat <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(list p) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(primes-in-list (rest potential-primes) (conj known-primes p)))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let [rest-primes (rest potential-primes)]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(recur rest-primes known-primes))))))<br /></span><br />Here's how I parallelized it:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(def w1 (agent 0))<br />&nbsp;&nbsp;(def w2 (agent 0))<br /><br />&nbsp;&nbsp;(defn par-primes-in-list [potential-primes known-primes]<br />&nbsp;&nbsp;&nbsp;&nbsp;(let [ x 10 x-times-2 (* x 2)]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(! w1 (fn[w pp kp] (primes-in-list pp kp)) (take x potential-primes) known-primes) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(! w2 (fn[w pp kp] (primes-in-list pp kp)) (take x (drop x potential-primes)) known-primes) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(await w1 w2)<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let [new-primes (concat @w1 @w2) ]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (= (count new-primes) 0) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(recur (drop x-times-2 potential-primes) known-primes)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let [real-primes <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (> (first new-primes) x-times-2) new-primes (primes-in-list new-primes []))]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lazy-cat real-primes (par-primes-in-list <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(drop x-times-2 potential-primes) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(into known-primes real-primes))))))))<br /></span><br />Basically, I send lists to the agents, wait for them, and then combine their answers. The original runs at about 100% of CPU, calculates 8000 primes in 21.6 seconds and this runs at about 125%, and calculates the primes in 17.1 seconds, or in about 80% of the time.]]></content:encoded></item><item><title>20 Days of Clojure: Day 13</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-13T20:08:56-04:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-13.html#unique-entry-id-50</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-13.html#unique-entry-id-50</guid><content:encoded><![CDATA[My brain still hurts from thinking about parallelizing prime number generation. I think the key is still to find primes two at a time in parallel, meaning that it's only useful if you want a list, which is necessary to find the nth prime anyway.<br /><br />So, I am starting with a rewrite of prime number generation -- this one is simpler for me to understand and for some reason runs three times faster.<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defn divisible? [x y]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(= (rem x y) 0)<br />&nbsp;&nbsp;)<br /><br />&nbsp;&nbsp;(defn is-prime? [p known-primes]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(every? (fn [kp] (not (divisible? p kp))) known-primes)<br />&nbsp;&nbsp;)<br /><br />&nbsp;&nbsp;(defn primes-in-list [potential-primes known-primes]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let [p (first potential-primes)]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (is-prime? p known-primes) <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lazy-cons <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;p <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(primes-in-list (rest potential-primes) (conj known-primes p)))<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(primes-in-list (rest potential-primes) known-primes)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;)<br /><br />&nbsp;&nbsp;(def primes (primes-in-list (iterate inc 2) []))<br />&nbsp;&nbsp;(time (reduce + (take 1000 primes))) <br />&nbsp;&nbsp;(time (reduce + (take 1000 primes)))<br /><br /></span>It's also lazy. Not sure why it's so much faster. Now that I can provide a list of potentials, I'll be able to run two lists in parallel, and then I have to reduce them in the main thread. When I get the lists back, I need to make sure that they aren't divisible by each other (since they won't be in each others list of known primes).]]></content:encoded></item><item><title>20 Days of Clojure: Day 12</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-12T19:37:46-04:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-12.html#unique-entry-id-49</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-12.html#unique-entry-id-49</guid><content:encoded><![CDATA[So, Rich thinks I'm on a fool's errand, but I'm going to keep going with this.<br /><blockquote><p>I'm sorry to say I can't think of a less promising candidate for parallelization :( The 3 things you would want in something to parallelize are  independent data/work, moderate-to-course-grained work units and/or  complex coordination logic that would be simplified with threads.  (Note - wanting it to run faster is not enough :) Here you have none of those - the data/work is interdependent (more  later), the work is very fine grained (seq walk and a remainder test),  and the logic was pretty elegant. </p></blockquote>So, in order to have something to parallelize, I need to get one of those things. Since I am trying to make a list of primes, I'll generate them two at a time. Each check is independent. There's probably better ways to do it, but to keep my life simple, I'll just generate pairs of primes lazily, but in parallel (each member of the pair in a separate thread).<br /><br />Hmmm. I'm too tired to figure this out right now. So, for some content today, I can explain a mistake that Rich pointed out in my <a href="files/20-Days-of-Clojure-Day-10.html" rel="self" title="Blog:20 Days of Clojure: Day 10">original prime sieve</a>.<br /><br />I defined primes as a function that returns a lazy seq, but a key benefit of lazy seqs is that they cache values -- so by creating a new lazy seq for each call to primes, I lose caching.  The right way is to define primes as a seq this way:<br /><br />&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(def primes (sieve (iterate inc 2)))<br /><br /></span>Then, when you do this:<br /><br />&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(time (reduce + (take 1000 primes)))<br /></span>&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(time (reduce + (take 1000 primes)))<br /></span><br />You get this:<br /><br />&nbsp;&nbsp;<span style="font:12px Courier, mono; ">"Elapsed time: 1080.253 msecs"<br /></span>&nbsp;&nbsp;<span style="font:12px Courier, mono; ">"Elapsed time: 5.36 msecs"<br /></span>]]></content:encoded></item><item><title>20 Days of Clojure: Day 11</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-11T17:40:52-04:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-11.html#unique-entry-id-48</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-11.html#unique-entry-id-48</guid><content:encoded><![CDATA[Ok, so now we've covered some basic clojure as lisp with immutable datastructures and built-in support for streaming (lazy) interfaces. I was playing around with Erlang right before I found clojure because I was looking for a <a href="http://clojure.sourceforge.net/features/concurrent_programming.html" rel="self">language with direct support for concurrency</a>, the lispiness and the JVM hosting were the deciding factor for me.<br /><br />I'm just getting started with clojure's concurrency support, so you'll have to bear with me as I might struggle a bit. <a href="files/20-Days-of-Clojure-Day-10.html" rel="self" title="Blog:20 Days of Clojure: Day 10">Yesterday</a>, I showed you a lazy prime number sieve built using the seq interface. Today, I'm going to try to parallelize it. Since I don't really know what I'm doing, I'll take you through my thinking.<br /><br />There are three ways in clojure to deal with memory in threads: <a href="http://clojure.sourceforge.net/reference/vars.html" rel="self">dynamic vars</a>, <a href="http://clojure.sourceforge.net/reference/refs.html" rel="self">software transactional memory</a>, and <a href="http://clojure.sourceforge.net/reference/agents.html" rel="self">agents</a>. I get dynamic vars and agents because I use things like them all the time. Dynamic vars are basically thread-local storage and agents are a kind of <a href="http://www.cs.wustl.edu/~schmidt/PDF/Act-Obj.pdf" rel="self">ActiveObject [pdf]</a>. Thread local storage isn't going to work for me since I need to gather answers from my worker threads, not have each work independently (there may be dynamic vars in the solution, but it's not the way you communicate between threads).  <br /><br />ActiveObject is a pattern that serializes access to an object so that one thing happens to it at a time. Individual calls are put in a queue which is serviced by a thread assigned to the object, and that thread is the only one to touch the object. In clojure, there's actually a thread pool that services all agents. Now, that I think about it -- maybe this will work. I kind of wanted to play with STM's, but I think I understand this better and I feel I can still make it streamable.<br /><br />Here's my naive idea. I'm going to make an agent per prime, then ask them all if a number is divisible by them, then wait for them all and if it is, make a new agent and go on.  That seems really dumb unless agents are super-cheap. Let's see. <br /><br />I'll start with something simple:<br /><br />&nbsp;<span style="font:12px Courier, mono; ">(defn divisible? [x y] (= (rem x y) 0))</span><br /><br />I'll define my agents to be maps that look like this at the start {:prime 2} and then define this:<br /><br />&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn check-divisible [agent x] <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(merge agent {:last x :notdivisible (not (divisible? x (:prime agent)))}))<br /></span><br />Here's a quick Repl session so far (no real agents yet)<br /><br />&nbsp;&nbsp;<span style="font:12px Courier, mono; ">user=> (check-divisible {:prime 2} 3)<br /></span>&nbsp;&nbsp;<span style="font:12px Courier, mono; ">{:last 3, :prime 2, :notdivisible true}<br /></span>&nbsp;&nbsp;<span style="font:12px Courier, mono; ">user=> (check-divisible {:prime 2} 4)<br /></span>&nbsp;&nbsp;<span style="font:12px Courier, mono; ">{:last 4, :prime 2, :notdivisible false}<br /></span>&nbsp;&nbsp;<span style="font:12px Courier, mono; ">user=> (check-divisible {:prime 2} 5)<br /></span>&nbsp;&nbsp;<span style="font:12px Courier, mono; ">{:last 5, :prime 2, :notdivisible true}<br /></span><br />Ok, now I'll start. Here's something weird -- I just did a CPU snapshot running the sequential prime sieve from yesterday and it looks like this:<br /><br /><img class="imageStyle" alt="cpu" src="http://www.loufranco.com/blog/files//page2_blog_entry48_1.png" width="124" height="98"/><br /><br />I don't know what to make of that -- I know purely functional programs are easy to parallelize, but this sieve seems particularly serial in implementation. Anyway, I'll press on:<br /><br />Here's my agent-sieve<br /><br />&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn agent-sieve [a ints]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(let [p (next-prime a ints)]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(lazy-cons p (agent-sieve (cons {:prime p} a) (iterate inc (inc p))))))<br /></span><br />I keep stuffing a new agent list into the rest of a lazy-cons -- just need to write next-prime:<br /><br />&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn next-prime [agents ints]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(let [x (first ints)]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(check-divisible-by-agents (take-nth 2 agents) x)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(check-divisible-by-agents (take-nth 2 (drop 1 agents)) x)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(apply await agents)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(if (every? (fn [a] (or (not= (:last @a) x) (:notdivisible @a))) agents)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">x<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(next-prime agents (rest ints)))))<br /></span><br />Ok, so I'm starting to see problems -- for one, I really want to short-circuit if an agent is divisible, but I also want to split up the work. So I decide to make a check-divisible-by-agents that will chain along each agent and stop when it finds an agent that divides into the prime. I start two chains each with half of the agents and wait -- this will mean that if one is done, the other won't know and it will keep going (which is bad).  I press on, here's the chain<br /><br />&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn check-divisible-by-agents [agents x]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(if (not= (count agents) 0)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(! (first agents) <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(fn [a y rest-agents]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(let [newstate (check-divisible a y)]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(if (:notdivisible newstate) <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(check-divisible-by-agents rest-agents y))         <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">newstate)) <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">x (rest agents))))<br /></span><br />So, I run a function on the first agent in the list (with !) that sets the new state to whether it was divisible. If it's not divisible then I recursively call the check on the rest of the agents. <br /><br />I'm not getting primes yet -- but it's close.  I don't think await works like I think it does -- even though I am chaining ! inside of !, I think that doesn't count for await.  Here's is next-prime rewritten.<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;(defn next-prime [agents ints]<br />&nbsp;&nbsp;&nbsp;&nbsp;(let [x (first ints)]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(check-divisible-by-agents (take-nth 2 agents) x)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(check-divisible-by-agents (take-nth 2 (drop 1 agents)) x)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if (every? <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(fn [a] <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(await a)     <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(or (not= (:last @a) x)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(:notdivisible @a)))  <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;agents)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(next-prime agents (rest ints)))))</span><br /><br />Ok, I get primes now<br /><br />To tell the truth, I can't believe I got this working at all, but running (time) on the serial and parallel versions give me this:<br /><br /><strong>sequential:</strong> "Elapsed time: 9.517 msecs"<br /><strong>parallel:</strong> "Elapsed time: 251.025 msecs"<br /><br />So, obviously, this is not a good way to do this.  I don't know yet if it's because I'm using too many agents or the extra work I'm doing because I'm not short-circuiting. Anyway, that's enough for now.<br /><br /><strong>Update: </strong>After a night's rest, I think I know what's going on with the other thread in the sequential version. My guess is the GC. The only other alternative I can come up with is that under the hoods clojure parallelizes some things I'm using. I started a <a href="http://groups.google.com/group/clojure/browse_frm/thread/825e4e2bf4a7827e" rel="self">thread on parallelizing prime sieves on the clojure google group</a>.<br /><br />(<a href="http://wmassdevs.com/wordpress/2008/02/28/clojure-talk-with-rich-hickey-on-march-20/" rel="self" title="Rich Hickey in Northampton">clojure day (March 20) in Northampton, MA is coming soon</a>)]]></content:encoded></item><item><title>20 Days of Clojure: Day 10</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-10T20:51:29-04:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-10.html#unique-entry-id-47</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-10.html#unique-entry-id-47</guid><content:encoded><![CDATA[<span style="font-size:13px; ">Woo hoo -- half way through.  Well, not yet, but at the end of this entry. <br /><br /></span><span style="font-size:13px; "><a href="files/20-Days-of-Clojure-Day-9.html" rel="self" title="Blog:20 Days of Clojure: Day 9">Yesterday, we saw sequences</a></span><span style="font-size:13px; ">. Now, here's a way to use them: the Prime Sieve of Eratosthenes. Here it is in Scheme from the </span><span style="font-size:13px; "><a href="http://video.google.com/videoplay?docid=-987280451799958416" rel="self">SICP lectures</a></span><span style="font-size:13px; ">:<br /><br /></span>&nbsp;<span style="font:13px Courier, mono; ">(define (sieve s)<br /></span>&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(cons-stream (head s)<br /></span>&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(sieve (filter<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(lambda  (x)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(not <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(divisible? x (head s))))<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(tail s)))))<br /><br /></span>&nbsp;<span style="font:13px Courier, mono; ">(define primes <br /></span>&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(sieve (integers-from 2)))<br /></span><span style="font-size:13px; "><br />And here is a complete clojure program (all undefined functions are defined as part of the seq interface)<br /><br /></span>&nbsp;<span style="font:13px Courier, mono; ">(defn sieve [s]<br /></span>&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(lazy-cons (first s)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(sieve (filter<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(fn [x]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(not<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(= (rem x (first s)) 0)))<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(rest s)))))<br /><br /></span>&nbsp;<span style="font:13px Courier, mono; ">(defn primes []<br /></span>&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(sieve (iterate inc 2)))<br /><br /></span>&nbsp;<span style="font:13px Courier, mono; ">(prn (take 10 (primes)))<br /></span><span style="font-size:13px; "><br />Here's how it works. The function sieve is a sequence and takes a sequence (in this case we pass in all numbers greater than or equal to two).<br /><br />Working from the inside, this filter:<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(filter<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(fn [x]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(not<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(= (rem x (first s)) 0)))<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:13px Courier, mono; ">(rest s))<br /></span><span style="font-size:13px; "><br />Filters the rest of the passed in stream based on whether the items are divisible by the first, so only the ones not divisible are passed. So for (iterate inc 2) we get a stream of all numbers not divsible by two.<br /><br />The first of that stream is 3, and it is passed into sieve, so the resulting stream is all those not divisible by two (already filtered) or three. Once the stream is returned (lazily), the heads are lazy-cons'd on (that means that you get a stream with a given first and the rest set to the given stream). The first head was 2, the second was 3, the next one is 5 (the first number not divisible by 2 and 3), and so on.<br /><br /></span>(<a href="http://wmassdevs.com/wordpress/2008/02/28/clojure-talk-with-rich-hickey-on-march-20/" rel="self" title="Rich Hickey in Northampton">clojure day (March 20) in Northampton, MA is coming soon</a>)<br /><br />Update: It may look like sieve is an infinitely recursive function, but it's not. lazy-cons is a macro that doesn't evaluate its argument until rest is called on its return value. I haven't looked at the implementation, but I imagine its implemented similarly to how cons-stream in the SICP lecture is.<br /><br />Update: <a href="files/20-Days-of-Clojure-Day-12.html" rel="self" title="Blog:20 Days of Clojure: Day 12">This prime sieve has been corrected here</a>.]]></content:encoded></item><item><title>20 Days of Clojure: Day 9</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-09T12:16:59-04:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-9.html#unique-entry-id-46</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-9.html#unique-entry-id-46</guid><content:encoded><![CDATA[<span style="font-size:13px; ">News from Rich on a </span><span style="font-size:13px; "><a href="http://groups.google.com/group/clojure/browse_thread/thread/52d11c95895af4df?hl=en" rel="self">optimization he found for vectors</a></span><span style="font-size:13px; ">:<br /></span><blockquote><p>I re-read Okasaki's paper yesterday and woke up today with an idea for speeding up the Clojure vector conj and pop. They are now 2-5x fasterthan they were. The trick is to keep the tail of the vector out of the tree until it is a full (32-element) node. Note that this greatlyspeeds up vector construction, which typically consists of repeatedly conj'ing onto [].</p></blockquote><span style="font-size:13px; ">Today, I'm going to look at sequences. I think it's helpful to go back to the SICP -- this time, the lecture on stream machines:<br /><br /></span><object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/RcPhdoL8RcE"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/RcPhdoL8RcE" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object><span style="font-size:13px; "><br /></span><span style="font-size:13px; "><br />This is just a small part of the whole </span><span style="font-size:13px; "><a href="http://video.google.com/videoplay?docid=-7496022041893185723&q=structure+computer+programs+abelson+site%3Avideo.google.com&total=21&start=0&num=10&so=0&type=search&plindex=5" rel="self">SICP stream machine lecture</a></span><span style="font-size:13px; "> (and </span><span style="font-size:13px; "><a href="http://video.google.com/videoplay?docid=-987280451799958416" rel="self">Streams Part II</a></span><span style="font-size:13px; ">).<br /><br /></span><span style="font-size:13px; "><a href="http://clojure.sourceforge.net/reference/sequences.html" rel="self">Sequences in clojure</a></span><span style="font-size:13px; "> are lazy like stream as described in the video, and if you watch the whole lecture, you will see a rich stream library built up that is present in clojure. <br /><br />This means that many operations can be incredibly efficient, but seem like they are operating on gigantic data structures at once (since my day job is image processing, this is especially intriguing to me -- maybe more on that later). To really see this, you need to play with infinite sequences. In clojure, it's easy to create an infinite sequence with cycle -- (cycle [1 2 3]) returns a sequence which continuously yields 1, 2, 3, 1, 2, 3, etc. If you use sequence operations on it, you can get a feel for how laziness works because it's obvious that these functions could not possibly be consuming the whole sequence.  Here's a clojure Repl script.<br /><br /></span><span style="font:13px Courier, mono; ">user=> (def cyc123 (cycle [1 2 3]))<br />#&lt;Var: user/cyc123&gt;<br />user=> (every? (fn [x] (= 1 x)) cyc123)                       <br />false<br />user=> (take 5 cyc123)<br />(1 2 3 1 2)<br />user=> (take 5 (filter (fn [x] (= x 1)) cyc123))<br />(1 1 1 1 1)<br />user=> (take 5 (drop 1 cyc123))<br />(2 3 1 2 3)<br />user=> (take 5 (interleave cyc123 cyc123))<br />(1 1 2 2 3)<br />user=> (take 5 (take-nth 3 cyc123))<br />(1 1 1 1 1)<br />user=> (take 10 (for [x cyc123] (< x 3) x))<br />(1 2 1 2 1 2 1 2 1 2)<br />user=> (mapcat (fn [x y] (list x y)) cyc123 '(10 11 12))<br />(1 10 2 11 3 12)<br /></span><span style="font-size:13px; "><br />More on this tomorrow.<br /></span>]]></content:encoded></item><item><title>20 Days of Clojure: Day 8</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-08T11:14:55-05:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-8.html#unique-entry-id-45</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-8.html#unique-entry-id-45</guid><content:encoded><![CDATA[<span style="font-size:13px; ">Rich Hickey comments on </span><span style="font-size:13px; "><a href="files/20-Days-of-Clojure-Day-7.html" rel="self" title="Blog:20 Days of Clojure: Day 7">yesterday's post</a></span><span style="font-size:13px; "> on the </span><span style="font-size:13px; "><a href="http://groups.google.com/group/clojure/browse_frm/thread/52d11c95895af4df" rel="self">clojure google group</a></span><span style="font-size:13px; ">:<br /></span><blockquote><p>An interesting point of comparison with purely-functional solutions such as that which Okasaki writes about is that the Clojure vector can be log<sub>32</sub>N specifically because I can use mutation - when cloning, i.e. the tree is log<sub>32</sub>N deep and a node can be copied in O(1) time due to arraycopy + indexed array set. And yes, the vectors are optimized for lookup - in practice, log<sub>32</sub>N blows logN out of the water.</p></blockquote><span style="font-size:13px; ">Exactly -- PersistentVector's internals are not purely functional. New objects (during construction only) make an array copy of Object arrays from existing vectors, then make changes to the copied array before returning. As Rich points out, this makes some operations O(1). And for comparison, a clojure vector of 32k elements is only 2 levels deep, but if implemented with a log</span><sub>2</sub><span style="font-size:13px; ">N structure would be 15 deep (with average access in maybe 7 operations). Since access by index is a very common operation, you can see that this would make a big difference.<br /><br />Today, I want to look at PersistentHashMap.java which is the implementation of clojure hash-maps. There are actually four implementations of the map interface (hash-map, sorted-map, struct-map, and array-map), but hash-map is what the reader will give you for when you build up a map with {}.<br /><br />A comment on the top of the source file says that it's a persistent version of </span><span style="font-size:13px; "><a href="http://lampwww.epfl.ch/papers/idealhashtrees.pdf" rel="self">Phil Bagwell's Hash Array Mapped Trie</a></span><span style="font-size:13px; ">. According to that paper, it looks like the bits of hashes of the key are used to traverse a </span><span style="font-size:13px; "><a href="http://en.wikipedia.org/wiki/Trie" rel="self" title="Wikipedia entry on Trie">trie</a></span><span style="font-size:13px; ">. Let's see how it's done in clojure.<br /><br />The first thing I like to look at is how lookup is done, that gives me a sense of the structure. So, to start with, here's entryAt:<br /><br /></span>&nbsp;<span style="font:13px Courier, mono; ">public IMapEntry entryAt(Object key){<br /></span>&nbsp;&nbsp;<span style="font:13px Courier, mono; ">return root.find(RT.hash(key), key);<br /></span>&nbsp;<span style="font:13px Courier, mono; ">}<br /></span><span style="font-size:13px; "><br />Simple enough -- it uses the root node to start the traversal. The root node is an INode, and clojure uses polymorphism to get different behavior for different nodes. There are five types of nodes, </span><span style="font-size:13px; font-weight:bold; ">EmptyNode</span><span style="font-size:13px; ">, </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; ">, </span><span style="font-size:13px; font-weight:bold; ">HashCollisionNode</span><span style="font-size:13px; ">, </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNode</span><span style="font-size:13px; ">, and </span><span style="font-size:13px; font-weight:bold; ">FullNode</span><span style="font-size:13px; ">. An empty hash-map has its root set to an instance of </span><span style="font-size:13px; font-weight:bold; ">EmptyNode</span><span style="font-size:13px; ">, which is an implementation of the </span><span style="font-size:13px; "><a href="http://www.cs.oberlin.edu/~jwalker/nullObjPattern/" rel="self">Null Object design pattern</a></span><span style="font-size:13px; ">, and find() on it returns null (by the way, the entire structure is a </span><span style="font-size:13px; "><a href="http://en.wikipedia.org/wiki/Composite_pattern" rel="self">Composite design pattern</a></span><span style="font-size:13px; ">, and by using each node type to handle part of traversal, I guess he's also using a </span><span style="font-size:13px; "><a href="http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern" rel="self">Chain of Responsibility</a></span><span style="font-size:13px; "> -- I mention this because there are many OO design patterns used in the Java implementation of clojure -- some by name, so if you are going to look at the code, you'll want to be familiar with these </span><span style="font-size:13px; "><a href="http://en.wikipedia.org/wiki/Design_Patterns" rel="self">patterns</a></span><span style="font-size:13px; ">). <br /><br />If I assoc() onto a hash-map, root.assoc() is called which is supposed to return a new root. EmptyNode.assoc() simply creates a </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; "> with the hash, key and value, so a map with one key has its root set to a </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; ">.<br /><br />If I call find() on a </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; ">, that's pretty simple too.  If this is the node I am looking for, it returns this, otherwise null.<br /><br />If I assoc onto a </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; ">, here is what can happen:<br /></span><ol class="arabic-numbers"><li><span style="font-size:13px; ">If the key and value match the </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; ">, then it is just returned -- nothing is created since you haven't changed anything.</span></li><li><span style="font-size:13px; ">If just the key is the same as this node, a new </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; "> is created with the same key and the new value and returned (remember, this is a persistent data-structure, so we do not change the </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; ">, we create new nodes to build up a new hash-map). </span></li><li><span style="font-size:13px; ">If the hash is the same, but the key is different, then a new </span><span style="font-size:13px; font-weight:bold; ">HashCollisionNode</span><span style="font-size:13px; "> is created that holds onto this </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; "> and a new one that has the same key and the new value. </span><span style="font-size:13px; font-weight:bold; ">HashCollisionNodes</span><span style="font-size:13px; "> simply hold onto an array of </span><span style="font-size:13px; font-weight:bold; ">LeafNodes</span><span style="font-size:13px; "> whose keys have the same hash.</span></li><li><span style="font-size:13px; ">If the new key doesn't have the same hash (the common case), then a </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNode</span><span style="font-size:13px; "> will be created. At first it contains just the old </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; "> in its array of nodes, and then assoc() is called on it with the new key and value. </span></li></ol><span style="font-size:13px; ">Case #4 is where we start to build up the trie. The hash of the key is going to be used to traverse the trie later, and a </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNode</span><span style="font-size:13px; "> knows if it contains the </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; "> you are looking for. A </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNode</span><span style="font-size:13px; "> has a bitmap of 32 bits. The bit corresponding to the bottom five bits of the hash of the LeafNode is turned on to indicate that the </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; "> is to be found in this branch (it can have 32 branches). For example if the bottom five bits of the key hash are 01100, then the 12th bit of the bitmap is turned on, and the node is put at this branch.<br /><br />If the bitmap bit for the new leaf node is off, then a new </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNode</span><span style="font-size:13px; "> is created with a new bitmap with the new bit turned on, and the node is put in its array which will be one bigger than the one we started with. If we kept doing this and hit each bit, the 32nd assoc would result in a </span><span style="font-size:13px; font-weight:bold; ">FullNode</span><span style="font-size:13px; "> -- which is a simple case of a </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNode</span><span style="font-size:13px; ">, except it knows that if it had a bitmap, all bits would be set. Again, remember, we create a new node because we never change nodes in a persistent data structure.<br /><br />If the new leaf node has a bottom five bits that match a node already in this </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNode</span><span style="font-size:13px; "> (the corresponding bit is already turned on), then we assoc() this key/value onto the node with the same five bits. <br /><br />I haven't mentioned this yet, but assoc takes a shift argument -- so far, that has always been 0 and that meant to use the bottom five bits (use a 0 shift) of the hash in any </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNodes</span><span style="font-size:13px; ">. On this assoc call, we're now going to pass in (shift+5) so that the next 5 bits will be used on any </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNodes</span><span style="font-size:13px; "> created under here. Later when we traverse, we'll use the bottom five bits on the first </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNode</span><span style="font-size:13px; "> we see, then the next five bits on the next one and so on. When we've used up all 32 bits of the hash, we should be at our </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; "> or a </span><span style="font-size:13px; font-weight:bold; ">HashCollisionNode</span><span style="font-size:13px; "> (meaning all 32 bits matched).<br /><br />So here's find(): <br /></span><ol class="arabic-numbers"><li><span style="font-size:13px; ">If the node is an </span><span style="font-size:13px; font-weight:bold; ">EmptyNode</span><span style="font-size:13px; ">, it returns null. </span></li><li><span style="font-size:13px; ">If the node is a </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; ">, it returns itself if the key matches, otherwise null.</span></li><li><span style="font-size:13px; ">If the node is a </span><span style="font-size:13px; font-weight:bold; ">HashCollisionNode</span><span style="font-size:13px; ">, it calls find() on each </span><span style="font-size:13px; font-weight:bold; ">LeafNode</span><span style="font-size:13px; "> it holds, if any return themselves, then that is returned, otherwise null. </span></li><li><span style="font-size:13px; ">If the node is a </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNode</span><span style="font-size:13px; ">, it first checks to see if it has a node where the five bits being used matches and then calls find() on it, if it doesn't, it returns null. </span></li><li><span style="font-size:13px; ">If the node is a </span><span style="font-size:13px; font-weight:bold; ">FullNode</span><span style="font-size:13px; ">, it acts like a </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNode</span><span style="font-size:13px; "> with all bits of the bitmap set, so it just calls find() on the node corresponding the five bits in the hash at its level.</span></li></ol><span style="font-size:13px; "><br />The data structure is persistent because it never changes constructed nodes. During an assoc, all non-traversed paths will be shared, and only the path that changed will be new.<br /><br />I should also mention that </span><span style="font-size:13px; font-weight:bold; ">FullNode</span><span style="font-size:13px; "> is purely an optimization -- a </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNode</span><span style="font-size:13px; "> could be used in its place, but it saves some branch calls to have them sprinkled throughout the map. Also, using a bitmap in </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNode</span><span style="font-size:13px; "> is also an optimization. An INode array of 32 elements could have always been constructed with null signifying that there was no nodes in that branch, but that would be very wasteful in space, and so by using the bitmap,  </span><span style="font-size:13px; font-weight:bold; ">BitmapIndexedNodes</span><span style="font-size:13px; "> have an INode array of the exact length they need.</span>]]></content:encoded></item><item><title>20 Days of Clojure: Day 7</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-07T21:06:51-05:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-7.html#unique-entry-id-44</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-7.html#unique-entry-id-44</guid><content:encoded><![CDATA[Ok, I'm going to try to describe how vectors are implemented in clojure. The implementation is in:<br /><br />&nbsp;&nbsp;&nbsp;src/jvm/clojure/lang/PersistentVector.java <br /><br />if you want to see the code.  I'm going to try to go through the important bits here.<br /><br />The PersistentVector object has three members: two ints named cnt and shift, and an Object[] named root. A simple PersistentVector built from this line:<br /><br />&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(def v1 [1 2 3])</span><br /><br />would look like this (using curly braces for root because it is a Java array):<br /><br />&nbsp;&nbsp;&nbsp;<strong>cnt:</strong> 3<br />&nbsp;&nbsp;&nbsp;<strong>shift:</strong> 0<br />&nbsp;&nbsp;&nbsp;<strong>root:</strong> { 1, 2, 3 }<br /><br />cnt is the count of elements and (count v1) simply returns it and is O(1)  -- I'll explain shift later, and root is the object array. When shift is 0, this line:<br /><br />&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(nth v1 1)</span><br /><br />Just simply resolves to root[1], and returns 2, and in this simple case is also O(1). If I do this:<br /><br />&nbsp;&nbsp;&nbsp;(def v2 (assoc v1 1 4))<br /><br />which returns a new vector, but with the value at index 1 set to 4, you get another PersistentVector that looks like this:<br /><br />&nbsp;&nbsp;&nbsp;<strong>cnt:</strong> 3<br />&nbsp;&nbsp;&nbsp;<strong>shift:</strong> 0<br />&nbsp;&nbsp;&nbsp;<strong>root:</strong> { 1, 4, 3 }<br /><br />The 1 and the 3 are shared between the v1 and v3 array. If I do this:<br /><br />&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(conj v1 5)</span><br /><br />I'll get yet another PersistentVector that looks like this:<br /><br />&nbsp;&nbsp;&nbsp;<strong>cnt:</strong> 4<br />&nbsp;&nbsp;&nbsp;<strong>shift:</strong> 0<br />&nbsp;&nbsp;&nbsp;<strong>root:</strong> { 1, 2, 3, 5 }<br /><br />with the 1, 2, and 3 shared with v1 (and the 1 and 3 shared with v2). This is all very simple until, you conj onto a vector of 32 elements. When the root array has 32 elements, then adding one more element (33) returns a new PeristentVector that looks like this (assume the starting array had 1, 2, 3, ... 32)<br /><br />&nbsp;&nbsp;&nbsp;<strong>cnt:</strong> 33<br />&nbsp;&nbsp;&nbsp;<strong>shift:</strong> 5<br />&nbsp;&nbsp;&nbsp;<strong>root:</strong> { {1, 2, 3, ...,  32 }, { 33 } }<br /><br />Root is a Java Object[] with the 0th element set to the Object[] from the input vector to conj (not a clone, but the actual one), and the next element is an array of just the new value. If I conj onto that, I get a new Vector:<br /><br />&nbsp;&nbsp;&nbsp;<strong>cnt:</strong> 34<br />&nbsp;&nbsp;&nbsp;<strong>shift:</strong> 5<br />&nbsp;&nbsp;&nbsp;<strong>root:</strong> { {1, 2, 3, ...,  32 }, { 33, 34 } }<br /><br />Now, I can explain <strong>shift</strong>. When a method that takes an index is called, a bit shift is done on it to determine how many levels deep in the structure we need to go to get to the part of the datastructure that has the element at that index. For instance, here is the main Java for nth(i):<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">Object[] arr = root;<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">for(int level = shift; level > 0; level -= 5)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">arr = (Object[]) arr[(i >>> level) & 0x01f];<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">return arr[i & 0x01f];</span><br /><br />so, when i < 32, then i >>> level is 0, and arr will be root[0] (the array of 32 elements). Then we return arr[i & 0x01f] (which is i % 32), to get the ith element in that array. <br /><br />When i == 32, then (i >>> level) is 1, arr is root[1], and then we return arr[i%32] which is the 0th element. Now, if I do an assoc to set the 0th element to 100, I get this PersistentVector:<br /><br />&nbsp;&nbsp;&nbsp;<strong>cnt:</strong> 34<br />&nbsp;&nbsp;&nbsp;<strong>shift:</strong> 5<br />&nbsp;&nbsp;&nbsp;<strong>root:</strong> { {100, 2, 3, 4, ...,  32 }, { 33, 34 } }<br /><br />assoc calls a recursive function (doAssoc). First it clones root so that the new root is an array of two elements, each an object array. Then it determines that index 0 is in the 0th branch and does an assoc on that, decrementing the shift by 5 and setting the index to (index % 32). This recursive call clones the array at root[0]. Since shift is now 0, it is at the base case of the recursion, and so it sets root[0][0] to 100. All of the numbers and the entire array at root[1] is shared with the starting vector.  Here is the Java code for that (doAssoc is called with the arguments shift, root, the index, and the new value) -- the return is the new root:<br /><br />&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">private static Object[] doAssoc(int level, Object[] arr, int i, Object val){<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">Object[] ret = arr.clone();<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">if(level == 0)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">{<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">ret[i & 0x01f] = val;<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">}<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">else<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">{<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">int subidx = (i >>> level) & 0x01f;<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">ret[subidx] = doAssoc(level - 5, (Object[]) arr[subidx], i, val);</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">}<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">return ret;<br /></span>&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">}</span><br /><br />By now, you might have realized that we are building up a tree.  If I keep conjing on numbers in order starting at 1, eventually I will conj on 65. If so, I get this<br /><br />&nbsp;&nbsp;&nbsp;<strong>cnt:</strong> 65<br />&nbsp;&nbsp;&nbsp;<strong>shift:</strong> 5<br />&nbsp;&nbsp;&nbsp;<strong>root:</strong> { {1, 2, 3, 4, ...,  32 },  { 33, 34, 35, ... 64 }, { 65 } }<br /><br />Graphically, it looks like this (for 1..32 elements)<br /><br /><span style="font:12px Courier, mono; ">+-------------+<br />|{1,2,3,...32}|<br />+-------------+</span><br /><br />and then for up to 32<sup>2</sup>, it looks like this:<br /><br /><span style="font:12px Courier, mono; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+-------+<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;{&nbsp;&nbsp;&nbsp;}&nbsp;|<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+--+-+--+<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;|<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+--------+&nbsp;+----------+<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|<br />+------+------+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+--------+-------+<br />|{1,2,3,...32}|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|{33,34,35,...64}|<br />+-------------+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+----------------+</span><br /><br />And, we get another level at 32<sup>2</sup>+1 (1025) elements, and another at 32k+1. This explains how index access is O(log<sub>32</sub>N). Another important point, is that not only is a PersistentVector immutable from an interface perspective, but it's internally immutable, and each individual Object[] that makes up the tree is never changed in a constructed vector. If it needs to change in a new vector, it is cloned and the changed clone is used in the new PersistentVector -- the benefit of this is that the vector never needs to lock. This may seem like a small point, but I believe that this is not a requirement of Persistence -- but this extra work makes vector very suitable for concurrent applications.<br /><br />This implementation is different from the one described in the Okasaki paper from <a href="files/20-Days-of-Clojure-Day-6.html" rel="self" title="Blog:20 Days of Clojure: Day 6">yesterday</a>. Rich appears to be trading away O(1) consing for better index performance, which was O(log<sub>2</sub>N) in that paper.<br /><br />(<a href="http://wmassdevs.com/wordpress/2008/02/28/clojure-talk-with-rich-hickey-on-march-20/" rel="self" title="Rich Hickey in Northampton">clojure day (March 20) in Northampton, MA is coming soon</a>)<br /><br />Update: Made a few corrections]]></content:encoded></item><item><title>20 Days of Clojure: Day 6</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-06T00:19:51-05:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-6.html#unique-entry-id-43</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-6.html#unique-entry-id-43</guid><content:encoded><![CDATA[So far, I've been working on the "clojure is a dialect of Lisp" part of the description of clojure. One of the things Lisps allow is purely functional programming, but to enforce it you need immutable datatypes. One of the things that bothered me as a C programmer, is that I could never understand how it worked for anything more complicated than a string.  Then I saw persistent data-structures. Their existence in clojure is what made me think that purely functional programming could work in real programs.<br /><br />The <a href="http://clojure.sourceforge.net/reference/data_structures.html" rel="self" title="Clojure Data Structures">clojure page on data structures,</a> but more importantly check out <a href="http://clojure.blip.tv/#714056" rel="self" title="Clojure TV on Data Structures">clojure TV on collections</a>.<br /><br />If you're interested in how these data structures are implemented, you want to read Okasaki. I would start here with his description of a <a href="http://citeseer.ist.psu.edu/okasaki95purely.html" rel="self">persistent random access list</a>, which I think is one of the more accessible.<br /><br />Tomorrow, I'm going to try to expand on this by describing how vectors are implemented in clojure.]]></content:encoded></item><item><title>20 Days of Clojure: Day 5</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-05T07:04:31-05:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-5.html#unique-entry-id-42</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-5.html#unique-entry-id-42</guid><content:encoded><![CDATA[This SICP lecture shows some of the power of Lisp by building a symbolic differentiator in a very small amount of code, and is also the first one to introduce the idea that code is data and data is code (watch from the beginning to 29:00 -- at 13:00 he starts talking about using Lisp as the data format):<br /><br /><embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=5571878527594356342&hl=en" flashvars=""> </embed><br /><br />Here is the first symbolic differentiator he writes (but, in clojure)<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn atom? [x]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(not (instance? clojure.lang.IPersistentCollection x)))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn const? [expr val] (and (atom? expr) (not= expr val)))<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn same-var? [expr val] (and (atom? expr) (= expr val)))<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn arith? [expr sym] (and (not (atom? expr)) (= (first expr) sym)))<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn sum? [expr] (arith? expr '+))<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn prod? [expr] (arith? expr '*))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn a1 [expr] (frest expr))<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn a2 [expr] (first (rrest expr)))<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn m1 [expr] (frest expr))<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn m2 [expr] (first (rrest expr)))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn deriv [expr val] (<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">cond <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(const? expr val) 0 <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(same-var? expr val) 1<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(sum? expr) <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(list '+ (deriv (a1 expr) val) (deriv (a2 expr) val))<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(prod? expr) <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(list '+ (list '* (m1 expr) (deriv (m2 expr) val)) <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(list '* (m2 expr) (deriv (m1 expr) val))<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(:else) nil<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">)<br /><br /></span>The things to note:<br /><ol class="arabic-numbers"><li>Quote syntax, which uses '</li><li>first and rest which are car and cdr.  Clojure also supports combinations like frest (cadr) and rrest (cddr)</li><li>In clojure, extra parens are usually not necessary -- look at how cond is implemented to use variable arguments rather than a single list argument (which would require extra parens)</li><li>My first usage of Java interop (using instance? to find out if an expression is atomic or a collection)</li></ol>]]></content:encoded></item><item><title>20 Days of Clojure: Day 4</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-04T13:31:47-05:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-4.html#unique-entry-id-41</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-4.html#unique-entry-id-41</guid><content:encoded><![CDATA[Continuing from yesterday, we made our first <a href="files/20-Days-of-Clojure-Day-3.html" rel="self" title="Blog:20 Days of Clojure: Day 3">function that takes a function and returns a function</a>. Go to 43:00 in this video to see why (you can stop at 54:00):<br /><br /><embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=-3980624564484146912&hl=en" flashvars=""> </embed><br /><br />In this example, we take the first-order functions idea further and define a derivative function (that takes and returns functions) and a newton function (which takes a function and uses fixed-point).  Here it is in clojure (you can find fixed-point and other functions in yesterday's post):<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn square [x] (* x x))<br /></span><span style="font:12px Courier, mono; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn deriv [f]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(let [dx 0.000001]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(fn [x] (/ (- (f (+ x dx)) (f x)) dx))))<br /></span><span style="font:12px Courier, mono; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn newton [f guess]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(let [df (deriv f)]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(fixed-point (fn [x] (- x (/ (f x) (df x)))) guess)))<br /></span><span style="font:12px Courier, mono; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn sqrt [x] (newton (fn [y] (- x (square y))) 1))</span><br /><br />(<a href="http://wmassdevs.com/wordpress/2008/02/28/clojure-talk-with-rich-hickey-on-march-20/" rel="self" title="Rich Hickey in Northampton">clojure day (March 20) in Northampton, MA is coming soon</a>)]]></content:encoded></item><item><title>20 Days of Clojure: Day 3</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-03T12:53:51-05:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-3.html#unique-entry-id-40</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-3.html#unique-entry-id-40</guid><content:encoded><![CDATA[Ok, so first we made <a href="files/20-Days-of-Clojure-Day-1.html" rel="self" title="Blog:20 Days of Clojure: Day 1">a very hard-coded sqrt</a>, then we improved it by adding <a href="files/20-Days-of-Clojure-Day-2.html" rel="self" title="Blog:20 Days of Clojure: Day 2">the concept of fixed points</a>, which made the algorithm easier to see in the code. Here's how we can improve it even further. Go to 32:30 in this video (stop at the break at 43:00):<br /><br /><embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=-3980624564484146912&hl=en" flashvars=""> </embed><br /><br />First we define an average-damp function. This function takes a function as its argument and returns another function which takes a number and returns the average of that number and result of calling the function argument on the number.  It's easier to say that in clojure:<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn average-damp [f]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(fn [x] (average x (f x))))<br /></span><br />f is a function, average-damp creates a new function that takes x (a number) and that new function averages x and f(x).<br /><br />Now that I have that, I define sqrt like this (you can find <a href="files/20-Days-of-Clojure-Day-2.html" rel="self" title="Blog:20 Days of Clojure: Day 2">average and fixed-point in yesterday's post</a>):<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn sqrt [x] (fixed-point (average-damp (fn [y] (/ x y))) 1))</span><br /><br />Tomorrow, we'll use this to implement another way of finding sqrt.]]></content:encoded></item><item><title>20 Days of Clojure: Day 2</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-02T12:04:27-05:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-2.html#unique-entry-id-39</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-2.html#unique-entry-id-39</guid><content:encoded><![CDATA[<a href="files/20-Days-of-Clojure-Day-1.html" rel="self" title="Blog:20 Days of Clojure: Day 1">Yesterday</a>, we built a simple sqrt using Heron of Alexandria's algorithm. The problem with the code is that it had some generically useful ideas, but it was all hard-coded to only work with sqrt. Also, it was not clear what exactly the algorithm was.<br /><br />In the SICP lecture they fix it by passing functions around since Lisp treats functions as first-class data-types. Here's the fix explained (23:30 in the video, stop at 32:30):<br /><br /><embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=-3980624564484146912&hl=en" flashvars=""> </embed><br /><br />See the video for the Scheme implementation. Here it is in clojure.<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn sum ([] 0)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">([x] x)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">([x & more] (+ x (apply sum more) )))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn average<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">([] 0)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">([x] x)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">([x & more]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(/ (+ x (apply sum more)) (+ 1 (count more)))))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn abs [x] (if (< x 0) (- x) x))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn close-enough? [u v]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(let [tolerance 0.00001]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(< (abs (- u v)) tolerance)))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn fixed-point [f start]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(loop [old start new (f start)]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(if (close-enough? old new)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">new<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(recur new (f new)))))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn sqrt [x] (fixed-point (fn [y] (average (/ x y) y)) 1 ))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(prn (sqrt 9))<br /></span><br />A fixed point of a function f is a value y such that f(y) = y. This improved code, which implements the same algorithm as before, uses a generic fixed-point function that takes a function and a guess and tries to find the fixed point by setting the next guess to f(guess). You can see it all in the video. The things in clojure you need to know:<br /><br /><ol class="arabic-numbers"><li>fn [arg1 arg2] is lambda (&lambda;), so where fixed-point takes a function as the first argument, we create one on the fly to be (average (/ x y) y) where x is the argument to sqrt (closed around our function) and y is the argument defined by fn.</li><li>Since clojure compiles to Java and uses Java calling conventions, it does not have automatic tail-recursion. Instead, you use the recur special operator which will create a constant space loop back to the last loop or function definition.  <a href="http://clojure.sourceforge.net/features/functional_programming.html" rel="self">Loop/recur is explained on the Functional Programming page</a> of the clojure site (at the bottom).</li></ol>But, even this can be improved, as we'll see tomorrow.<br />]]></content:encoded></item><item><title>20 Days of Clojure: Day 1</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-03-01T10:11:57-05:00</dc:date><link>http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-1.html#unique-entry-id-38</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/20-Days-of-Clojure-Day-1.html#unique-entry-id-38</guid><content:encoded><![CDATA[Ok, this might be hard to keep up, but nothing ventured ...<br /><br /><a href="http://wmassdevs.com/wordpress/2008/02/28/clojure-talk-with-rich-hickey-on-march-20/" rel="self" title="Rich Hickey in Northampton">Rich Hickey is going to be coming to Northampton on March 20th to talk about clojure</a>. In preparation for that, I am going to blog every day in March about clojure -- then, hopefully we can post the results of his talk.<br /><br />If you don't have clojure yet -- go get it. Start here to learn <a href="http://clojure.sourceforge.net/reference/getting_started.html" rel="self">basic clojure syntax</a> (there is also a download link). Clojure is trivial to get running and you definitely want to play with it a little to understand the rest of this. The quick description of clojure is that it's a dialect of Lisp that runs on the JVM. It has support for immutable and persistent data-structures and a concurrent programming model.<br /><br />Today I'm going to start with some of the work I did to learn clojure through translating SICP lectures. In the first lecture, the first interesting program starts at 56:20 in this video:<br /><br /><embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=5546836985338782440&hl=en" flashvars=""> </embed><br /><br />Starting there presumes you know some basic Lisp syntax. This is the program he eventually writes to calculate the square root (in Scheme):<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(define (improve guess x)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(average guess (/ x guess)))<br /></span><span style="font:12px Courier, mono; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(define (good-enough? guess x)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(< (abs (- (square guess) x)) .001))<br /></span><span style="font:12px Courier, mono; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(define (try guess x)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(if (good-enough? guess x)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">guess<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(try (improve guess x) x)))<br /></span><span style="font:12px Courier, mono; "><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(define (sqrt x) (try 1 x))</span><br /><br />I have left out implementations for square and average here. This code and the algorithm is explained in the video.  Here it is in clojure (I defined average and square -- you could just use Java, but I am trying to keep it all in clojure for now)<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn sum ([] 0)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">([x] x)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">([x & more] (+ x (apply sum more) )))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn average<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">([] 0)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">([x] x)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">([x & more]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(/ (+ x (apply sum more)) (+ 1 (count more)))))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn abs [x] (if (< x 0) (- x) x))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn square [x] (* x x))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn improve [guess x]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(average guess (/ x guess)))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn good-enough? [guess x]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(< (abs (- (square guess) x)) 0.001))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn try-sqrt [guess x]<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(if (good-enough? guess x)<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">guess<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(try-sqrt (improve guess x) x)))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(defn sqrt [x]  (try-sqrt 1 x))<br /><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">(prn (sqrt 9))<br /></span><br />If you take this and put it in a file named sqrt.clj, you can run it like this (after downloading clojure):<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">java -cp clojure.jar clojure.lang.Script sqrt.clj</span><br /><br />And it will print out:<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="font:12px Courier, mono; ">65537/21845</span><br /><br />Explanation of the differences between the two examples:<br /><ol class="arabic-numbers"><li>clojure uses defn to define functions and puts arguments in []</li><li>sum and average implementations show how clojure can overload on arity (number of arguments)</li><li>try is reserved for exception handling, so I renamed the try function to try-sqrt</li><li>the answer is in clojure's rational number format, since all of the computation was adding and averaging integers and rationals, the result is a rational number and clojure preserves that.</li></ol>Tomorrow, I'll improve this so to make it more generic.  A lot of what I used in this can is explained in the <a href="http://clojure.sourceforge.net/features/functional_programming.html" rel="self" title="Clojure Functional Programming">clojure Functional Programming support</a> page.]]></content:encoded></item><item><title>Book Review: Collective Intelligence&#x2c; Part I</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2008-01-16T18:38:24-05:00</dc:date><link>http://www.loufranco.com/blog/files/CollectiveIntelligencePart1.html#unique-entry-id-37</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/CollectiveIntelligencePart1.html#unique-entry-id-37</guid><content:encoded><![CDATA[<div class="image-right"><img class="imageStyle" alt="51AnWLR89xL._AA240_" src="http://www.loufranco.com/blog/files//page2_blog_entry37_1.jpg" width="240" height="240"/></div>I'm at about the halfway point in <a href="http://www.amazon.com/Programming-Collective-Intelligence-Building-Applications/dp/0596529325">Programming Collective Intelligence</a> by <a href="http://blog.kiwitobes.com/">Toby Segaran</a>, and even if the second half was blank, I'd be telling everyone to read this book. It's been a while since I've read a technical book this good.  It excels because of a combination of three factors:<br /><ol><li>Useful algorithms</li><li>Shown in real-life situations</li><li>With real data that you can get yourself</li></ol>The basic idea of the book is to show different ways of using user-generated datasets to extract information and make decisions. Each chapter tackles a different problem and shows a few ways to approach it.  All of the examples are in Python, but even if you don't know Python, you should be fine if you know any structured language. Practically every example either uses some dataset that he tells you how to get, or a public API that is free to access. <br /><br />This book is the first I've read that relies on the current maturity level of the web.  A few years ago, it wouldn't have been possible to provide so many relevant examples.  In a few years, the API's might be out-of-date -- but don't let that stop you -- the algorithms are timeless.<br /><br />The choice of Python is a good one for a few reasons.  Firstly, it's pretty clear to read even if you don't know it. Secondly, list-comprehensions are a pretty good way to think about data crunching (which these algorithms do a lot), and, finally, there are many freely available Python libraries to deal with the public APIs he uses.<br /><br />To give you an idea of Toby's approach, I'm going to run through my favorite chapter so far, the one on Optimization.  The quick description of the problem domain should be familiar to every programmer -- solve NP complete problems like the traveling salesperson.  Basically, Optimization techniques can be used in situations where (1) you can't analytically solve the problem (2) the solution set is too large to exhaustively search (3) you can express the cost of a possible solution such that better solutions have a lower cost than worse ones and (4) solutions that are "near" each other have similar values for cost.<br /><br />Toby's first example is how a family can choose flights so that they arrive at the same airport from all over the country at nearly the same time and depart for home a few days later at nearly the same time.  The cost function is a combination of the actual costs of the tickets and the difference in time between the earliest and latest flights. He goes on to provide a dataset of flights, describes setting up optimization problems in general, and shows the python code to implement the cost function. <br /><br />Then he introduces a few optimization algorithms, implements them, and discusses their relative strengths and weaknesses. The next section applies these techniques to real data on Kayak.com using the standard Python DOM parser, minidom. The final section shows how to apply these techniques to other problems like pairing students in a dorm and network visualization. Other chapters get a similarly exhaustive treatment.<br /><br />I really hope that this is the future of technical books.]]></content:encoded></item><item><title>Django&#x2c; WebFaction&#x2c; RapidWeaver and Christmas</title><dc:creator>Lou Franco</dc:creator><category>Personal</category><dc:date>2006-12-25T16:38:07-05:00</dc:date><link>http://www.loufranco.com/blog/files/DjangoWebFactionRapidWeaverXMas.html#unique-entry-id-35</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/DjangoWebFactionRapidWeaverXMas.html#unique-entry-id-35</guid><content:encoded><![CDATA[Atalasoft closes for the week between Christmas and New Year's, so I have time to catch up on some things I've been wanting to do -- namely move this site to <a href="http://www.webfaction.com?affiliate=loumf" rel="self">WebFaction</a>. I chose WebFaction once I decided that I wanted to learn Django (after reading this great <a href="http://jesusphreak.infogami.com/blog/why_django" rel="self">comparison of Django and Rails</a>). Following along the forums, there were more than a few good mentions of WebFaction as a good Django host.<br /><br />A couple of weeks into Django and a couple of days into WebFaction, so far, I'm pretty happy. It's Christmas today, so even though I had some questions for WebFaction, I really didn't expect them to get back to me -- so I was pretty happy that they do -- I guess I should expect 365 24x7 from an ISP, but I hadn't been getting it before.<br /><br />In addition to new hosting and a new webapp framework -- I've also settled on <a href="http://www.realmacsoftware.com/rapidweaver/" rel="self">RapidWeaver</a> for content management. I moved to Mac for home a couple of years ago, and I still hadn't found a good desktop CMS that was as good as CityDesk on the PC. RapidWeaver has beautiful themes built in, and a lot of the features that I've gotten used to.  The built-in blog is nice (would be nicer with an import from RSS), but it didn't take too long to transfer everything over.<br /><br />All of this work is to get this site going again.]]></content:encoded></item><item><title>API UI</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2006-03-15T12:44:16-05:00</dc:date><link>http://www.loufranco.com/blog/files/APIUI.html#unique-entry-id-34</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/APIUI.html#unique-entry-id-34</guid><content:encoded><![CDATA[I saw this article on <a href="http://www.reddit.com" rel="self">Reddit</a> that <a href="http://www.acmqueue.com/modules.php?name=Content&pa=showpage&pid=317&page=1" rel="self">applies principles from human-factors design to API design</a>. I think that's a great idea, but this suggestion sounds weird to me.<br /><blockquote><p>Now we could use progressive disclosure to help reduce the complexity of that JButton class: put the expert stuff in an object returned by a getExpertKnobs() method, and the graphics subsystem hooks in an object returned by a getIntegrationHooks() method, and you would be left with a button API that had just a handful of methods&mdash;the basic methods we all need.</p></blockquote> This brings the number of methods in a button from about 100 to about 10, which sounds great until you try to subclass. Overriding a method that's in the object returned by the getExpertKnobs method would be a little tricky. It's likely that you don't even know the real class of that object because those methods are likely to return references to interfaces, plus the component code is in charge of constructing the objects, so there would have to be a way plug into that too, so that your subclass is constructed. I agree that the interface would be much simpler for most users, but it is much more complex for subclassers.<br /><br />There's also the issue of what would be in the expertKnobs object. Some of the methods in JButton that would go in there actually come from JComponent or Component. In fact, as we progressively go down the class hierarchy, we are adding methods into the class that might belong together, which means that there is probably an expertKnobs class for each component class.&nbsp; But, the getExpertKnobs() method is probably introduced early in the hierarchy -- what does it return? Will I have to downcast to get to the button's expert knobs?<br /><br />I don't have a solution -- but these issues probably drove the design of JButton more than the idea that they just sloppily added a 100 methods without giving a thought to the complexity of the class.<br /><br />But the point is taken, and I like the idea of using hard won knowledge from other domains in software design.&nbsp;<br /><br />Another recent blog on this topic was Steve Hawley's <a href="http://atalasoft.com/cs/blogs/stevehawley/archive/2006/02/27/9590.aspx" rel="self">Principle of Least Astonishment</a><a href="http://atalasoft.com/cs/blogs/stevehawley/archive/2006/02/27/9590.aspx" rel="self">&nbsp;</a> essay -- the last topic, on layering APIs, has his approach to this problem.]]></content:encoded></item><item><title>UML Cheatsheet</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2006-02-25T12:39:40-05:00</dc:date><link>http://www.loufranco.com/blog/files/UMLCheatsheet.html#unique-entry-id-33</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/UMLCheatsheet.html#unique-entry-id-33</guid><content:encoded><![CDATA[Here is a download for a <a href="assets/cheatsheet.pdf" rel="self">UML Cheatsheet</a> I made for my UML presentation this week. It's released under this <a href="http://creativecommons.org/licenses/by-nc-sa/2.5/" rel="self">creative commons license</a>. When I'm done with the slides, I will post them here as well.<br /><br /><strong>Update</strong>: Here are the <a href="assets/umlintro.ppt" rel="self">slides</a>.]]></content:encoded></item><item><title>WinFX Generation</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2006-02-18T12:36:37-05:00</dc:date><link>http://www.loufranco.com/blog/files/WinFX.html#unique-entry-id-32</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/WinFX.html#unique-entry-id-32</guid><content:encoded><![CDATA[Been looking around WinFX and specifically XAML recently. Not sure I think writing XML is a great way to write a GUI (but billions of web pages clearly prove me wrong). My first impression is that one of the benefits is going to be automated creation of XAML -- not from tools, but from our own programs.<br /><br />It's been 10 years since HTML was a household word and I still feel like it's easier to hand write it than use a WYSIWYG tool. I also never really liked using dialog editors. Even good ones like Delphi have too many limitations, especially when it comes to layout. So, I'm not expecting much from the tool support for XAML. But since it's just a text format, it should be pretty easy to generate from a simpler format (or from models). I'm looking forward to playing around with that idea.]]></content:encoded></item><item><title>Presentation Links</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2006-02-11T12:32:31-05:00</dc:date><link>http://www.loufranco.com/blog/files/PresentationLinks.html#unique-entry-id-31</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/PresentationLinks.html#unique-entry-id-31</guid><content:encoded><![CDATA[Here are some links I've collected on giving presentations. I have been revisiting them lately in preparation for my talk next month.<br /><ul><li>Kathy Sierra, in her Creating Passionate Users blog, thinks that <a href="http://headrush.typepad.com/creating_passionate_users/2005/06/kill_your_prese.html" rel="self">we shouldn't have slides</a>. She also has this <a href="http://headrush.typepad.com/creating_passionate_users/2006/01/crash_course_in.html" rel="self">crash course on learning theory</a> (with pictures and a workbook).&nbsp;</li><li>Seth Godin has a free-book called <a href="http://www.sethgodin.com/freeprize/reallybad.html" rel="self">Really Bad PowerPoint</a>. First tip: "No more than six words on a slide. EVER"</li><li>Guy Kawasaki has the <a href="http://blog.guykawasaki.com/2005/12/the_102030_rule.html" rel="self">10/20/30 rule of PowerPoint</a>.</li><li>The <a href="http://www.beyondbullets.com/" rel="self">Beyond Bullets</a> blog has great tips.</li><li>The Zen master of presentations, <a href="http://presentationzen.blogs.com/presentationzen/2005/11/the_zen_estheti.html" rel="self">Steve Jobs, vs. Bill Gates</a>.</li></ul>]]></content:encoded></item><item><title>Apple XCode CPlusTest Port</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2006-02-09T12:04:35-05:00</dc:date><link>http://www.loufranco.com/blog/files/AppleCppUnitPort.html#unique-entry-id-30</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/AppleCppUnitPort.html#unique-entry-id-30</guid><content:encoded><![CDATA[C++ (and Cocoa) unit testing is built into XCode on OSX. I am writing an application right now that I plan to port to Windows. When I wanted to see how portable my code was, I decided to get all of the unit tests passed on Windows.<br /><br />Unfortunately, XCode does not use CppUnit, which is already portable. <a href="assets/CPlusTest.zip" rel="self">Here is some code</a> I wrote that you can use to run your XCode generated unit tests on Windows.]]></content:encoded></item><item><title>UML Primer at WMass Dot Net Users Group</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2006-02-09T12:01:16-05:00</dc:date><link>http://www.loufranco.com/blog/files/UMLatWMassDotNet.html#unique-entry-id-29</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/UMLatWMassDotNet.html#unique-entry-id-29</guid><content:encoded><![CDATA[I'll be giving the next presentation at the Western Mass Dot Net Users Group on the basics of UML.&nbsp;<a href="http://wmassdotnet.org/cs/blogs/events/archive/2006/02/09/38.aspx" rel="self">Details</a>:<br /><br /><strong>Presentation:<br /></strong>Get an introduction to the Unified Modeling Language. Learn the basics of Class, Sequence, and Use Case diagrams.&nbsp; The emphasis will be on UML sketching, but UML code generation will be discussed.<br /><br />This session will be platform independent.&nbsp; Please bring an open mind, your ideas, and experience to share with the group.&nbsp; Software developers, project managers, students and anyone interested in software development on any platform are encouraged to attend.<br /><br /><strong>Meeting Details:</strong><br />Tuesday, February 7 at 6:00 PM<br />Fazzi Associates, Inc.<br />243 King St. Suite 236, Northampton]]></content:encoded></item><item><title>Servware</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2006-02-06T11:58:36-05:00</dc:date><link>http://www.loufranco.com/blog/files/Servware.html#unique-entry-id-28</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/Servware.html#unique-entry-id-28</guid><content:encoded><![CDATA[Despite his rants against C++, <a href="http://opal.cabochon.com/~stevey/blog-rants/" rel="self">Stevey's Drunken Blog Rants</a>, is one of my favorite new blogs. Actually, since it hasn't been updated in about nine months, and was written mostly in 2004, it's not really new. It was rediscovered by social bookmarking sites (I found it on <a href="http://www.reddit.com/" rel="self">reddit.com</a>). I was reminded of it while thinking about resuscitating this site.<br /><br />There are a lot of great articles, but this one about <a href="http://opal.cabochon.com/~stevey/blog-rants/its-not-software.html" rel="self">server-side service software</a> is one of my favorites (given that I've spent a lot of time thinking about the subject). Another great article on this topic is Paul Graham's <em><a href="http://paulgraham.com/road.html" rel="self">The Other Road Ahead</a></em>.]]></content:encoded></item><item><title>Western Mass .Net Users Group</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2006-02-06T11:54:20-05:00</dc:date><link>http://www.loufranco.com/blog/files/WMassDotNet.html#unique-entry-id-27</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/WMassDotNet.html#unique-entry-id-27</guid><content:encoded><![CDATA[The old .NET SAPs is rechristened under the name: <a href="http://wmassdotnet.org/" rel="self">Western Mass .NET Users Group</a><a href="http://wmassdotnet.org/" rel="self">&nbsp;</a><a href="http://wmassdotnet.org/" rel="self"> (and a spiffy new website)</a>. Tomorrow's meeting topic is XML.<br /><br />Speaking of XML, I saw this recently: <a href="http://www.tbray.org/ongoing/When/200x/2006/01/08/No-New-XML-Languages" rel="self">Don't Invent XML Languages</a> by Tim Bray. In addition to pointing to this <a href="http://xml.coverpages.org/xmlApplications.html" rel="self">great list of XML Languages</a>, he introduces "The Big Five" XML Languages and where they should be used.<br /><br /><blockquote><p>Suppose you&rsquo;ve got an application where a markup language would be handy, and you&rsquo;re wisely resisting the temptation to build your own. What are you going to do, then?<br/><br/>The smartest thing to do would be to find a way to use one of the perfectly good markup languages that have been designed and debugged and have validators and authoring software and parsers and generators and all that other good stuff. Here&rsquo;s a radical idea: don&rsquo;t even think of making your own language until you&rsquo;re sure that you can&rsquo;t do the job using one of the Big Five: XHTML, DocBook, ODF, UBL, and Atom.</p></blockquote>]]></content:encoded></item><item><title>The Chameleon Developer</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2004-03-14T11:50:12-05:00</dc:date><link>http://www.loufranco.com/blog/files/TheChameleonDeveloper.html#unique-entry-id-26</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/TheChameleonDeveloper.html#unique-entry-id-26</guid><content:encoded><![CDATA[<div class="image-right"><img class="imageStyle" alt="Chameleon camouflaged against some leaves" src="http://www.loufranco.com/blog/files//page2_blog_entry26_1.jpg" width="218" height="188"/></div><br />I was talking to a developer friend today about developers who don't&nbsp;get the importance of communication. Look through any want-ads for developers and right after the alphabet soup of technologies you have to know is the same requirement&mdash;must have &ldquo;excellent written and verbal communication skills&rdquo;. You'd be hard pressed to find a programmer job without those requirements.<br /><br />But, expressing yourself clearly is only half of communication. The more important aspect is listening to and understanding other people. This is what separates good and great developers. And the most important skill in understanding is empathy&mdash;not only understanding someone, but being able to&nbsp;be them.<br /><br />To be a successful developer, you have to learn more than just the software aspect of your business. Developers who understand this are more valuable and valued more. If you want to do this effectively, you have to think like a saleperson, a marketer, a CEO, your customers, the press, etc. You have to think like them because they aren't all going to try to think like you (but if they're going to try, by all means, help them).<br /><br />Another part of empathy is thinking about how they will interpret your words. For instance, avoid rhetorical tics like &ldquo;think about it&rdquo; and &ldquo;you're not seeing the big picture&rdquo;. Not only are they annoying, but it puts people on the defensive.<br /><br />If you understand&mdash;not only what someone is saying, but why&mdash;you'll have a much better chance of expressing yourself. So, the next time you have a communication gap with a non-developer, stop talking and ask yourself how much you even understand about them.]]></content:encoded></item><item><title>Software Addins as a Business</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2004-03-12T11:46:11-05:00</dc:date><link>http://www.loufranco.com/blog/files/SoftwareAddinsasaBusiness.html#unique-entry-id-25</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/SoftwareAddinsasaBusiness.html#unique-entry-id-25</guid><content:encoded><![CDATA[<div class="image-right"><a href="http://www.spheresoft.com/highlighter/" rel="self"><img class="imageStyle" alt="Animated demo of Highlighter" src="http://www.loufranco.com/blog/files//page2_blog_entry25_1.gif" width="280" height="200"/></a></div>I've written a couple of add-ins for CityDesk, and of course, it's tempting to think about trying to make some money off of them. I'm already a partner in another add-ins business (<a href="http://www.spheresoft.com/" rel="self">Spheresoft</a>, which makes MS Office addins -- check out <a href="http://www.spheresoft.com/highlighter/" rel="self">Highlighter</a> if you view realtime data in Excel), so I know a little about what goes into the development, support, marketing, sales, licensing, etc. of doing this kind of thing.<br /><br />Add-ins are a great business for small companies, because they are much easier to implement than a full-blown product and the software you are adding on to will help with marketing. Another add-in that Spheresoft will be publishing soon, <a href="http://www.spheresoft.com/modeler" rel="self">changes the nature of spreadsheet modeling</a>, but we'd never want to compete with Excel on thousands of other tiny features, so it makes more sense for this to be an add-in.<br /><br />But, with any business, you have to decide how much effort you are willing to put in and what you are expecting back. The market of people who have Office is huge and I have a reasonable expectation of making a good enough return on Office add-ins to warrant the time spent writing and supporting them.&nbsp;<br /><br />I'm not sure I can do the same for add-ins for Fogcreek products. I'm glad that people find them useful, and I do them because I need them as well. I would never make enough money to recoup support costs I would have to undertake if I charged for them.<br /><br />So, they will remain free and pretty much unsupported for now.]]></content:encoded></item><item><title>Code Generation from Requirements</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2004-03-12T11:24:42-05:00</dc:date><link>http://www.loufranco.com/blog/files/CodeGenerationfromRequire.html#unique-entry-id-24</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/CodeGenerationfromRequire.html#unique-entry-id-24</guid><content:encoded><![CDATA[This article on <a href="http://martinfowler.com/bliki/DomainSpecificLanguage.html" rel="self">Domain Specific Languages from Martin Fowler</a> got me thinking on code generation in general. A project I'm working on right now has many opportunities for generating code&mdash;not from a domain specific language, but from the requirements documentation and other source information.<br /><br />I am working on a project now with a very detailed requirements document. For instance, practically all of the strings shown to the user are in there. Since the project uses string resources, there's no reason to manually copy and paste&mdash;the string resource is easily generated from the document.<br /><br />The project also has a lot of images.&nbsp; And images are associated with each other (some images are the small version of another, or meant to be the same object but from another angle). Luckily, the artist used a directory and naming convention that makes these associations easy to figure out, and generate an XML resource descriptor and object building code from.<br /><br />Tying together this information (each image has a string descriptor) is also easy, given the detail in the document and the naming convention.<br /><br />Actual logical code generation is almost possible as well, as the project is a kind of complicated finite state machine. In this case the document doesn't go far enough to be a source. A Domain Specific Language might help here, and leaves me with source that might be able to be maintained more easily that a Java representation.<br /><br />This brings up a good lesson for those writing requirements, even if you don't know how to program. If you follow conventions closely, it might be a good starting point for generating code.]]></content:encoded></item><item><title>Furl</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2004-03-09T11:22:14-05:00</dc:date><link>http://www.loufranco.com/blog/files/Furl.html#unique-entry-id-23</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/Furl.html#unique-entry-id-23</guid><content:encoded><![CDATA[I've been playing around with the <a href="http://www.furl.net" rel="self">Furl Beta</a>. It's looking pretty good. Essentially, it's a personal online cache of web pages that you can view, search, and share. Better than bookmarking because you can search the contents of it later, and it's accessible from any browser. You can even provide RSS feeds to your Furl content.&nbsp; Collaborative research on the internet just got a whole lot easier.<br /><br />One drawback, there does not appear to be any way to make the cache private, so I can't possibly use it, because there is no way I want a public database of every site I find interesting or useful. I definitely would share some of it with some people, but totally public is not an option.<br /><br />Perhaps there will be a pay service for a private space.<br /><br /><strong>Update</strong>: My bad. Just mark the item private when you Furl it.&nbsp; Duh.&nbsp; Didn't see that on the Furl dialog and still don't see how to make a public item private (except to change its topic to personal, but then I lose the utility of topics). Anyway, now the biggest drawback is that the first dialog comes up at the wrong size and there is no scrollbar, so you can't see the buttons.]]></content:encoded></item><item><title>Informational Integrity</title><dc:creator>Lou Franco</dc:creator><category>Software Development</category><dc:date>2004-02-21T11:14:40-05:00</dc:date><link>http://www.loufranco.com/blog/files/InformationalIntegrity.html#unique-entry-id-22</link><guid isPermaLink="true">http://www.loufranco.com/blog/files/InformationalIntegrity.html#unique-entry-id-22</guid><content:encoded><![CDATA[<blockquote><p>Any program that accepts user input will need to separate good input from bad, and to make sure little of the latter gets recorded.</p></blockquote> So begins the <a href="http://c2.com/ppr/checks.html" rel="self">CHECKS Pattern Language of Informational Integrity</a> by Ward Cunningham. I read this article years ago and loved it&mdash;mostly because it jibed with my experience for writing usable business software. The theme of the language is to make sure that data entered by the user is correct, and accomplishes this with various kinds of validation and data representation techniques.<br /><br />I find that two of the patterns are invaluable for usable user interface