Babashka is a great Clojure entry point
Posted on October 8, 2023 • 3 minutes • 514 words
There are a lot of great ways to get started with Clojure. The community around Clojure and the love they feel for the language has driven numerous resources, books, websites, and tutorials aimed at helping newcomers get started. One easy way to take Clojure for a test-drive is Babashka .
Approaching Clojure for the first time
Everyone learns differently. My first experience with Clojure was following a tutorial that used Leiningen to produce a “hello world!” project. I had this up and running within seconds (not counting installing a few prerequisites). The experience was smooth, easy, and fast largely due to all the power and refinement Leiningen brings to the table.
With this project, I was able to begin editing that single source
.clj file, tweaking it and trying out a few things then relaunching the program each time to see the results (this was before I had any concept of the REPL).
A lot of other things were put in place by Leiningen when the project was created. It produced a
project.clj file which made little sense to my new eyes, along with a decent starting directory structure for the project including a test directory, a resource directory, doc directory, license file, …etc. At this point in time, I didn’t understand exactly how these pieces all fit together. I’m certainly familiar with this sort of structure and scaffolding from other languages and frameworks, but I still had a lot of questions. After a bit of time, I unraveled each piece of what Leiningen had done and gained understanding of why. Overall this was a good experience, but at the time I think I was hoping for something even simpler.
Babashka as a your “hello world!” experience
Per the website, Babashka is a scripting environment made with Clojure, compiled to native with GraalVM. It starts up super quick and can serve as a your Clojure environment. It’s especially adept at testing things out as well as writing scripts or smaller programs. For a more in depth explanation of how it works and a deeper understanding of what it is, see the Babashka Book .
If you have brew installed as package manager you can install Babashka with a single command:
brew install borkdude/brew/babashka
Once installed you can open your favorite editor and write your first script. Babashka has a ton of libraries “baked in”, so to get started you won’t need anything more than your single script file.
The demo above shows me installing Babashka, creating a small script to get the weather from an online service, and then running that script.
(require '[cheshire.core :as json])
(def latitude "52.52")
(def longitude "13.41")
(let [url (str "https://api.open-meteo.com/v1/forecast?latitude="
data (json/parse-string (:body (curl/get url)))]
(doseq [[key value] (get data "current_weather")]
(println (str key ": " value))))
Clojure is a great language with an excellent community around it and you have lots of options if you want to take it for a quick spin. Running your first few lines of Clojure with Babashka is one great way to get started.