clojure lisp for the real world - o'reilly mediaassets.en.oreilly.com/1/event/61/clojure_ lisp...

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

Upload: letram

Post on 31-Jul-2018

239 views

Category:

Documents


0 download

TRANSCRIPT

ClojureLisp for the Real World

@stuartsierra#clojure

1Friday, July 22, 2011

Bullet Points

• Values

• Code is data

• Generic data access

• Concurrency

2Friday, July 22, 2011

Stuart SierraRelevance, Inc.

Clojure/core

Clojure contributor

3Friday, July 22, 2011

Values

4Friday, July 22, 2011

Values

3

5Friday, July 22, 2011

Values

3 + 2 = 5

6Friday, July 22, 2011

Values

3let x =

7Friday, July 22, 2011

Values

3let x =

let x = 5

8Friday, July 22, 2011

Values

3let = 5

9Friday, July 22, 2011

Values

3define = 5

10Friday, July 22, 2011

Values

3

11Friday, July 22, 2011

Values

3Immutable

12Friday, July 22, 2011

Values

3Immutable Persistent

13Friday, July 22, 2011

He ll o, world!" "

14Friday, July 22, 2011

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

greeting

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

15Friday, July 22, 2011

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

greeting

char* name = greeting + 7;

name

16Friday, July 22, 2011

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

greeting

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

name

17Friday, 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

H e ll o , w o r l d !

java.lang.String

greeting

Immutable

20Friday, July 22, 2011

In the real world,values don't change.

21Friday, 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

Immutable Persistent

Clojure Values

24Friday, 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

...

...

...

...

27Friday, 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

In the real world,log32n is fast enough.

30Friday, July 22, 2011

Code is Data

31Friday, 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

(+ 3 4 5)List

Symbol

34Friday, July 22, 2011

(+ 3 4 5)List

Symbol

Function call

35Friday, 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

You

Lexer/Parser

Characters

AbstractSyntax Tree

Compiler/Interpreter

38Friday, July 22, 2011

Clojure Reader

ClojureCompiler

You Characters

Data Structures

39Friday, July 22, 2011

Clojure Reader

You Characters

Data Structures

Macros

ClojureCompiler

40Friday, 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

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

44Friday, July 22, 2011

Generic data access

45Friday, 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 #(get % :first) authors)("Stuart" "Luke" "Michael")

Anonymous function

50Friday, July 22, 2011

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

Maps are functions

51Friday, July 22, 2011

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

Keywords are functions!

52Friday, July 22, 2011

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

Keywords are functions

53Friday, 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)

55Friday, 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

Polymorphism

59Friday, July 22, 2011

Object-Oriented,The Good Parts

60Friday, July 22, 2011

multimethod

method

method

method

method

method

dispatchfunction

61Friday, 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

Concurrency

65Friday, July 22, 2011

Concurrency

Parallelism

State

66Friday, 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

Programming with immutable values

means never having to say you're sorry.

71Friday, July 22, 2011

July 25

today

72Friday, July 22, 2011

July 25 July 26

today

73Friday, July 22, 2011

July 25

Identity

State

July 26

today

Value Value

74Friday, July 22, 2011

July 25

Identity

State

July 26

today

function

Value Value

function

75Friday, 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

Mutable References

Ref Atom

Var Agent

78Friday, July 22, 2011

readers

Atomwriter

writer

abortretry

79Friday, July 22, 2011

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

tick

1

80Friday, July 22, 2011

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

(swap! tick inc)@tick 2

tick

1

2

inc

81Friday, 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

Refwriter

writer

readers

abortretryRef

transaction

85Friday, July 22, 2011

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

A 1

10B

86Friday, 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

AtomicConsistentIsolatedDurable

Database Transactions

89Friday, July 22, 2011

AtomicConsistentIsolated

Clojure Transactions

90Friday, July 22, 2011

Agentactionaction

readers

action queue

Agent Thread Pool

91Friday, July 22, 2011

Agent(def A (agent 1))

A 1queue

92Friday, July 22, 2011

Agent(def A (agent 1))

(send A inc)@A 1

A 1inc

queue

93Friday, July 22, 2011

Agent(def A (agent 1))

(send A inc)@A 1

A 1inc

2@A 2

queue

94Friday, July 22, 2011

Varroot

binding

thread-local

binding

thread-local

binding

other threads

95Friday, July 22, 2011

Ref Atom

Var Agent

Concurrency

96Friday, July 22, 2011

In the real world, JavaScript is everywhere

97Friday, July 22, 2011

ClojureScript

98Friday, 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