Interactive Processing Development with Clojure REPL in IntelliJ IDEA

Kadir Malak
5 min readMar 4, 2018

--

As I’m currently learning Clojure, trying to convert Processing examples into Clojure code, I thought that it would be nice to leverage Clojure’s REPL (Read-Eval-Print-Loop) to interact with the running app. Dynamically changing the runtime behavior — especially in such interactive programs — is a big plus.

By the way…

  • Processing is an environment and a language (and also a library), which is mainly used for interactive computer graphics (and other visual arts stuff). You may use Processing IDE and/or the language (like a simpler subset of Java) or you can just use the library jars.
  • Clojure is a JVM hosted, dynamic, functional programming language. One of the best things about it is that you can use existing Java libraries.
  • There are other ways to use Processing with Clojure, such as https://github.com/quil/quil project. But I just wanted to use the Processing library as it is.
  • Cursive is a great Clojure plugin (https://cursive-ide.com)

First we need to create a new Clojure project. I’ll use Leiningen just to create the project and continue within IntelliJ IDEA.

Notice: As I’ll add jars to the project using the IDE, not Leiningen dependency management system, Leiningen commands will fail when it cannot find the jars for Processing. One workaround for that is:

to create a local repo,

deploy local jars to that repo using “lein deploy” command

and use local repo libraries as dependencies.

There are tutorials for that on the Internet, I’ve tried once but I got strange errors when I tried to run app within IDE and I gave up for now. But later, I’ll return to that issue because I want to be able to use Leiningen commands (especially uberjar) later on.

Anyway… Let’s create a new app using the command below:

> lein new app processing-with-clojure

Import project into IntelliJ IDEA.

From Project Structure -> Libraries, add Processing jars for your environment.

your native library paths may differ. choose carefully.

To use Processing outside its development environment, one must normally create a Java class as described here: https://processing.org/tutorials/eclipse/. We’ll just create that class using Clojure.

Replace core.clj contents with the code below (a minimal Processing app). One thing to notice: While generating the .class files, hyphens will be replaced by underscores. So, on line 8 we’re passing main class name argument as “processing_with_clojure.core”, not “processing-with-clojure.core”.

Now we need to change Clojure compiler settings because processing_with_clojure/core.class must be created before the app starts as Processing will look for it since we’ve given its name. Otherwise you’ll get no class definition found errors. Normally Clojure source files are compiled into .class files on the fly but for the reason below, we need .class file before that. (As I’m new to Clojure stuff, I may have said something wrong but you get the idea)

Change Clojure Compiler settings as below:

note: I’m not sure about the “Compile Clojure files before Java” option but I check it anyway :)

I think you may also select “Compile all Clojure namespaces” but it’s not necessary.

Now it’s time to build the project and confirm that the class files are generated.

when we build the project, these class files should be created

As you see, core.class is generated and it extends processing.core.PApplet as intended.

And now, you may run the main function using the green arrow within IDE (that will also create a run configuration for us to use later)

run -main method using the green arrow

Processing app should start now as seen below:

a minimal processing app

It’s now time to edit run configuration to ensure that build step always runs before running the app. (Notice that we didn’t check the “run with Leiningen” option for the reasons mentioned at the top.

build project before running

Now, we can edit the code and run the app within the IDE, but would not it be better to run within the REPL and to be able to change the code on the fly :)

Now stop the app and right click on the project, select “create REPL…”

create REPL for our app

In the dialog below, again we’ll not choose the Leiningen REPL. And don’t forget to add “build project” step before launch.

REPL configuration for our app

Run the REPL (to see the REPL, you may need to click the REPL button on the right side of the IDE)

REPL running…

Now the REPL is running, right click on the code and select “Load file in REPL”. And I strongly suggest that you memorize and use the shortcuts from later on. Because you’ll want to use them repeatedly.

this will load the current file into REPL

Switch to current file’s namespace

to change the REPL’s namespace to the current file’s

type (-main) and press enter to start the main function of the app within the REPL

-main function is the starting point of our app

The app should start again, but now as it’s started within the REPL, we may change the code on the fly :)

Change the contents of the -draw method as below: (we’ve changed the .ellipse method with .rect method)

Now right click on the function name and select “Send…” (Or write the same code in REPL input window and press enter)

we’ve changed the function and now we’re sending the new code to the REPL

Return to the app screen and notice the change :)

app was drawing ellipses on the screen, now it’s drawing rectangles

It’s just a proof of concept for now. I haven’t throughly tested it but I’m very positive about it :)

--

--