Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to draw the line between Clojure and Java?

I have an interesting architectural question regarding an application that I am developing using both Clojure and Java. The application involves a lot of intensive, concurrent data processing tasks that need to be orchestrated.

Here's the rationale for mixing both Clojure and Java:

  • Java is needed for some pretty CPU-intensive numerical code, where I need to optimise the algorithms to run as fast as possible on the JVM. Clojure can't quite achieve this yet, and such code would not be very idiomatic in Clojure because the algorithms require a lot of mutable data for performance reasons.
  • Clojure is (IMHO) far better for orchestrating the overall flow of the application, with its excellent support for functional programming, interactive dynamic development at the REPL and concurrency features.

Given that I'm using both languages - what logic or principles should I apply to determine the dividing line between the two? In particular, I'm interested in how to design an API/interface that would be at the right kind of level to take advantage of the relative strengths of both languages.

like image 523
mikera Avatar asked Jan 21 '11 13:01

mikera


2 Answers

Without commenting on your perception of the relative advantages of Java and Clojure, and assuming that you did at least some micro-benchmarking to validate that the assumption has some chance of being correct, then the correct approach would seem to be to leave Java only for the parts that require optimization.

The classes responsible for the numeric code and calculation should be written in Java, and everything else in Clojure. I would even take a more aggressive approach and just design the classes to be distinct so that they could be written in Java, but actually write them in Clojure and rewrite them in Java if performance proves to be a problem.

like image 110
Yishai Avatar answered Oct 24 '22 10:10

Yishai


Clojure does a good job of helping developers get most of their part in simple functional style, and isolating mutational work into confined areas.

I would apply the same guidelines here: isolate java code as much as you can, as you would do as much of your clojure code in "pure functional style". So the java island would be as small as possible given your constraints, and access to the java island would go through a small set of clojure functions.

Not sure this helps much, but anyway !

like image 27
Laurent Petit Avatar answered Oct 24 '22 10:10

Laurent Petit