We wrote this book for
you.

We've worked with Clojure
for many years now, and have enjoyed using it to build
projects, websites, products, and businesses of all kinds. We love
the language, and we think you will too.

Clojure is a dynamic, general-purpose programming language that runs
on the Java Virtual Machine that offers superior interactive
development with the speed and reliable runtime of the JVM.
Clojure Programming is our attempt to get you up to speed
as efficiently and pleasantly as possible.

To ease your way, we included examples throughout the
book to help relate aspects of Clojure to their analogues in Java,
Ruby, and Python; these landmarks should help you keep your bearings
throughout your new language adventures. Why those languages and not others?
First, because nearly all programmers know at least one of them; and second,
Clojure has a great deal to offer to Java, Ruby, and Python developers:

Engaged Java developers are usually found
working in demanding environments solving nontrivial, often
domain-specific problems. For you, you'll find Clojure to be
welcome relief. It runs on the JVM with excellent performance
characteristics, interoperates with all of your existing
libraries, tools, and applications, and is simpler than
Java yet remains demonstrably more expressive and more concise.

Ruby and Python developers are usually unwilling to
compromise on language expressivity, developer happiness, and the joys of
working within a thriving community. Clojure is a natural next step for you.
It delivers on these things in spades, but runs on a more capable execution
platform with better runtime performance and a larger selection of libraries
than those available for either Ruby or Python.

In the end, we hope you
find the book as accessible and as thought-provoking as Clojure
itself.

Brought to you by…

Chas has been a consistent presence in the
Clojure community since early 2008. He has made contributions
to the core language and been involved in dozens of Clojure
open source projects. Chas writes about Clojure, software
development, entrepreneurship, and other passions at cemerick.com.

Christophe was a long-time FP enthusiast
lost in Java-land when he encountered Clojure in early 2008
and it was love at first sight! He authored Enlive, Parsley, and Moustache, all
widely-popular open source Clojure projects. As an independent
consultant, he develops, coaches and gives trainings in
Clojure, and writes about the language at clj-me.cgrand.net.

Brian is a Ruby programmer turned Clojure
devotee. He’s been programming Clojure since 2008, using it for
projects at home and at work, for everything from web
development to data analysis to GUI apps. He is the author of
Gaka (a
Clojure-to-CSS compiler), and Oyako, (a
Clojure ORM). He writes about Clojure and other topics at briancarper.net.

Examples and Sample Code

All of the code from the book (including sample projects,
inline REPL interactions, etc) is available for browsing,
download, cloning, and so on from Github.
We will continue to keep that repository alive, so that the
code therein is up to date, compatible with any future
releases of Clojure, and so on. And, if you see any problems,
please don't hesitate to open an issue.

Table of Contents

Down the rabbit hole

Why Clojure?

Obtaining Clojure

The Clojure REPL

No, parentheses actually won’t make you go blind

Expressions, operators, syntax, and precedence

Homoiconicity

The Reader

Scalar Literals

Strings

Booleans

nil

Characters

Keywords

Symbols

Numbers

Regular Expressions

Comments

Whitespace and Commas

Collection Literals

Miscellaneous reader sugar

Namespaces

Symbol evaluation

Special Forms

Suppressing evaluation: quote

Code blocks: do

Defining vars: def

Local bindings: let

Destructuring (let, part 2)

Sequential destructuring

Map destructuring

Creating functions: fn

Destructuring function arguments

Function literals

Conditionals: if

Looping: loop and recur

Referring to vars: var

Java interop: . and new

Exception handling: try and throw

Specialized mutation: set!

Primitive locking: monitor-enter and monitor-exit

Putting it all together

eval

This is just the beginning

Part I: Functional Programming

Functional programming

What does functional programming mean?

On the importance of values

About values

Comparing values to mutable objects

A critical choice

First class and higher-order functions

applying ourselves partially

Composition of Function(ality)

Writing higher order functions

Building a primitive logging system with composable higher-order functions