This document provides a summary of an introduction to the Clojure programming language. It discusses what Clojure is, its timeline and adoption, functional programming concepts, concurrency features using Software Transactional Memory, the Lisp ideology it is based on including homoiconicity and its macro system. It also provides an overview of getting started with Clojure including using the REPL, basic syntax like symbols and keywords, data types, sequences, functions, and Java interoperability. Resources for learning more about Clojure are also listed.
6. What is Clojure
General Purpose Language
Dynamic
Functional
Lisp Strengths
Java/CLR/Javascript
7. Clojure - Timeline
First released October 16, 2007
1.0 released 05/04/09
1.1 released 12/31/09
1.2 released 08/19/10
1.3 released 09/23/11
ClojureScript released July, 2011
10. H/W Perf Free Lunch is over
http://www.gotw.ca/publications/concurrency-ddj.htm
11. Functional Programming
No side effects – immutable
Functions are first class citizens
Functions passed in as parameters
Functions return functions
Functions can be created at runtime
12. Software Transactional Memory
Provides transactional feature for application in
memory
Implements Multi Value Concurrency Control
(MVCC)
Atomic, Consistent, and Isolated
14. LISP
Lisp is worth learning for the profound
enlightenment experience you will have when
you finally get it; that experience will make
you a better programmer for the rest of your
days, even if you never actually use Lisp itself
a lot.
- Eric Raymond, How to Become a Hacker
30. Atomic Data Types
Clojure Example Java
string “hello” String
character a Character
integer 10 Int/Long/BigInteger
double 3.14159 Double
double 3.14159M BigDecimal
boolean true Boolean
nil nil null
symbol foo, + N/A
keyword :foo, ::foo N/A
31. Data Structures
Type Example Characteristics
list (2 4 6 8) • singly linked
• grows at front
vector [2 4 6 8] • indexed
• grows at end
map {:fname “Rich”, :lname • key/value pairs
“Hickey”} • unique key
set #{2 4 6 8} • unordered
• unique keys
32. Sequences
An abstraction over traditional lisp lists
Provides a view into the lists
Are lazy
Some functions on sequences:
(first seq): returns first element
(cons elem seq): prepends element
33. Syntax – (Prefix) Polish Notation
The operator appears before the operands
Format:
(operator operand operand)
36. Function Definition
define fn fn name
(defn add-numbers
“Add two numbers” documentation
[x y] arguments
(println “result: ” (+ x y))) fn body
37. Comments
Comments start with a semi-colon
(do
;; line comment
;;; block comment
(println “Hello World”)
(println “GoodBye!”)
) ; end of line comment
38. Predicate
It is a function that returns a boolean.
The name ends with a ?.
Example:
even?
40. Essence vs Ceremony
public class AddNumbers {
public static void main(String args[]) {
long sum = 0;
for (int i = 1; i < 1001; i++) {
sum += i;
}
System.out.println("The sum is: " + sum);
}
}
42. Useful Functions
count: returns number of items in a collection
– (count *4 “hello” c+) => 3
apply: applies a function on the arguments
– (apply – [10 5 2]) => 3
43. Program Flow
if, if-not
– (if test consequent alternative)
cond, condp
– (cond & clauses)
when, when-not
– (when test & body)
44. REPL: Doc
Documentation can be viewed with the doc
macro
user=> (doc +)
-------------------------
clojure.core/+
([] [x] [x y] [x y & more])
Returns the sum of nums. (+) returns 0.
nil
45. REPL: javadoc
Opens a browser with the javadoc for the
requested class
user=> (javadoc java.util.Random)
"http://java.sun.com/javase/6/docs/api/java/uti
l/Random.html"
46. REPL: source
Displays the source for the function
user=> (source even?)
(defn even?
"Returns true if n is even, throws an exception
if n is not an integer"
{:added "1.0"}
[n] (zero? (bit-and n 1)))
nil
47. REPL: inspector
Creates a Swing inspector on supplied object
user=> (use 'clojure.inspector)
nil
user=> (inspect (Math/PI))
54. Calling Clojure from Java
public class ClojureGreeter {
public static void main(String[] args) {
RT.loadResourceScript("clojure_script.clj");
Var greet = RT.var("clj.sample", “say");
String result = (String) greet.invoke(“John");
System.out.println(“Greeting: " + result);
}
}
55. Error Handling
All exceptions thrown are Runtime Exceptions.
(try expr* catch-clause* finally-clause?)
Example:
(try (/ 1 0)
(catch Exception e (println "in catch"))
(finally (println "in finally")))
56. MultiMethods
Provides a general indirect dispatching
mechanism
Dispatches based on type values, attributes, and
metadata
defmulti creates new multimethods
defmethod create methods for defmulti