Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clojure & ClojureScript

Clojure & ClojureScript

A presentation at I.T.A.K.E. 2015

Stefan Kanev

May 28, 2015
Tweet

More Decks by Stefan Kanev

Other Decks in Programming

Transcript

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

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

    May 2015 Sofia
  3. None
  4. twitter: @skanev github: skanev blog: http://skanev.com/

  5. Clojure from 10,000 feet

  6. A modern LISP, hosted in the JVM, with a focus

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

    on concurrency
  8. LISP: “that language with the parentheses”
 also: a subculture

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

  10. LISP

  11. None
  12. a * b + c * d (+ (* a

    b) (* c d))
  13. homoiconicity data is code, code is data

  14. None
  15. A modern LISP, hosted in the JVM, with a focus

    on concurrency
  16. Sans the antiquities: car cdr lambda

  17. Way better design

  18. Less parentheses

  19. A modern LISP, hosted in the JVM, with a focus

    on concurrency
  20. Stable platform

  21. Access to full Java ecosystem

  22. Occasionally a huge P.I.T.A.

  23. A modern LISP, hosted in the JVM, with a focus

    on concurrency
  24. parallelism vs. concurrency

  25. parallelism Breaking a problem down to smaller parts that can

    be computed at the same time
  26. concurrency Synchronising a number of independent processes that are fighting

    for the same resources
  27. Syntax

  28. (func arg-1 arg-2 …)

  29. (println "Hello world")

  30. (+ 1 2) (< x y)

  31. (+ 1 2 3 4 5 6) (< u v

    w x y z)
  32. (+ (* a b) (* c d)) (+ (* a

    b) (* c d))
  33. (defn say-hello [who] (println "Hello" who "!")) (say-hello "Doctor")

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

    (( (( (( )) )) )) ))
  35. Parentheses in LISP are not unlike metric time

  36. (defn classify [age] (if (<= 13 age 19) "Teenager" "A

    normal person")) (classify 18) ; "Teenager"
  37. (defn factorial [n] (if (= n 1) 1 (* n

    (factorial (- n 1)))))
  38. (defn fib [n] (cond (= n 0) 1 (= n

    1) 1 :else (+ (fib (- n 1)) (fib (- n 2)))))
  39. (fn [x] (* x 2))

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

    ; ("Mr. Anderson” ; "Mr. Bond" ; "Mr. Bean")
  41. (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)
  42. (defn prime? [n] (not-any? (fn [x] (zero? (rem n x)))

    (range 2 (inc (Math/sqrt n)))))
  43. Data Structures

  44. maps (hashes) vectors (arrays) sets

  45. immutable

  46. “Modifying” a structure creates a copy containing the change. The

    original remains unmodified.
  47. Y

  48. simplicity

  49. multicore ❤ immutable

  50. persistent

  51. The “originals” are maximally reused

  52. a ! (3 2 1)

  53. 1 2 3 a a ! (3 2 1)

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

    (4 3 2 1)
  55. 1 2 3 a a ! (3 2 1) b

    ! (4 3 2 1) 4 b 5 c c ! (conj a 5)
  56. Hash Table 1020394597 1020205863 {:foo first, :bar second} first second

  57. Hash Table 1020394597 1020205863 Hash Table 1020394597 1020205863 1021027443 (conj

    table :qux 1024)
  58. not that simple

  59. Vectors (“arrays” in other langs)

  60. Vectors are represented by trees Each node has 32 children

  61. log32 … …

  62. … … ⨯

  63. O(?)

  64. log32 n

  65. 325 = 33 554 432 326 = 1 073 741

    824
  66. “essentially constant time”

  67. None
  68. Software Transactional Memory

  69. concurrency 101

  70. 100 € +50 € ⨯2 How much money will the

    account have?
  71. 300 € 250 €

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

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

  74. 100 € 200 € 150 € x2 +50 100 €

    100 €
  75. 100 € 150 € 200 € x2 +50 100 €

    100 €
  76. ref

  77. state mutation is modelled as a transaction

  78. if two transactions “get in each others’ way”, one of

    them will restart
  79. (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))
  80. 100 € 300 € 150 € x2 +50 100 €

    100 € X x2 150 €
  81. Y

  82. familiar

  83. safe, easy, no deadlocks

  84. Macros

  85. Powerful instrument allowing expressive code

  86. Also known as: the mother of all metaprogramming

  87. Macro Code Some other code

  88. None
  89. doStuff();

  90. Outputs on STDOUT. We want to have the result in

    a string instead.
  91. 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();
  92. (do-stuff)

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

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

  95. String output = withOutStr { doStuff(); }

  96. unless

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

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

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

  100. (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)))))
  101. DSL Domain Specific Languages

  102. Transparency

  103. (source if-let) (source await)

  104. ClojureScript

  105. om

  106. Very dynamic

  107. DEMO

  108. REPL Oriented Programming

  109. None