Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
http://xkcd.com/{224,297}
Clojure & ClojureScript
Stefan Kanev
http://skanev.com/
@skanev
I.T.A.K.E. Unconf
26 May 2015
Sofia
twitter: @skanev
github: skanev
blog: http://skanev.com/
Clojure from 10,000 feet
A modern LISP, hosted
in the JVM, with a
focus on concurrency
A modern LISP, hosted
in the JVM, with a
focus on concurrency
LISP:“that language with
the parentheses”

also: a subculture
“LISP is too
hip, even for
me”
– a hipster
LISP
a * b + c * d
(+ (* a b) (* c d))
homoiconicity
data is code, code is
data
A modern LISP, hosted
in the JVM, with a
focus on concurrency
Sans the antiquities:
car cdr lambda
Way better design
Less parentheses
A modern LISP, hosted
in the JVM, with a
focus on concurrency
Stable platform
Access to full
Java ecosystem
Occasionally a huge P.I.T.A.
A modern LISP, hosted
in the JVM, with a
focus on concurrency
parallelism vs.
concurrency
parallelism
Breaking a problem down to
smaller parts that can be
computed at the same time
concurrency
Synchronising a number of
independent processes that are
fighting for the same resources
Syntax
(func arg-1 arg-2 …)
(println "Hello world")
(+ 1 2)
(< x y)
(+ 1 2 3 4 5 6)
(< u v w x y z)
(+ (* a b) (* c d))
(+ (* a b)
(* c d))
(defn say-hello [who]
(println "Hello" who "!"))
(say-hello "Doctor")
OMG Parentheses!
Or should I say:
((o) ((m)) (g) ( (( (( (( )) )) )) ))
Parentheses in LISP are
not unlike metric time
(defn classify [age]
(if (<= 13 age 19)
"Teenager"
"A normal person"))
(classify 18) ; "Teenager"
(defn factorial [n]
(if (= n 1)
1
(* n (factorial (- n 1)))))
(defn fib [n]
(cond (= n 0) 1
(= n 1) 1
:else (+ (fib (- n 1))
(fib (- n 2)))))
(fn [x] (* x 2))
(map (fn [n] (str "Mr. " n))
["Anderson"
"Bond"
"Bean"])
; ("Mr. Anderson”
; "Mr. Bond"
; "Mr. Bean")
(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)
(defn prime? [n]
(not-any? (fn [x] (zero? (rem n x)))
(range 2 (inc (Math/sqrt n)))))
Data Structures
maps (hashes)
vectors (arrays)
sets
immutable
“Modifying” a structure creates
a copy containing the change.
The original remains unmodified.
Y
simplicity
multicore ❤
immutable
persistent
The “originals” are
maximally reused
a ! (3 2 1)
123
a
a ! (3 2 1)
a ! (3 2 1)
b ! (conj a 4)
(4 3 2 1)
123
a
a ! (3 2 1)
b ! (4 3 2 1)
4b
5c
c ! (conj a 5)
Hash Table
1020394597
1020205863
{:foo first, :bar second}
first
second
Hash Table
1020394597
1020205863
Hash Table
1020394597
1020205863
1021027443
(conj table :qux 1024)
not that simple
Vectors
(“arrays” in other langs)
Vectors are represented by trees
Each node has 32 children
log32
…
…
…
… ⨯
O(?)
log32n
325 = 33 554 432
326 = 1 073 741 824
“essentially constant
time”
Software Transactional Memory
concurrency 101
100 € +50 € ⨯2
How much money will the account have?
300 €250 €
100 €
+50 € = 150 €
x2 = 300 €
100 €
x2 = 200 €
+50 € = 250 €
100 €
200 €
150 €
x2
+50
100 €
100 €
100 €
150 €
200 €
x2
+50
100 €
100 €
ref
state mutation is
modelled as a
transaction
if two transactions
“get in each others’
way”, one of them will
restart
(def account (ref 100))
; Thread 1 - Uncle Scrooge
(dosync
(alter account (fn [n] (* n 2)))
(println "Scrooge: set to " @a...
100 €
300 €
150 €
x2
+50
100 €
100 €
X
x2
150 €
Y
familiar
safe, easy, no
deadlocks
Macros
Powerful instrument
allowing expressive code
Also known as:
the mother of all
metaprogramming
MacroCode
Some other
code
doStuff();
Outputs on STDOUT.
We want to have the
result in a string
instead.
PrintStream original = System.out;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
PrintStream fileStrea...
(do-stuff)
(with-out-str
(do-stuff))
(with-out-str (do-stuff))
String output = withOutStr {
doStuff();
}
unless
(unless (hungry?)
(sleep)
(eat))
(if (not (hungry?))
(sleep)
(eat))
(defmacro unless [condition consequent alternative]
`(if (not ~condition)
~consequent
~alternative))
unless (isHungry()) {
sleep();
} else {
eat();
}
(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)...
DSLDomain Specific Languages
Transparency
(source if-let)
(source await)
ClojureScript
om
Very dynamic
DEMO
REPL Oriented Programming
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Unconference 2015
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Unconference 2015
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Unconference 2015
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Unconference 2015
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Unconference 2015
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Unconference 2015
Upcoming SlideShare
Loading in …5
×

Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Unconference 2015

468 views

Published on

Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Unconference 2015

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Unconference 2015

  1. 1. http://xkcd.com/{224,297}
  2. 2. Clojure & ClojureScript Stefan Kanev http://skanev.com/ @skanev I.T.A.K.E. Unconf 26 May 2015 Sofia
  3. 3. twitter: @skanev github: skanev blog: http://skanev.com/
  4. 4. Clojure from 10,000 feet
  5. 5. A modern LISP, hosted in the JVM, with a focus on concurrency
  6. 6. A modern LISP, hosted in the JVM, with a focus on concurrency
  7. 7. LISP:“that language with the parentheses”
 also: a subculture
  8. 8. “LISP is too hip, even for me” – a hipster
  9. 9. LISP
  10. 10. a * b + c * d (+ (* a b) (* c d))
  11. 11. homoiconicity data is code, code is data
  12. 12. A modern LISP, hosted in the JVM, with a focus on concurrency
  13. 13. Sans the antiquities: car cdr lambda
  14. 14. Way better design
  15. 15. Less parentheses
  16. 16. A modern LISP, hosted in the JVM, with a focus on concurrency
  17. 17. Stable platform
  18. 18. Access to full Java ecosystem
  19. 19. Occasionally a huge P.I.T.A.
  20. 20. A modern LISP, hosted in the JVM, with a focus on concurrency
  21. 21. parallelism vs. concurrency
  22. 22. parallelism Breaking a problem down to smaller parts that can be computed at the same time
  23. 23. concurrency Synchronising a number of independent processes that are fighting for the same resources
  24. 24. Syntax
  25. 25. (func arg-1 arg-2 …)
  26. 26. (println "Hello world")
  27. 27. (+ 1 2) (< x y)
  28. 28. (+ 1 2 3 4 5 6) (< u v w x y z)
  29. 29. (+ (* a b) (* c d)) (+ (* a b) (* c d))
  30. 30. (defn say-hello [who] (println "Hello" who "!")) (say-hello "Doctor")
  31. 31. OMG Parentheses! Or should I say: ((o) ((m)) (g) ( (( (( (( )) )) )) ))
  32. 32. Parentheses in LISP are not unlike metric time
  33. 33. (defn classify [age] (if (<= 13 age 19) "Teenager" "A normal person")) (classify 18) ; "Teenager"
  34. 34. (defn factorial [n] (if (= n 1) 1 (* n (factorial (- n 1)))))
  35. 35. (defn fib [n] (cond (= n 0) 1 (= n 1) 1 :else (+ (fib (- n 1)) (fib (- n 2)))))
  36. 36. (fn [x] (* x 2))
  37. 37. (map (fn [n] (str "Mr. " n)) ["Anderson" "Bond" "Bean"]) ; ("Mr. Anderson” ; "Mr. Bond" ; "Mr. Bean")
  38. 38. (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)
  39. 39. (defn prime? [n] (not-any? (fn [x] (zero? (rem n x))) (range 2 (inc (Math/sqrt n)))))
  40. 40. Data Structures
  41. 41. maps (hashes) vectors (arrays) sets
  42. 42. immutable
  43. 43. “Modifying” a structure creates a copy containing the change. The original remains unmodified.
  44. 44. Y
  45. 45. simplicity
  46. 46. multicore ❤ immutable
  47. 47. persistent
  48. 48. The “originals” are maximally reused
  49. 49. a ! (3 2 1)
  50. 50. 123 a a ! (3 2 1)
  51. 51. a ! (3 2 1) b ! (conj a 4) (4 3 2 1)
  52. 52. 123 a a ! (3 2 1) b ! (4 3 2 1) 4b 5c c ! (conj a 5)
  53. 53. Hash Table 1020394597 1020205863 {:foo first, :bar second} first second
  54. 54. Hash Table 1020394597 1020205863 Hash Table 1020394597 1020205863 1021027443 (conj table :qux 1024)
  55. 55. not that simple
  56. 56. Vectors (“arrays” in other langs)
  57. 57. Vectors are represented by trees Each node has 32 children
  58. 58. log32 … …
  59. 59. … … ⨯
  60. 60. O(?)
  61. 61. log32n
  62. 62. 325 = 33 554 432 326 = 1 073 741 824
  63. 63. “essentially constant time”
  64. 64. Software Transactional Memory
  65. 65. concurrency 101
  66. 66. 100 € +50 € ⨯2 How much money will the account have?
  67. 67. 300 €250 €
  68. 68. 100 € +50 € = 150 € x2 = 300 €
  69. 69. 100 € x2 = 200 € +50 € = 250 €
  70. 70. 100 € 200 € 150 € x2 +50 100 € 100 €
  71. 71. 100 € 150 € 200 € x2 +50 100 € 100 €
  72. 72. ref
  73. 73. state mutation is modelled as a transaction
  74. 74. if two transactions “get in each others’ way”, one of them will restart
  75. 75. (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))
  76. 76. 100 € 300 € 150 € x2 +50 100 € 100 € X x2 150 €
  77. 77. Y
  78. 78. familiar
  79. 79. safe, easy, no deadlocks
  80. 80. Macros
  81. 81. Powerful instrument allowing expressive code
  82. 82. Also known as: the mother of all metaprogramming
  83. 83. MacroCode Some other code
  84. 84. doStuff();
  85. 85. Outputs on STDOUT. We want to have the result in a string instead.
  86. 86. 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();
  87. 87. (do-stuff)
  88. 88. (with-out-str (do-stuff))
  89. 89. (with-out-str (do-stuff))
  90. 90. String output = withOutStr { doStuff(); }
  91. 91. unless
  92. 92. (unless (hungry?) (sleep) (eat)) (if (not (hungry?)) (sleep) (eat))
  93. 93. (defmacro unless [condition consequent alternative] `(if (not ~condition) ~consequent ~alternative))
  94. 94. unless (isHungry()) { sleep(); } else { eat(); }
  95. 95. (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)))))
  96. 96. DSLDomain Specific Languages
  97. 97. Transparency
  98. 98. (source if-let) (source await)
  99. 99. ClojureScript
  100. 100. om
  101. 101. Very dynamic
  102. 102. DEMO
  103. 103. REPL Oriented Programming

×