Clojure

Clojure
Clojure logo.gif
Paradigm functional
Designed by Rich Hickey
First appeared 2007; 8 years ago (2007)
Stable release 1.7[1] / June 30, 2015; 2 months 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

Clojure (pronounced like "closure"[3]) is a dialect of the Lisp programming language created by Rich Hickey. Clojure is a general-purpose programming language with an emphasis on functional programming. It runs on the Java Virtual Machine, Common Language Runtime, and JavaScript engines. Like other Lisps, Clojure treats code as data and has a macro system.

Clojure's 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.

History

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.[5]

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.

Philosophy

Hickey developed Clojure because he wanted a modern Lisp for functional programming, symbiotic with the established Java platform, and designed for concurrency.[6][7]

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

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.

Macros

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.[9]

Language characteristics

  • Tight Java integration: Clojure applications can be easily packaged and deployed to JVMs and application servers. The included standard library provides macros which make it simple to use existing Java APIs. Likewise, Clojure internals and Clojure code are easily accessible from Java. The community uses Leiningen for project automation, which integrates with the Maven Java library ecosystem.
  • Dynamic development with a read-eval-print loop.
  • Functions as first-class objects.
  • Emphasis on recursion and higher-order functions instead of side-effect-based looping.
  • Lazy sequences.
  • Provides a rich set of immutable, persistent data structures (including hashmaps, sets and lists).
  • Concurrent programming through software transactional memory, an agent system, and a dynamic var system.
  • Multimethods to allow dynamic dispatch on the types and values of any set of arguments (cf. the usual object-oriented polymorphism which dispatches on the type of what is effectively the first method argument).
  • Protocols and Datatypes,[10] which provide a high-performance, dynamic polymorphism construct as an alternative to interfaces, while avoiding the Expression Problem.
  • Limit of 4 primitive parameters.[11] This means that a function constructed to accept 4 primitives in its signature would need refactoring with a workaround to accept 5.

Variants

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

  • ClojureCLR,[12] a full port of Clojure to the Common Language Runtime, interoperable with .NET libraries
  • ClojureScript,[13] a subset of Clojure, compiled to optimized JavaScript, intended for use in web browsers and mobile devices
  • las3r,[14] a subset of Clojure that runs on the ActionScript Virtual Machine (the Adobe Flash Player platform)
  • clojure-py,[15] an implementation of Clojure in pure Python
  • rouge,[16] an implementation of Clojure on top of YARV in Ruby
  • CljPerl,[17] an implementation of Clojure on top of Perl.

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. ^ "Index of /maven2/org/clojure/clojure/1.7.0/". http://central.maven.org. 2015-06-30. Retrieved 2015-06-30. 
  2. ^ "Clojure Programming" (PDF). Retrieved 2013-04-30. 
  3. ^ "meaning and pronunciation of Clojure". Rich Hickey. Retrieved 2012-04-20. 
  4. ^ "Clojure inventor Hickey now aims for Android". 
  5. ^ "[ANN] dotLisp - a Lisp dialect for .Net". 
  6. ^ "Rationale". Rich Hickey. clojure.org. Retrieved 2008-10-17. 
  7. ^ 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. 
  8. ^ "On State and Identity". Rich Hickey. clojure.org. Retrieved 2010-03-01. 
  9. ^ "edn". Rich Hickey. Github.com. Retrieved 2014-05-24. 
  10. ^ "Protocols". clojure.org. Retrieved 2014-05-24. 
  11. ^ "fns taking primitives support only 4 or fewer args". 
  12. ^ "clojure/clojure-clr · GitHub". Github.com. Retrieved 2012-06-28. 
  13. ^ "clojure/clojurescript · GitHub". Github.com. Retrieved 2012-06-28. 
  14. ^ aemoncannon (2010-12-30). "Home · aemoncannon/las3r Wiki · GitHub". Github.com. Retrieved 2012-06-28. 
  15. ^ "halgari/clojure-py · GitHub". Github.com. Retrieved 2012-07-10. 
  16. ^ "rouge-lang/rouge · GitHub". Github.com. Retrieved 2013-01-25. [dead link]
  17. ^ "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 
  • 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