Clojure

Clojure
Paradigm functional
Designed by Rich Hickey
First appeared 2007 (2007)
Stable release
1.8[1] / January 19, 2016 (2016-01-19)
Typing discipline dynamic, strong
Platform JVM, CLR, JavaScript
License Eclipse Public License
Filename extensions .clj, .cljs, .cljc, .edn
Website clojure.org
Influenced by
C++,[2] C#, Common Lisp, Erlang, Haskell, Mathematica,[3] ML, Prolog, Scheme, Java, Racket,[4] Ruby[5]
Influenced
Elixir, Hy, Pixie, Rhine

Clojure (pronunciation: /ˈklʒɜːr/, like "closure"[6]) is a dialect of the Lisp programming language created by Rich Hickey.[7] Clojure is a general-purpose programming language with an emphasis on functional programming.[8] It runs on the Java virtual machine, Common Language Runtime,[9] and JavaScript[10] engines. Like other Lisps, Clojure treats code as data and has a macro system.[11] The current development process is community-driven,[12] overseen by Rich Hickey as its benevolent dictator for life (BDFL).[13]

Clojure encourages immutability and immutable data structures. While its type system is entirely dynamic, recent efforts have also sought the implementation of gradual typing.[14] Clojure encourages programmers to be explicit about managing state and identity.[15] This focus on programming with immutable values and explicit progression-of-time constructs is intended to facilitate developing more robust programs, especially multithreaded ones.[16][17]

Clojure is used in industry by firms such as Walmart,[18] Puppet Labs,[19] and other large software firms.[20] Commercial support for Clojure is provided by Cognitect.[20] Annual Clojure conferences are organised every year across the globe, the most famous of them being Clojure/conj (US east coast),[21] Clojure/West (US west coast),[22] and EuroClojure (Europe).[23]

The latest stable version of Clojure is 1.8,[24] released on January 19, 2016. The first stable release was version 1.0, released on May 4, 2009.[25] Clojure is free software released under the Eclipse Public License.[26]

History and development process

Rich Hickey in San Francisco

Rich Hickey is the creator of the Clojure language.[7] Before Clojure, he developed dotLisp, a similar project based on the .NET Framework,[27] and three earlier attempts to provide interoperability between Lisp and Java: a Java foreign language interface for Common Lisp (jfli),[28] A Foreign Object Interface for Lisp (FOIL),[29] and a Lisp-friendly interface to Java Servlets (Lisplets).[30]

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[12] and is managed at the Clojure Community website.[31] The website contains planning documents and an issue tracker where bugs may be filed. General development discussion occurs at the Clojure Dev Google Group.[32] While anyone can submit bug reports and ideas, to contribute patches one must sign the Clojure Contributor agreement.[33] JIRA tickets are processed by a team of screeners and finally Rich Hickey approves the changes.[34]

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.[16][17][35]

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

Features

Version Release date Major features added
2007-10-16[37] Initial public release
1.0 2009-05-04[38] First stable release[39]
1.1 2009-12-31[38] Futures[40]
1.2 2010-08-19[38] Protocols[41]
1.3 2011-09-23[38] Enhanced primitive support[42]
1.4 2012-04-15[38] Reader literals
1.5 2013-03-01[38] Reducers
1.6 2014-03-25[43] Java API, improved hashing algorithms
1.7 2015-06-30[24] Transducers, reader conditionals
Current stable version: 1.8 2016-01-19[44] Additional string functions, direct linking, socket server
Future release: 1.9 future
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,[45] and Clojure code can be called from Java also.[46] The community uses Leiningen[47] for project automation, providing support for Maven integration. Leiningen handles project package management and dependencies and is configured using Clojure syntax.[47]

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.[48] Clojure's reader supports literal syntax for maps, sets and vectors in addition to lists, and these are compiled to the mentioned structures directly.[48] 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.[48][49]

As a Lisp dialect, Clojure supports functions as first-class objects, a read–eval–print loop (REPL), and a macro system.[50] 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.[51] Clojure supports multimethods[52] and for interface-like abstractions has a protocol[53] based polymorphism and data type system using records,[54] 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;[55] it is possible to do so explicitly by using the recur keyword.[56] For parallel and concurrent programming Clojure provides software transactional memory[57] a reactive agent system,[58] and channel-based concurrent programming.[59]

Recently Clojure introduced reader conditionals by allowing the embedding of Clojure and ClojureScript code in the same namespace.[24][60] 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.[61][62][63]

Platforms and popularity

The primary platform of Clojure is the JVM,[8][45] but other target implementations exist. The most notable of these are ClojureScript,[10] which compiles to JavaScript, and ClojureCLR,[64] 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[65] found that 47% of respondents used both Clojure and ClojureScript when working with Clojure. In 2014 this number had increased to 55%,[66] in 2015, based on 2,445 respondents, to 66%.[67] Popular ClojureScript projects include implementations of the React library such as Reagent and Om.[68]

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

Variant implementations of the Clojure language have been developed for platforms other than the above:

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

See also

References

  1. "Index of /maven2/org/clojure/clojure/1.8.0/". central.maven.org. 2015-06-30. Retrieved 2015-06-30.
  2. "Rich Hickey Q&A on Code Quarterly". Retrieved 2016-05-08.
  3. "Rich Hickey's Amazon Bookshelf of books that influenced Clojure". Retrieved 2016-05-08.
  4. Bonnaire-Sergeant, Ambrose (2012). A Practical Optional Type System for Clojure (Thesis). The University of Western Australia.
  5. "Clojure Programming" (PDF). Retrieved 2013-04-30.
  6. "meaning and pronunciation of Clojure". Rich Hickey. Retrieved 2012-04-20.
  7. 1 2 "Clojure inventor Hickey now aims for Android".
  8. 1 2 "Clojure - home". clojure.org. Retrieved 2015-09-15.
  9. "clojure/clojure-clr". GitHub. Retrieved 2015-09-15.
  10. 1 2 "clojure/clojurescript". GitHub. Retrieved 2015-09-15.
  11. "Clojure - lisp". clojure.org. Retrieved 2015-09-15.
  12. 1 2 "Contributing FAQ - Clojure Community - Clojure Development". dev.clojure.org. Retrieved 2015-09-15.
  13. "Clojure - funding". clojure.org. Retrieved 2015-09-15.
  14. "clojure/core.typed". GitHub. Retrieved 2015-09-15.
  15. 1 2 "Clojure - state". clojure.org. Retrieved 2015-09-15.
  16. 1 2 "Rationale". Rich Hickey. clojure.org. Retrieved 2008-10-17.
  17. 1 2 Charles (2009-10-06). "Expert to Expert: Rich Hickey and Brian Beckman – Inside Clojure | Going Deep | Channel 9". Channel9.msdn.com. Retrieved 2012-06-28.
  18. "Walmart Runs Clojure at Scale". Retrieved 2015-09-15.
  19. "A New Era of Application Services at Puppet Labs". Puppet Labs. Retrieved 2015-09-15.
  20. 1 2 "Clojure Programming Language :: Cognitect, Clojure Consulting, Clojure Support, Functional Programming, JVM". cognitect.com. Retrieved 2015-09-15.
  21. Clojure/conj
  22. Clojure/West
  23. EuroClojure
  24. 1 2 3 "Clojure 1.8 is now available". Retrieved 2015-09-15.
  25. "Clojure: Clojure 1.0". clojure.blogspot.fi. Retrieved 2015-09-16.
  26. "Clojure - license". clojure.org. Retrieved 2015-09-15.
  27. "[ANN] dotLisp - a Lisp dialect for .Net".
  28. "jfli, a Java foreign language interface for Common Lisp".
  29. "Foil - a Foreign Object Interface for Lisp".
  30. "Lisplets - a Lisp-friendly interface to Java Servlets".
  31. Clojure Community website
  32. Clojure Dev Google Group
  33. "Contributing FAQ - Clojure Community - Clojure Development". dev.clojure.org. Retrieved 2015-09-16.
  34. "JIRA workflow - Clojure Community - Clojure Development". dev.clojure.org. Retrieved 2015-09-16.
  35. "Economy Size Geek - Interview with Rich Hickey, Creator of Clojure | Linux Journal". www.linuxjournal.com. Retrieved 2015-09-15.
  36. 1 2 "On State and Identity". Rich Hickey. clojure.org. Retrieved 2010-03-01.
  37. "Clojure: Clojure is Two!". Clojure Blog. Retrieved 2015-09-16.
  38. 1 2 3 4 5 6 Fingerhut, Andy. "Clojure version history". jafingerhut.github.io. Retrieved 2015-09-16.
  39. "Clojure: Clojure 1.0". Clojure Blog. Retrieved 2015-09-16.
  40. "Clojure: Clojure 1.1 Release". clojure.blogspot.com. Retrieved 2015-09-16.
  41. "Clojure - protocols". clojure.org. Retrieved 2015-09-16.
  42. "clojure/clojure". GitHub. Retrieved 2015-09-16.
  43. "Google Groups". groups.google.com. Retrieved 2015-09-16.
  44. "Google Groups". groups.google.com. Retrieved 2016-01-25.
  45. 1 2 "Clojure - jvm_hosted". clojure.org. Retrieved 2015-09-15.
  46. "Clojure - java_interop". clojure.org. Retrieved 2015-09-15.
  47. 1 2 contributors, Phil Hagelberg and. "Leiningen". leiningen.org. Retrieved 2015-09-15.
  48. 1 2 3 "Clojure - reader". clojure.org. Retrieved 2015-09-15.
  49. "Clojure - lisps". clojure.org. Retrieved 2015-09-15.
  50. "Clojure - macros". clojure.org. Retrieved 2015-09-15.
  51. "edn". Rich Hickey. Github.com. Retrieved 2014-05-24.
  52. "Clojure - multimethods". clojure.org. Retrieved 2015-09-15.
  53. "Clojure - protocols". clojure.org. Retrieved 2015-09-15.
  54. "Clojure - datatypes". clojure.org. Retrieved 2015-09-15.
  55. "Brian Goetz - Stewardship: the Sobering Parts". YouTube. ClojureTV. Retrieved 2015-09-15.
  56. "Clojure - special_forms". clojure.org. Retrieved 2015-09-15.
  57. "Clojure - Refs". clojure.org. Retrieved 2015-09-15.
  58. "Clojure - Agents". clojure.org. Retrieved 2016-07-04.
  59. "Clojure :: Clojure core.async Channels". clojure.com. Retrieved 2015-09-15.
  60. "Clojure - reader". clojure.org. Retrieved 2015-09-15.
  61. "Transducers" by Rich Hickey. https://www.youtube.com/watch?v=6mTbuzafcII. Retrieved on 2015-09-15.
  62. "Transducers are Coming". Retrieved 2015-09-15.
  63. "Rich Hickey - Inside Transducers". YouTube. Cognitect Inc. Nov 20, 2014. Retrieved 2015-09-15.
  64. "clojure/clojure-clr · GitHub". Github.com. Retrieved 2012-06-28.
  65. Emerick, Chas. "Results of the 2013 State of Clojure & ClojureScript survey". cemerick. Retrieved 2015-09-17.
  66. "State of Clojure 2014 Survey Results". Cognitect Blog. Retrieved 2015-09-17.
  67. "State of Clojure 2015 Survey Results". Cognitect Blog. Retrieved 2016-09-08.
  68. "Om: Enhancing Facebook's React with Immutability". InfoQ. Retrieved 2015-09-17.
  69. Meier, Carin. "Creative computing with Clojure - O'Reilly Radar". radar.oreilly.com. Retrieved 2015-09-17.
  70. aemoncannon (2010-12-30). "Home · aemoncannon/las3r Wiki · GitHub". Github.com. Retrieved 2012-06-28.
  71. "halgari/clojure-py · GitHub". Github.com. Retrieved 2012-07-10.
  72. "rouge-lang/rouge · GitHub". Github.com. Retrieved 2015-12-19.
  73. "A lisp on Perl . MetaCPAN". metacpan.org. Retrieved 2014-05-25.

Further reading

  • Rochester, Eric (2015), Clojure Data Analysis Cookbook (2nd ed.), Packt Publishing, ISBN 9781784390297 
  • Gamble, Julian (2015), Clojure Recipes (1st ed.), Pearson Publishing, ISBN 9780321927736 
  • Rochester, Eric (2014), Mastering Clojure Data Analysis (1st ed.), Packt Publishing, ISBN 9781783284139 
  • Fogus, Michael; Houser, Chris (2014), The Joy of Clojure (2nd ed.), Manning, ISBN 1-617291-41-2 
  • Fogus, Michael; Houser, Chris (2010), The Joy of Clojure (1st ed.), Manning, ISBN 1-935182-64-1 
  • Halloway, Stuart (2012), Programming Clojure (2nd ed.), Pragmatic Bookshelf, ISBN 978-1-93435-686-9 
  • Rathore, Amit (2011), Clojure in Action (1st ed.), Manning, ISBN 1-935182-59-5 
  • VanderHart, Luke; Sierra, Stuart (June 7, 2010), Practical Clojure (1st ed.), Apress, ISBN 1-4302-7231-7 
  • Emerick, Chas; Carper, Brian; Grand, Christophe (April 19, 2012), Clojure Programming (1st ed.), O'Reilly Media, ISBN 1-4493-9470-1 

External links

This article is issued from Wikipedia - version of the 11/25/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.