Programming Languages from a Javascript Lens ep.4 — Clojure
Welcome back to my series Programming Languages from a Javascript Lens where once again I will talk about a different programming language every week, discuss their origin stories, go over some basic fundamentals of the language, and give my opinion on them from the perspective of myself, a Javascript developer. In this episode, we will be going over a respected language built off the Java Virtual Machine: Clojure
Clojure is a dynamic, general-purpose programming language. The language was created by programmer Rich Hickey in 2007 with it’s most recent release being June 6th of 2019. It’s intended to facilitate developing more robust, concurrent programs that are very simple and fast. In fact, the functional language was intended for concurrency.
Clojure is a functional dialect of the Lisp programming language and considered to be apart of the Lisp family. Lisp is the second oldest programming language in the game, created in 1958 by John McCarthy. During it’s prime it was considered the programming language for Artificial Intelligence research. Present-day Lisp has many dialects that are more widely used than the originator. Clojure being a functional dialect means that it is designed to support programming in a functional style. It has a Lisp macro system, interpreting all code as data.
Something that makes Clojure quite unique is that it is intended to be a hosting language. Clojure was created to take advantage of other existing platforms and built on top of them. It was created off the Java Virtual Machine, similar to how Elixir was built off the Erlang Virtual Machine. All of Clojure’s functions compile to JVM bytecode. Clojure also has the ability to access any Java library or framework, you can implement Java code inside Clojure and Vise-Versa.
Now we move on to the hands-on stuff:
Setting up Clojure
The installation process for Clojure has a bit more steps than usual for you to use the language. A major prerequisite is that Java needs to be installed as well. You can verify if Java is installed by inputting this into your terminal: java -version
. If the output gives you either -bash: java: command not found or No Java runtime present (cancel the prompt that offers you to install it), then this means you need to install it. There are 3 things needed to complete the Clojure set-up:
1. Java (JDK)
2. Leiningen: project tool for Clojure (makes Clojure easy to use)
3. Clojure
To install Java we need to go to this site which will allow us to install the JDK: https://adoptopenjdk.net/. Then we will see this:
After clicking “Latest Release” a pkg file should pop up on the bottom of the browser indicating it is a package like so:
Then we want to click that package to get us to the Installation interface:
Proceed through the steps until everything is finished then you can throw the prompter away if you’d like. To verify if we have successfully installed it we can run this line one more time:
java -verion
This would be the expected output indicating we have successfully installed Java:
Now we can install Leiningen. Leiningen is a build automation and dependency management tool for Clojure applications. It is not required to develop Clojure applications but it makes it much easier. This article displays the benefits of using Leiningen and compares the process of building a Clojure application with and without the tool: http://www.flyingmachinestudios.com/programming/how-clojure-babies-are-made-lein-run/.
Leiningen can be easily installed using Homebrew:
brew install leiningen
And we can verify our installation via:
lein version
~expected output:
Leiningen 2.9.1 on Java 11.0.4 OpenJDK 64-Bit Server VM
And finally, we install the thing that we did all of this for, Clojure. We can install this with Homebrew as well like so:
brew install clojure
Verify:
clj
~expected output:
Clojure 1.10.1
user=>
If you are using a different operating system than MacOS you can follow this source here: https://purelyfunctional.tv/guide/how-to-install-clojure/#mac. Now we are all set up to use Clojure!
This short read shows you how to generate some boiler code and output things to the terminal using the lein CLI: https://www.braveclojure.com/getting-started/.
Basic Clojure Syntax and Fundamentals
Declaring variables in Clojure are a bit different than declaring in Javascript. The syntax for declaring would be structured like this:
(def var-name var-value)
the keyword def would signify you defining a variable, and one thing to notice is that variables are declared inside parentheses. Whereas JavaScript’s syntax would be :
var/let/const var-name = var-value
With JavaScript you have the option between the var, let, or const keyword to define your variable. Then like Clojure, you create the variable name then assign the name to the value with the equal sign. We can display key differences with our traditional “age” example here:
Creating functions got a bit confusing for me since the syntax and structure is so different but if you keep it basic you can somewhat get a feel on how functions should be built out:
A Javascript Developers take on Clojure
Back in week one with Elixir, my main personal concern was that there were no brackets to showcase your separations. You had to strictly rely on new lines and indents. With Clojure though, it’s a parentheses nightmare. I personally dislike the syntax but I understand why some may prefer it. There is one thing I personally find interesting about Clojure, ClojrueScript. ClojureScript is a version of Clojure that compiles to JavaScript. You can acquire more information on ClojureScript here: https://glossarytech.com/terms/front_end-technologies/clojurescript. I can see many benefits of using Clojure, having access to all of Java’s libraries and frameworks, but I personally would want to know why I wouldn’t just use Java in that case. Clojure also is not that high in demand in the industry at all. It almost seems as if the overall number of Clojure devs are decreasing each year since the language isn’t what most companies are looking for. Overall, Clojure and other JVM’s seem like a very beneficial and intriguing language, but from a Java developer’s standpoint. I would like to get into Java itself and see why I would choose a JVM language over Java. From a Javascript developers perspective, I rate the Clojure programming language: 4/10.