Top Banner
Clojure Lisp for the Real World @stuartsierra #clojure 1 Friday, July 22, 2011
99

Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Jul 31, 2018

Download

Documents

letram
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

ClojureLisp for the Real World

@stuartsierra#clojure

1Friday, July 22, 2011

Page 2: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Bullet Points

• Values

• Code is data

• Generic data access

• Concurrency

2Friday, July 22, 2011

Page 3: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Stuart SierraRelevance, Inc.

Clojure/core

Clojure contributor

3Friday, July 22, 2011

Page 4: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Values

4Friday, July 22, 2011

Page 5: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Values

3

5Friday, July 22, 2011

Page 6: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Values

3 + 2 = 5

6Friday, July 22, 2011

Page 7: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Values

3let x =

7Friday, July 22, 2011

Page 8: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Values

3let x =

let x = 5

8Friday, July 22, 2011

Page 9: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Values

3let = 5

9Friday, July 22, 2011

Page 10: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Values

3define = 5

10Friday, July 22, 2011

Page 11: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Values

3

11Friday, July 22, 2011

Page 12: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Values

3Immutable

12Friday, July 22, 2011

Page 13: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Values

3Immutable Persistent

13Friday, July 22, 2011

Page 14: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

He ll o, world!" "

14Friday, July 22, 2011

Page 15: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

H e ll o , w o r l d ! \0

greeting

char greeting[14];strcpy(greeting, "Hello, World!");

15Friday, July 22, 2011

Page 16: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

H e ll o , w o r l d ! \0

greeting

char* name = greeting + 7;

name

16Friday, July 22, 2011

Page 17: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

H e ll o , b o r e d ! \0

greeting

name[7] = 'b';name[10] = 'e';

name

17Friday, July 22, 2011

Page 18: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

String greeting = new String("Hello, world!");

H e ll o , w o r l d !

java.lang.String

greeting

18Friday, July 22, 2011

Page 19: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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

Page 20: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

H e ll o , w o r l d !

java.lang.String

greeting

Immutable

20Friday, July 22, 2011

Page 21: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

In the real world,values don't change.

21Friday, July 22, 2011

Page 22: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Clojure Values

3 Long

6.022e23 Double

"Hello, world!" String

3.0M BigDecimal

9223372036854775808N BigInt

2/3 Ratio

22Friday, July 22, 2011

Page 23: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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

Page 24: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Immutable Persistent

Clojure Values

24Friday, July 22, 2011

Page 25: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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

Page 26: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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

Page 27: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

...

...

...

...

27Friday, July 22, 2011

Page 28: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

321 = 32322 = 1024323 = 32,768324 = 1,048,576325 = 33,554,432326 = 1,073,741,824327 = 34,359,738,368

28Friday, July 22, 2011

Page 29: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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

Page 30: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

In the real world,log32n is fast enough.

30Friday, July 22, 2011

Page 31: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Code is Data

31Friday, July 22, 2011

Page 32: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

#{ {:first "Stuart" :last "Sierra"} {:first "Luke" :last "VanderHart"} {:first "Michael" :last "Fogus"} }

Code is Data

32Friday, July 22, 2011

Page 33: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

#{ {:first "Stuart" :last "Sierra"} {:first "Luke" :last "VanderHart"} {:first "Michael" :last "Fogus"} }

Code is Data

A set of maps

33Friday, July 22, 2011

Page 34: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(+ 3 4 5)List

Symbol

34Friday, July 22, 2011

Page 35: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(+ 3 4 5)List

Symbol

Function call

35Friday, July 22, 2011

Page 36: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(defn average [& nums] (/ (reduce + nums) (count nums)))

(average 3 4 5)

ListSymbols

VectorSymbols

Function call

36Friday, July 22, 2011

Page 37: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Host Interop.

(def m (ConcurrentHashMap. 100))

(.put m "key" "value")

constructor

method call

37Friday, July 22, 2011

Page 38: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

You

Lexer/Parser

Characters

AbstractSyntax Tree

Compiler/Interpreter

38Friday, July 22, 2011

Page 39: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Clojure Reader

ClojureCompiler

You Characters

Data Structures

39Friday, July 22, 2011

Page 40: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Clojure Reader

You Characters

Data Structures

Macros

ClojureCompiler

40Friday, July 22, 2011

Page 41: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(let [file ...initializer...] (try ... do something ... (finally (.close file))))

41Friday, July 22, 2011

Page 42: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(defmacro with-open [bindings & body] `(let ~bindings (try ~@body (finally (.close ~(first bindings)))))

42Friday, July 22, 2011

Page 43: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(let [file ...initializer...] (try ... do something ... (finally (.close file))))

43Friday, July 22, 2011

Page 44: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(with-open [file ...initializer...] ... do something ...)

44Friday, July 22, 2011

Page 45: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Generic data access

45Friday, July 22, 2011

Page 46: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

#{ {:first "Stuart" :last "Sierra"} {:first "Luke" :last "VanderHart"} {:first "Michael" :last "Fogus"} }

Generic data access

A set of maps

46Friday, July 22, 2011

Page 47: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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

Page 48: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(defn get-first-name [author] (get author :first))

(map get-first-name authors)("Stuart" "Luke" "Michael")

Higher-orderfunction

48Friday, July 22, 2011

Page 49: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(map (fn [a] (get a :first)) authors)("Stuart" "Luke" "Michael")

Anonymous function

49Friday, July 22, 2011

Page 50: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(map #(get % :first) authors)("Stuart" "Luke" "Michael")

Anonymous function

50Friday, July 22, 2011

Page 51: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(map #(% :first) authors)("Stuart" "Luke" "Michael")

Maps are functions

51Friday, July 22, 2011

Page 52: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(map #(:first %) authors)("Stuart" "Luke" "Michael")

Keywords are functions!

52Friday, July 22, 2011

Page 53: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(map :first authors)("Stuart" "Luke" "Michael")

Keywords are functions

53Friday, July 22, 2011

Page 54: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(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

Page 55: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(map function set-of-maps)

55Friday, July 22, 2011

Page 56: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(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

Page 57: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

(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

Page 58: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

mapfilter

reducecountsome

removereplace

Sequence API

and lots more...

ListVectorMap

ResultSetStream

DirectoryIterator

XML

and lots more...

Sequence Generators

58Friday, July 22, 2011

Page 59: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Polymorphism

59Friday, July 22, 2011

Page 60: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Object-Oriented,The Good Parts

60Friday, July 22, 2011

Page 61: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

multimethod

method

method

method

method

method

dispatchfunction

61Friday, July 22, 2011

Page 62: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

protocolmethod

method

method

method

protocolmethod

method

method

method

Protocol

62Friday, July 22, 2011

Page 63: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Existing Interfaces Your newprotocol here

Existing Types

Existing Method Implementations

Your new type here

and here!

63Friday, July 22, 2011

Page 65: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Concurrency

65Friday, July 22, 2011

Page 66: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Concurrency

Parallelism

State

66Friday, July 22, 2011

Page 67: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

class Invoice { private Date date;

public Date getDate() { return this.date; }

public void setDate(Date date) { this.date = date; }}

67Friday, July 22, 2011

Page 68: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

class Date { public void setDay(int day); public void setMonth(int month); public void setYear(int year);}

Mutable!

68Friday, July 22, 2011

Page 69: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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

Page 70: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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

Page 71: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Programming with immutable values

means never having to say you're sorry.

71Friday, July 22, 2011

Page 72: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

July 25

today

72Friday, July 22, 2011

Page 73: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

July 25 July 26

today

73Friday, July 22, 2011

Page 74: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

July 25

Identity

State

July 26

today

Value Value

74Friday, July 22, 2011

Page 75: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

July 25

Identity

State

July 26

today

function

Value Value

function

75Friday, July 22, 2011

Page 76: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

July 25

Identity

State

Past

July 26July 24 function

Present Future

function

today

76Friday, July 22, 2011

Page 77: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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

Page 78: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Mutable References

Ref Atom

Var Agent

78Friday, July 22, 2011

Page 79: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

readers

Atomwriter

writer

abortretry

79Friday, July 22, 2011

Page 80: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Atom(def tick (atom 1))(deref tick) 1

tick

1

80Friday, July 22, 2011

Page 81: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Atom(def tick (atom 1))(deref tick) 1

(swap! tick inc)@tick 2

tick

1

2

inc

81Friday, July 22, 2011

Page 82: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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

Page 83: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

value value value value

value value value value

value value value value

identity

identityidentity

83Friday, July 22, 2011

Page 84: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

value value value value

value value value value

value value value value

identity

identityidentity

84Friday, July 22, 2011

Page 85: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Refwriter

writer

readers

abortretryRef

transaction

85Friday, July 22, 2011

Page 86: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Ref(def A (ref 1))(def B (ref 10))

A 1

10B

86Friday, July 22, 2011

Page 87: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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

Page 88: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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

Page 89: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

AtomicConsistentIsolatedDurable

Database Transactions

89Friday, July 22, 2011

Page 90: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

AtomicConsistentIsolated

Clojure Transactions

90Friday, July 22, 2011

Page 91: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Agentactionaction

readers

action queue

Agent Thread Pool

91Friday, July 22, 2011

Page 92: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Agent(def A (agent 1))

A 1queue

92Friday, July 22, 2011

Page 93: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Agent(def A (agent 1))

(send A inc)@A 1

A 1inc

queue

93Friday, July 22, 2011

Page 94: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Agent(def A (agent 1))

(send A inc)@A 1

A 1inc

2@A 2

queue

94Friday, July 22, 2011

Page 95: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Varroot

binding

thread-local

binding

thread-local

binding

other threads

95Friday, July 22, 2011

Page 96: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

Ref Atom

Var Agent

Concurrency

96Friday, July 22, 2011

Page 97: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

In the real world, JavaScript is everywhere

97Friday, July 22, 2011

Page 98: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

ClojureScript

98Friday, July 22, 2011

Page 99: Clojure Lisp for the Real World - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Clojure_ Lisp for the Real World... · Clojure Lisp for the Real World @stuartsierra #clojure Friday,

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