||This article includes a list of references, but its sources remain unclear because it has insufficient inline citations. (July 2012) (Learn how and when to remove this template message)|
|Designed by||Rich Hickey|
1.8 / January 19, 2016
|Typing discipline||dynamic, strong|
|License||Eclipse Public License|
|Filename extensions||.clj, .cljs, .cljc, .edn|
|C++,C#, Common Lisp, Erlang, Haskell, Mathematica,ML, Prolog, Scheme, Java, Racket,Ruby|
|Elixir, Hy, Pixie, Rhine|
Clojure encourages immutability and immutable data structures. While its type system is entirely dynamic, recent efforts have also sought the implementation of gradual typing. Clojure encourages programmers to be explicit about managing state and identity. This focus on programming with immutable values and explicit progression-of-time constructs is intended to facilitate developing more robust programs, especially multithreaded ones.
Clojure is used in industry by firms such as Funding Circle,Walmart,Puppet Labs, and other large software firms. Commercial support for Clojure is provided by Cognitect. Annual Clojure conferences are organised every year across the globe, the most famous of them being Clojure/conj (US east coast), Clojure/West (US west coast), and EuroClojure (Europe).
The latest stable version of Clojure is 1.8, released on January 19, 2016. The first stable release was version 1.0, released on May 4, 2009. Clojure is free software released under the Eclipse Public License.
Rich Hickey is the creator of the Clojure language. Before Clojure, he developed dotLisp, a similar project based on the .NET Framework, 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 Community website. The website contains planning documents and an issue tracker where bugs may be filed. General development discussion occurs at the Clojure Dev Google Group. While anyone can submit bug reports and ideas, to contribute patches one must sign the Clojure Contributor agreement. JIRA tickets are processed by a team of screeners and finally Rich Hickey approves the changes.
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.
|Version||Release date||Major features added|
|2007-10-16||Initial public release|
|1.0||2009-05-04||First stable release|
|1.3||2011-09-23||Enhanced primitive support|
|1.6||2014-03-25||Java API, improved hashing algorithms|
|1.7||2015-06-30||Transducers, reader conditionals|
|Current stable version: 1.8||2016-01-19||Additional string functions, direct linking, socket server|
|Future release: 1.9||future|
Clojure runs on the Java virtual machine and as a result integrates with Java and fully supports calling Java code from Clojure, and Clojure code can be called from Java also. 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. 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 macro system is very similar to that in Common Lisp with the exception that Clojure's version of the backquote (called "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
recur keyword. For parallel and concurrent programming Clojure provides software transactional memory a reactive agent system, and channel-based concurrent programming.
Recently Clojure introduced reader conditionals by allowing the embedding of Clojure and ClojureScript code in the same namespace. Transducers have been added as a way for composing transformations. Transducers enable higher-order functions such as map and fold to generalize over any source of input data, as traditionally these functions operate on sequences, transducers allow them to work on channels and let the user define their own models for transduction.
Clojure has also been used for creative computing, including visual art, music, games, and poetry.
Variant implementations of the Clojure language have been developed for platforms other than the above:
(println "Hello world!")
Defining a function:
(defn square [x] (* x x))
GUI "Hello world" by calling the Java Swing library:
(javax.swing.JOptionPane/showMessageDialog nil "Hello World")
(println (str "Hello, " \u4e16)) ; to the console (javax.swing.JOptionPane/showMessageDialog nil (str "Hello, " \u4e16 "!")) ; using Java GUI
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):
(let [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 expressions with all printing to *out* silenced." [& forms] `(binding [*out* bit-bucket-writer] ~@forms)) (noprint (println "Hello, nobody!"))
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 (->> (range (* nvecs nitems)) (partition nitems) (map (comp ref vec)) vec) 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 #(let [derefed (map deref vec-refs)] (prn derefed) (println "Distinct:" (->> derefed (apply concat) distinct count)))] (report) (dorun (apply pcalls (repeat nthreads #(dotimes [_ niters] (swap))))) (report))) (run 100 10 10 100000)
Output of prior example:
([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
|Lisp 1.5||Lisp 1.5|
|ZetaLisp||Lisp Machine Lisp|
|Common Lisp||Common Lisp|
|Emacs Lisp||Emacs Lisp|
Manage research, learning and skills at defaultLogic. Create an account using LinkedIn or facebook to manage and organize your IT knowledge. defaultLogic works like a shopping cart for information -- helping you to save, discuss and share.