I can perfectly see why Clojure is really good for concurrent programming. I can see the advantages of FP also in this regard.
But clearly, not every line of code that we write is part of a thread or needs concurrent access. For those parts of the code (the more simple and sequential piece of code) what is it that Java really missed that Clojure provided?
Were features like Multimethods, Dynamic binding, Destructuring bind really missed in Java?
I supposed my question can also be framed as:
- If Clojure did not have the
Concurrency features that it had and
the whole Immutability/Mutability
issue was not of our concern, then
what other features Clojure provides
that would make you use it instead of
Java ?
Yes. Also…
First-class functions. Delicious first-class functions. This isn’t just an FP thing. There’s a good reason people are clamoring for closures in Java 7.
Code-is-data. This is the benefit of any Lisp. Lisp code isn’t just blobs of text you feed into the mouth of a compiler and never see again, it’s structures of lists and vectors and symbols and literals that you can manipulate progammatically. This leads to powerful macros and first-class Symbols and lots of other goodies. It leads to a highly extensible and powerful language.
Clojure has better control and looping constructs and the ability to create your own via macros and first-class functions. Java has
forandforeachandwhile(and didn’t even haveforeachfor years). Clojure hasmap,filter,reduce,mapcat, lots ofdoforms, lots ofifandwhenforms, list comprehensions viafor, and so on. If these didn’t exist, you could write them yourself. In Java you get to wait a decade for a committee to (maybe) approve such features.Minus those dealing with static typing, all of the features set for Java 7, Clojure either already has or could have trivially. “Automatic resource management”, Clojure has as
with-open. “Language support for collections”, Clojure (and Ruby, Perl, Python…) has already. “Strings in switch”, Clojure has more powerful case-like constructs likecondp, and whatever else you can think up. You could write any of these yourself in a dozen lines of Clojure.Concise syntax for lists, maps, arrays, sets, sorted sets, sorted maps etc. and nearly interchangeable use of them all thanks to the
seqabstraction. Literal support for regexes, characters, anonymous functions, etc.Java has mandatory checked exceptions, which are annoying; Clojure doesn’t.
Java syntax is verbose and irregular. Clojure syntax is concise and regular. Even Java written in Clojure is often more concise than Java written in Java thanks to macros like
->anddoto, and constructs likeproxyand (soon)reify.Java code has too much mandatory boilerplate and endless repetition.
public static void main(String[] args){...}etc. Clojure has next to none of this boilerplate, while sacrificing little to nothing in terms of expressiveness or power. Even other statically typed languages today seem to be going the way of type inference. There’s good reason you need a bulky Java-centric IDE to write and endlessly “refactor” Java code; writing it by hand would drive you insane and wear your fingers down to nubs.In Java everything is a class or interface, whether it should be or not, which is a cause of unnecessary complexity. There are many programs that have to be mangled beyond recognition to fit into an OOP style. Clojure lets you avoid this. A nice rant to this effect. Clojure focuses largely on verbs.
Interactive programming via REPL is fun. Compile/run/debug cycles are not. Clojure still compiles to .class files if you want it; in the meantime you can sit in the middle of your code and tinker freely while it’s running.
Clojure’s metadata and sane equality testing are enjoyable to work with. As are its auto-promotion of int to long to Bigint, native handling of rational numbers, and so on.
Dynamic typing leads to shorter, more generic thus more reusable thus more powerful code than static typing. (This is a highly debatable point, obviously, so I put it last.)
The popularity of Scala and Groovy and JRuby and Jython and endless other JVM-languages-that-aren’t-Java should be seen as a good indication that while the JVM is good, Java-the-language is unpleasant for many people.