Clojure

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

<templatestyles src="Module:Hatnote/styles.css"></templatestyles>

Lua error in package.lua at line 80: module 'strict' not found.

Clojure
64px
Paradigm functional
Designed by Rich Hickey
First appeared 2007; 17 years ago (2007)
Stable release 1.7[1] / June 30, 2015; 8 years ago (2015-06-30)
Typing discipline dynamic, strong
Platform JVM, CLR, JavaScript
License Eclipse Public License
Filename extensions .clj, .cljs, .cljc, .edn
Website clojure.org
Influenced by
Common Lisp, Erlang, Haskell, ML, Prolog, Scheme, Java, Ruby[2]
Influenced
Elixir, Hy, Pixie, Rhine

Clojure (pronounced /ˈkloʊʒɜr/, like "closure"[3]) is a dialect of the Lisp programming language created by Rich Hickey.[4] Clojure is a general-purpose programming language with an emphasis on functional programming.[5] It runs on the Java Virtual Machine, Common Language Runtime,[6] and JavaScript[7] engines. Like other Lisps, Clojure treats code as data and has a macro system.[8] The current development process is community-driven,[9] overseen by Rich Hickey as its BDFL.[10]

Clojure takes a modern stance on programming by encouraging immutability and immutable data structures. While its type system is entirely dynamic, recent efforts have also sought the implementation of gradual typing.[11] Clojure encourages programmers to be explicit about managing state and identity.[12] This focus on programming with immutable values and explicit progression-of-time constructs are intended to facilitate the development of more robust programs, particularly multithreaded ones.[13][14]

Clojure is successfully used in industry by companies such as Walmart,[15] Puppet Labs,[16] and other large software companies.[17] Commercial support for Clojure is provided by Cognitect.[17] 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.7,[18] released on June 30, 2015. The first stable release was version 1.0, released on May 4, 2009.[19] Clojure is free software released under the Eclipse Public License.[20]

History and development process

Rich Hickey in San Francisco

Rich Hickey is the creator of the Clojure programming language.[4] Before Clojure, he developed dotLisp, a similar project based on the .NET platform.[21] He also developed jfli[22] (a Java foreign language interface for Common Lisp), FOIL[23] (a Foreign Object Interface for Lisp) and Lisplets[24] (a Lisp-friendly interface to Java Servlets). Those three were earlier attempts to provide interoperability between Lisp and Java.

Hickey spent about 2½ years working on Clojure before publicly releasing it, much of that time working exclusively on Clojure without external funding. At the end of this period Hickey sent an email announcing the language to some friends in the Common Lisp community.

The development process is community-driven[9] and the development is managed at the Clojure Community website. The website contains planning documents and an issue tracker where bugs may be filed. General development discussion takes place at the Clojure Dev Google Group. While anyone can submit bug reports and ideas, to contribute patches one must sign the Clojure Contributor agreement,[25] JIRA tickets are processed by a team of screeners and finally Rich Hickey approves the changes.[26]

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.[13][14][27]

Clojure's approach to state is characterized by the concept of identities,[28] 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.[12][28]

Features

Version Release date Major features added
2007-10-16[29] initial public release
1.0 2009-05-04[30] First stable release[31]
1.1 2009-12-31[30] Futures[32]
1.2 2010-08-19[30] Protocols[33]
1.3 2011-09-23[30] Enhanced primitive support[34]
1.4 2012-04-15[30] Reader literals
1.5 2013-03-01[30] Reducers
1.6 2014-03-25[35] Java API, improved hashing algorithms
Current stable version: 1.7 2015-06-30[18] Transducers, reader conditionals
Future release: 1.8 future release
Legend:
Old version
Older version, still supported
Latest version
Latest preview version
Future release

Clojure runs on the Java virtual machine and as a result integrates with Java and fully supports calling Java code from Clojure,[36] and Clojure code can be called from Java as well.[37] The community uses Leiningen[38] for project automation, providing support for Maven integration. Leiningen handles project package management and dependencies and is configured using Clojure syntax.[38]

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.[39] Clojure's reader supports literal syntax for maps, sets and vectors in addition to lists, and these are compiled to the mentioned structures directly.[39] 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 not compatible with other Lisps.[39][40]

As a Lisp dialect, Clojure supports functions as first-class objects, a read-eval-print loop, and a macro system.[41] 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 this must be done explicitly. Clojure does not allow user-defined reader macros, but the reader supports a more constrained form of syntactic extension.[42] Clojure supports multimethods[43] and for interface-like abstractions has a protocol[44] based polymorphism and data type system using records,[45] 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[46] it is possible to do so explicitly by using the recur keyword.[47] For parallel and concurrent programming Clojure provides software transactional memory[48] a reactive agent system,[49] and channel-based concurrent programming.[50]

Recently Clojure introduced reader conditionals by allowing the embedding of Clojure and ClojureScript code in the same namespace.[18][51] 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.[52][53][54]

Platforms and popularity

The primary platform of Clojure is the JVM,[5][36] but other target implementations exist. The most notable of these are ClojureScript,[7] which compiles to JavaScript, and ClojureCLR,[55] a full port to the Common Language Runtime, interoperable with the .NET ecosystem. A survey of the Clojure community with 1,060 respondents conducted in 2013[56] found that 47% of respondents used both Clojure and ClojureScript when working with Clojure. In 2014 this number had increased to 55%.[57] Popular ClojureScript projects include implementations of the React library such as Reagent and Om.[58]

Clojure has also been used for creative computing, including visual art, music, games, and poetry.[59]

Variations on the Clojure language have been developed for platforms other than the above:

  • las3r,[60] a subset of Clojure that runs on the ActionScript Virtual Machine (the Adobe Flash Player platform)
  • clojure-py,[61] an implementation of Clojure in pure Python
  • rouge,[62] an implementation of Clojure on top of YARV in Ruby
  • CljPerl,[63] an implementation of Clojure on top of Perl.
  • Pixie, a Clojure-inspired Lisp dialect written in RPython.

Examples

Hello world:

(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" )

Using Unicode (Hello 世 ("World") using the CJK code point for that word):

(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 previous 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

See also

References

  1. Lua error in package.lua at line 80: module 'strict' not found.
  2. Lua error in package.lua at line 80: module 'strict' not found.
  3. Lua error in package.lua at line 80: module 'strict' not found.
  4. 4.0 4.1 Lua error in package.lua at line 80: module 'strict' not found.
  5. 5.0 5.1 Lua error in package.lua at line 80: module 'strict' not found.
  6. Lua error in package.lua at line 80: module 'strict' not found.
  7. 7.0 7.1 Lua error in package.lua at line 80: module 'strict' not found.
  8. Lua error in package.lua at line 80: module 'strict' not found.
  9. 9.0 9.1 Lua error in package.lua at line 80: module 'strict' not found.
  10. Lua error in package.lua at line 80: module 'strict' not found.
  11. Lua error in package.lua at line 80: module 'strict' not found.
  12. 12.0 12.1 Lua error in package.lua at line 80: module 'strict' not found.
  13. 13.0 13.1 Lua error in package.lua at line 80: module 'strict' not found.
  14. 14.0 14.1 Lua error in package.lua at line 80: module 'strict' not found.
  15. Lua error in package.lua at line 80: module 'strict' not found.
  16. Lua error in package.lua at line 80: module 'strict' not found.
  17. 17.0 17.1 Lua error in package.lua at line 80: module 'strict' not found.
  18. 18.0 18.1 18.2 Lua error in package.lua at line 80: module 'strict' not found.
  19. Lua error in package.lua at line 80: module 'strict' not found.
  20. Lua error in package.lua at line 80: module 'strict' not found.
  21. Lua error in package.lua at line 80: module 'strict' not found.
  22. Lua error in package.lua at line 80: module 'strict' not found.
  23. Lua error in package.lua at line 80: module 'strict' not found.
  24. Lua error in package.lua at line 80: module 'strict' not found.
  25. Lua error in package.lua at line 80: module 'strict' not found.
  26. Lua error in package.lua at line 80: module 'strict' not found.
  27. Lua error in package.lua at line 80: module 'strict' not found.
  28. 28.0 28.1 Lua error in package.lua at line 80: module 'strict' not found.
  29. Lua error in package.lua at line 80: module 'strict' not found.
  30. 30.0 30.1 30.2 30.3 30.4 30.5 Lua error in package.lua at line 80: module 'strict' not found.
  31. Lua error in package.lua at line 80: module 'strict' not found.
  32. Lua error in package.lua at line 80: module 'strict' not found.
  33. Lua error in package.lua at line 80: module 'strict' not found.
  34. Lua error in package.lua at line 80: module 'strict' not found.
  35. Lua error in package.lua at line 80: module 'strict' not found.
  36. 36.0 36.1 Lua error in package.lua at line 80: module 'strict' not found.
  37. Lua error in package.lua at line 80: module 'strict' not found.
  38. 38.0 38.1 Lua error in package.lua at line 80: module 'strict' not found.
  39. 39.0 39.1 39.2 Lua error in package.lua at line 80: module 'strict' not found.
  40. Lua error in package.lua at line 80: module 'strict' not found.
  41. Lua error in package.lua at line 80: module 'strict' not found.
  42. Lua error in package.lua at line 80: module 'strict' not found.
  43. Lua error in package.lua at line 80: module 'strict' not found.
  44. Lua error in package.lua at line 80: module 'strict' not found.
  45. Lua error in package.lua at line 80: module 'strict' not found.
  46. Lua error in package.lua at line 80: module 'strict' not found.
  47. Lua error in package.lua at line 80: module 'strict' not found.
  48. Lua error in package.lua at line 80: module 'strict' not found.
  49. Lua error in package.lua at line 80: module 'strict' not found.
  50. Lua error in package.lua at line 80: module 'strict' not found.
  51. Lua error in package.lua at line 80: module 'strict' not found.
  52. "Transducers" by Rich Hickey. https://www.youtube.com/watch?v=6mTbuzafcII. Retrieved on 2015-09-15.
  53. Lua error in package.lua at line 80: module 'strict' not found.
  54. Lua error in package.lua at line 80: module 'strict' not found.
  55. Lua error in package.lua at line 80: module 'strict' not found.
  56. Lua error in package.lua at line 80: module 'strict' not found.
  57. Lua error in package.lua at line 80: module 'strict' not found.
  58. Lua error in package.lua at line 80: module 'strict' not found.
  59. Lua error in package.lua at line 80: module 'strict' not found.
  60. Lua error in package.lua at line 80: module 'strict' not found.
  61. Lua error in package.lua at line 80: module 'strict' not found.
  62. Lua error in package.lua at line 80: module 'strict' not found.
  63. Lua error in package.lua at line 80: module 'strict' not found.

Further reading

  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.
  • Lua error in package.lua at line 80: module 'strict' not found.

External links