Clojure

Clojure
Clojure
Clojure-glyph.svg
Paradigm(s) functional, multi-paradigm
Appeared in 2007
Designed by Rich Hickey
Stable release 1.3 (September 23, 2011; 58 days ago (2011-09-23))
Typing discipline dynamic, strong
Influenced by Common Lisp, Scheme, Prolog, ML, Haskell, Erlang[1]
Platform JVM, CLR
License Eclipse Public License
Website clojure.org
Wikibooks logo Clojure Programming at Wikibooks

Clojure (pronounced like "closure"[2]) is a recent dialect of the Lisp programming language created by Rich Hickey. It is a general-purpose language supporting interactive development that encourages a functional programming style, and simplifies multithreaded programming.

Clojure runs on the Java Virtual Machine and the Common Language Runtime. Like other Lisps, Clojure treats code as data and has a sophisticated macro system.

Contents

History

Rich Hickey in San Francisco

Rich Hickey is the creator of the Clojure programming language. Before Clojure, he developed dotLisp, a similar project based on the .NET platform. Hickey is an independent software developer and a consultant with over 20 years of experience in many facets of software development. He has worked on scheduling systems, broadcast automation, audio analysis and fingerprinting, database design, yield management, exit poll systems, and machine listening. He spent about 2½ years working on Clojure before releasing it to the world, much of that time working exclusively on Clojure without external funding. When he finally announced it, the announcement consisted of one email 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.[3][4]

Clojure's approach to concurrency is characterized by the concept of identities,[5] which represent 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 given to the compiler as they are. In other words, the Clojure compiler does not compile only list data structures, but supports all of the mentioned types 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 also disallows rebinding global names in other namespaces that have been imported into the current namespace.

Language features

Variants

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

  • ClojureCLR[6], a full port of Clojure to the Common Language Runtime, interoperable with .NET libraries
  • ClojureScript[7], a subset of Clojure, compiled to optimized JavaScript, intended for use in web browsers and mobile devices
  • las3r[8], a subset of Clojure that runs on the ActionScript Virtual Machine (the Adobe Flash Player platform)

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

A thread-safe generator of unique serial numbers (though note that like many other Lisp dialects, Clojure has a built-in gensym function for this purpose):

(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 that 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. That's why even after 100 000 iterations of each thread no number got lost.

(defn run [nvecs nitems nthreads niters]
  (let [vec-refs (vec (map (comp ref vec)
                           (partition nitems (range (* nvecs nitems)))))
        swap #(let [v1 (rand-int nvecs)
                    v2 (rand-int nvecs)
                    i1 (rand-int nitems)
                    i2 (rand-int nitems)]
                (dosync
                 (let [temp (nth @(vec-refs v1) i1)]
                   (alter (vec-refs v1) assoc i1 (nth @(vec-refs v2) i2))
                   (alter (vec-refs v2) assoc i2 temp))))
        report #(do
                 (prn (map deref vec-refs))
                 (println "Distinct:"
                          (count (distinct (apply concat (map deref vec-refs))))))]
    (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

References

Rich Hickey

Further reading

External links


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Clojure — Класс языка: функциональный, мультипарадигмальный Появил …   Википедия

  • Clojure — Desarrollador(es) http://clojure.org Información general …   Wikipedia Español

  • Clojure — Lisp Dialekt Basisdaten Paradigmen: Funktionale Programmiersprache Erscheinungsjahr …   Deutsch Wikipedia

  • Clojure — Apparu en 2007 Auteur Rich Hickey …   Wikipédia en Français

  • Хикки, Ричард — Ричард Хикки англ. Richard Hickey …   Википедия

  • List comprehension — A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical set builder notation (set comprehension) as distinct from the use of map… …   Wikipedia

  • List of programming languages by category — Programming language lists Alphabetical Categorical Chronological Generational This is a list of programming languages grouped by category. Some languages are listed in multiple categories. Contents …   Wikipedia

  • Multiple dispatch — Theories and practice of polymorphism Double dispatch Multiple dispatch Operator overloading Polymorphism in computer science Polymorphism in OOP Subtyping …   Wikipedia

  • Multiversion concurrency control — (abbreviated MCC or MVCC), in the database field of computer science, is a concurrency control method commonly used by database management systems to provide concurrent access to the database and in programming languages to implement… …   Wikipedia

  • Processing (programming language) — Processing Paradigm(s) object oriented Appeared in 2001; 9 years ago (2001) …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”