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.
[sourcecode language="clojure"]
(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))
[/sourcecode]
The output is:
[sourcecode language="clojure"]
({:name Bambi, :age 17})
({:name Boris, :age 40} {:name Betty, :age 32})
[/sourcecode]
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.
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.
ReplyDeleteWith 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? ;-)
Ah, gist wasn't embedded - http://gist.github.com/553031
ReplyDeleteNice, thanks for the defrecord hint! I hadn't yet looked too deep into the new stuff in Clojure 1.2.0.
ReplyDeletedefprotocol 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)
ReplyDeleteNice 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. :-)
ReplyDelete