Clojure is a dynamically-typed general-purpose programming language with Lisp syntax.
Its features support the functional style of programming with first-class functions and immutable values by default. Using reassignable variables is not as easy in Clojure as in many mainstream languages, since variables have to be created and updated like container objects. This encourages use of pure values that will stay the way they were at the moment they were last seen. This typically makes code much more predictable, testable and concurrency-capable. This works for collections too, since Clojure’s built-in data structures are persistent.
For performance, Clojure supports type-hinting to eliminate unnecessary reflection where possible. Also, groups of changes to persistent collections can be done to transient versions, reducing the amount of objects involved. This is not necessary most of the time, since persistent collections fast to copy since they share most of their data. Their performance guarantees are not far from their mutable counterparts.
Among other features, Clojure also has:
- software transactional memory (STM)
- several concurrency primitives not involving manual locking (atom, agent)
- composable sequence transformers (transducers),
- functional tree manipulation facilities (zippers)
Due to its simple syntax and high extensibility (via macros, implementation of core interfaces and reflection), some commonly-seen language features can be added to Clojure with libraries. For instance,
core.typed brings a static type checker,
core.async brings simple channel-based concurrency mechanisms,
core.logic brings logic programming.
|Version||Change log||Release Date|
|1.8||Latest change log||2016-01-19|
|1.7||Change log 1.7||2015-06-30|
|1.6||Change log 1.6||2014-03-25|
|1.5.1||Change log 1.5.1||2013-03-10|
|1.4||Change log 1.4||2012-04-15|
|1.3||Change log 1.3||2011-09-23|
Installation and Setup
Option 1: Leiningen
Requires JDK 6 or newer.
The easiest way to get started with Clojure is to download and install Leiningen, the de facto standard tool to manage Clojure projects, then run
lein repl to open a REPL.
curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein > ~/bin/lein export PATH=$PATH:~/bin chmod 755 ~/bin/lein
Follow Linux steps above or install with macOS package managers.
Install with Homebrew
brew install leiningen
Install with MacPorts
First install Clojure
sudo port -R install clojure
Leiningen, a build tool for Clojure
sudo port -R install leiningen
Option 2: Official Distribution
Requires JRE 6 or newer.
Clojure releases are published as simple JAR files to be run on the JVM. This is what typically happens inside the Clojure build tools.
Go to https://clojure.org and download the latest Clojure archive
Extract the downloaded ZIP file into a directory of your choice
java -cp clojure-1.8.0.jar clojure.mainin that directory
You may have to substitute the
clojure-1.8.0.jarin that command for the name of the JAR file that you actually downloaded.
For a better command-line REPL experience (e.g. cycling through your previous commands), you might want to install
rlwrap java -cp clojure-1.8.0.jar clojure.main
Option 3: Boot
Requires JDK 7 or newer.
Boot is a multi-purpose Clojure build tool. Understanding it requires some knowledge of Clojure, so it may not be the best option for beginners. See the website (click Get Started there) for installation instructions.
Once it’s installed and in your
PATH, you can run
boot repl anywhere to start a Clojure REPL.
“Hello, world!” in the REPL
The Clojure community puts a large emphasis on interactive development, so quite a lot of interaction with Clojure happens within a REPL (read-eval-print-loop). When you input an expression into it, Clojure reads it, evaluates it, and prints the result of the evaluation, all in a loop.
You should be able to launch a Clojure REPL by now. If you don’t know how, follow the Installation and Setup section in this topic. Once you’ve got it running, type the following into it:
(println "Hello, world!")
Then hit Enter. This should print out
Hello, world!, followed by this expression’s return value,
If you want to run some clojure instantly, try online REPL. For example https://www.tryclj.com/.
Create a new application
After following the instructions above and installing Leiningen, start a new project by running:
lein new <project-name>
This will setup a Clojure project with the default Leiningen template within the
<project-name> folder. There are several templates for Leiningen, which affect the project’s structure. Most commonly is the template “app” used, which adds a main-function and prepares the project to be packed into a jar-file (which the main-function being the entrypoint of the application). This can be achieved with this by running:
lein new app <project-name>
Assuming you used the app-template to create a new application, you can test that everything was setup correctly, by entering the created directory and running the application using:
If you see
Hello, World! on your console, you’re all set and ready to start building your application.
You can pack this simple application into two jar-files with the following command:
“Hello, world!” using Boot
Note: you need to install Boot before trying this example out. See the Installation and Setup section if you haven’t installed it yet.
Boot allows making executable Clojure files using shebang (#!) line. Place the following text into a file of your choice (this example assumes it’s in the “current working directory” and is named
#!/usr/bin/env boot (defn -main [& args] (println "Hello, world!"))
Then mark it as executable (if applicable, typically by running
chmod +x hello.clj).
…and run it (
The program should output “Hello, world!” and finish.
Create a new application (with boot)
boot -d seancorfield/boot-new new -t app -n <appname>
This command will tell boot to grab the task
boot-new from https://github.com/seancorfield/boot-new and execute the task with the
app template (see link for other templates). The task will create a new directory called
<appname> with a typical Clojure application structure. See the generated README for more information.
To run the application:
boot run. Other commands are specified in
build.boot and described in the README.