Learning Clojure Part 1: Preparing local setup

Pavel Polívka - Jan 4 '21 - - Dev Community

In the spirit of learning new things, this year I decided to learn a new programming language. I scrabbed my backlog of this to do and learn and found that I want to learn Ruby and Clojure. I went to Twitter for help to decide what would be better for me. As the headline of this article suggests I decided on Clojure. The reason behind this is that Ruby is more of the traditional C like language, I would not be learning that much new. Clojure (Lisp dialect) is different to force you to think differently about the code. This could help me improve my coding overall.

What is Clojure

When we are talking about Clojure we need to distinguish between Clojure language and Clojure compiler. The Clojure language is a Lisp dialect independent of any implementation. The compiler is executable jar file clojure.jar. This jar file takes code written in Clojure language and compiles it to JVM bytecode. (There are other compilers to JavaScript, CLR, etc..) This means that Clojure relies on JVM for core JVM features like threading, garbage collections, etc... Same like Scala, Kotlin, Groovy, etc...

Local Setup

Build tools - Leiningen

It's 2020 so you need to have some kind of build tools to work with your languages. Java has Maven, JavaScript NPM, and Clojure has Leningen. We will go over step by step how to install it. And use it to create a small project.

Installing

First things first you need to have Java installed. Make sure of it java -version.

Now we create a directory for it.

sudo mkdir /Applications/clojure
Enter fullscreen mode Exit fullscreen mode

Now we download the install script and make it executable.

cd /Applications/clojure
sudo curl -O https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
sudo chmod +x lein
Enter fullscreen mode Exit fullscreen mode

And we add the new directory to $PATH.

cd ~
echo "export PATH=$PATH:/Applications/clojure" >> .profile
Enter fullscreen mode Exit fullscreen mode

Close the current terminal and open a new one to activate the changes.

Generating new project

Now we can create a new Clojure project.

lein new app humble-beginning
Enter fullscreen mode Exit fullscreen mode

It creates a directory structure that looks like this.

humble-beginning/
├── CHANGELOG.md
├── LICENSE
├── README.md
├── doc
│  └── intro.md
├── project.clj
├── resources
├── src
│  └── humble_beginning
│  └── core.clj
├── target
│  └── default
│  └── classes
└── test
 └── humble_beginning
 └── core_test.clj
Enter fullscreen mode Exit fullscreen mode

Most important files right now are project.clj and src/humble_beginning\core.clj.
The first one is pom.xml equivalent. It's a configuration file for Leiningen, it's talking about dependencies, what program to run, etc... The second one is a source code file with our first main method.

Running the code

To run this awesome program we can do.

lein run
Enter fullscreen mode Exit fullscreen mode

If you want to distribute your Clojure code, you can compile it into a normal jar file by running.

lein uberjar
Enter fullscreen mode Exit fullscreen mode

IDE

There are tons of IDEs to help you with Clojure. My IDE of choice for everything is IntelliJ IDEA so I naturally started there. There is an amazing plugin for Clojure called Cursive. It has all you need, integrates with Leiningen, etc... If you have IntelliJ I definitely recommend using this.


You can follow me on Twitter to get more content like this.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .