Posts Tagged ‘clojure’

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.

Advertisements