clojure Getting started with clojure

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

enter image description here

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.

Designed as a hosted language, it can interoperate with the platform it runs on. While the primary target is JVM and the entire ecosystem behind Java, alternative implementations can run in other environments too, such as ClojureCLR running on the Common Language Runtime or ClojureScript running on JavaScript runtimes (including web browsers). While alternative implementations may lack some of the functionality from the JVM version, they are still considered one family of languages.

Versions

VersionChange logRelease Date
1.8Latest change log2016-01-19
1.7Change log 1.72015-06-30
1.6Change log 1.62014-03-25
1.5.1Change log 1.5.12013-03-10
1.4Change log 1.42012-04-15
1.3Change log 1.32011-09-23
1.2.12011-03-25
1.22010-08-19
1.12010-01-04
1.02009-05-04

"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, nil .

If you want to run some clojure instantly, try online REPL. For example http://www.tryclj.com/.

"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 hello.clj ).

#!/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 (./hello.clj ).

The program should output "Hello, world!" and finish.

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:

lein run
 

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:

lein uberjar
 

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.

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.

Linux

curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein > ~/bin/lein
export PATH=$PATH:~/bin
chmod 755 ~/bin/lein

OS X

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

Install Leiningen , a build tool for Clojure

sudo port -R install leiningen
lein self-install

Windows

See the official documentation.

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.

  1. Go to http://clojure.org and download the latest Clojure archive

  2. Extract the downloaded ZIP file into a directory of your choice

  3. Run java -cp clojure-1.8.0.jar clojure.main in that directory

    You may have to substitute the clojure-1.8.0.jar in 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 : 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.



Got any clojure Question?