Home      Discussion      Topics      Dictionary      Almanac
Signup       Login
Clojure

Clojure

Discussion
Ask a question about 'Clojure'
Start a new discussion about 'Clojure'
Answer questions from other users
Full Discussion Forum
 
Encyclopedia
Clojure is a recent dialect of the Lisp programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....

 created by Rich Hickey. It is a general-purpose language supporting interactive development that encourages a functional programming
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...

 style, and simplifies multithreaded
Thread (computer science)
In computer science, a thread of execution is the smallest unit of processing that can be scheduled by an operating system. The implementation of threads and processes differs from one operating system to another, but in most cases, a thread is contained inside a process...

 programming.

Clojure runs on the Java Virtual Machine
Java Virtual Machine
A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...

 and the Common Language Runtime
Common Language Runtime
The Common Language Runtime is the virtual machine component of Microsoft's .NET framework and is responsible for managing the execution of .NET programs. In a process known as just-in-time compilation, the CLR compiles the intermediate language code known as CIL into the machine instructions...

. Like other Lisps, Clojure treats code as data
Homoiconicity
In computer programming, homoiconicity is a property of some programming languages, in which the primary representation of programs is also a data structure in a primitive type of the language itself, from the Greek words homo meaning the same and icon meaning representation...

 and has a sophisticated macro system.

History


Rich Hickey is the creator of the Clojure programming language. Before Clojure, he developed dotLisp, a similar project based on the .NET
.NET Framework
The .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...

 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
Broadcast automation
Broadcast automation incorporates the use of broadcast programming technology to automate broadcasting operations. Used either at a broadcast network, radio station or a television station, it can run a facility in the absence of a human operator...

, audio analysis and fingerprinting, database design, yield management
Yield management
Revenue management is the process of understanding, anticipating and influencing consumer behavior in order to maximize yield or profits from a fixed, perishable resource...

, exit poll
Exit poll
An election exit poll is a poll of voters taken immediately after they have exited the polling stations. Unlike an opinion poll, which asks whom the voter plans to vote for or some similar formulation, an exit poll asks whom the voter actually voted for. A similar poll conducted before actual...

 systems, and machine listening
Machine listening
Machine listening is a technique using software and hardware to extract meaningful information from audio signals. The engineer Paris Smaragdis, interviewed in Technology Review, talks about these systems --"software that uses sound to locate people moving through rooms, monitor machinery for...

. 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
Functional programming
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state...

, symbiotic with the established Java platform, and designed for concurrency
Concurrency (computer science)
In computer science, concurrency is a property of systems in which several computations are executing simultaneously, and potentially interacting with each other...

.

Clojure's approach to concurrency is characterized by the concept of identities, 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-expression
S-expression
S-expressions or sexps are list-based data structures that represent semi-structured data. An S-expression may be a nested list of smaller S-expressions. S-expressions are probably best known for their use in the Lisp family of programming languages...

s that are first parsed into data structures by a reader before being compiled. Clojure's reader supports literal syntax for maps
Hash table
In computer science, a hash table or hash map is a data structure that uses a hash function to map identifying values, known as keys , to their associated values . Thus, a hash table implements an associative array...

, 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
Common Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...

 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

  • A compiled language
    Compiled language
    A compiled language is a programming language whose implementations are typically compilers , and not interpreters ....

     producing Java Virtual Machine
    Java Virtual Machine
    A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...

     (JVM) bytecode
    Bytecode
    Bytecode, also known as p-code , is a term which has been used to denote various forms of instruction sets designed for efficient execution by a software interpreter as well as being suitable for further compilation into machine code...

    • Tight Java
      Java (programming language)
      Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

       integration: By compiling into JVM Byte code, Clojure applications can be easily packaged and deployed to JVMs and app servers without added complexity. The language also provides macros which make it simple to use existing Java APIs. Clojure's data structures all implement standard Java Interfaces, making it easy to run code implemented in Clojure from Java.
  • Dynamic development with a read-eval-print loop
    Read-eval-print loop
    A read–eval–print loop , also known as an interactive toplevel, is a simple, interactive computer programming environment. The term is most usually used to refer to a Lisp interactive environment, but can be applied to command line shells and similar environments for F#, Smalltalk, Standard ML,...

  • Functions as first-class objects
    First-class function
    In computer science, a programming language is said to have first-class functions if it treats functions as first-class objects. Specifically, this means that the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning...

  • Emphasis on recursion
    Recursion (computer science)
    Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem. The approach can be applied to many types of problems, and is one of the central ideas of computer science....

     and higher-order function
    Higher-order function
    In mathematics and computer science, higher-order functions, functional forms, or functionals are functions which do at least one of the following:*take one or more functions as an input*output a function...

    s instead of side-effect-based looping
  • Lazy
    Lazy evaluation
    In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until the value of this is actually required and which also avoids repeated evaluations...

     sequences
  • Provides a rich set of immutable, persistent data structure
    Persistent data structure
    In computing, a persistent data structure is a data structure which always preserves the previous version of itself when it is modified; such data structures are effectively immutable, as their operations do not update the structure in-place, but instead always yield a new updated structure...

    s (including hashmaps, sets and lists)
  • Concurrent programming
    Concurrency (computer science)
    In computer science, concurrency is a property of systems in which several computations are executing simultaneously, and potentially interacting with each other...

     through software transactional memory
    Software transactional memory
    In computer science, software transactional memory is a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing. It is an alternative to lock-based synchronization. A transaction in this context is a piece of code that...

    , an agent system, and a dynamic var system
  • Multimethods
    Multiple dispatch
    Multiple dispatch or multimethods or function overloading is the feature of some object-oriented programming languages in which a function or method can be dynamically dispatched based on the run time type of more than one of its arguments...

     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)

Variants


Variations on the clojure language have been developed for platforms other than the JVM:
  • ClojureCLR, a full port of Clojure to the Common Language Runtime, interoperable with .NET libraries
  • ClojureScript, a subset of Clojure, compiled to optimized JavaScript, intended for use in web browsers and mobile devices
  • las3r, 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
Thread-safe
Thread safety is a computer programming concept applicable in the context of multi-threaded programs. A piece of code is thread-safe if it only manipulates shared data structures in a thread-safe manner, which enables safe execution by multiple threads at the same time...

 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
Software transactional memory
In computer science, software transactional memory is a concurrency control mechanism analogous to database transactions for controlling access to shared memory in concurrent computing. It is an alternative to lock-based synchronization. A transaction in this context is a piece of code that...

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

Rich Hickey

  • http://www.lisp50.org/schedule/schedule/hickey.html
  • http://www.simple-talk.com/opinion/geek-of-the-week/rich-hickey-geek-of-the-week/
  • http://clojure.org/
  • http://blog.objectmentor.com/articles/2009/06/05/rich-hickey-on-testing
  • http://www.codequarterly.com/2011/rich-hickey/

External links