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.
8
u/dkvasnicka Nov 29 '19 edited Nov 29 '19
Can I troll this subreddit by adding a not-so-Clojure alternative to the list? ;) https://docs.racket-lang.org/rash/index.html
2
u/didibus Nov 30 '19
The thing about just using another Lisp like Racket or Janet or Hy or Fennel is that the core set of functions and macros are different. So you have to re-learn that. And sometimes even the literals and syntax differs.
For scripts, you can generally manage by googling here and there and figure out what you need for any language. But that's true of me using Python, Perl, Bash, Lua et all as well.
So for me to start using Racket for scripting is the same as me trying to use Ruby for scripting. I'll have to look up their references either way and google how to do anything.
That's why all the above options I listed are more appealing to me. I already know Clojure, so I can use all those without needing to lookup anything. So I'm done writing my script a lot quicker.
4
u/ws-ilazki Nov 29 '19
There's also Janet, which is isn't Clojure but has taken some inspiration from it. Fast start-up and a self-contained static binary that can be used almost anywhere, so it's convenient to keep around even if you can't just write normal Clojure in it.
Unfortunately, the problem with most of the proper Clojure options is that start-up is far too slow for anything you plan to run interactively. Aggressive JVM tweaking still only gets you to about the same speed as using lumo or planck, which is still noticeable and annoying. The options that are faster always seem to either be more limited or just look-alikes like Janet.
2
u/victorbjelkholm Nov 30 '19
Seems like future GraalVM will be the best option for hobby/open source projects wanting to use Clojure and have binaries that start fast. GraalVM still have a long way to go though.
1
u/didibus Nov 30 '19
It doesn't have as much a way to go as you think. Beyond windows support, it's pretty much usable.
1
u/didibus Nov 30 '19 edited Nov 30 '19
Janet looks nice, but like I just replied elsewhere, it's still not Clojure, so I have to re-learn the core functions and macros and all that, which doesn't make it as appealing.
I think you're wrong about your second point. That was true a year or two ago, but is no longer the case. Joker and Babashka can both be used to write scripts that start really fast (faster than Node and JSCore). And for command line applications, GraalVM is really changing the game, and can be used now to compile fast startup executables. In my tests, it matched up pretty well with Go in startup time. Much faster than Node and Ruby. Joker and Babashka can also be used to write command line apps, but while they'll start fast, and run comparatively fast (in the realm of say ruby), a GraalVM app will start and run really fast (in the realm of go).
I'm also curious, what command line app do you write that needs to start that fast? Like I mean faster than Node or JavaScriptCore?
1
u/ws-ilazki Dec 01 '19
Janet looks nice, but like I just replied elsewhere, it's still not Clojure, so I have to re-learn the core functions and macros and all that, which doesn't make it as appealing.
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.
I think you're wrong about your second point. That was true a year or two ago, but is no longer the case. Joker and Babashka can both be used to write scripts that start really fast (faster than Node and JSCore)
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.
I'm also curious, what command line app do you write that needs to start that fast? Like I mean faster than Node or JavaScriptCore?
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.
3
u/Eno6ohng Dec 01 '19
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.
~> time babashka -e '(println "hello")' hello babashka -e '(println "hello")' 0.00s user 0.01s system 99% cpu 0.013 total ~> time joker -e '(println "hello")' hello joker -e '(println "hello")' 0.05s user 0.11s system 114% cpu 0.139 total2
u/Borkdude Dec 01 '19
Haven't tried Babashka so I don't know if that's also the case with it or not.
Babashka tries to be as compatible with JVM Clojure as it can, so you can easily switch between the two. E.g. see this portable script:
https://github.com/borkdude/babashka/blob/master/README.md#portable-tree-command
1
u/Eno6ohng Dec 01 '19
While you are here, a couple of unrelated questions if I may:
Can Babashka be extended to run in a closh-like mode? I like Closh, but JVM still feels too heavy for a bash killer. Should it be done in a separate project? A fork of Closh maybe?
Why Babashka doesn't have a repl client? To avoid overhead?
Can Babashka support extra dependencies via deps.edn git provider?
2
u/Borkdude Dec 01 '19
Babashka is built using the sci library which can be used as an "eval" for GraalVM:
https://github.com/borkdude/sciCurrently closh is working towards a native build using sci.
You can use
rlwrap nc 127.0.0.1 1666as a socket-repl client, but personally I feel connecting from an editor and evaluating expressions gives a much better experience. What's also an option is to develop a script using normal JVM clojure first and then see if it runs with babashka, tweaking it where needed. Babashka supports reader conditionals and takes the first:bbbranch, or:defaultotherwise.You can use external scripts using
load-file. I am considering loading files from classpaths generated by deps.edn, but it's not there yet.1
u/Eno6ohng Dec 01 '19
Currently closh is working towards a native build using sci.
Awesome! The related issues for others reading this to follow:
personally I feel connecting from an editor and evaluating expressions gives a much better experience.
In general, I agree; at the same time, when trying out bb for the first time I was somewhat confused that I don't see an option to start a repl, like in all other clojure distributions. Not a big problem of course, just a minor 'principle of least surprise'-related quirk/feature.
I am considering loading files from classpaths generated by deps.edn, but it's not there yet.
Cool! I'll be definitely following the project more closely. Thank you! (and thanks to didibus for bringing extra attention to all these projects)
3
u/Borkdude Dec 01 '19
when trying out bb for the first time I was somewhat confused that I don't see an option to start a repl,
Might be a nice feature to add, I agree. I'll make an issue for it.
1
u/Borkdude Dec 08 '19
when trying out bb for the first time I was somewhat confused that I don't see an option to start a repl
This feature has now been added to babashka:
1
1
u/ws-ilazki Dec 01 '19
Cool, that could be a good option for some stuff then. Shame about no external libraries, but I'm guessing that's a GraalVM limitation, right?
1
u/Borkdude Dec 01 '19
Correct. There may be an option to have libraries, but that's probably a giant rabbit hole :-) https://github.com/lread/clj-graal-docs/issues/15
1
u/ws-ilazki Dec 01 '19
Yeah, about what I figured. Still, cool project despite that.
I'll eventually get around to trying out making projects with GraalVM myself, since that's probably the path that I need to take to get everything I want, especially once it becomes a viable route to make fast-starting Android applications in Clojure, which is one of the things I've always wanted to use it for. I don't have a lot of free time, though, so I've been holding off for now.
1
u/Borkdude Dec 08 '19
Btw, is possible to add dependencies to a fork of babashka relatively easy and compile it with those.
2
Jan 03 '20
A minimal janet script starts up absurdly fast. I think janet competes with babashka in terms of startup time.
3
u/thenackjicholson Nov 29 '19
Is lein-exec no longer in favor? I recently used it.
1
u/didibus Nov 30 '19
I prefer inlein over it personally, but you're right, it's another option as well.
3
u/Eno6ohng Nov 30 '19
I wish Joker supported the usual nrepl/cider tooling and had (out of the box) the ability to create go-like single-file executables (basically a bundle of joker executable and clj source files to interpret). That'd make it the best way to start with clojure imo.
1
u/didibus Nov 30 '19
Eh 😅, that's a lot to ask, but yeah that would be neat.
It does support a socket repl and has a basic REPL at the command line. Which mean you can use Atom with Chlorine for it. Or Emacs with inf-clojure.
I believe the nRepl and co machinery makes use of Java facilities. So I think that would make a port to Joker hard.
I like that bundling idea. I feel it's possible there exist already a bundler that could do that. Take arbitrary files and pack them in a single executable with one file being selected as the main entry.
1
u/Eno6ohng Dec 01 '19
I believe the nRepl and co machinery makes use of Java facilities. So I think that would make a port to Joker hard.
Yeah, unfortunately. It also uses parallel features that are lacking in Joker.
Take arbitrary files and pack them in a single executable with one file being selected as the main entry.
Yeah, basically just an archive with executable headers, similar to how people bundle jars sometimes. Even if scripting per se can be considered an (almost) solved problem, it still requires quite a bit of env setup; the ability to create a single portable binary would solve this, and also a 'getting started' guide could be reduced to like 3 lines ("download this, save your file, run this command; boom that's all!"), which is neat for newcomers.
1
u/Eno6ohng Dec 01 '19
Soo, actually I think I'm totally wrong on this one, the best way to do it is to use babashka + any 3rd-party bundling tool, like https://makeself.io or something similar. You can "build" a single binary in a couple of minutes, and potentionally you can use jvm clojure for development phase so you have all the cider goodies (that won't be too friendly for newcomers though due to the differences between dialects). Also another problem is that babashka doesn't ship ARM binaries (yet?) so you can't put your clojure scripts (not clojurescripts, clojure _ scripts) on your raspberry (yet), but graal support is already there as far as I understand, so it should be doable... in any case graal+sci for sure seems like THE best way forward, with git deps support and overall better tooling it would be a totally viable option not just for scripting, but for any small(ish) apps as well.
The only thing I'm left wondering about is whether Oracle would get to precompiling the whole maven central someday, because dealing with java dependencies on yourself is not that much fun...
2
u/danielszm Nov 30 '19
Nice list +1
On top of that, just a reminder that regular Clojure code can compile down to native code via graalvm. That's what I do for scripts. That and using org.clojure/tools.cli
2
u/didibus Nov 30 '19
I think people have different definitions of scripting and what a script is. I don't personally consider command line applications to be scripts or scripting. That's why I didn't mention that. But yes, that's totally an option now and I've used GraalVM to write command line apps as well, it's awesome.
11
1
Dec 30 '19
I think I will try closh, babashka, and joker. I was disappointed by lumo and planck because it takes 200~500 milliseconds to start the simplest script in lumo or planck. And, they make it difficult to make linux distribution packages by downloading a bunch of files from various places instead of specifying dependencies explicitly.
Inlein would be slow to start up, too. So, it's going to use either babashka or joker for clojure scripting. Closh might become a good replacement for fish shell in the future.
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.