Programming Languages from a Javascript Lens ep.4 — Clojure

Alex Ruben
6 min readOct 11, 2019

--

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:

For “Choose a Version” select the second option being “OpenJDK 11 (LTS)” then select “HotSpot” for the JVM. After choosing the two options click The blue button that says “ Latest release

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:

Here we define our variable age on line 3, wrapping the statement in parentheses as it is mandatory. And to output something onto the console we can use the keyword “println”. String interpolation is simplistic as well without the need for any dollar sign or curly braces.
As usual, we use let to declare our variable “age” then output to the console with “console.log”, utilizing string interpolation with the curly braces and dollar signs.

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:

With Clojure functions, we would wrap the whole thing in parentheses first, then initiate the function declaration with the keyword “defn”, next would be the function name and lastly the parameter/s in brackets. In the code block, we have a conditional statement that is structured similar to Javascript’s ternary operator. Inside the condition essentially says “if number is less than 100”, although the formatting is a bit odd since the comparison operator is before the parameter. And those purple parentheses, being the conditional statement, will ultimately give us the returned value once called. In the second function, the keyword is a bit different to showcase another way to define a function. Here we initialize the function with “def” rather than “defn” and in a separate line, we then use “fn” to define the parameters. On line 12, we create a variable named fullName, setting it equal to the two parameters concatenated. Then on line 13, we outputted the variable to the console with println then lastly returned it on line 14.
This is simply the Javascript equivalent to the two functions created in Clojure above.

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.

--

--