views:

13024

answers:

5

Can someone please explain the major differences between Scala, Groovy and Clojure. I know each of these compiles to run on the JVM but I'd like a simple comparison between them.

+5  A: 

They can be differentiated with where they are coming from or which developers they're targeting mainly.

Groovy is a bit like scripting version of Java. Long time Java programmers feel at home when building agile applications backed by big architectures. Groovy on Grails is, as the name suggests similar to the Rails framework. For people who don't want to bother with Java's verbosity all the time.

Scala is an object oriented and functional programming language and Ruby or Python programmers may feel more closer to this one. It employs quite a lot of common good ideas found in these programming languages.

Clojure is a dialect of the Lisp programming language so Lisp, Scheme or Haskell developers may feel at home while developing with this language.

Mehmet Duran
Scala isn't really a functional programming language. It is an object oriented programming language first, with functional features.
Daniel
I have to say, this answer feels a lot like a shot in the dark. I think a good case could be made that Python is closer to Groovy than to Scala, and Ruby is (in my opinion) not too close to any of the above, perhaps closest again to Groovy. Haskell is not too much like (Common) Lisp or Scheme (and thus not much like Clojure). To me, this answer feels (at best!) like "I don't know either, let me Wikipedia that for you".
John Y
@Daniel I would argue that Scala *is* a functional language by most definitions. Actually, I have argued in the past (more than once) that Scala is *not* functional, but I have since modified that view. While it is true that some functional idioms are not as elegant in Scala as in other languages, the fact remains that all of the core features of impure functional languages (function values, immutability, currying) are present and idiomatic. This to me says Scala is functional *and* object-oriented.
Daniel Spiewak
Scala is an imperative language with some functional features. If people continue to call a language functional as soon as it adopts idioms from the functional world then the term will become just another marketing term. Might as well start calling C++ functional and Haskell imperative.
jon hanson
I opened this post as a community wiki because generally there's no one single answer when you're comparing two languages. I've played around with all three of them but never really went deep with any. I was generally talking about the syntax and idioms.
Mehmet Duran
Ok, i agree, it's not entirely germane to the comparison of the languages, i just think the first comment by Daniel is correct. And it can be relevant - i was slightly disappointed to find out that after all the hype Scala isn't a functional language as many people claimed.
jon hanson
As with most comparisons, the truth is a shade of gray. If we are to believe the creator Martin Odersky, Scala was designed to be both object oriented and functional, with both paradigms being first class citizens. See his interview with Computer World for an answer in his exact words: http://www.computerworld.com.au/article/315254/-z_programming_languages_scala
alanlcode
@alanlcode Odersky may say what he wants. Scala doesn't have any system to isolate side effects, it isn't lazy by default, and doesn't treat code as data -- it treats _function calls_ as data, which is different. These are big problems if you want to be fully functional. On the other hand, Scala goes all the way to ensure its Object Model isn't flawed. I love Scala, but it's clearly functional second.
Daniel
On the other hand, the ML family of languages is recognized as functional but is strict and allow side effects/imperative code.
GClaramunt
A: 

Obviously, the syntax are completely different (Groovy is closest to Java), but I suppose that is not what you are asking for.

If you are interested in using them to script a Java application, Scala is probably not a good choice, as there is no easy way to evaluate it from Java, whereas Groovy is especially suited for that purpose.

Thilo
I don't understand your point about using Scala to script Java. You can certainly write a Scala script that drives Java code; no `eval` required.
Daniel Yankowsky
@Daniel, please see the question about using Scala for scripting that I linked. The accepted answer there is that the lack of an "eval" facility and javax.scripting support makes it trickier to use Scala to script a Java application then it is with, say, Groovy.
Thilo
+117  A: 

Groovy is a dynamically typed scripting language, whose syntax is very close to Java. It's good for fast prototyping, scripts, and learning dynamic languages without having to learn a new syntax (assuming you know Java).

Clojure is a dialect of Lisp with a few advanced features like Software Transactional Memory. If you like Lisp and would like to use something like it under the JVM, Clojure is for you. It's possibly the most functional JVM language, and certainly the most famous one. Also, it has a stronger emphasis on immutability than other Lisp dialects, which takes it closer to the heart of functional language enthusiasts.

Scala is a fully Object Oriented language, more so than Java, with one of the most advanced type systems available on non-research languages, and certainly the most advanced type system on the JVM. It also combines many concepts and features of functional languages, without compromising the object orientation, but its compromise on functional language characteristics put off some enthusiasts of the latter.

While Groovy has good acceptance and a strong web framework in Grails, I personally think it is a language with limited utility, particularly as Jython and JRuby start making inroads on the JVM-land, compared to the others.

Clojure, even discounting some very interesting features, has a strong appeal just by being a Lisp dialect on JVM. It might limit its popularity, granted, but I expect it will have loyal community around it for a long time.

Scala can compete directly with Java, and give it a run for its money on almost all aspects. It can't compete in popularity at the moment, of course, and the lack of a strong corporate backing may hinder its acceptance on corporate environments. It's also a much more dynamic language than Java, in the sense that the language evolve. From the perspective of the language, that's a good thing. From the perspective of users who plan on having thousands of lines of code written on it, not so.

As a final disclosure, I'm very familiar with Scala, and only acquainted with the other two.

Daniel
A *very* good comparison! Obviously biased in favor of Scala and (to a lesser extent) static typing, but quite nice none-the-less.
Daniel Spiewak
You might add the point that Clojure has greater emphasis on immutability than other Lisps.
Nathan Sanders
Got it. I'm trusting you on this, Nathan! :-)
Daniel
Very biased against Groovy. I'd add that Groovy is closest syntactically of all the alternate JVM languages to Java (except jJava ;-) and will be the easiest of them for most developers to pick up. It supports both dynamic and static typing. It also has cleaner integration with Java (Both Groovy calling Java classes and vice versa) than Scala or Closure. Pigeon-holing it by saying it's only good for prototyping, scripts and learning is really only the opinion of Daniel and in no way reflects how other people are using it.
hohonuuli
@hohonuuli I *said* its syntax is very close to Java, endorsed Grails and indicated it was deficient *in my opinion*. But it doesn't support static typing, just a type annotation which doesn't really enforce type -- look it up. It might have slightly cleaner integration with Java than Scala, but most Scala programmers wouldn't even know it, as most stuff just works. I stand by my words -- it can't compete with Scala as far as static typing goes, Jython and JRuby are more popular dynamic languages, and it isn't really functional. It's only clear advantage is being a Java-like dynamic language.
Daniel
Just for the record, Groovy doesn't support anything remotely like static typing. It has type *assertions* built into the language, but they only apply at runtime. The canonical example is `String s = 42`, which will compile without a hitch, but throws an error at runtime.
Daniel Spiewak
+6  A: 

I recommend interview with Venkat Subramaniam by Scott Davis.

cetnar
Great interview. Thanks for the link.
jjnguy
+5  A: 

Scala

Scala evolved out of a pure functional language known as Funnel (http://lamp.epfl.ch/funnel/) and represents a clean-room implementation of almost all Java's syntax, differing only where a clear improvement could be made or where it would compromise the functional nature of the language. Such differences include singleton objects instead of static methods, and type inference.

Much of this was based on Martin Odersky's prior work with the Pizza language (http://pizzacompiler.sourceforge.net/). The OO/FP integration goes far beyond mere closures and has led to the language being described as post-functional.

Despite this, it's the closest to Java in many ways. Mainly due to a combination of OO support and static typing, but also due to a explicit goal in the language design that it should integrate very tightly with Java.

Groovy

Groovy explicitly tackles two of Java's biggest criticisms by: a. being dynamically typed, which removes a lot of boilerplate; b. adding closures to the language. It's perhaps syntactically closest to Java, not offering some of the richer functional constructs that clojure and Scala provide, but still offering a definite evolutionary improvement - especially for writing script-syle programs.

Groovy has the strongest commercial backing of the three languages, mostly via springsource.

Clojure

Clojure is a pure functional language in the LISP family, it's also dynamically typed.

Features such as STM support give it some of the best out-of-the-box concurrency support, whereas Scala requires a 3rd-party library such as Akka to duplicate this.

Syntactically, it's also the furthest of the three languages from typical Java code.

I also have to disclose that I'm most acquainted with Scala :)

Kevin Wright
I never heard of this Funnel language before. Thanks for filling a little gap in the historical record.
Randall Schulz