Paradigm | multi-paradigm: |
---|---|
Family | Lisp |
Designed by | Rich Hickey |
First appeared | 2007 |
Stable release | 1.11.1[8]
/ 5 April 2022 |
Typing discipline | |
Platform | |
License | Eclipse Public |
Filename extensions |
|
Website | clojure |
Influenced by | |
Influenced | |
|
Clojure (/ˈkloʊʒər/, like closure)[16][17] is a dynamic and functional dialect of the Lisp programming language on the Java platform.[18][19]
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.[20][16] Clojure's reader supports literal syntax for maps, sets and vectors along with lists, and these are compiled to the mentioned structures directly.[20] Clojure treats code as data and has a Lisp macro system.[21] 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.[21]
Clojure advocates immutability and immutable data structures and encourages programmers to be explicit about managing identity and its states.[22] 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.[23][24][16] While its type system is entirely dynamic, recent efforts have also sought the implementation of a dependent type system.[25]
The language was created by Rich Hickey in the mid-2000's, originally for the Java platform; the language has since been ported to other platforms, such as the Common Language Runtime (.NET). Hickey continues to lead development of the language as its benevolent dictator for life.
Rich Hickey is the creator of the Clojure language.[18] Before Clojure, he developed dotLisp
, a similar project based on the .NET platform,[26] and three earlier attempts to provide interoperability between Lisp and Java: a Java foreign language interface for Common Lisp (jfli
),[27] A Foreign Object Interface for Lisp (FOIL),[28] and a Lisp-friendly interface to Java Servlets (Lisplets).[29]
Hickey spent about two and a half years working on Clojure before releasing it publicly in October 2007,[30] 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 restricted to the Clojure core team, though issues are publicly visible at the Clojure JIRA project page.[31] Anyone can ask questions or submit issues and ideas at ask.clojure.org.[32] If it's determined that a new issue warrants a JIRA ticket, a core team member will triage it and add it. JIRA issues are processed by a team of screeners and finally approved by Rich Hickey.[33][34]
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.[17]
Rich Hickey developed Clojure because he wanted a modern Lisp for functional programming, symbiotic with the established Java platform, and designed for concurrency.[23][24][35][16]
Clojure's approach to state is characterized by the concept of identities,[22] 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.[22]
Version | Release date | Major features, improvements |
---|---|---|
October 17, 2007[30] | Initial public release | |
1.0 | May 4, 2009[36] | First stable release |
1.1 | December 31, 2009[37] | Futures |
1.2 | August 19, 2010[38] | Protocols |
1.3 | September 23, 2011[39] | Enhanced primitive support |
1.4 | April 15, 2012[40] | Reader literals |
1.5 | March 1, 2013[41] | Reducers |
1.5.1 | March 10, 2013[42] | Fixing a memory leak |
1.6 | March 25, 2014[43] | Java API, improved hashing algorithms |
1.7 | June 30, 2015[44] | Transducers, reader conditionals |
1.8 | January 19, 2016[45] | Additional string functions, direct linking, socket server |
1.9 | December 8, 2017[46] | Integration with spec, command-line tools |
1.10 | December 17, 2018[47] | Improved error reporting, Java compatibility |
1.10.1 | June 6, 2019[48] | Working around a Java performance regression and improving error reporting from clojure.main
|
1.10.2 | January 26, 2021[49] | Java interoperability/compatibility improvements and other important language fixes |
1.10.3 | March 4, 2021[50] | prepl support for reader conditionals |
1.11.0 | March 22, 2022[8] | New syntax for keyword argument invocation, new clojure.math namespace, namespace aliasing without loading, and new helper functions added to clojure.core
|
1.11.1 | April 5, 2022[51] | Rolling back unintended change in binary serialisation of objects of types clojure.lang.Keyword and clojure.lang.ArraySeq .
|
Latest version |
Clojure runs on the Java platform and as a result, integrates with Java and fully supports calling Java code from Clojure,[52][16] and Clojure code can be called from Java, too.[53] The community uses tools such as Clojure command-line interface (CLI)[54] or Leiningen for project automation, providing support for Maven integration. These tools handle project package management and dependencies and are configured using Clojure syntax.
As a Lisp dialect, Clojure supports functions as first-class objects, a read–eval–print loop (REPL), and a macro system.[6] 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.[55] Clojure supports multimethods[56] and for interface-like abstractions has a protocol[57] based polymorphism and data type system using records,[58] 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;[59][60][61] it is possible to do so explicitly by using the recur
keyword.[62] For parallel and concurrent programming Clojure provides software transactional memory,[63] a reactive agent system,[1] and channel-based concurrent programming.[64]
Clojure 1.7 introduced reader conditionals by allowing the embedding of Clojure and ClojureScript code in the same namespace.[44][20] 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.[65][66][67]
Extensible Data Notation, or edn,[68] is a subset of the Clojure language intended as a data transfer format. It can be used to serialize and deserialize Clojure data structures, and Clojure itself uses a superset of edn to represent programs.
edn is used in a similar way to JSON or XML, but has a relatively large list of built-in elements, shown here with examples:
true
, false
"foo bar"
\c
, \tab
name
:key
123
3.14
(a b 42)
[a b 42]
{:a 1, "foo" :bar, [1 2 3] four}
#{a b [1 2 3]}
nil
(a null-like value)In addition to those elements, it supports extensibility through the use of tags, which consist of the character #
followed by a symbol. When encountering a tag, the reader passes the value of the next element to the corresponding handler, which returns a data value. For example, this could be a tagged element: #myapp/Person {:first "Fred" :last "Mertz"}
, whose interpretation will depend on the appropriate handler of the reader.
This definition of extension elements in terms of the others avoids relying on either convention or context to convey elements not included in the base set.
The primary platform of Clojure is Java,[19][52] but other target implementations exist. The most notable of these is ClojureScript,[69] which compiles to ECMAScript 3,[70] and ClojureCLR,[71] a full port on the .NET platform, interoperable with its ecosystem. A survey of the Clojure community with 1,060 respondents conducted in 2013[72] found that 47% of respondents used both Clojure and ClojureScript when working with Clojure. In 2014, this number had risen to 55%,[73] in 2015, based on 2,445 respondents, to 66%.[74] Popular ClojureScript projects include implementations of the React library such as Reagent,[75] re-frame,[76] Rum,[77] and Om.[78][79]
Other implementations of Clojure on different platforms include:
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,[94][95][96] Eric Evans,[97][98] James Gosling,[99] Paul Graham,[100] and Robert C. Martin.[101][102][103][104] ThoughtWorks, while assessing functional programming languages for their Technology Radar,[105] described Clojure as "a simple, elegant implementation of Lisp on the JVM" in 2010 and promoted its status to "ADOPT" in 2012.[106]
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".[107] Clojure is used in industry by firms[108] such as Apple,[109][110] Atlassian,[111] Funding Circle,[112] Netflix,[113] Nubank,[114] Puppet,[115] and Walmart[116] as well as government agencies such as NASA.[117] It has also been used for creative computing, including visual art, music, games, and poetry.[118]
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:[119]
In addition to the tools provided by the community, the official Clojure command-line interface (CLI) tools[54] have also become available on Linux, macOS, and Windows since Clojure 1.9.[132]
The following examples can be run in a Clojure REPL such as one started with the Clojure CLI tools[54] or an online REPL such as one available on REPL.it.[133]
Because of its strong emphasis on simplicity, Clojure programs typically consist of mostly functions and simple data structures (i.e., lists, vectors, maps, and sets):
;; A typical entry point of a Clojure program:
;; `-main` function
(defn -main ; name
[& args] ; (variable) parameters
(println "Hello, World!")) ; body
Like other Lisps, one of Clojure's iconic features is interactive programming at the REPL.[134] In the following examples, ;
starts a line comment and ;; =>
indicates the expected output:
; define a var
(def a 42)
;; => #'user/a
; call a function named `+`
(+ a 8)
;; => 50
; call a function named `even?`
(even? a)
;; => true
; define a function that returns the remainder of `n` when divided by 10
(defn foo [n] (rem n 10))
;; => #'user/foo
; call the function
(foo a)
;; => 2
; print the docstring of `rem`
(doc rem)
;; =>
-------------------------
clojure.core/rem
([num div])
remainder of dividing numerator by denominator.
; print the source of `rem`
(source rem)
;; =>
(defn rem
"remainder of dividing numerator by denominator."
{:added "1.0"
:static true
:inline (fn [x y] `(. clojure.lang.Numbers (remainder ~x ~y)))}
[num div]
(. clojure.lang.Numbers (remainder num div)))
Unlike other runtime environments where names get compiled away, Clojure's runtime environment is easily introspectable using normal Clojure data structures:
; define a var
(def a 42)
;; => #'user/a
; get a map of all public vars interned in the `user` namespace
(ns-publics 'user)
;; => {a #'user/a}
; reference the var via `#'` (reader macro) and
; its associated, namespace-qualified symbol `user/a`
#'user/a
;; => #'user/a
; de-reference (get the value of) the var
(deref #'user/a)
;; => 42
; define a function (with a docstring) that
; returns the remainder of `n` when divided by 10
(defn foo "returns `(rem n 10)`" [n] (rem n 10))
;; => #'user/foo
; get the metadata of the var `#'user/foo`
(meta #'user/foo)
;; =>
{:arglists ([n]),
:doc "returns `(rem n 10)`",
:line 1,
:column 1,
:file "user.clj",
:name foo,
:ns #namespace[user]}
Similar to other Lisps, Clojure is homoiconic (also known as "code as data"). In the example below, we can see how to write code that modifies code itself:
; call a function (code)
(+ 1 1)
;; => 2
; quote the function call
; (turning code into data, which is a list of symbols)
(quote (+ 1 1))
;; => (+ 1 1)
; get the first element on the list
; (operating on code as data)
(first (quote (+ 1 1)))
;; => +
; get the last element on the list
; (operating on code as data)
(last (quote (+ 1 1)))
;; => 1
; get a new list by replacing the symbols on the original list
; (manipulating code as data)
(map (fn [form]
(case form
1 'one
+ 'plus))
(quote (+ 1 1)))
;; => (plus one one)
The threading macros (->
, ->>
, and friends) can syntactically express the abstraction of piping a collection of data through a series of transformations:
(->> (range 10)
(map inc)
(filter even?))
;; => (2 4 6 8 10)
This can also be achieved more efficiently using transducers:
(sequence (comp (map inc)
(filter even?))
(range 10))
;; => (2 4 6 8 10)
A thread-safe generator of unique serial numbers (though, like many other Lisp dialects, Clojure has a built-in gensym
function that it uses internally):
(def i (atom 0))
(defn generate-unique-id
"Returns a distinct numeric ID for each call."
[]
(swap! i inc))
An anonymous subclass of java.io.Writer
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!"))
;; => nil
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:
; call an instance method
(.toUpperCase "apple")
;; => "APPLE"
; call a static method
(System/getProperty "java.vm.version")
;; => "12+33"
; create an instance of `java.util.HashMap` and
; add some entries
(doto (java.util.HashMap.)
(.put "apple" 1)
(.put "banana" 2))
;; => {"banana" 2, "apple" 1}
; create an instance of `java.util.ArrayList` and
; increment its elements with `clojure.core/map`
(def al (doto (java.util.ArrayList.)
(.add 1)
(.add 2)
(.add 3)))
(map inc al)
;; => (2 3 4)
; show a message dialog using Java Swing
(javax.swing.JOptionPane/showMessageDialog
nil
"Hello, World!")
;; => nil
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)))
(run 100 10 10 100000)
;; =>
[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