ClojureScript Cheat Sheet Abstraction ([Link]
org/protocols)
[Link] Protocols
Definition: (defprotocol Slicey (slice
Documentation [at]))
[Link] Extend: (extend-type js/String Slicey
(slice [at] ...))
Extend null: (extend-type nil Slicey (slice
[_] nil))
Listing 1: Example Namespace Declaration Reify: (reify Slicey (slice [at] ...))
( ns my-cool-lib Records
(: require [ some-lib : as lib ]) Definition: (defrecord Pair [h t])
(: use [ another-lib : only ( a-func )]) Access: (:h (Pair. 1 2)) ;=> 1
(: require-macros [ my . macros : as macs ]) Constructing: Pair. ->Pair map->Pair
(: use-macros [ mo . macs : only ( my-mac )])) Types
Definition: (deftype Pair [h t])
Access: (.-h (Pair. 1 2)) ;=> 1
Constructing: Pair. ->Pair
Rich Data Literals With Method(s): (deftype Pair [h t] Object
Maps: {:key1 :val1, :key2 :val2} (toString [] ...))
Vectors: [1 2 3 4 :a :b :c 1 2]
Sets: #{:a :b :c 1 2 3} Multimethods
Truth and nullity: true, false, nil Definition: (defmulti my-mm
Keywords: :kw, :a-2, :prefix/kw, ::pi dispatch-function)
Symbols: sym, sym-2, prefix/sym Method Define: (defmethod my-mm
Characters: \a, \u1123, \space :dispatch-value [args] ...)
Numbers/Strings: same as in JavaScript
JS Interop ([Link]
Method Call: (.meth obj args)
Frequently Used Functions Method Call: (. obj (meth args))
Math: + - * / quot rem mod inc Property Access: (. obj -prop)
dec max min Property Access: (.-prop obj)
Comparison: = == not= < > <= >= Set Property: (set! (.-prop obj) val)
Tests: nil? identical? zero? JS Global Access: js/window
pos? neg? even? odd? JS this: (this-as me (.method me))
true? false? nil? Create JS Object: (js-obj)
Keywords: keyword keyword?
Symbols: symbol symbol? gensym Compilation ([Link]
Data Processing: map reduce filter cljsc src-home
partition split-at Simple Compile: ’{:optimizations :simple
split-with :pretty-print true}’
Data Create: vector vec hash-map set cljsc src-home
list list* for Advanced Compile: ’{:optimizations
Data Examination: first rest count get nth :advanced}’
get get-in contains? find
keys vals Extra ClojureScript Libraries
Data Manipulation: seq into conj cons clojure.{string set zipper}
assoc assoc-in dissoc [Link].{dom event net repl}
zipmap merge merge-with
select-keys update-in Other Useful Libraries
Arrays: into-array to-array aget
App Sample: [Link]
aset amap areduce alength
Client/Server: [Link]
More information Data Viz: [Link]
[Link] DOM: [Link]
jQuery: [Link]
Templating: [Link]
Frequently Used Macros
Defining: defmacro
Macros: if if-let cond and or -> ->> $Revision: 1.0, $Date: Feb 08, 2012
doto when when-let .. Fogus (fogus -at- clojure -dot- com)
Implementation: Must be written in Clojure
Emission: Must emit ClojureScript