Tools for Learning Clojure

As I wrote on Saturday, I’m learning to program in Clojure; and being a tool user at heart I immediately went looking for the right tools for the job. Here’s what I wanted:

  1. A REPL at which to experiment with Clojure code, with good command-line editing and command history.
  2. An editor in which to edit Clojure code, integrated with the REPL.
  3. An easy way to build a stand-alone application in Clojure.
  4. A solution that works on both Mac and Windows.

“REPL” stands for “read-eval-print loop”, and it’s a LISP-ism for a terminal-style interface to a language. You type a command at the language interpreter, it reads it, evaluates it, and returns the result. Then you type something else, and it responds to that, and so on. It makes it possible to develop programs interactively, and test your code as you write, and it’s very cool.

    user=> (defn square [x] (* x x))
    #'user/square
    user=> (square 5)
    25
    user=>

LISP programmers like to talk as though they invented the REPL, which they did, and they keep using the term “REPL”, which most people don’t; they just call it an interpreter. (And then they tell you that it isn’t really interpreted, it’s all compiled under the covers.) The first programming language I learned was BASIC on a PDP-11; it was an interpreter, and when you started it up you got a command prompt and developed your program interactively. REPLs went into a decline about the time BASIC lost its line numbers, but have since resurged; and most of the new languages I see have something like one.

Anyway, in order to learn Clojure properly, you need a REPL so that you can experiment and see how the language works. The easiest way is just to download the Clojure “.jar” file, and use Java to execute it. For example, I can go the Terminal app on my Mac, switch to a folder that contains the Clojure “.jar” file, and type this at the command line:

% java -cp clojure.jar clojure.main
Clojure 1.6.0
user=> (+ 1 1)
2
user=>

It’s a perfectly good REPL; except that it’s perfectly unusable to anyone used to using this kind of terminal interface. Almost every program that provides a command prompt these days has some variant of what’s usually called “readline” support: you can use the Left and Right arrow keys to edit the line you’re on, and you can use the Up arrow key to recall previously entered commands. As a programmer, I live at the command line, and my muscle memory expects those things to just work. In this REPL they don’t. Instead, you get funny control characters which do nothing good for your code.

Consequently, I needed something better.

The first program I tried is the aptly named Light Table. Light Table is an attempt to completely rethink the traditional programmer’s IDE, giving instant and immediate feedback about the state of the programmer’s application even as he’s coding it. Light Table is written in a variant of Clojure called ClojureScript, and has built-in support for Clojure editing; it also has a built-in “InstaREPL”. It runs on Mac and Windows and Linux. So I downloaded it and gave it a try.

It’s…interesting. Most programmer’s editor’s these days do something called syntax coloring, to make the syntax of the language stand out, and Light Table does it a little differently than most:

LISP by its nature has lots and lots of parentheses; Clojure has fewer, but makes up for it by having many more brackets and braces. And they all have to come in nested pairs, which can get confusing. If you look at the screen shot just above, notice that the parens, brackets and braces are colored based on how deeply they are nested. This is very cool, and is Light Table’s best feature in my view.

The InstaREPL, on the other hand, is not so good. It tries hard, mind you. Here’s what it looks like:

It isn’t like a normal REPL, where the user and the language take turns in a kind of dialog. As soon as you start typing, Clojure is trying to make sense of it. And when it makes sense, it shows the result to the right of what you were typing. Above, for example, you see “4″ to the right of “(square 2)”, which makes sense. On the line above, you see “2 2 2″, which appears to be the value of variable “x” each time it appears in the definition of “square”, for the last thing you entered. This is the “light” in “Light Table”: you can immediately see what your code is doing.

I wanted to like Light Table a whole lot. But there’s something about how the InstaREPL works that confuses me. Every time I’ve tried to use it, I’ve somehow ended up confusing it, or me, or both, such that the code I’m typing in doesn’t work when it should. I spent forty-five minutes trying to figure out why something I was trying wasn’t working, and assuming that it was all my fault as a clueless newbie. Turned out my code was perfectly good; the InstaREPL was mis-interpreting it. It’s still my fault, mind you: there’s something about the InstaREPL’s behavior that I’m not understanding that’s leading me astray. But after a couple of wasted programming sessions, I decided I needed to find something more traditional. (Let me be clear: Light Table is an interesting tool, and it’s going to remain on my radar for the time being. Your mileage may vary.)

To Be Continued! Why? Because it interests me, even if it interests no one else!

Print Friendly

About willduquette
  • Colin Fleming

    If you’re interested in something easy to use but with a more “traditional” experience, you might like Cursive. We try to make it as easy to use as possible so it’s pretty easy to get started with. Instructions are at: https://cursiveclojure.com/userguide. We’d be interested to know what you think!

    • Will Duquette

      Thanks, I’ll take a look. I’ve got a post going up about Leiningen on Saturday, and then I’ve been using Nightcode so I’ll have a post about that. But I’ll take a look at Cursive and see how it compares.

    • Will Duquette

      Aha! That’s why I hadn’t looked at Cursive—it requires IntelliJ, which I don’t use. Your “traditional” confused me; from my point of view, “traditional” would be downloading the Clojure tools for Vim or Emacs. ;-)

      • Colin Fleming

        Ah, fair enough – I meant a more traditional REPL experience, as you’d mentioned you didn’t like the LT experience much.

        Cursive does require IntelliJ but you don’t have to use any of the IntelliJ functionality, in the same way that most Emacs users use probably 3% of everything it does. That said, I don’t know why you wouldn’t – it’s pretty great. The git integration is really nice, and the all the project-wide functionality and diffing is much nicer than anything Emacs offers IMO.

        I’m planning to make a standalone IDE for Cursive which will be based on the IntelliJ infrastructure but will be a separate product, in the same way that PyCharm or RubyMine are. That will be basically the equivalent of the free version of IntelliJ with the Cursive plugin pre-installed, and some unrelated functionality removed.

        • Will Duquette

          Mind you, I’m not looking for a Vim or Emacs solution either. I used Emacs for decades, and Vim for a couple of years, and then I moved on.

          For this particular effort I’m looking for something simple, that lets me engage with the language without having to engage with much else that’s new. The environment I’d use if I were planning on writing production code might be quite different.

  • JonyEpsilon

    Let me shamelessly plug a project of mine, Gorilla REPL (http://gorilla-repl.org) :-) It’s a REPL with some nice extra features (saving sessions as worksheets, plotting graphs, tables etc). An example session: http://viewer.gorilla-repl.org/view.html?source=github&user=JonyEpsilon&repo=gorilla-test&path=ws/graph-examples.clj

    • Will Duquette

      Gorilla looks interesting. I’ll give it a try.

  • Theo Belaire

    There’s a command line tool called `rlwrap` (for readline wrapper).

    You can use it to make any command line tool have backspace and such work.

    In this case, you could do “rlwrap java -cp clojure.jar clojure.main”,

    and it should do the same thing, but with readline support.
    It’s installable through homebrew with just a `brew install rlwrap`.

    • Will Duquette

      Now that’s useful. I’ll look for it.


CLOSE | X

HIDE | X