H e ll o , w o r l d ! \0
greeting
char greeting[14];strcpy(greeting, "Hello, World!");
15Friday, July 22, 2011
String greeting = new String("Hello, world!");
H e ll o , w o r l d !
java.lang.String
greeting
18Friday, July 22, 2011
H e ll o , w o r l d !
java.lang.String
greeting
H e ll o , b o r e d !
java.lang.String
String alt = greeting.replace("world", "bored");
alt
19Friday, July 22, 2011
Clojure Values
3 Long
6.022e23 Double
"Hello, world!" String
3.0M BigDecimal
9223372036854775808N BigInt
2/3 Ratio
22Friday, July 22, 2011
Clojure Values
even? Symbol
:last-name Keyword
(print 99 "bottles") List
[3.14 :pi] Vector
{:x 3 "y" 4} Map
#{7 9 "foo"} Set
23Friday, July 22, 2011
H e ll o , w o r l d !
java.lang.String
greeting
H e ll o , b o r e d !
java.lang.String
String alt = greeting.replace("world", "bored");
alt
25Friday, July 22, 2011
Clojure Values
ListO(1) at the head
O(n) anywhere else
Vector O(log32n) access
Map O(log32n) access
Set O(log32n) contains?
26Friday, July 22, 2011
321 = 32322 = 1024323 = 32,768324 = 1,048,576325 = 33,554,432326 = 1,073,741,824327 = 34,359,738,368
28Friday, July 22, 2011
log32 1000 < 2log32 10,000 < 3
log32 1,000,000 < 4log32 10,000,000 < 5
log32 1,000,000,000 < 6
29Friday, July 22, 2011
#{ {:first "Stuart" :last "Sierra"} {:first "Luke" :last "VanderHart"} {:first "Michael" :last "Fogus"} }
Code is Data
32Friday, July 22, 2011
#{ {:first "Stuart" :last "Sierra"} {:first "Luke" :last "VanderHart"} {:first "Michael" :last "Fogus"} }
Code is Data
A set of maps
33Friday, July 22, 2011
(defn average [& nums] (/ (reduce + nums) (count nums)))
(average 3 4 5)
ListSymbols
VectorSymbols
Function call
36Friday, July 22, 2011
Host Interop.
(def m (ConcurrentHashMap. 100))
(.put m "key" "value")
constructor
method call
37Friday, July 22, 2011
(let [file ...initializer...] (try ... do something ... (finally (.close file))))
41Friday, July 22, 2011
(defmacro with-open [bindings & body] `(let ~bindings (try ~@body (finally (.close ~(first bindings)))))
42Friday, July 22, 2011
(let [file ...initializer...] (try ... do something ... (finally (.close file))))
43Friday, July 22, 2011
#{ {:first "Stuart" :last "Sierra"} {:first "Luke" :last "VanderHart"} {:first "Michael" :last "Fogus"} }
Generic data access
A set of maps
46Friday, July 22, 2011
Author[] authors = ...
String names[] = new names[authors.length];for (int i = 0; i < authors.length; i++) { names[i] = authors[i].getFirstName();}
47Friday, July 22, 2011
(defn get-first-name [author] (get author :first))
(map get-first-name authors)("Stuart" "Luke" "Michael")
Higher-orderfunction
48Friday, July 22, 2011
(map (fn [a] (get a :first)) authors)("Stuart" "Luke" "Michael")
Anonymous function
49Friday, July 22, 2011
(map #(:first %) authors)("Stuart" "Luke" "Michael")
Keywords are functions!
52Friday, July 22, 2011
(map :first authors)
String names[] = new names[authors.length];for (int i = 0; i < authors.length; i++) { names[i] = authors[i].getName();}
vs.
54Friday, July 22, 2011
(map function set-of-maps)(map function vector-of-sets-of-maps)(map function list-of-vectors)(map function map-of-maps)
56Friday, July 22, 2011
(map function set-of-maps)
(map function (resultset-seq query))
(map function (line-seq file))(map function (xml-seq xml-tree))
(map function (file-seq directory))
(map function list-of-vectors)(map function map-of-maps)
(map function vector-of-sets-of-maps)
57Friday, July 22, 2011
mapfilter
reducecountsome
removereplace
Sequence API
and lots more...
ListVectorMap
ResultSetStream
DirectoryIterator
XML
and lots more...
Sequence Generators
58Friday, July 22, 2011
protocolmethod
method
method
method
protocolmethod
method
method
method
Protocol
62Friday, July 22, 2011
Existing Interfaces Your newprotocol here
Existing Types
Existing Method Implementations
Your new type here
and here!
63Friday, July 22, 2011
• Sierra "Solving the Expression Problem with Clojure 1.2" IBM DeveloperWorks
• Chris Houser "Clojure's Solutions to the Expression Problem" Strange Loop 2010
The Expression Problem
64Friday, July 22, 2011
class Invoice { private Date date;
public Date getDate() { return this.date; }
public void setDate(Date date) { this.date = date; }}
67Friday, July 22, 2011
class Date { public void setDay(int day); public void setMonth(int month); public void setYear(int year);}
Mutable!
68Friday, July 22, 2011
class Invoice { private Date date;
public Date getDate() { return this.date; }
public void setDate(Date date) { this.date = date; }} Better not
change it!
69Friday, July 22, 2011
class Invoice { private Date date;
public Date getDate() { return this.date; }
public void setDate(Date date) { this.date = date; }}
Better notchange it!
Better notchange it!
70Friday, July 22, 2011
July 25
Identity
State
Past
July 26July 24 function
Present Future
function
today
76Friday, July 22, 2011
July 25
Identity
State
Past
July 26July 24 function
Present Future
function
today
The future is a function of the past.
77Friday, July 22, 2011
Atom(def tick (atom 1))(deref tick) 1
(swap! tick inc)@tick 2
(swap! tick + 10)@tick 12
tick
2
12
+10
82Friday, July 22, 2011
value value value value
value value value value
value value value value
identity
identityidentity
83Friday, July 22, 2011
value value value value
value value value value
value value value value
identity
identityidentity
84Friday, July 22, 2011
Ref(def A (ref 1))(def B (ref 10))
(dosync (alter A inc) (alter B + 10))
A 1
10
20
inc
+10
B
2
transaction
87Friday, July 22, 2011
Ref(def A (ref 1))(def B (ref 10))
(dosync (alter A inc) (alter B + 10))
A
20
B
2
@A 2@B 20
88Friday, July 22, 2011
Image Credits
More• Clojure: clojure.org
• Clojure/core: clojure.com
• Me: stuartsierra.com
• @stuartsierra
• openclipart.org
• pdtextures.blogspot.com
• Clojure logo by Tom Hickey99Friday, July 22, 2011