Slide 1

Slide 1 text

http://xkcd.com/{224,297}

Slide 2

Slide 2 text

Clojure & ClojureScript Stefan Kanev http://skanev.com/ @skanev I.T.A.K.E. Unconf 26 May 2015 Sofia

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

twitter: @skanev github: skanev blog: http://skanev.com/

Slide 5

Slide 5 text

Clojure from 10,000 feet

Slide 6

Slide 6 text

A modern LISP, hosted in the JVM, with a focus on concurrency

Slide 7

Slide 7 text

A modern LISP, hosted in the JVM, with a focus on concurrency

Slide 8

Slide 8 text

LISP: “that language with the parentheses”
 also: a subculture

Slide 9

Slide 9 text

“LISP is too hip, even for me” – a hipster

Slide 10

Slide 10 text

LISP

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

a * b + c * d (+ (* a b) (* c d))

Slide 13

Slide 13 text

homoiconicity data is code, code is data

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

A modern LISP, hosted in the JVM, with a focus on concurrency

Slide 16

Slide 16 text

Sans the antiquities: car cdr lambda

Slide 17

Slide 17 text

Way better design

Slide 18

Slide 18 text

Less parentheses

Slide 19

Slide 19 text

A modern LISP, hosted in the JVM, with a focus on concurrency

Slide 20

Slide 20 text

Stable platform

Slide 21

Slide 21 text

Access to full Java ecosystem

Slide 22

Slide 22 text

Occasionally a huge P.I.T.A.

Slide 23

Slide 23 text

A modern LISP, hosted in the JVM, with a focus on concurrency

Slide 24

Slide 24 text

parallelism vs. concurrency

Slide 25

Slide 25 text

parallelism Breaking a problem down to smaller parts that can be computed at the same time

Slide 26

Slide 26 text

concurrency Synchronising a number of independent processes that are fighting for the same resources

Slide 27

Slide 27 text

Syntax

Slide 28

Slide 28 text

(func arg-1 arg-2 …)

Slide 29

Slide 29 text

(println "Hello world")

Slide 30

Slide 30 text

(+ 1 2) (< x y)

Slide 31

Slide 31 text

(+ 1 2 3 4 5 6) (< u v w x y z)

Slide 32

Slide 32 text

(+ (* a b) (* c d)) (+ (* a b) (* c d))

Slide 33

Slide 33 text

(defn say-hello [who] (println "Hello" who "!")) (say-hello "Doctor")

Slide 34

Slide 34 text

OMG Parentheses! Or should I say: ((o) ((m)) (g) ( (( (( (( )) )) )) ))

Slide 35

Slide 35 text

Parentheses in LISP are not unlike metric time

Slide 36

Slide 36 text

(defn classify [age] (if (<= 13 age 19) "Teenager" "A normal person")) (classify 18) ; "Teenager"

Slide 37

Slide 37 text

(defn factorial [n] (if (= n 1) 1 (* n (factorial (- n 1)))))

Slide 38

Slide 38 text

(defn fib [n] (cond (= n 0) 1 (= n 1) 1 :else (+ (fib (- n 1)) (fib (- n 2)))))

Slide 39

Slide 39 text

(fn [x] (* x 2))

Slide 40

Slide 40 text

(map (fn [n] (str "Mr. " n)) ["Anderson" "Bond" "Bean"]) ; ("Mr. Anderson” ; "Mr. Bond" ; "Mr. Bean")

Slide 41

Slide 41 text

(filter prime? (range 2 100)) ; (2 3 5 7 11 13 17 19 23 29 31 37 41 ; 43 47 53 59 61 67 71 73 79 83 89 97)

Slide 42

Slide 42 text

(defn prime? [n] (not-any? (fn [x] (zero? (rem n x))) (range 2 (inc (Math/sqrt n)))))

Slide 43

Slide 43 text

Data Structures

Slide 44

Slide 44 text

maps (hashes) vectors (arrays) sets

Slide 45

Slide 45 text

immutable

Slide 46

Slide 46 text

“Modifying” a structure creates a copy containing the change. The original remains unmodified.

Slide 47

Slide 47 text

Y

Slide 48

Slide 48 text

simplicity

Slide 49

Slide 49 text

multicore ❤ immutable

Slide 50

Slide 50 text

persistent

Slide 51

Slide 51 text

The “originals” are maximally reused

Slide 52

Slide 52 text

a ! (3 2 1)

Slide 53

Slide 53 text

1 2 3 a a ! (3 2 1)

Slide 54

Slide 54 text

a ! (3 2 1) b ! (conj a 4) (4 3 2 1)

Slide 55

Slide 55 text

1 2 3 a a ! (3 2 1) b ! (4 3 2 1) 4 b 5 c c ! (conj a 5)

Slide 56

Slide 56 text

Hash Table 1020394597 1020205863 {:foo first, :bar second} first second

Slide 57

Slide 57 text

Hash Table 1020394597 1020205863 Hash Table 1020394597 1020205863 1021027443 (conj table :qux 1024)

Slide 58

Slide 58 text

not that simple

Slide 59

Slide 59 text

Vectors (“arrays” in other langs)

Slide 60

Slide 60 text

Vectors are represented by trees Each node has 32 children

Slide 61

Slide 61 text

log32 … …

Slide 62

Slide 62 text

… … ⨯

Slide 63

Slide 63 text

O(?)

Slide 64

Slide 64 text

log32 n

Slide 65

Slide 65 text

325 = 33 554 432 326 = 1 073 741 824

Slide 66

Slide 66 text

“essentially constant time”

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

Software Transactional Memory

Slide 69

Slide 69 text

concurrency 101

Slide 70

Slide 70 text

100 € +50 € ⨯2 How much money will the account have?

Slide 71

Slide 71 text

300 € 250 €

Slide 72

Slide 72 text

100 € +50 € = 150 € x2 = 300 €

Slide 73

Slide 73 text

100 € x2 = 200 € +50 € = 250 €

Slide 74

Slide 74 text

100 € 200 € 150 € x2 +50 100 € 100 €

Slide 75

Slide 75 text

100 € 150 € 200 € x2 +50 100 € 100 €

Slide 76

Slide 76 text

ref

Slide 77

Slide 77 text

state mutation is modelled as a transaction

Slide 78

Slide 78 text

if two transactions “get in each others’ way”, one of them will restart

Slide 79

Slide 79 text

(def account (ref 100)) ; Thread 1 - Uncle Scrooge (dosync (alter account (fn [n] (* n 2))) (println "Scrooge: set to " @account)) ; Thread 2 - Donald Duck (dosync (alter account (fn [n] (+ n 50))) (println "Donald: set to " @account))

Slide 80

Slide 80 text

100 € 300 € 150 € x2 +50 100 € 100 € X x2 150 €

Slide 81

Slide 81 text

Y

Slide 82

Slide 82 text

familiar

Slide 83

Slide 83 text

safe, easy, no deadlocks

Slide 84

Slide 84 text

Macros

Slide 85

Slide 85 text

Powerful instrument allowing expressive code

Slide 86

Slide 86 text

Also known as: the mother of all metaprogramming

Slide 87

Slide 87 text

Macro Code Some other code

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

doStuff();

Slide 90

Slide 90 text

Outputs on STDOUT. We want to have the result in a string instead.

Slide 91

Slide 91 text

PrintStream original = System.out; ByteArrayOutputStream output = new ByteArrayOutputStream(); try { PrintStream fileStream = new PrintStream(output); orgStream = System.out; System.setOut(fileStream); doStuff(); } finally { System.setOut(original); } String output = output.toString();

Slide 92

Slide 92 text

(do-stuff)

Slide 93

Slide 93 text

(with-out-str (do-stuff))

Slide 94

Slide 94 text

(with-out-str (do-stuff))

Slide 95

Slide 95 text

String output = withOutStr { doStuff(); }

Slide 96

Slide 96 text

unless

Slide 97

Slide 97 text

(unless (hungry?) (sleep) (eat)) (if (not (hungry?)) (sleep) (eat))

Slide 98

Slide 98 text

(defmacro unless [condition consequent alternative] `(if (not ~condition) ~consequent ~alternative))

Slide 99

Slide 99 text

unless (isHungry()) { sleep(); } else { eat(); }

Slide 100

Slide 100 text

(cond (= n 0) 1 (= n 1) 1 :else (+ (fib (- n 2)) (fib (- n 1)))) (if (= n 0) 1 (if (= n 1) 1 (+ (fib (- n 2)) (fib (- n 1)))))

Slide 101

Slide 101 text

DSL Domain Specific Languages

Slide 102

Slide 102 text

Transparency

Slide 103

Slide 103 text

(source if-let) (source await)

Slide 104

Slide 104 text

ClojureScript

Slide 105

Slide 105 text

om

Slide 106

Slide 106 text

Very dynamic

Slide 107

Slide 107 text

DEMO

Slide 108

Slide 108 text

REPL Oriented Programming

Slide 109

Slide 109 text

No content