r/Clojure • u/didibus • 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.
1
u/ws-ilazki Dec 01 '19
Agreed, though I'm willing to learn the differences if the language meets my needs otherwise. It would be preferable to have some kind of ClojureNative, but I'll take what I can get.
Joker's different and more limited by design, though, so trying to use it for normal scripting is going to end up in a similar place as using Janet, just perhaps with fewer superficial differences. Haven't tried Babashka so I don't know if that's also the case with it or not.
What would be ideal for me is some sort of ClojureNative that is Clojure(script) compatible but replaces the Java/JS interop with some form of FFI, but I understand that isn't Clojure's purpose, so I don't criticise it for not being exactly what I want. GraalVM is an interesting alternative to that for its potential to make proper Clojure applications fast, though I'm (possibly irrationally) wary of due to Oracle and its licensing.
Anything I expect to execute interactively. I generally make things that I either run directly from the shell, or run indirectly through another command or shortcut (e.g. inside a script or fired off by a global hotkey). Half second (at best) delays between command invocation and any sort of program responsiveness feels absurdly bad for anything being run interactively like that.
One example that I used as a test of a few lisp dialects for scripting potential like that was a script that cycles predefined volume levels. Anything that took more than a tenth of a second to do its job felt like my system was misbehaving and laggy. With some JVM tweaking I could get Clojure down to about half a second, so that it was slightly faster than ClojureScript via lumo, but a half second delay between pressing a global shortcut and hearing its effect felt terrible. Racket wasn't any better until I took an extra step to compile it, at which point it was floating just above the "feels fast enough" threshold. I was able to get Gauche, a scheme dialect targeted specifically at scripting, below that threshold.
One day when I'm bored I should take another stab at that with some other options and see how they do sometime, it's a fairly simple task with a not-too-demanding performance need, and makes a decent test of how convoluted it is to get execution speed down to something reasonable for my tastes.
Right now, though, I'm just using OCaml for these things. I miss macros and some of the lisp interactivity, but I really like the type system, makes it feel like I'm using a scripting language at times. Especially since I can use a shebang to run programs like scripts until they're closer to done and then switch to a proper compile-and-run cycle (which still compiles really quickly) later on.