Clojure

Clojure (, like "closure")<ref name="interview/a-z"></ref> is a dynamic and functional dialect of the Lisp programming language on the Java platform.<ref name="platform/android"></ref> Like other Lisp dialects, Clojure treats code as data and has a Lisp macro system.<ref name="reference/lisps"></ref> The current development process is community-driven,<ref name="dev/dev"></ref> overseen by Rich Hickey as its benevolent dictator for life (BDFL).
Clojure advocates immutability and immutable data structures and encourages programmers to be explicit about managing identity and its states.<ref name="about/state"></ref> This focus on programming with immutable values and explicit progression-of-time constructs is intended to facilitate developing more robust, especially concurrent, programs that are simple and fast.<ref name="about/rationale"></ref><ref name="msdn/inside-clojure"></ref> While its type system is entirely dynamic, recent efforts have also sought the implementation of gradual typing.
Commercial support for Clojure is provided by Cognitect. Clojure conferences are organized every year across the globe, the most famous of them being Clojure/conj.
History and development process.
Rich Hickey is the creator of the Clojure language. Before Clojure, he developed dotLisp, a similar project based on the .NET platform, and three earlier attempts to provide interoperability between Lisp and Java: a "Java foreign language interface for Common Lisp" (jfli), A "Foreign Object Interface for Lisp" (FOIL), and a "Lisp-friendly interface to Java Servlets" (Lisplets).
Hickey spent about 2½ years working on Clojure before releasing it publicly, much of that time working exclusively on Clojure with no outside funding. At the end of this time, Hickey sent an email announcing the language to some friends in the Common Lisp community.
The development process is community-driven and is managed at the Clojure JIRA project page. General development discussion occurs at the Clojure Google Group. Anyone can submit issues and ideas at ask.clojure.org, but to contribute patches, one must sign the Clojure Contributor agreement. JIRA issues are processed by a team of screeners and finally Rich Hickey approves the changes.
Clojure's name, according to Hickey, is a word play on the programming concept "closure" incorporating the letters C, L, and J for C#, Lisp, and Java respectively—three languages which had a major influence on Clojure's design.
Design philosophy.
Rich Hickey developed Clojure because he wanted a modern Lisp for functional programming, symbiotic with the established Java platform, and designed for concurrency.
Clojure's approach to state is characterized by the concept of identities, which are represented as a series of immutable states over time. Since states are immutable values, any number of workers can operate on them in parallel, and concurrency becomes a question of managing changes from one state to another. For this purpose, Clojure provides several mutable reference types, each having well-defined semantics for the transition between states.
Language overview.
Clojure runs on the Java platform and as a result, integrates with Java and fully supports calling Java code from Clojure,<ref name="about/jvm_hosted"></ref> and Clojure code can be called from Java, too. The community uses Leiningen for project automation, providing support for Maven integration. Leiningen handles project package management and dependencies and is configured using Clojure syntax.
Like most other Lisps, Clojure's syntax is built on S-expressions that are first parsed into data structures by a reader before being compiled.<ref name="reference/reader"></ref> Clojure's reader supports literal syntax for maps, sets and vectors in addition to lists, and these are compiled to the mentioned structures directly. Clojure is a Lisp-1 and is not intended to be code-compatible with other dialects of Lisp, since it uses its own set of data structures incompatible with other Lisps.
As a Lisp dialect, Clojure supports functions as first-class objects, a read–eval–print loop (REPL), and a macro system. Clojure's Lisp macro system is very similar to that of Common Lisp with the exception that Clojure's version of the backquote (termed "syntax quote") qualifies symbols with their namespace. This helps prevent unintended name capture, as binding to namespace-qualified names is forbidden. It is possible to force a capturing macro expansion, but it must be done explicitly. Clojure does not allow user-defined reader macros, but the reader supports a more constrained form of syntactic extension. Clojure supports multimethods and for interface-like abstractions has a protocol based polymorphism and data type system using records, providing high-performance and dynamic polymorphism designed to avoid the expression problem.
Clojure has support for lazy sequences and encourages the principle of immutability and persistent data structures. As a functional language, emphasis is placed on recursion and higher-order functions instead of side-effect-based looping. Automatic tail call optimization is not supported as the JVM does not support it natively; it is possible to do so explicitly by using the codice_1 keyword. For parallel and concurrent programming Clojure provides software transactional memory, a reactive agent system, and channel-based concurrent programming.
Clojure 1.7 introduced reader conditionals by allowing the embedding of Clojure and ClojureScript code in the same namespace. Transducers were added as a method for composing transformations. Transducers enable higher-order functions such as map and fold to generalize over any source of input data. While traditionally these functions operate on sequences, transducers allow them to work on channels and let the user define their own models for transduction.
Alternate Platforms.
The primary platform of Clojure is Java, but other target implementations exist. The most notable of these is ClojureScript, which compiles to ECMAScript 3, and ClojureCLR, a full port on the .NET platform, interoperable with its ecosystem. A survey of the Clojure community with 1,060 respondents conducted in 2013 found that 47% of respondents used both Clojure and ClojureScript when working with Clojure. In 2014 this number had increased to 55%, in 2015, based on 2,445 respondents, to 66%. Popular ClojureScript projects include implementations of the React library such as Reagent, re-frame, Rum, and Om.
Other Implementations.
Other implementations of Clojure on different platforms include:
Popularity.
With continued interest in functional programming, Clojure's adoption by software developers using the Java platform has continued to increase. The language has also been recommended by software developers such as Brian Goetz, Eric Evans, James Gosling, Paul Graham, and Robert C. Martin. ThoughtWorks, while assessing functional programming languages for their Technology Radar, described Clojure as "a simple, elegant implementation of Lisp on the JVM" in 2010 and promoted its status to "ADOPT" in 2012.
In the "JVM Ecosystem Report 2018" (which was claimed to be "the largest survey ever of Java developers"), that was prepared in collaboration by Snyk and Java Magazine, ranked Clojure as the 2nd most used programming language on the JVM for "main applications". Clojure is used in industry by firms such as Apple, Atlassian, Funding Circle, Netflix, Puppet, and Walmart as well as government agencies such as NASA. It has also been used for creative computing, including visual art, music, games, and poetry.
Tools.
Tooling for Clojure development has seen significant improvement over the years. The following is a list of some popular IDEs and text editors with plug-ins that add support for programming in Clojure:
In addition to the tools provided by the community, the official Clojure CLI tools<ref name="guides/deps_and_cli"></ref> have also become available on Linux, macOS, and Windows since Clojure 1.9.
Features by example.
The following examples can be run in a Clojure REPL such as one started with the Clojure CLI tools or an online REPL such as one available on REPL.it.
Simplicity.
Because of the strong emphasis on simplicity, typical Clojure programs consist of mostly functions and simple data structures (i.e., lists, vectors, maps, and sets):
(defn -main ; name
 [& args] ; (variable) parameters
 (println "Hello, World!")) ; body
Programming at the REPL.
Like other Lisps, one of the iconic features of Clojure is interactive programming at the REPL. Note that, in the following examples, codice_2 starts a line comment and codice_3 indicates the expected output:
clojure.core/rem
 remainder of dividing numerator by denominator.
(defn rem
 "remainder of dividing numerator by denominator."
 {:added "1.0"
 :static true
 [num div]
 (. clojure.lang.Numbers (remainder num div)))
Names at runtime.
Unlike other runtime environments where names get compiled away, Clojure's runtime environment is easily introspectable using normal Clojure data structures:
 :doc "returns `(rem n 10)`",
 :line 1,
 :column 1,
 :file "user.clj",
 :name foo,
Code as data (homoiconicity).
Similar to other Lisps, Clojure is homoiconic (also known as code as data). In the example below, we can see how easy it is to write code that modifies code itself:
(map (fn [form]
 (case form
 1 'one
 + 'plus))
 (quote (+ 1 1)))
Expressive operators for data transformation.
The threading macros (codice_4, codice_5, and friends) can syntactically express the abstraction of piping a collection of data through a series of transformations:
 (map inc)
 (filter even?))
This can also be achieved more efficiently using transducers:
 (filter even?))
 (range 10))
Thread-safe management of identity and state.
A thread-safe generator of unique serial numbers (though, like many other Lisp dialects, Clojure has a built-in codice_6 function that it uses internally):
(defn generate-unique-id
 "Returns a distinct numeric ID for each call."
 (swap! i inc))
Macros.
An anonymous subclass of codice_7 that doesn't write to anything, and a macro using it to silence all prints within it:
(def bit-bucket-writer
 (proxy [java.io.Writer] []
 (write [buf] nil)
 (close [] nil)
 (flush [] nil)))
(defmacro noprint
 "Evaluates the given `forms` with all printing to `*out*` silenced."
 [& forms]
 `(binding [*out* bit-bucket-writer]
 ~@forms))
(noprint
 (println "Hello, nobody!"))
Language interoperability with Java.
Clojure was created from the ground up to embrace its host platforms as one of its design goals and thus provides excellent language interoperability with Java:
 (.put "apple" 1)
 (.put "banana" 2))
 (.add 1)
 (.add 2)
 (.add 3)))
(javax.swing.JOptionPane/showMessageDialog
 nil
 "Hello, World!")
Software transactional memory.
10 threads manipulating one shared data structure, which consists of 100 vectors each one containing 10 (initially sequential) unique numbers. Each thread then repeatedly selects two random positions in two random vectors and swaps them. All changes to the vectors occur in transactions by making use of Clojure's software transactional memory system:
(defn run
 [nvecs nitems nthreads niters]
 (let [vec-refs
 (-» (* nvecs nitems)
 (range)
 (into [] (comp (partition-all nitems)
 (map vec)
 (map ref))))
 swap
 #(let [v1 (rand-int nvecs)
 v2 (rand-int nvecs)
 i1 (rand-int nitems)
 i2 (rand-int nitems)]
 (dosync
 (let [tmp (nth @(vec-refs v1) i1)]
 (alter (vec-refs v1) assoc i1 (nth @(vec-refs v2) i2))
 (alter (vec-refs v2) assoc i2 tmp))))
 report
 #(-» vec-refs
 (into [] (comp (map deref)
 (map (fn [v] (prn v) v))
 cat
 (distinct)))
 (count)
 (println "Distinct:"))]
 (report)
 (-» #(dotimes [_ niters] (swap))
 (repeat nthreads)
 (apply pcalls)
 (dorun))
 (report)))
[0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[990 991 992 993 994 995 996 997 998 999]
Distinct: 1000
[382 318 466 963 619 22 21 273 45 596]
[808 639 804 471 394 904 952 75 289 778]
[484 216 622 139 651 592 379 228 242 355]
Distinct: 1000
nil