13. e.g. for macro (sequence comprehension)
;; ClojureScript
cljs.user=> (for [x (range 10)
#_=> :when (odd? x)]
#_=> (* x x))
(1 9 25 49 81)
cljs.user=> (macroexpand-1
#_=> '(for [x (range 10)
#_=> :when (odd? x)]
#_=> (* x x)))
(cljs.core$macros/let
[iter__9116__auto__
(cljs.core$macros/fn
,,,
# Python
>>> [x ** 2 for x in range(10) if x % 2 != 0]
[1, 9, 25, 49, 81]
-- Haskell
> [x ^ 2 | x <- [0..9], odd x]
[1,9,25,49,81]
14. REPL-driven DevelopmentREPL-driven Development
Create a ClojureScript project with Leiningen
$ brew install leiningen
$ lein new figwheel cljs-demo # e.g. "figwheel" template
$ cd cljs-demo
$ lein figwheel # or `cider-jack-in-clojurescript` on Emacs
20. Immutable PersistentImmutable Persistent
CollectionsCollections
no mutations, no side e ects
high performance
ljs.user=> (conj [1 2] 3) ; add an element to vector
[1 2 3]
cljs.user=> (conj '(1 2) 3) ; add an element to list
(3 1 2)
cljs.user=> (conj #{1 2} 3) ; add an element to set
#{1 2 3}
cljs.user=> (assoc {:a 1 :b 2} :c 3) ; add an entry to map
{:a 1, :b 2, :c 3}
21. Map and SequenceMap and Sequence
as Core Abstractionsas Core Abstractions
maps: get, assoc
sequences: first, rest, cons
lazy sequences
rare to de ne something like classes or algebraic
data types
few data abstractions and many functions
25. speci cation system
similar to Racket's
cf. gradual typing
e.g. (Typed Clojure)
clojure.specclojure.spec
contract system
core.typed
26. Common mistakes with maps ...
;; typo in key name
geometry.sphere=> (surface-area #::{:radias 2})
0
;; incorrect value type
geometry.sphere=> (volume #::{:radius "2"})
33.510321638291124