IntroductionBasics
ConcurrencyConclusion
Clojure
Marcel Klinzing
December 13, 2012
M. Klinzing Clojure 1/18
IntroductionBasics
ConcurrencyConclusion
Overview/History
Functional programminglanguage
Lisp dialect
Compiles to Java Bytecode
Implemented in Java
Created by Rich Hickey
Version 1.0.0 released in2009
Current Version: 1.4.0 Rich Hickey
M. Klinzing Clojure 2/18
IntroductionBasics
ConcurrencyConclusion
Features
“Program as data”
First-class functions
Dynamically typed
Java interoperability
Immutable, persistent data
Mutable references to data
Support for concurrent programming
and more. . .
M. Klinzing Clojure 3/18
IntroductionBasics
ConcurrencyConclusion
Forms
Part of Clojure called Reader reads chunks of the program calledforms
Reader translates forms into Clojure data structures
Clojure compiles data structures and executes them
Forms (excerpt):
Form Example
Boolean true, falseList (+ 1 2), (map + [1 2] [3 4])
Number 1, 3.142, 22/7String “hello world”
Symbol foo, foo-?, java.lang.ThreadVector [1 2 3]
M. Klinzing Clojure 4/18
IntroductionBasics
ConcurrencyConclusion
Variables and Functions
Defining a (global) variable: (def identifier value)
Defining a function: (defn identifier (parameter-vec body)+)
Calling a function: (identifier param1 param2 ...)
1 (defn f
2 ([] (println "hello, world!"))
3 ([x] (println x)))
45 ; brackets can be omitted if6 ; there is only one parameter−vec and body7 (defn g [] (println "bar"))
89 (f) ; Output: hello, world!
10 (f "foo") ; Output: foo11 (g) ; Output: bar
M. Klinzing Clojure 5/18
IntroductionBasics
ConcurrencyConclusion
Destructuring
Destructuring used to bind single values in a collection to variables
1 (defn f [ [x y] ]
2 (+ x y))
34 (println (f [1 1])) ; Output: 25 (println (f [4 5 6 7])) ; Output: 9
M. Klinzing Clojure 6/18
IntroductionBasics
ConcurrencyConclusion
Java Interoperability
Package java.lang imported automatically
Create new Object: (new classname & args)
Call of variables/methods:(. class-or-instance member-symbol & args)
Note that every Clojure function is an Object which implementsjava.lang.Runnable and java.lang.Callable
1 (defn delay-print []
2 (. Thread sleep 100)
3 (println "new Thread"))
45 (. (new Thread delay-print) start)
6 (print "start ")
7 ; Output: start new Thread
M. Klinzing Clojure 7/18
IntroductionBasics
ConcurrencyConclusion
Reference Types
Reference types = mutable references to immutable data
Reference types and their purpose:
Refs: Synchronous, coordinated updates of dataAgents: Asynchronous update of a single valueAtoms: Synchronous update of a single valueVars: Synchronous update of a thread-local value
M. Klinzing Clojure 8/18
IntroductionBasics
ConcurrencyConclusion
Software Transactional Memory
Software Transactional Memory (STM) is a model/concept to viewmemory as a database
STM used for coordinated updates (refs)
Updates of values are done in transactions
Properties of STM transactions:
Atomic updatesConsistent updatesIsolated updates
M. Klinzing Clojure 9/18
IntroductionBasics
ConcurrencyConclusion
STM - Multiversion Concurrency Control
STM uses Multiversion Concurrency Control (MVCC):
Start of a Transaction: Unique ID (called point) is generatedTransaction works against private copies of references (associatedwith the point)Changes to refs will be visible only after the transactioncommits/endsIf a transaction notices that a ref has been already set/altered byanother transaction, the transaction will restart
To start a transaction: (dosync & exprs)
M. Klinzing Clojure 10/18
IntroductionBasics
ConcurrencyConclusion
Refs
Creating a ref: (ref initial-value)
Dereferencing a ref: (deref ref-inst)
or shorter: @ref-inst
To change the reference: (ref-set new-value)
Note: ref-set can only be called in a transaction; otherwise aIllegalStateException is thrown
M. Klinzing Clojure 11/18
IntroductionBasics
ConcurrencyConclusion
Refs – Example
1 (def x (ref 0))
23 (defn increase []
4 (. Thread sleep 100)
5 (dosync (ref-set x (+ @x 1))))
67 (defn decrease []
8 (. Thread sleep 100)
9 (dosync (ref-set x (- @x 1))))
1011 (def inc-thread (new Thread increase))
12 (def dec-thread (new Thread decrease))
1314 (. inc-thread start) (. dec-thread start)
15 (. inc-thread join) (. dec-thread join)
16 (println @x) ; Output: 0
M. Klinzing Clojure 12/18
IntroductionBasics
ConcurrencyConclusion
Agents
Creating an agent: (agent initial-value)
Dereferencing for agents as for refs: @agent-inst
Changing of agents per update function:(send agent-inst update-fn & args)
(update-fn @agent-inst arg1 arg2 ...) is run later on a thread ina thread pool and the return value is “assigned” to the agent
M. Klinzing Clojure 13/18
IntroductionBasics
ConcurrencyConclusion
Agents – Example
1 (def ag (agent 0))
23 (defn increase [x]
4 (+ x 1))
56 (println @(send ag increase)) ; Output: 07 (. Thread sleep 100)
8 (println @ag) ; Output: 1
M. Klinzing Clojure 14/18
IntroductionBasics
ConcurrencyConclusion
Benchmark
Benchmark taken from the “Computer Language BenchmarksGame” ( http://shootout.alioth.debian.org )
“thread-ring” program:
Create 503 linked pre-emptive threadsThread 503 should be linked to thread 1, forming an unbroken ringPass a token to thread 1Pass the token from thread to thread N timesPrint the name of the last thread (1 to 503) to take the token
M. Klinzing Clojure 15/18
IntroductionBasics
ConcurrencyConclusion
Benchmarks Results (excerpt)
N = 50,000,000Quad-core 2.4Ghz Intel R© Q6600 R©; 4GB RAM; 250GB SATA II diskdrivex64 Ubuntu
TM
M. Klinzing Clojure 16/18
IntroductionBasics
ConcurrencyConclusion
Conclusion
Pros:
Functional programming (first-class functions, small code, . . . )Java interoperabilitySupport for concurrent programming
Cons:
Speed (since it runs on the JVM)
M. Klinzing Clojure 17/18
IntroductionBasics
ConcurrencyConclusion
Other Clojure Implementations
ClojureScript
Compiles Clojure code to JavaScriptimplemented in Clojure
ClojureCLR
Native implementation of Clojure on the Common LanguageRuntime (CLR; virtual machine of Microsoft’s .NET framework)Implemented in C# (and Clojure itself)
M. Klinzing Clojure 18/18
References
Official site:http://clojure.org
Book:Stuart Halloway and Aaron Bedra. Programming Clojure.2nd ed. The Pragmatic Programmers, 2012.
Tutorial:R. Mark Volkmann. Clojure - Functional Programming for the JVMhttp://java.ociweb.com/mark/clojure/article.html
Benchmark:http://shootout.alioth.debian.org/
Pictures:
Slide 2: http://en.wikipedia.org/wiki/File:Rich_Hickey.jpg
M. Klinzing Clojure
Thank you for your attention.
M. Klinzing Clojure
Backup Slides – Multimethods
Declaring a multimethod: (defmulti identifier choosing-fn)
Considering a function call (identifier arg1 arg2 ...);(choosing-fn arg1 arg2 ...) is called and the return valuedetermines the implementation to be called
Defining a multimethod:(defmethod identifier choosing-val (parameter-vec body)+)
M. Klinzing Clojure
Backup Slides – Metadata
Metadata = Data about data that doesn’t change its logical valuesWrite ^metadata-map before symbols to attach metadataExample:
1 (def x 1)
2 (defn ^{:doc "Returns its first argument"} f [x y] x)
34 (println (meta #’x))
5 ; Output: {:ns #<Namespace user>, :name x, :line 1,6 ; : file D:\Clojure\metadata. clj }7 (println (meta #’f))
8 ;Output: {: arglists ([ x y ]), :ns #<Namespace user>, :name f,9 ; :doc Returns its first argument, : line 3, : file ...}
10 (println (doc f))
11 ; Output: −−−−−−−−−−−−−−−−−−−−−−−−−12 ; user/f13 ; ([ x y ])14 ; Returns its first argument
M. Klinzing Clojure