Slide 1

Slide 1 text

Clojure

Slide 2

Slide 2 text

@teropa

Slide 3

Slide 3 text

Clojure is a dynamic, functional, general purpose programming language for the JVM.

Slide 4

Slide 4 text

Clojure is a dynamic, functional, general purpose programming language for the JVM. dynamically typed, extensible, interactive

Slide 5

Slide 5 text

Clojure is a dynamic, functional, general purpose programming language for the JVM. dynamically typed, extensible, interactive programming with functions, emphasis on immutability

Slide 6

Slide 6 text

Clojure is a dynamic, functional, general purpose programming language for the JVM. dynamically typed, extensible, interactive programming with functions, emphasis on immutability “Use it anywhere you would use Java”

Slide 7

Slide 7 text

Clojure is a dynamic, functional, general purpose programming language for the JVM. dynamically typed, extensible, interactive programming with functions, emphasis on immutability “Use it anywhere you would use Java” runs on the JVM, designed for the JVM

Slide 8

Slide 8 text

1991 Python 1995 Java Ruby JavaScript PHP 2003 Scala Groovy 2007 Clojure

Slide 9

Slide 9 text

imperative functional static dynamic

Slide 10

Slide 10 text

imperative functional static dynamic Java

Slide 11

Slide 11 text

imperative functional static dynamic Java Scala

Slide 12

Slide 12 text

imperative functional static dynamic Java Scala Python Ruby Groovy

Slide 13

Slide 13 text

imperative functional static dynamic Java Scala Python Ruby Groovy Clojure

Slide 14

Slide 14 text

Six Mindwarps ... so far.

Slide 15

Slide 15 text

1/6: Data & Behavior

Slide 16

Slide 16 text

OOP talk() String name int age Animal fetchBall() String breed Dog purr() String breed boolean grumpy Cat

Slide 17

Slide 17 text

The “Anemic Domain Model” String number String owner String bank int balance Account String firstName String lastName String number String ssn int yearStarted Employee String name String code int inventory Product String name String code String address String tel Customer DateTime timestamp String clerk int totalAmount int vatAmount Receipt

Slide 18

Slide 18 text

VerbEscorters action() Controller calculate() Calculator build() Builder validate() Validator makeImageTag() makeForm() Helper load() save() Service create() delete() Manager formatTime() formatNumber() getVatClasses() Util

Slide 19

Slide 19 text

Steve Yegge Verbs in Javaland are responsible for all the work, but as they are held in contempt by all, no Verb is ever permitted to wander about freely. If a Verb is seen in public at all, it must be escorted at all times by a Noun. Of course “escort”, being a Verb itself, is hardly allowed to run around naked; one must procure a VerbEscorter to facilitate the escorting. http://steve-yegge.blogspot.fi/2006/03/ execution-in-kingdom-of-nouns.html

Slide 20

Slide 20 text

Rich Hickey Object-orientation is overrated. [It was] born out of simulation, [but is] now used for everything, even when inappropriate http://clojure.org/rationale

Slide 21

Slide 21 text

Data List Vector Map Set (1 2 3 4) [1 2 3 4] {:name “Joe”, :parents [:jane :bob]} #{:one :two :three :four}

Slide 22

Slide 22 text

Functions conj assoc dissoc first rest nth peek pop keys vals empty? every? some cycle map reduce filter remove concat mapcat interleave interpose group-by partition split-at zipmap take drop

Slide 23

Slide 23 text

Data is data. Functions are functions. That’s OK.

Slide 24

Slide 24 text

Steve Yegge Classes are really the only modeling tool Java provides you. So whenever a new idea occurs to you, you have to sculpt it or wrap it or smash at it until it becomes a thing, even if it began life as an action, a process, or any other non-”thing” concept. I’ve really come around to what Perl folks were telling me 8 or 9 years ago: “Dude, not everything is an object.” http://steve-yegge.blogspot.fi/2006/03/ execution-in-kingdom-of-nouns.html

Slide 25

Slide 25 text

1/6: Data & Behavior Separate

Slide 26

Slide 26 text

2/6: Syntax

Slide 27

Slide 27 text

Syntactic Patterns InputStream in = file.openStream(); try { // ... } catch (IOException ioe) { // ... } finally { try { in.close(); } catch (IOException ioe) { // ... } }

Slide 28

Slide 28 text

Language Extensions try(InputStream in = file.openStream()) { // ... } catch (IOException ioe) { // ... }

Slide 29

Slide 29 text

...after 15 years.

Slide 30

Slide 30 text

Parentheses! (defn all-valid? [records] (every? empty? (map :errors records)))

Slide 31

Slide 31 text

Parentheses! record.getErrors() (errors record)

Slide 32

Slide 32 text

Parentheses! record.getErrors() ( ) errors record

Slide 33

Slide 33 text

Code Function definition Arguments Body (defn all-valid? [records] (every? empty? (map :errors records)))

Slide 34

Slide 34 text

(defn all-valid? [records] (every? empty? (map :errors records))) Data

Slide 35

Slide 35 text

( defn all-valid? [records] ( every? empty? ( map :errors records ) ) ) Data

Slide 36

Slide 36 text

( defn all-valid? [records] ( every? empty? ( map :errors records ) ) ) Data List Symbol Vector List List Keyword

Slide 37

Slide 37 text

Code is data. Functions transform data... ...functions transform code!

Slide 38

Slide 38 text

Macros (let [in (.openStream file)] (try (.read in) (finally (.close in))))

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Macros (with-open [in (.openStream file)] (.read in))

Slide 41

Slide 41 text

Paul Graham If you understand how compilers work, what’s really going on is not so much that Lisp has a strange syntax as that Lisp has no syntax. You write programs in the parse trees that get generated within the compiler when other languages are parsed. But these parse trees are fully accessible to your programs. You can write programs that manipulate them. In Lisp, these programs are called macros. They are programs that write programs. http://www.paulgraham.com/avg.html

Slide 42

Slide 42 text

2/6: Syntax Extensible

Slide 43

Slide 43 text

3/6: State

Slide 44

Slide 44 text

org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize (AbstractLazyInitializer.java:86) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation (AbstractLazyInitializer.java:140) at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke (JavassistLazyInitializer.java:190)

Slide 45

Slide 45 text

Mutable Stateful Object Graphs Portfolio Customer User Permission Currency CalculationPolicy

Slide 46

Slide 46 text

Stateless Code Database Library Results Query

Slide 47

Slide 47 text

Stateless Code Function Results Arguments

Slide 48

Slide 48 text

Stateless Code Results Arguments

Slide 49

Slide 49 text

Stateless Functions conj assoc dissoc first rest nth peek pop keys vals empty? every? some cycle map reduce filter remove concat mapcat interleave interpose group-by partition split-at zipmap take drop

Slide 50

Slide 50 text

3/6: State Mostly Unnecessary

Slide 51

Slide 51 text

4/6: State & Concurrency

Slide 52

Slide 52 text

["joe", "bob", "jane", "engelbert", "phyllis"]

Slide 53

Slide 53 text

["joe", "bob", "jane", "engelbert", "phyllis"]

Slide 54

Slide 54 text

["joe", "bob", "jane", "engelbert", "phyllis"]

Slide 55

Slide 55 text

["joe", "bob", "jane", "engelbert", "phyllis"]

Slide 56

Slide 56 text

["joe", "bob", "jane", “jill”, "engelbert", "phyllis"]

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

["joe", "bob", "jane", "engelbert", "phyllis"] Locking

Slide 59

Slide 59 text

["joe", "bob", "jane", "engelbert", "phyllis"] Locking

Slide 60

Slide 60 text

["joe", "bob", "jane", "engelbert", "phyllis"] Locking

Slide 61

Slide 61 text

["joe", "bob", "jane", "engelbert", "phyllis"] Locking

Slide 62

Slide 62 text

["joe", "bob", "jane", "engelbert", "phyllis"] Locking

Slide 63

Slide 63 text

["joe", "bob", "jane", "engelbert", "phyllis"] Locking

Slide 64

Slide 64 text

Rich Hickey One could argue that systems with manual locking are usually broken, and therefore their behavior itself, never mind their performance, is unpredictable. That’s been my experience with manual locking in the hands of mere mortal developers. http://www.azulsystems.com/blog/cliff/ 2008-05-27-clojure-stms-vs-locks

Slide 65

Slide 65 text

Immutable Data Structures List Vector Map Set (1 2 3 4) [1 2 3 4] {:name “Joe”, :parents [:jane :bob]} #{:one :two :three :four}

Slide 66

Slide 66 text

...And Stateless Functions conj assoc dissoc first rest nth peek pop keys vals empty? every? some cycle map reduce filter remove concat mapcat interleave interpose group-by partition split-at zipmap take drop

Slide 67

Slide 67 text

...And Managed References (def bob (ref 10)) (def joe (ref 50)) (defn transfer [from to amount] (dosync (alter from - amount) (alter to + amount))) (transfer bob joe 5)

Slide 68

Slide 68 text

4/6: State & Concurrency Managed

Slide 69

Slide 69 text

5/6: Workflow

Slide 70

Slide 70 text

Edit Compile Run

Slide 71

Slide 71 text

REPL Read- Eval- Print- Loop

Slide 72

Slide 72 text

Ron Garret The Remote Agent software, running on a custom port of Harlequin Common Lisp, flew aboard Deep Space 1, the first mission of NASA’s New Millennium program. Remote agent controlled DS1 for two days in 1999. During that time we were able to debug and fix a race condition that had not shown up during ground testing. Debugging a program on a $100M piece of hardware that is 100 million miles away is an interesting experience. http://www.flownet.com/gat/jpl-lisp.html

Slide 73

Slide 73 text

5/6: Workflow Continuous

Slide 74

Slide 74 text

6/6: Java

Slide 75

Slide 75 text

Java Ruby Python Platform Libraries Language Platform Libraries Language Platform Libraries Language

Slide 76

Slide 76 text

Java JRuby Jython Platform Libraries Language Libraries Language Libraries Language

Slide 77

Slide 77 text

The JVM is Badass

Slide 78

Slide 78 text

Language Ports

Slide 79

Slide 79 text

Clojure

Slide 80

Slide 80 text

(defn checknum [n] (if (zero? (rem n 2)) "even" "odd")) public interface IFn extends Callable, Runnable java.lang.String long

Slide 81

Slide 81 text

(doto (Executors/newCachedThreadPool) (.submit work1) (.submit work2) (.shutdown) (.awaitTermination 20 TimeUnit/SECONDS)) Java static method call Java static field reference Java instance method call

Slide 82

Slide 82 text

Stuart Halloway Clojure is a better Java than Java. http://www.infoq.com/presentations/Clojure- Java-Interop

Slide 83

Slide 83 text

6/6: Java As platform

Slide 84

Slide 84 text

Data & Behavior: Separate Syntax: Extensible State: Mostly Unnecessary State & Concurrency: Managed Workflow: Continuous Java: As Platform

Slide 85

Slide 85 text

@teropa