Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure Worksheets

Tags:

clojure

I have been learning Clojure a bit recently. Is there such a thing in Clojure world as Scala-like worksheets, into which I can put any code and get it evaluated as soon as I save it? Or maybe there's a similar solution for Clojure?

I am now working with lein repl and, sadly, can't say it's the most usable tool ever.

like image 485
t.O Avatar asked Oct 14 '12 06:10

t.O


2 Answers

In Lisp development in general (and Clojure in particular) the preferred programming style is what's usually dubbed interactive programming - the developer keeps an image of the app loaded at all times and interacts with it via a REPL. You can easily modify the loaded code on the fly and test changes immediately in the REPL (that's not easy at all with Scala - one has to resort to something like JRebel to do it). I find the Scala worksheets a pretty primitive solution in comparison...

The workflow that I follow in Clojure is:

  1. I open nREPL.el in Emacs - this loads my lein2 project with all of its dependencies and gives me a REPL which I can use the try out stuff
  2. I write some code in source code and load the changed functions (maybe by evaluating a top level form with C-M-x
  3. Afterwards I'd press C-x C-z to jump back to the REPL and I try out the new code in it
  4. I go back to step 2

Basically the Clojure REPL is much more powerful than the Scala REPL and I personally consider it hugely superior to the Scala IDE worksheets. Once you get used to the interactive incremental style of programming Lisp offers everything else starts to look strangely complex by comparison. I'm not familiar with Eclipse's CounterClockWise Clojure plugin, but I'm pretty sure it offers similar functionality to Emacs's nREPL.el.

like image 84
Bozhidar Batsov Avatar answered Oct 26 '22 01:10

Bozhidar Batsov


You might want to take a look at the autoexpect plugin for Leiningen. Every time you save any file in the working directory, the plugin compiles and runs your code; as a bonus, it will evaluate any "expect" function calls which can serve as tests. This is very helpful for test driven development and is a nice compliment to working with the REPL as described in the other answer (I often use one or the other or both together depending on how many test cases I have in place).

I should note that running autoexpect is far faster than running "lein test" or "lein run" repeatedly, due to the startup cost of the JVM and Leiningen.

like image 24
JohnJ Avatar answered Oct 25 '22 23:10

JohnJ