Clojure is a newer dialect of Lisp that runs on the Java Virtual Machine
(JVM). It is intended for general-purpose use. It encourages functional
programming and is designed to make writing multithreaded applications
easier. Because of the close integration with Java, Clojure applications can be
packaged and deployed to JVM environments without adding complexity. It
also provides easy access to Java frameworks, and Clojure’s data structures
all implement standard Java interfaces.
Closures are a common element in programming, especially in functional
programming languages, such as Clojure. (The language’s name is rumored to
be a mash-up of closure and Java.) Typically, variables are designed to work
only within a defined function. Using a closure is a way to bend that rule
temporarily. A closure starts with a function and allows the value of one or
more variables from that function to be available outside that function while
being maintained in the function. Another way to phrase that is to say a
closure is a combination of a function and the variables that were in scope at
the time the function was defined; the function can refer to those variables
even if they are no longer in scope when the function is called. When you are
working in a programming language with first-class functions that can be
passed around like variables, using closures is a convenient way to provide
encapsulation without using objects or classes. An example of the use of a
closure is a function being encapsulated completely within another function
but still being able to read the state of a variable that exists in the containing
function.
Clojure is unlike most other languages in that you don’t generally install
Clojure itself; it’s just a library that’s loaded into the JVM. You don’t interact
with it directly but use a build tool and editor/IDE integration instead (well,
except when you are interacting using the REPL; in any case, you do not
typically run a tool called clojure from the command line other than the
REPL). That process is a bit beyond the short introduction of this chapter;
however, you can get started quickly by installing the clojure package.
Among other things, installing this package also installs a REPL (read-eval-
print loop, an interactive programming environment) that can be used by
entering clojure at the command line. When you install this package, you
get what you need for using Clojure in a JVM, but you need to set up your
development environment to use Clojure. See the documentation from each
environment for instructions. Note that the most popular, perhaps even the de
facto, build tool for Clojure is Leiningen, which adds some really useful
functionality, such as the ability to manage project dependencies, start a REPL
easily, and even install Clojure itself. See
https://github.com/technomancy/leiningen#readme for more.