Comparing Lines of Code: Scala and Clojure (FUD version)
A completely tongue in cheek comparison between Scala and Clojure, implementing the famous closure test.
Scala Version1
type Supercalifragilisticexpialidocious = java.lang.Integer
implicit def Supercalifragilisticexpialidocious2Int(tehThingToConvert:Supercalifragilisticexpialidocious) = {
tehThingToConvert.asInstanceOf[Int]
}
trait ClosureTestMixin {
def makeAdder(tehAdditionValue:Supercalifragilisticexpialidocious):Function1[Int,Int]
}
class AdderMaker extends ClosureTestMixin {
def apply(tehAdditionValue:Supercalifragilisticexpialidocious):Function1[Int,Int] = {
new Function1[Int,Int] {
def apply(tehOtherAdditionValue:Int):Int = {
return tehAdditionValue.$plus(tehOtherAdditionValue.asInstanceOf[Supercalifragilisticexpialidocious])
}
}
}
def makeAdder(tehAdditionValue:Supercalifragilisticexpialidocious):Function1[Int,Int] = {
return this.apply(tehAdditionValue)
}
}
val tehAdderMakerInstance = new AdderMaker()
val tehAdderPlus10 = tehAdderMakerInstance.makeAdder(10)
tehAdderPlus10.apply(9)
//=> 19
Clojure Version
(defn mk-adder [x] #(+ % x))
(def add10 (mk-adder 10))
(add10 9)
;=> 19
To perform this simple exercise the Scala version requires2 29 lines and ~960 characters! The Clojure version on the other hand requires only 3 lines and 64 characters.
Need I say more?3
-m
-
Any suggestions for making this longer? ↩
-
Nope, not at all. ↩
-
Just in case there was any ambiguity — the Scala version was intentionally obfuscated for chuckles. For a much more eloquent rebutal of flawed LOC comparisions read Stephan Schmidt’s latest post on the subject. ↩
Related posts:
- Clojure Golf episode 1 This is an attempt to have some fun with Clojure....
- Baysick: A Scala DSL Implementing BASIC This post was featured on the Scala website. It is...
- Clojure Golf – Episode 2: Largest Prime Factor In the last episode of Clojure Golf we saw some...
- Understanding the Clojure -> macro On page 109 of Paul Graham’s ANSI Common Lisp he...
- De-chunkifying Sequences in Clojure At the first CAP CLUG meetup I gave a presentation...
Related posts brought to you by Yet Another Related Posts Plugin.


8 Comments, Comment or Ping
Gabriel C
You should replace the “+” in the Scala code with “$plus”, a beginners mistake, but hey, we’re all learning :) Also, consider making it a monad…
Jan 4th, 2010
fogus
@Gabriel Done! I will play around later with making the Scala version more monadic. Thanks for the tips!
Jan 4th, 2010
Josh Cough
:) can you do it in reverse too?
Jan 4th, 2010
Daniel E. Renfer
I can’t believe you wasted so many lines in your Clojure version.
(((fn mk-adder [x] #(+ % x)) 10) 9)
only 35 characters and does roughly the same thing. :)
Jan 4th, 2010
jneira
-= Comparing Lines of Code: Scala and Clojure (FUD version) =- http://bit.ly/7hbc5V #ironic #programming #language #war
This comment was originally posted on Twitter
Jan 5th, 2010
bubbl_scala
-= Comparing Lines of Code: Scala and Clojure (FUD version) =- http://ff.im/-dOazU
This comment was originally posted on Twitter
Jan 5th, 2010
fogus
@Josh Cough
That will be the next post. :-)
Jan 6th, 2010
mauritsrijk
#Clojure versus #Scala: http://tr.im/KiCi
This comment was originally posted on Twitter
Jan 13th, 2010
Reply to “Comparing Lines of Code: Scala and Clojure (FUD version)”