I made a Clojure application to allow a user to visualise the Newton method of calculating the square root of a number. The user is prompted for a number, and a precision. The program will show the user each estimation that was taken up until an estimation was made that was deemed ‘good enough’ under the given precision.
The source code is available here. I don’t have a deployment working yet but will try to get one up, and running after I get my server reorganised.
I originally became aware of the Newton method through the excellent Structure and Interpretation of Computer Programs which uses it as one of its examples. I was looking for a small scale project to complete in order to practice creating web apps in Clojure, and thought this would be a perfect example. I would normally code this sort of application as a frontend application since the processing being done here is incredibly basic, and doesn’t really need to be done on a server. But this was moreso an exercise in creating a backend with Clojure as I’ve already had a go at creating a frontend application using Reagent.
Creating a backend web application in Clojure seems to be a very different beast from any other programming language. In Python, we have frameworks such as Flask, and Django. In Ruby, Rails has proved to be a very popular framework, and it is one of the major reasons someone might wish to use the language. However, the approach of frameworks doesn’t seem to be as common in Clojure. That’s not to say frameworks don’t exist, but rather the approach more favoured in Clojure is to use individual libraries to achieve what you want to.
Most (but not all) Clojure backends start off with Ring. Its allows you to turn a HTTP request into a map data structure in Clojure which we can easily pass around our application, and read data from. For this, we’ll also need some server software to accept the requests, and for this application I’ve chosen Jetty.
Most backends will also need a routing mechanism. I’ve chosen the Reitit library for this purpose. It supports Ring but interestingly it can also be integrated in frontend applications as well to provide client-side routing.
Finally, for this application we also want a HTML templating engine. There are two main options in Clojure: Selmer, and Hiccup. Selmer takes an approach similar to Jinja from the Python world where we write our HTML as normal but use macros in order to add data from our backend to the template. Hiccup, however, uses a different approach. Instead, we write our DOM using Clojure data structures, and then Hiccup will convert this into a raw HTML string. This is actually a library which Reagent uses as well, and is one of the reasons I find Reagent to be so powerful. It allows us to embed Clojure code directly in our website’s DOM as, well, we’re already writing it in Clojure. Selmer can still be a popular approach when you’re collaborating with someone who doesn’t know Clojure. If I were to ask someone to create the HTML for this website, with Selmer they wouldn’t have to code any Clojure; they’d just need to understand Selmer’s macros.
I really enjoyed the process of creating a web application in Clojure. Particularly, taking advantage of repl-driven development to quickly test new code was indeed very powerful. Next, I want to create an application that queries a database all in Clojure. I’m also looking into enhancing this application with HTMX. Additionally, I want to learn to take advantage of Integrant in a web application as well.