Did you ever need to know the date and time 30 hours from now, because that is the time you could check into your plane to EuroClojure 2016 (and you’re too lazy to do this in your head)? Or maybe you just saw an interesting Clojure library on Twitter or Reddit that you wanted to try out? How convenient would it be if you didn’t have to create a project for such one off experiments.
There are several good options in Clojure for this. In this post let’s
assume we were going to try
clj-time, an excellent
date and time library based on Joda Time. We’ll show how to make a
script that gives you almost instantaneous access to this library from
the command line using Boot. And then we’ll
make it even faster using Planck.
1 2 3 4 5 6
For Boot this story seems even simpler as there
is no need to install a plugin. Boot supports an option for including
dependencies from the command line. Just type
boot -d clj-time repl
to get a REPL with the latest
clj-time as a dependency:
1 2 3 4
Note that Boot’s
repl task also supports the
--eval option (
for short), so we can already put the
require on the command line:
1 2 3 4 5
How convenient this is. This allows us to write it as a script:
1 2 3 4
Great to have handy for EuroClojure 2017!
Note that Boot allows us to load dependencies dynamically. Suppose you’re experimenting but need another library. No need to restart the REPL. You can just type:
1 2 3 4
Planck can use jar files from
~/.m2, but you have to specify the
full classpath. This is easily done with the help of Boot:
1 2 3
The list of dependencies is now written to
.classpath. You can re-use this file if your dependency hasn’t changed.
Now we’re ready to start the Planck REPL. It’s fast! Even faster when
you use the
K option which caches compiled ClojureScript.
1 2 3 4
Typing directly in a REPL only goes so far. For larger expressions it is more convenient to write in a text editor and then send the code to the REPL. For experiments started with Leiningen or Boot you can use an nREPL client. I use CIDER. For Planck you can use inf-clojure.
That’s it. I hope this also helpful to beginners. Performing little Clojure experiments can grow into an addiction. Before you know it, you’re soaked into your first Clojure project.