Clojure for Non-Clojurians

Clojure for Non-Clojurians

A presentation given at the #kansaifp meetup in Kyoto on Dec 6, 2014

http://kansaifp.doorkeeper.jp/events/17206

Takahiro Noda

December 06, 2014

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

56 slides
  1. Outline Clojure? . . . The Good Parts . .

    . The Bad Parts . . . Applications . . .
  2. 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!
  3. Nyampass Co., Ltd. Figure : http://nyampass.com/ They have been using

    Clojure to develop their mobile BaaS services.
  4. Hello World! (recap) Example (Java) $ java -cp clojure -1.6.0.

    jar clojure.main \ -e ’(println "Hello␣World!")’
  5. 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 … )
  6. Type Inference Example (Java) ArrayList list = new ArrayList ();

    list.add (1); list.add (2); list.add (3);
  7. 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))
  8. 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))
  9. clojure.core Java (JVM, Bootstrap classes) • Primitives, classes, methods, objects

    clojure.core • Immutable/Persistent data structures • Collections • Lazy sequences • Stream (Java) • List (Lisp)
  10. 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}
  11. 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)
  12. 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
  13. Checkpoint 1 • Java • clojure-x.y.z.jar • Syntax • Type

    inference • Macro • Immutable/Persistent data structures • Lazy sequences
  14. The Good Parts (1/6) • Dynamic development • Functional programming

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

    • Lisp • Runtime polymorphism • Concurrent programming • Hosted on JVM
  16. Functional Programming • First-class functions • fn • defn =

    def + fn • Immutable data structures • Extensible absraction • seq • Constant-space recursive looping • recur
  17. The Good Parts (3/6) • Dynamic development • Functional programming

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

    • Lisp • Runtime polymorphism • Concurrent programming • Hosted on JVM
  19. 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)
  20. The Good Parts (5/6) • Dynamic development • Functional programming

    • Lisp • Runtime polymorphism • Concurrent programming • Hosted on JVM
  21. Multithreaded programming Functions Callable, Runnable public interface Callable <V> {

    V call (); } Collections Thread-safe Immutability Immutable variables
  22. 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
  23. 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)
  24. 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
  25. The Good Parts (6/6) • Dynamic development • Functional programming

    • Lisp • Runtime polymorphism • Concurrent programming • Hosted on JVM
  26. Hosted on JVM • Type system • GC • Threads

    • JVM bytecode • Basic classes • java.util.*, Regular expressions • java.lang.Math • java.io.*
  27. 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); ; ... }
  28. Checkpoint 2 Dynamic development • REPL • dynamic compilation Functional

    programming • First-class functions • Immutable data structures • Extensible absraction • Constant-space recursive looping Lisp • Macro
  29. Checkpoint 2 Runtime polymorphism • Java Interface • Protocol •

    Multimethod Concurrent programming • Multithreaded programming • Concurrent operations • JSR-166y (Fork/Join) Hosted on JVM • Type system
  30. 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)
  31. 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=
  32. Applications Middleware • Storm • TrapperKeeper • Datomic Products •

    Puppet Server • Netflix’s internal services
  33. 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

More Decks by Takahiro Noda

Other Decks in Programming