All Articles

Relearning Clojure One Step at a Time: First Trial

Call to Adventure

Man on a mountain summit - Photo by Wojciech Then on Unsplash

Learning is an integral part of a developer’s life. One might have heard from a colleague about an interesting new framework. Some might want to deep dive on what they’ve learned new from a user group meeting. Many learn old technologies because of work.

Me? I wanted to discover the joy I found the first time I went through my Clojure adventure.

Crossing the First Threshold

Two girls spraying the same line for start and finish - Photo by Aw Creative on Unsplash

So where do we start? The official website of Clojure seems a logical starting point. The getting started section, introduction to the REPL (first 3 sections), and the basic tutorial will allow us to get the basics down.

If you feel like https://clojure.org/ is not for you, Clojure for the Brave and True might be up your alley. You can read through the whole book or you can end your first run through with Chapter 8.

Those two options will prepare you for your first Clojure web application. If you need more resources, the community section of Clojure’s website is your friend.

And if you are asking yourself if learning Clojure in 2020 is worth it, take a look at the state of Clojure 2020.

The Road of Trials

Two man running in a triathlon - Photo by Victoire Joncheray on Unsplash

Ready to dive deeper? We’ll definitely do something more complex than the perennial “Hello World” exercise. But we wouldn’t go straight to creating a web application for an e-commerce site.

The subject of our web application is Spotify’s Squad Health Check Model. I’ve recently facilitated our team’s squad health check with Google forms. Unfortunately, the free tool that we’ve been using was not working that time.

So with that in mind, I thought it was an easy concept to build as a web application. We need to present a form, collate the answers of each team member, and display a summary. We won’t care about authentication at this point. We can just present each member with the same form and collect the answers for that form.

The First Trial

We will need at least 5 components for our application:

  • A HTTP server
  • web application library
  • datastore
  • HTML
  • build tool

We can use Ring as the HTTP server and web application library. Ring is the popular basic building block for web applications. We can use it on its own. But as suggested in their wiki, I chose Compojure. Compojure is a routing library built on top of Ring.

I wanted to make this first exercise as simple as it can be. So I decided not to use any permanent datastore and opted to use an atom. Atoms don’t have the overhead of learning something else outside of the Clojure language. But it has a certain complexity that will teach us more about Clojure.

Lastly, there are two popular build tools that I know of and are in the toolbox: Leiningen and Boot. I used Leiningen before, but for this exercise I chose building upon deps.edn.

A useful place to learn about the different libraries that we can use is The Clojure Toolbox.

I’d like to go deeper into the technical nitty and gritty. But it’s better to stop here and give you time to let everything sink in.

You can visit my GitHub repo and use it as a guide. It uses additional components. Hiccup for HTML rendering. environ for accessing environment variables. It’s also deployable to Heroku. A deployed version is at http://shc.bobsantosjr.com/. Be warned though that it might look different in the future.

Feel free to use different components. The most important thing is to take it one component at a time. Learn how to accept a request and return a response. Dive into persisting data by using different data structures. It’s also useful to read docs and others’ code.

I’ll see you at the Second Trial, fellow Clojure adventurer.