One of the services my current team maintains was written in Clojure. Being a big fan of meetups, user groups, conferences, and brown-bag lunches, I have had the occasion to read some Clojure code in the past and write some hello worlds.
Given today we have this project, it is certainly a good time to learn Clojure for real!
I started then doing some research about Clojure, mainly about things one needs to know to get started. I turned some questions I’ve asked myself into a FAQ, which I’m publishing here mainly for self reference. If my initial research ends up helping someone else as a side effect, even better 😀
If you think something is not quite accurate or something really important is missing, let me know in the comments.
What are the language’s fundamentals?
- Clojure is based on Lisp
- it is a general purpose language
- it is a functional programming language
- has dynamic type system
- runs on the JVM
- is open source
- was created by Rich Hickey
Does it have a build automation system?
Leiningen is for the Clojure ecosystem what Maven and Gradle are for Java. It helps developers to scaffold new projects, resolve dependencies, run tests, etc. Leiningen has a command line interface called lein.
$ lein help Leiningen is a tool for working with Clojure projects. Several tasks are available: change Rewrite project.clj by applying a function. check Check syntax and warn on reflection. classpath Print the classpath of the current project. clean Remove all files from project's target-path. compile Compile Clojure source into .class files. deploy Build and deploy jar to remote repository. # ...and many other tasks
Most Leiningen tasks apply in the context of a project. A project is a directory containing all the resources of an application, like source files, scripts, etc. The project’s metadata is stored in a file called project.clj, which lives in the project’s root directory.
Here‘s a nice tutorial about Leiningen.
There’s also this other tool called Boot, but I haven’t look at it yet.
How do I install Clojure?
Clojure is a library managed as part of a project and Leiningen is the user interface to that library.
So if you are using MacOS, you can use Homebrew to install it:
brew install leiningen
How do I create a Clojure project?
You can use Leiningen to generate the scaffolding of a new Clojure application:
$ lein new app my-project Generating a project called my-project based on the 'app' template.
The example above uses a template called app, which creates applications. It is also possible to use templates to create libraries.
Where can I learn Clojure?
To get a quick overview, I’d recommend Learn X In Y Minutes. Next, I’d recommend this post Clojure From The Ground Up: Welcome, by Kyle Kingsbury, a.k.a “Aphyr”. Clojure From The Ground Up has other chapters worth going through as well.
How do I get my feet wet?
If you cannot install Clojure/Leiningen, you can go to tryclj.com and start playing online.
There’s also the 4Clojure website where you can learn and practice by solving problems.
Otherwise, Leiningen has a REPL (Read-Evaluate-Print-Loop):
$ lein repl nREPL server started on port 55517 on host 127.0.0.1 - nrepl://127.0.0.1:55517 REPL-y 0.3.7, nREPL 0.2.12 Clojure 1.8.0 Java HotSpot(TM) 64-Bit Server VM 1.8.0_144-b01 Docs: (doc function-name-here) (find-doc "part-of-name-here") Source: (source function-name-here) Javadoc: (javadoc java-object-or-class-here) Exit: Control+D or (exit) or (quit) Results: Stored in vars *1, *2, *3, an exception in *e user=>
If you want to start a REPL without using Leiningen, check this link.
Where can I find documentation?
There’s also the popular ClojureDocs. ClojureDocs is a community driven website where you can find documentation and examples.
How do I write tests in Clojure?
Clojure has its own unit testing library clojure.test. It is somehow limited IMHO, but it does the job.
There’s another library called Midje, which, from what I’ve understood, it’s quite popular within the Clojure community. Midje aims to provide a more flexible and readable style of testing compared to clojure.test.
How do I get code coverage?
Cloverage seems to be the most popular tool to extract code coverage.
From where Clojure libs come from?
As of today, Leiningen defaults to two repositories:
Clojars is the default repository for Clojure libraries.
I found some interesting points about Clojars in this InfoQ interview with Alex Osborne, the creator of Clojars.
Which text editor or IDE should I use?
Being a Vim user for a while, I am going with Neovim + a couple of plugins:
Are there nice talks I can watch?
Among the talks I watched, I would recommend:
- Clojure for Java Programmers Part 1 – Rich Hickey
- Clojure for Java Programmers Part 2 – Rich Hickey
- Radical Simplicity – Stuart Halloway
The Changelog podcast has also compiled this required viewing list of videos from Rich Hickey.
And finally, there’s this ClojureTV channel on Youtube where you might find some interesting stuff as well.