Foognostic blogs Seeking knowledge of foo

23Apr/10Off

A quick tour of labrepl

A quick tour of labrepl is something I put together for the Washington DC Clojure meetup. Relevance did a great job with labrepl and graciously made it freely available. It works great for instructor-led training and is clearly designed to work in a self-study context. I added a little bit of background describing it and a couple of ways you could take different paths through it to cover just the basics, go far enough to actually write some code, or even jump straight to some of the more interesting topics of Clojure which labrepl features.

Many thanks to the awesome @brweber2 for presenting a quick tour of labrepl.

Filed under: clojure No Comments
7Apr/10Off

Want to get started with Clojure?

Randomly curious about Clojure, but not quite sure how to get started? Members of the Clojure community have established this page as the best place to find current, complete instructions for using Clojure in your favorite IDE.

Unless of course NOTEPAD.EXE is your thing... and who wants to go down the path of manually adding ))))))? Let an editor manage that for you!

Filed under: clojure No Comments
8Feb/10Off

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:

(def banana (agent "ba"))

Here is the function which produces its new value:

(defn tally-man [berry]
  (let [input berry, output (str berry "na")]
    (println (format "input:output %s:%s" input output))
    output))

Here it is in action:

user> (def banana (agent "ba"))
(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.

(defn growl [msg]
  (.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.

(defn tally-man [berry]
  (let [input berry, output (str berry "na")]
    (binding [*out* growler]
      (println (format "input:output %s:%s" input output)))
      output))

Ta DA!

Filed under: clojure, code No Comments
26Jan/10Off

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:

Image of a test failing

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.
Filed under: clojure No Comments
18Jan/10Off

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:

;; define the struct
(defstruct Card :value :suit)

;; instantiate it
(def card (struct Card :ace :spades))

Here's my first baby step down the new path:

(deftype Card [#^clojure.lang.Keyword value
               #^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!

(def old-Card Card)

(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 :ace :spades)
   (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.

Filed under: clojure, code No Comments
12Jan/10Off

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!

Tagged as: 1 Comment
3Jan/10Off

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:

   :dependencies [[org.clojure/clojure "1.1.0-new-SNAPSHOT"]
                  [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:

$ rake
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 -T
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.

$ time rake clojure:new:all
# 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:

$ ls -l ~/.m2/repository/org/clojure/clojure/
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.

$ ls -l ~/.clojure/
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)

$ rake -T
# ... 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:

$ cat > user.yml
technomancy-clojure:
  clone-from: git://github.com/technomancy/clojure.git
  branches: [master, new]
  use-jar: clojure.jar

And then something like this:

$ rake -T
 # ... 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!

Tagged as: No Comments
16Dec/09Off

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.

Filed under: clojure No Comments
24Nov/09Off

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.

private void makeFluent(Customer customer) {
    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.

(defn add-order [customer]
  (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:

;; define a new customer and add an order
(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":

(def me (add-order 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.

example> (. {} getClass)
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!

Filed under: clojure, code Continue reading
15Nov/09Off

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:

(deal-holdem (new-deck) [:al :bill :chas])

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.

  1. A little more work on judging hands (identifies groups but not by suit)
  2. Add support for chips
  3. Add support for checking, calling, raising
  4. Add support for folding
  5. BIG: very simplistic logic for having CPU players bet/call/fold
  6. VERY BIG: Rewrite most of everything with agents and refs in order to...
  7. 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!

Filed under: clojure, pokerepl 2 Comments

Foognostic blogs is Digg proof thanks to caching by WP Super Cache