Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What classes of problems is Clojure good/bad at solving vis-a-vis Scala?

Tags:

scala

clojure

Both languages are JVM based with strong support for functional programming. I'm aware that there would be a large class of problems where both languages would provide excellent solutions.. What I would like to know is if there are any particular types of problems where features of Clojure would give it a notable edge against Scala and vice versa. At the moment we do a lot of our work in Scala but I would like to be on the lookout for particular problem spaces where Clojure can potentially provide a better solution.

like image 998
deltanovember Avatar asked Oct 01 '11 04:10

deltanovember


3 Answers

Both languages are extremely capable and suitable for almost all domains. You can't particularly go wrong with either language - I'd go so far as to say that they are probably the two most promising languages around at the present.

I still think there are a couple of areas where they each have distinctive advantages:

Clojure's particular strengths relative to Scala:

  • Concurrency - Clojure has unique and very powerful concurrency support built into the language, based around a new way of thinking about object identity and mutable state. This has been explained very well elsewhere so I won't particularly go into details here, but this video by Rich Hickey is a great place to get some insights.
  • Metaprogramming - Clojure is a homoiconic language which makes it particularly suitable for macro based metaprogramming, DSL creation and code generation. It follows the Lisp "code is data" philosophy in this respect.
  • Dynamic typing - Clojure is a dynamic language, with all the advantages usually associated with dynamic languages (less boilerplate, rapid prototyping, very concise code etc.)
  • Functional programming - although you can do FP in Scala, Clojure definitely feels more like a functional langauge (whereas Scala is probably best described as multi-paradigm). This functional emphasis in Clojure manifests itself in several ways, for example built in lazy evaluation support across all the core libraries, all data structures are immutable, idiomatic Clojure "coding style" is functional rather than imperative/OOP.

Scala's particular strengths relative to Clojure:

  • Object orientation - Scala is conceptually closer to Java and has better support for Java-style OOP approaches. While you can do OOP in Clojure, it's not such a comfortable fit.
  • Syntactic familiarity - Scala syntax will probably be more comfortable to people coming from other non-Lisp languages
  • Static typing - Scala has a very sophisticated static type system. In situation where static typing is advantageuos, Scala will have a clear edge. The usual advantages of static typing apply - the compiler can catch more potential "type" errors, the compiler has more opportunity to do performance optimisations etc.
  • More mature - Scala has been around a bit longer than Clojure (2003 vs. 2007), and as a result has some of the benefits that you would expect from a more mature language (better tool support, slightly larger community)

For completeness, there are a couple of distinctive advantages that both languages share:

  • Active and innovative communities - both languages have built up an active community with a wide variety of contributors
  • Excellent interoperability with Java - both languages can easily make use of the huge array of libraries and tools in the Java ecosystem
  • JVM advantages - both languages benefit from all the great engineering in the JVM (JIT compiler, garbage collection algorithms, optimised execution environment etc.)
like image 132
mikera Avatar answered Oct 18 '22 20:10

mikera


I prefer using Clojure for writing DSLs because of Clojure's macro system. Also, I prefer Clojure for certain kinds of concurrent systems. Clojure's native support for software transactional memory is quite different than Scala's actor model.

Clojure doesn't have great support for object oriented programming. For projects where the object oriented paradigm works well, or you will be relying heavily on Java libraries/frameworks, I prefer Scala.

like image 24
dbyrne Avatar answered Oct 18 '22 19:10

dbyrne


One of the great things about Clojure and Scala is that you can use both of them in the same place, with minimal friction. I'd only suggest doing most of the interop in Clojure, since I think it has an edge there.

Just try both, and see which problems and languages fit together. You can always interoperate between Java, Scala, and Clojure code in the same system very easily.

like image 4
John Cromartie Avatar answered Oct 18 '22 19:10

John Cromartie