Slide 1

Slide 1 text

Clojure for Non-Clojurians Takahiro Noda @tnoda_ Kansai Kansugata Dojo [2014-12-06 Sat]

Slide 2

Slide 2 text

Outline Clojure? . . . The Good Parts . . . The Bad Parts . . . Applications . . .

Slide 3

Slide 3 text

Clojure? Clojure

Slide 4

Slide 4 text

Clojure? Clojure Java

Slide 5

Slide 5 text

Hello World! Example (Java) $ curl -O http :// repo1.maven.org/maven2/org/clojure/ clojure /1.6.0/ clojure -1.6.0. zip $ unzip clojure -1.6.0. zip $ cd clojure -1.6.0/ $ java -cp clojure -1.6.0. jar clojure.main -e ’(println " Hello␣World!")’ Hello World!

Slide 6

Slide 6 text

Nyampass Co., Ltd. Figure : http://nyampass.com/ They have been using Clojure to develop their mobile BaaS services.

Slide 7

Slide 7 text

Non non biyori (2013)

Slide 8

Slide 8 text

Hello World! (recap) Example (Java) $ java -cp clojure -1.6.0. jar clojure.main \ -e ’(println "Hello␣World!")’

Slide 9

Slide 9 text

Sudden Jar _人人人人人人人人人人_ > clojure-1.6.0.jar <  ̄YYYYYYYYYY ̄

Slide 10

Slide 10 text

Java SE !"# javac $%%&'&()*+,-)''.'

Slide 11

Slide 11 text

Clojure (Java) !"# javac $%%&'&()*+,-)''.' clojure-x.y.z.jar

Slide 12

Slide 12 text

cloure-x.y.z.jar !"# javac $%%&'&()*+,-)''.' clojure-x.y.z.jar clojure.core ,%/*0-.(

Slide 13

Slide 13 text

Syntax (Clojure) new ClassName(arg0 , arg1 , ...); ClassName.staticMethod(arg0 , arg1 , ...); obj.method(arg0 , arg1 , ...); Syntax (Java interop) (ClassName. arg0 arg1 ...) (ClassName/staticMethod arg0 arg1 ...) (. method obj arg0 arg1 ...) Syntax (Clojure) (func arg0 arg1 … )

Slide 14

Slide 14 text

Type Inference Example (Java) ArrayList list = new ArrayList (); list.add (1); list.add (2); list.add (3);

Slide 15

Slide 15 text

Type Inference Example (Java) ArrayList list = new ArrayList (); list.add (1); list.add (2); list.add (3); Example (Clojure) (let [alist (ArrayList .)] (.add alist 1) (.add alist 2) (.add alist 3))

Slide 16

Slide 16 text

Macro Example (Plain Clojure) (let [alist (ArrayList .)] (.add alist 1) (.add alist 2) (.add alist 3))

Slide 17

Slide 17 text

Macro Example (Plain Clojure) (let [alist (ArrayList .)] (.add alist 1) (.add alist 2) (.add alist 3)) Example (Macro) (doto (ArrayList .) (.add 1) (.add 2) (.add 3))

Slide 18

Slide 18 text

clojure-x.y.z.jar !"# javac $%%&'&()*+,-)''.' clojure-x.y.z.jar clojure.core ,%/*0-.(

Slide 19

Slide 19 text

clojure.core Java (JVM, Bootstrap classes) • Primitives, classes, methods, objects clojure.core • Immutable/Persistent data structures • Collections • Lazy sequences • Stream (Java) • List (Lisp)

Slide 20

Slide 20 text

Built-in collection List • (1 2 3 4) Vector (Stack) • [1 2 3 4] Map • {:a 1, :b 2, :c 3} or {:a 1 :b 2 :c 3} Set • #{1 2 3}

Slide 21

Slide 21 text

Clojure Programming !"##$%&'"( )*+,-. /$01$(%$ !"##$%&'"( )*+,-. /$01$(%$

Slide 22

Slide 22 text

Clojure Programming !"##$%&'"( )*+,-. /$01$(%$ !"##$%&'"( )*+,-. /$01$(%$

Slide 23

Slide 23 text

Conj Conjoin col and x (conj col x) Example (Built-in collections) • (conj [1 2 3 4] 5) • [1 2 3 4 5] • (conj {:a 1 :b 2 :c 3} [:d 4]) • {:a 1 :b 2 :c 3 :d 4} • (conj #{1 2 3} 4) • #{1 2 3 4} • (conj clojure.lang.PersistentQueue/EMPTY 1)

Slide 24

Slide 24 text

Map-filter-reduce map (map inc [1 2]) ;=> (2 3) filter (filter #{1 2} [1 2 3 4]) ;=> (2) reduce (reduce + [1 2 3]) ;=> 6

Slide 25

Slide 25 text

Clojure Programming !"##$%&'"( )*+,-. /$01$(%$ !"##$%&'"( )*+,-. /$01$(%$

Slide 26

Slide 26 text

Checkpoint 1 • Java • clojure-x.y.z.jar • Syntax • Type inference • Macro • Immutable/Persistent data structures • Lazy sequences

Slide 27

Slide 27 text

The Good Parts (1/6) • Dynamic development • Functional programming • Lisp • Runtime polymorphism • Concurrent programming • Hosted on JVM

Slide 28

Slide 28 text

Dynamic Development • REPL • Dynamic compilation • Automatically compiled to JVM bytecode on the fly.

Slide 29

Slide 29 text

REPL $ java -cp clojure -1.6.0. jar clojure.main

Slide 30

Slide 30 text

The Good Parts (2/6) • Dynamic development • Functional programming • Lisp • Runtime polymorphism • Concurrent programming • Hosted on JVM

Slide 31

Slide 31 text

Functional Programming • First-class functions • fn • defn = def + fn • Immutable data structures • Extensible absraction • seq • Constant-space recursive looping • recur

Slide 32

Slide 32 text

The Good Parts (3/6) • Dynamic development • Functional programming • Lisp • Runtime polymorphism • Concurrent programming • Hosted on JVM

Slide 33

Slide 33 text

Lisp • Macro

Slide 34

Slide 34 text

Macro C, C++, Scala String or AST Clojure (Lisp) Sequence

Slide 35

Slide 35 text

The Good Parts (4/6) • Dynamic development • Functional programming • Lisp • Runtime polymorphism • Concurrent programming • Hosted on JVM

Slide 36

Slide 36 text

Runtime Polymorphism • Java Interface • Protocol • Type, Record, Protocol • Multimethod

Slide 37

Slide 37 text

Implementing Java Interface (reify java.util.Comparator (compare [this x y] (- y x)) (equals [this x] (= this x))

Slide 38

Slide 38 text

Multimethod (defmulti zoo (fn [x y] [(: diet x) (:diet y)])) (defmethod zoo [: carnivorous :herbivorous] [x y] :eat) (defmethod zoo [: herbivorous :carnivorous] [x y] : run-away) (defmethod zoo [: carnivorous :carnivorous] [x y] :fight) (defmethod zoo [: herbivorous :herbivorous] [x y] :mate)

Slide 39

Slide 39 text

The Good Parts (5/6) • Dynamic development • Functional programming • Lisp • Runtime polymorphism • Concurrent programming • Hosted on JVM

Slide 40

Slide 40 text

Concurrent programming • Multithreaded programming • Concurrent operations • JSR-166y (Fork/Join)

Slide 41

Slide 41 text

Multithreaded programming Functions Callable, Runnable public interface Callable { V call (); } Collections Thread-safe Immutability Immutable variables

Slide 42

Slide 42 text

Concurrent Operations • Atomic updates • atom • Asynchronous agents • agent

Slide 43

Slide 43 text

Atomic updates Example (atom) (def a (atom 0)) (swap! a inc) (compare-and-set! a 1 10) (reset! a 20) Java java.util.concurrent.atomic

Slide 44

Slide 44 text

Asynchronous agents Example (agent) (def a (agent 5000)) (send a inc) (senf-off a #( Thread/sleep %)) (await a b) Java • ExecutorService • Fixed-size thread pool (send) • Unbounded thread pool (send-off)

Slide 45

Slide 45 text

Reducers (Fork/Join) Plain Old Clojure (->> s (map ->heptagonal) (filter triangular ?) (reduce +)) Reducer Version (->> s (map ->heptagonal) (r/filter triangular ?) (r/fold +)) http://tnoda-clojure.tumblr.com/post/60197800824/reducers

Slide 46

Slide 46 text

The Good Parts (6/6) • Dynamic development • Functional programming • Lisp • Runtime polymorphism • Concurrent programming • Hosted on JVM

Slide 47

Slide 47 text

Hosted on JVM • Type system • GC • Threads • JVM bytecode • Basic classes • java.util.*, Regular expressions • java.lang.Math • java.io.*

Slide 48

Slide 48 text

Type System Combining static and dynamic type-checking Example (IFn) public interface IFn extends Callable , Runnable { public Object invoke (); public Object invoke(Object arg1); public Object invoke(Object arg1 , Object arg2); ; ... }

Slide 49

Slide 49 text

Checkpoint 2 Dynamic development • REPL • dynamic compilation Functional programming • First-class functions • Immutable data structures • Extensible absraction • Constant-space recursive looping Lisp • Macro

Slide 50

Slide 50 text

Checkpoint 2 Runtime polymorphism • Java Interface • Protocol • Multimethod Concurrent programming • Multithreaded programming • Concurrent operations • JSR-166y (Fork/Join) Hosted on JVM • Type system

Slide 51

Slide 51 text

The Bad Parts • Arrays

Slide 52

Slide 52 text

Type Hinting Example (Java) int x = a[i][j][k]; Example (Clojure) (aget a i j k) ; extremely slow (aget ^ints (aget ^objects (aget a i) j ) k)

Slide 53

Slide 53 text

Remedy http://tnoda-clojure.tumblr.com/post/28996774133/arrays-as-funcions Macro (definline a [i j] (let [larray (gensym "larray")] ‘(let [~ larray (aget ~( with-meta ‘-a {:tag ’objects }) ~i)] (aget ~( with-meta larray {:tag ’longs }) ~j)))) (definline a= [i j x] (let [larray (gensym "larray")] ‘(let [~ larray (aget ~( with-meta ‘-a {:tag ’objects }) ~i)] (aset ~( with-meta larray {:tag ’longs }) ~j (long ~x))))) a, a=

Slide 54

Slide 54 text

Checkpoint 3 Do not use mulitdimensional arrays in Clojure.

Slide 55

Slide 55 text

Applications Middleware • Storm • TrapperKeeper • Datomic Products • Puppet Server • Netflix’s internal services

Slide 56

Slide 56 text

Conclusion Clojure? Java The Good Parts Dynamic development, functional programming, lisp, runtime polymorphism concurrent programming, hosted on JVM The Bad Parts Multidimensinal arrays Applications Storm, Puppet Server