Articles

Age discrimination with Clojure

In General on 2010-08-26 by Jukka Zitting Tagged: ,

Michael Dürig, a colleague of mine and big fan of Scala, wrote a nice post about the relative complexity of Scala and Java.

Such comparisons are of course highly debatable, as seen in the comments that Michi’s post sparked, but for the fun of it I wanted to see what the equivalent code would look like in Clojure, my favourite post-Java language.

(use '[clojure.contrib.seq :only (separate)])

(defstruct person :name :age)

(def persons
  [(struct person "Boris" 40)
   (struct person "Betty" 32)
   (struct person "Bambi" 17)])

(let [[minors majors] (separate #(<= (% :age) 18) persons)]
  (println minors)
  (println majors))

The output is:

({:name Bambi, :age 17})
({:name Boris, :age 40} {:name Betty, :age 32})

I guess the consensus among post-Java languages is that features like JavaBean-style structures and functional collection algorithms should either be a built-in part of the language or at least trivially implementable in supporting libraries.

About these ads

5 Responses to “Age discrimination with Clojure”

  1. I didn’t knew, that you’re also interested in Clojure ;-) I used it intensively with Tika in one of my project, writing custom handlers for special mime type.
    With 1.2.0 this code could be rewritten to defrecords, that generate proper java class with normal fields. See:

    P.S. I’ll add you by clojure tag into Planet Clojure – maybe you’ll write more about it? ;-)

  2. Ah, gist wasn’t embedded – http://gist.github.com/553031

  3. Nice, thanks for the defrecord hint! I hadn’t yet looked too deep into the new stuff in Clojure 1.2.0.

  4. defprotocol and deftype/defrecord are much faster than multimethods. I just rewrote my small tika bindings to protocols (http://github.com/alexott/clojure-libs/blob/master/tika/src/tika.clj)

  5. Nice to see a Clojure proponent in this discussion. IMHO, it equals Scala in power and expressiveness, while avoiding its complexities. Unfortunately, the list of Clojure jobs is short, so I’m stuck with Java. :-)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: