r/Clojure • • Nov 29 '19

Scripting in Clojure, many great options!

I'd recommend looking at all the awesome scripting options for Clojure:

Honestly, scripting is just a solved problem for Clojure in my opinion. At this point. There's even too many good options probably.

My current favorite is Joker. I find it is the most bash like in that the standard library is very small, so just like on bash, you end up delegating to forked processes using sh. Works on Windows, Linux and Mac alike. Single binary.

Inlein is best if your script needs a ton of Clojure dependencies.

Babashka is quite promising, it competes with Joker, but windows support isn't great yet, and it is still alpha. But it has great potential.

Closh is just awesome. It is a literal replacement for bash, in that it is a full shell implemented in Clojure/Script. I really want to see it evolve and get super stable and feature complete. I'd love to make it my default shell instead of zsh.

Lumo and Planck are both great ClojureScript repls that can be used for scripts. Since I'm less familiar with Node and JavaScriptCore APIs I don't tend to use them as much. Also, installing Node is cumbersome, and Planck doesn't support windows. So I've stopped using them for scripting. But great options if you know the JS world a lot.

The last one can be a good replacement for inlein. Similarly, it's a good option if you need a lot of extra Clojure dependencies. It's a clever hack that lets a simple sh script self bootstrap Clojure code within itself and pull in dependencies from tools.deps. There were times when I needed to do more maneuvering to get it working, but it only needs sh and Clojure, which is nice.

73 Upvotes

33 comments sorted by

View all comments

6

u/TheLastSock Nov 29 '19

This is a good starting point, but is there anything that compares these options in depth?

I assume they all exist to fulfill a need.

2

u/didibus Nov 30 '19

That's what I was trying to do 😝

I guess I didn't go super in-depth. But their differences matter more from what your needs are.

  • If you want a Clojure shell, use closh.

  • If you want to use Java and Clojure libraries, use inlein (if you like lein) or the gist (if you prefer sh and tools.deps), or GraalVM (if you want fast startup)

  • If you can manage with core functions and a few extra scripting specific bells and whistles and using OS commands, use Joker. Joker is a Clojure interpreter written in Go with a scripting focused batteries included standard lib bundled into a single binary. Babashka can also be used, but is in alpha and lacks windows support. Otherwise, it's the same as Joker, except it is a Clojure interpreter in Clojure with scripting focused batteries included standard libs and compiled into a single binary with GraalVM.

  • If you don't want a complicated install or setup to be able to run your script, use Joker or Babashka, both are single binary, no install.

  • If you want to use Node.js or JavaScriptCore dependencies and libraries, use lumo (for Node), or Planck (for JS Core).

That's pretty much the rundown. They'll all support Clojure core functionality out of the box. What differs is mostly their runtime, and thus what extra libraries you can make use of as well as how fast they start and how quickly they run.

  • Joker is its own runtime with its own set of libraries specifically designed for scripting. Starts super fast, and run at about the speed of Ruby in my tests.

  • Babashka is it's own runtime which includes a set of well known Clojure libs chosen with scripting in mind. Starts super fast. I didn't measure its runtime performance. Probably will be Ruby or slower.

  • inlein uses Clojure as the runtime. It let's you define a self contained Clojure file with lein dependencies defined in it. And adds support for shebang to Clojure scripts. Starts as slow as Clojure and runs as fast as Clojure. Can make use of all Clojure libs.

  • Lumo's runtime is Node.JS. It can use everything npm and Node offers. Can also use self-hosted compatible CLJS libs. Will start and run at similar speeds then Node does.

  • Planck's runtime is JavaScriptCore. It can use self-hosted compatible CLJS libs and JS foreign libs.

4

u/Borkdude Dec 01 '19 edited Dec 01 '19

Babashka can also be used, but is in alpha and lacks windows support. Otherwise, it's the same as Joker

Babashka depends on GraalVM and Windows support is improving, so I expect this to be there in the near future.

Some differences from other scripting solutions:

  • Babashka supports multi-threading operations like pmap and JVM interop for a selection of classes:

$ FOO=123 bb '(System/getenv "FOO")'
"123"
  • Babashka can also be used to execute very small Clojure snippets while integrating well with bash:

$ ls | bb -i '(count *in*)'
12

Read the rationale for more.