A swank growl
I ran into a frustrating puzzle while learning about agents in Clojure. The problem was with a tool, not Clojure itself but a few neat things shook out along the way.
Agents in Clojure are a high level concept for concurrency. They are intended for independent, asynchronous data munging. They're pretty easy to use... basically a value is associated with an agent when it is created, and then when a function is applied to the agent the result becomes its new value.
Here is the definition of a banana agent:
Here is the function which produces its new value:
(let [input berry, output (str berry "na")]
(println (format "input:output %s:%s" input output))
output))
Here it is in action:
(await (send banana tally-man))
@banana
"bana"
user> (await (send banana tally-man))
@banana
"banana"
The debug statements are missing! Code invoked by (send) doesn't yield output to the REPL -- it went to a different buffer.
The major symptom of my pernicious problem was that my precious debug statements weren't printing anything. This was a major problem since that was the only way I had to know what was happening. At this point it's important to disclose that I am an unrepentant GNU/Emacs user. This leads to using swank-clojure since a symbiotic REPL is really, really cool.
After much confusion I finally found my precious debug statements! They were hiding in the rarely-used inferior-lisp buffer. It's awkward for me to keep this buffer open so I looked for something... cooler.
Growl is a MacOS X utility which displays notifications in self-dismissing windows. The gap between Clojure and Growl is little more than implementing java.io.Writer. Clojure actually makes it easier than it should be.
(.exec (Runtime/getRuntime)
(str "/path/to/growlnotify -a emacs -m " msg)))
(def growler
(proxy
[java.io.Writer] []
(flush [] )
(write [text] (growl text))))
It's worth taking a close look at the proxy statement, specifically the write method. It is an abstract, overloaded method in Writer. Only one of those overloads is abstract, and it's NOT the one implemented above. The one implemented above is the only one actually called. Or, you know, quacked. That's neat! I didn't even have to implement all of the abstract methods! All I had to do is what I actually needed to do.
The last piece of the puzzle was making Clojure use the Growl bridge. The binding function made this happen with one line of code. out is the "variable" which Clojure uses for writing to standard out. A call to (binding) gives out a new value. and the old value is restored when (binding) exits. All of the compiled references to out within Clojure automatically use my binding. TOO COOL.
(let [input berry, output (str berry "na")]
(binding [*out* growler]
(println (format "input:output %s:%s" input output)))
output))
Ta DA!

My week of Clojure and Emacs
This week has been very busy and useful for me with Clojure and Emacs.
Clojure
- I continued to mature Pokerepl. The interfaces/defprotocols were reorganized into one file. Implementations/deftypes each have their own file.
- Switched Leiningen branch from stable to master (think 1.1 alpha) and chopped out all those :gen-class warts
- Lightly adopted @cemerick's very cool string interpolation macro -- it's the #{bar} in "foo #{bar}".
- Got Nailgun working to speed up unit tests.
- But @technomancy pointed out that I could use TONS less memory and have a better experience with clojure-test-mode. It runs instantly and highlights errors:

Emacs
- I~ stopped~ backup~ files~ from~ appearing~ everywhere~ (thanks to ESK for clueing me in to backup-directory-alist)
- Stopped avoiding color-theme finally. Using the rotor and late-night themes mostly, but will probably have to write my own at some point. It will be ugly, but such are my aesthetics.
- Now using and happy with the Inconsolata typeface.
- I installed but still shy away from paredit-mode. It's my own fault for not having watched this awesome preso yet.
Wrapping deftype factory methods
(Standard disclaimer: Clojure is a new hobby.)
I'm exploring the upcoming feature in Clojure called deftype. Similar to defining a class in Java, deftype is a way of grouping related data and methods. Actually, it's quite an improvement over the old (defstruct) way. defstruct only groups related data together.
As ever, code examples are based on my side project pokerepl. This entire post focuses on the suits and values in a deck of playing cards.
Here's the old way of doing things:
(defstruct Card :value :suit)
;; instantiate it
(def card (struct Card :ace :spades))
Here's my first baby step down the new path:
#^clojure.lang.Keyword suit]
Object
(toString []
(str (get value-map value "?")
(get suit-map suit "?"))))
(def card (Card :ace :spades))
It's longer, but it does more. We get optional type hinting for the data (seen here as #^clojure.lang.Keyword). The type being extended is listed (Object in this case). The override of Object.toString means all instances will be pretty printed automatically.
What you don't see is a "constructor". There's no need because Clojure provides reasonable defaults here; the values for "value" and "suit" are passed to new instances of Card.
Unfortunately that wasn't good enough. Fortunately Clojure is a very dynamic programming language, so I could easily replace the implicitly defined factory function with my own code. To summarize, a call to (deftype Foo [bar]) generates two factory functions (Foo [bar]) and (Foo [bar meta-map ext-map]). It's possible to overwrite these but unclear how to honor the postconditions... exactly what gets returned?
So rather than replace the factory function entirely, I decided to embrace and extend it!
(defn Card
([two-chars]
(apply Card (map (partial nth (seq two-chars)) [0 1])))
([value suit]
(let [value-sym (if (= clojure.lang.Keyword (class value))
value
(get (zipmap (vals value-map) (keys value-map)) value))
suit-sym (if (= clojure.lang.Keyword (class suit))
suit
(get (zipmap (vals suit-map) (keys suit-map)) suit))]
(old-Card value-sym suit-sym))))
So that's a bit of a mess, but the requirements were too. I needed syntactical sugar to make dealing with this code palatable. And now I have said sugar. The following is now true:
(Card "AS")
(Card \A \S))
My main gripe with (deftype) is that it complicates editor support. Previously, "defn" and a relatively small list of keywords meant a list was a function definition. Functions indent differently than other types of lists. deftype means the editor is going to have to understand the source better. But, it's a small and good problem to have IMO. Also hoping for destructuring support in vector for param arguments.
Presenting on Leiningen to Cap-Clug
I'm excited to present Leiningen to a Wash. DC metro area Clojure meetup, a.k.a. Cap-Clug. Leiningen is a Clojure-friendly build tool, and I've been very happy using it on Pokerepl.
In the presentation I am going to spend most of my time explaining how to build a minimal yet friendly jar file starting from scratch. Time permitting we will be able to cover more spiffy features like integration with Clojars.
The other scheduled presentation is going to be great; @Fogus is co-writing the next Clojure book and he will be presenting on features new in version 1.1.
If you are in the Washington DC metro area and have any sort of interest in Clojure, run don't walk to register -- the first meeting is Thurs Jan 14th, 2010. See you there!
Juggling versions of Clojure?
(Standard disclaimer: Clojure is a new hobby.)
EDIT: also try rake clojure:new:repl
I am looking forward to trying datatypes on Pokerepl. However, that means using the new branch of Clojure, and indirectly, more work on my part to clone/track/build/deploy the new branch.
Almost everyone would and should use Leiningen to experiment with the new branch. Simply update your project.clj something like this:
[org.clojure/clojure-contrib "1.1.0-new-SNAPSHOT"]]
At the moment though, updating ~/.clojure was beyond the ken of any automated tool I knew of. So I wrote a Rakefile and gave it a cheesy name... juggling Clojure... hmm... howzabout Clojuggle? Well... yeah... sorry for that.
Back to the point: automating thorough (~/.m2 and ~/.clojure) updates and deployments of various versions of Clojure. Supporting common tasks (wipe, clone, build, &c.) for several branches of several repositories means a thousand tasks will bloom and make the tool hard to use.
Ruby and the Rake API make it easy to define or show tasks only when relevant. I really enjoyed writing this code. Anyways, typing rake brings you to this:
Welcome! Try 'rake -T' for a list of useful tasks, or try
rake clojure:master:all clojure-contrib:master:all
The default task provides some basic usage. Let's take a look at the initial set of tasks:
rake clojure-contrib:master:all # Do everything for master branch of clojure-contrib
rake clojure-contrib:new:all # Do everything for new branch of clojure-contrib
rake clojure:master:all # Do everything for master branch of clojure
rake clojure:new:all # Do everything for new branch of clojure
rake default # HOWTO use this file
Fine, let's give it a whirl.
# oodles of lines omitted
ci-build:
BUILD SUCCESSFUL
Total time: 25 seconds
rm -f /.../.clojure/clojure.jar
ln -s /.../clojuggle/src/clojure-new/clojure.jar /.../.clojure/clojure.jar
real 0m55.306s
user 1m10.544s
sys 0m6.873s
Three subtle points here. First, the "ci-build" line is the clue that the local Maven2 repository was updated:
total 8
drwxr-xr-x 7 seths staff 238 Jan 3 17:57 1.1.0-new-SNAPSHOT
-rw-r--r-- 1 seths staff 322 Jan 3 17:57 maven-metadata-local.xml
Point #2: the ~/.clojure directory has a symlink pointing into the clojuggle/src/clojure-new directory.
total 8
lrwxr-xr-x 1 seths staff 70 Jan 3 17:57 clojure.jar -> /.../clojuggle/src/clojure-new/clojure.jar
Point #3: a few more commands have surfaced from the Rakefile (e.g. clojure:new:update)
# ... omitted
rake clojure:blast:it # Removes files for clojure from $HOME/.clojure and .m2
rake clojure:new:publish # Push build to local m2 repo and ~/.clojure
rake clojure:new:update # Update the new branch of clojure
So that's all well and good, but what if you wanted to support Technomancy's fork of Clojure? You would do two things. First this:
technomancy-clojure:
clone-from: git://github.com/technomancy/clojure.git
branches: [master, new]
use-jar: clojure.jar
And then something like this:
# ... omitted
rake technomancy-clojure:master:all # Do everything for master branch of technomancy-clojure
rake technomancy-clojure:new:all # Do everything for new branch of technomancy-clojure
DANGER!
Do NOT use this tool to develop on various versions of Clojure. The wipe task always deletes files, so eventually you will lose changes unintentionally. Eventually 'git status' will be used to prevent wiping changes but not yet.
Penultimately, despite all this I still don't have the new branch working with Slime/Swank. It doesn't help that the pom.xml of contrib on the new branch wants 1.1.0-alpha of Clojure. Minor nits though that can be worked out.
Finally, if anyone is still reading and cares about these sort of things, I picked Ruby, v3 of the GPL, and Mercurial/BitBucket because I have strong feelings about those things.
Please respond in some way if you have any questions!
tl;dr: Leiningen is great -- try using it with the new branch of Clojure!
Why Clojure is worth it to me
I was glad to see Rich Hickey ask the Clojure community to financially support his dedicated time and energy during 2010. Much better this than unraveling with very little warning and causing a thousand forks to bloom.
The asking price was very reasonable when you compare it to any large, respectable software development book. I have many of those (well, maybe not all 10 volumes of SOA in a nutshell)... but even my favorite books cover very narrow topics in great depth. That's it, just some dead trees waiting to be used as references for types of problems I don't run into often.
Clojure covers many topics in depth with consistent pragmatism. No time to bootstrap libraries? Use the plethora of .jar files out there. No time/focus to sustain a world-class virtual machine? Use Sun's! Not interested in churning out another keyword-heavy language grammar? Use Lisp... but thanks for cleaning it up first
The rest of the time can go writing a compiler sufficiently smart to avoid head holding by implementing fine-grained local clearing... add some high level concepts for concurrency via software transactional memory? Sure!
Another tangible reason I'm happy to pay real money is the Clojure community. It has been helpful in many ways. Every major editor has some level of support... the IRC channel is great. People are even building infrastructure (looking at you Leiningen and Clojars!)
Ultimately I'm just being selfish. Swearing at the REPL until enlightenment conks you on the head is a rush... it's a minor victory with a thousand similar battles on the horizon. I like supporting good projects and I'm looking forward to spending lots more time with Clojure.
The path of least resistance in Clojure
(Edit 2: Standard disclaimer: Clojure is a new hobby.)
Immutable data structures seem like an odd idea, but they are on the path of least resistance in Clojure. This is not the case in languages like Java. In this post I will rewrite a snippet of Java into Clojure and explain the major differences.
The snippet of Java comes from Martin Fowler's bliki posting on Fluent Interfaces. I picked this code snippet because it is concise, reads easily, and fills a common need.
customer.newOrder()
.with(6, "TAL")
.with(5, "HPK").skippable()
.with(3, "LGV")
.priorityRush();
}
This code clearly links an existing customer to a new, rush order of three items. Consider the "skippable" method... that is probably on the Order object yet works on an OrderLine. The Order code probably finds the newest OrderLine and updates its skippable property. Modifying objects is the path of least resistance in Java, or rather creating objects in an incomplete state and updating them until complete.
Modifying objects in Clojure is not on the path of least resistance. Many Clojure methods return a new, lazy version of the input. Working with return values like this encourages creating complete objects and then using them. Here's an example -- please temporarily ignore the unfamiliar functions and low level of abstraction.
(let [lines [(struct order-line 6, "TAL")
(struct-map order-line :quantity 5,
:code "HPK", :skippable? true)
(struct order-line 3, "LGV")]
order (struct-map order :lines lines,
:rush? true)]
(merge customer
{:orders (conj (get customer
:orders []) order)})))
- (struct foo 123 "abc") is basically calling the constructor for the foo class with two arguments.
- (struct-map foo :num 123, :text "abc") calls the constructor with named params rather than positional params
- (let) is how you define local variables, lines and order in the above example.
- (merge old-map new-map) creates a new map by merging existing maps
- (conj) creates a new collection from its arguments
- (get) returns the value for a key in a hash map, or a default value if specified.
It's not obvious but the return value is the output from (merge). That is the last statement in the (let) block, which is the only statement in the body of the (add-order) function. Values flowing like this is in the path of least resistance in Clojure.
Besides doing things in a different order than Java, there is one more very important but subtle difference. Let's say a customer wants two orders:
Some quick code to show how it works:
(def me (add-order (struct customer "seth")))
;; how many orders?
(count (me :orders)) ;; 1
;; I'd like another order, please...
(add-order me)
;; Hey, where'd that go?
(count (me :orders)) ;; 1
;; Is the function even working?
(count (:orders (add-order me))) ;; 2, so yes
The new order has been lost because the return value from the function was not captured. This is a silent runtime failure -- not a fun type of problem to chase down. One solution is to rebind "me":
(count (me :orders)) ;; 2
I've been a little dishonest here talking about "objects" in Clojure. As seen here they are little different than hash maps.
clojure.lang.PersistentArrayMap
example> (defstruct foo :bar)
#'example/foo
example> (. (struct foo) getClass)
clojure.lang.PersistentStructMap
For anyone who is interested, the entire source listing for this example follows. Thanks for reading!
Announcing Pokerepl
Standard disclaimer: Clojure is a new hobby.
Clojure code to perform a very little bit of Texas Hold'em has been uploaded to BitBucket. Fire up a REPL and paste in the contents of poker.clj, then try it out with something like:
BitBucket supports Mercurial. If it's installed, here's the clone URL: hg clone https://seths@bitbucket.org/seths/pokerepl/
Otherwise, here's a quick link to a bz2 of the source. This link will only ever get the first version of the code... so consider installing Mercurial or hitting the project page.
Much work remains to make this code interactive.
- A little more work on judging hands (identifies groups but not by suit)
- Add support for chips
- Add support for checking, calling, raising
- Add support for folding
- BIG: very simplistic logic for having CPU players bet/call/fold
- VERY BIG: Rewrite most of everything with agents and refs in order to...
- HUGE: Support network based play with real people
Lastly, this is my first foray into the functional fray so don't spare the code reviews. PLEASE. Fork it mercilessly!
More poker with Clojure: what’s in a hand?
Standard disclaimer: Clojure is a new hobby.
In a previous post I wrote some code to create and shuffle a deck of cards. This post demonstrates one way to figure out what a hand is worth according to basic 5 card draw rules.
The implementation is basically one switch statement from a C/Java/Ruby/Groovy/Python perspective. Ruby and Groovy coders will recognize that the return value of the function is the value of the last expression evaluated. So an if statement returns either the value of the true expression or the value of the false expression.
"good, 1 < 2 < 3"
"what? someone file a bug report!")
;; "good, 1 < 2 < 3"
When conditional logic returns values like this, there is no need to use a stack variable to store a return value.
A quick introduction to some of the functions used in the code:
- (if): Aside from returning its true/false expression result, no major difference than Java.
- (cond): Basically a switch statement, returning a value just like (if)
- (let): Defines and initializes "variables" which are immutable.
- #(): This defines an anonymous function. Believe this is a macro for (fn).
- #{}: This defines a set data structure; unordered and no duplicates. Also see the (set). function
- {}: This defines a map data structure, populated with unordered key/value pairs. Bonus: (map key) AND (key map) both return the value for key in a map.
With some minimal descriptions listed, let's dig into the code:
When judging a hand it's critical to know how many of each suit are present AND how many of each value are present. One way to model this is to build histograms for suit and value.
(take 5 (shuffle deck))
:suit
suits)
;; {:clubs 2, :diamonds 2, :spades 0, :hearts 1}
(build-property-histogram
(take 5 (shuffle deck))
:name
names)
;; {:queen 2, :king 1, :jack 0, :seven 1, :eight 0,
;; :six 0, :nine 0, :five 0, :ace 0, :ten 0, :three 1,
;; :two 0, :four 0}
The implementation behind that is not an easy read (probably because it's not great Clojure). Basically this code iterates over the pool of attributes (suits or values) and uses (conj) to aggregate maps into one big map. In the middle a filter function finds all cards matching the current property and then the total number is counted up.
(reduce
(fn [sieve value]
(conj sieve
{ value
(count (filter
(fn [card] (= value (card property-name)))
cards)) }))
{}
property-pool))
Only one more helper method to go. This method determines whether the hand is a straight, whether all of its cards have consecutive values. The approach taken was a little odd. An array of all possible straight values is built and then segmented into overlapping hands. For example, the first two arrays would be [ace, two, three, four, five] and [two, three, four, five, six]. Each of these possibilities is matched against the hand, using Clojure's facility to compare sets easily.
(some
(fn [straight] (= (set (map :name hand)) (set straight)))
(partition 5 1 [:ace, :two, :three, :four, :five, :six, :seven, :eight,
:nine, :ten, :jack, :queen, :king, :ace])))
(some) is a function which returns true when its filter function returns true for any of the collection items it examines.
After all that, here is the hand judging function. It's basically a few local "variables" and a switch statement. Some additional conditional logic appears inside the switch statement as well. This is required to determine whether a hand with three of a kind is a full house (are the remaining cards a pair?).
(let [suit-histo (build-property-histogram cards :suit suits)
value-histo (build-property-histogram cards :name names)
max-per-suit (apply max (vals suit-histo))
max-per-value (apply max (vals value-histo))]
(cond
(= 4 max-per-value)
:quads
(= 3 max-per-value)
(if (some #(= 2 %) (vals value-histo))
:full-house
:trips)
(= 2 max-per-value)
(if (< 1 (count (filter #(= 2 %) (vals value-histo))))
:two-pair
:pair)
(and (straight? cards) (> 5 max-per-suit))
:straight
(and (not (straight? cards)) (= 5 max-per-suit))
:flush
(= 5 max-per-suit)
(if (= #{:ace, :king, :queen, :jack, :ten} (set (map :name cards)))
:royal-flush
:straight-flush)
true
:high-card)))
In the near future all this code will be cleaned up and moved out to BitBucket. Ideally some basic chip tracking and logic would be added so you could actually play poker from a REPL. That's probably much more difficult than it seems, but hopefully this has been useful so far. Please don't hesitate to contact me (goof at foognostic dot net) with any questions!
Shuffling cards with Clojure
Standard disclaimer: Clojure is a new hobby.
One of my friends and coworkers has been gently cluesticking me into learning poker. Along the way he suggested I write a very simple hand generator to get a feel for how the number of players changes the relative strength of your hole cards. Shuffling cards is one small step along the way, and that seemed like a good place to start.
First, let's create a poker namespace to work in and then define some basic constants as keywords in sets.
(def suits #{:hearts, :diamonds, :clubs, :spades})
(def names #{:two, :three, :four, :five, :six,
:seven, :eight, :nine, :ten, :jack,
:queen, :king, :ace})
Next, let's "define a class." I'm quoting that because a struct in Clojure is just a map with some keys to be expected.
Next we actually need, you know, a full deck of cards to shuffle. This was harder than I expected. At first I tried two loops, first iterating over suits and then values. This resulted in a list of ... four lists of... thirteen cards.
(map (fn [suit]
(map (fn [name]
(struct card suit name))
names))
suits))
(count sequence-of-suits) ;; 4
(count (first sequence-of-suits)) ;; 13
Unfortunately (map) didn't work as hoped. I decided to try a list comprehension. This was something I remembered from using once in Python. It turned out to be exactly what I needed:
(for [suit suits, name names]
(struct-map card :suit suit :name name)))
This yields a lot of text, so let's inspect the output programmatically. (filter) is a function which takes a function and a list; the output consists of the list items for which the function inspects and returns true.
;; 52
(first deck)
;; {:suit :hearts, :name :queen}
(last deck)
;; {:suit :clubs, :name :four}
(defn heart? [card]
(= :hearts (card :suit)))
(count (filter heart? deck))
;; 13
(defn king? [card]
(= :king (card :name)))
(count (filter king? deck))
;; 4
(defn king-of-hearts? [card]
(and (king? card) (heart? card)))
(filter king-of-hearts? deck)
;; ({:suit :hearts, :name :king})
Now that we have the deck straightened out, it's time to shuffle it. I'm getting the feeling there is a beautiful mathmatical way to sort 52 cards involving permutations which would be easy to code functionally. Unfortunately I'm not there yet on either side. This implementation is really crude.
For example, this random-card implementation instantiates java.util.Random each time it is called (and without a seed value). (nth) returns the nth item of a sequence; (seq) is being used here because deck is a hashmap, which doesn't support ordered extraction.
(nth (seq deck)
(. (new java.util.Random)
nextInt
(count deck))))
Continuing on with the vulgar display of crude code... here is the shuffle method:
([deck]
(shuffle [] deck))
([clean dirty]
(if (empty? dirty)
clean
(let [pick (random-card dirty)]
(shuffle (conj clean pick)
(disj (set dirty) pick))))))
Let's ignore the lack of comments of any sort here. This implementation uses variable arity. That means when the function is passed one parameter the following code will run:
(shuffle [] deck))
And when two parameters are passed the following code runs:
(if (empty? dirty)
clean
(let [pick (random-card dirty)]
(shuffle (conj clean pick)
(disj (set dirty) pick))))))
This is a feature of Clojure. I kind of like it. The way recursion was used here the variable arity made the code simple to read. I probably could have used (recur) for a very similar amount of code, but the dogs had to go out.
So the second chunk of (shuffle) picks a card at random from the unsorted pile, uses (conj) to include it in the sorted pile, uses (disj) to exclude the picked card from the unsorted pile, and then GOTO 10.
And now, with lots of gory code I'm not proud of, here is my first hand of 5 card draw:
({:suit :diamonds, :name :three}
{:suit :clubs, :name :ten}
{:suit :diamonds, :name :four}
{:suit :spades, :name :three}
{:suit :hearts, :name :three})
Wow, three of a kind. It's almost as if I programmed in a "be nice to Seth" bit somewhere...


