-
Notifications
You must be signed in to change notification settings - Fork 3
/
cljs-cheatsheet.tex
250 lines (203 loc) · 7.16 KB
/
cljs-cheatsheet.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
\documentclass[footexclude,twocolumn,DIV25,fontsize=10pt]{scrreprt}
% Author: Fogus (based on Steve Tayon's Clojure and David Liebke's Incanter cheat sheets)
% Comments, errors, suggestions: fogus -at- clojure -dot- com
% License
% Eclipse Public License v1.0
% http://opensource.org/licenses/eclipse-1.0.php
% Packages
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage[english]{babel}
\usepackage{tabularx}
\usepackage{verbatim}
\usepackage[table]{xcolor}
% Set column space
\setlength{\columnsep}{0.25em}
% Define colours
\definecolorset{hsb}{}{}{red,0,.6,0.95;orange,.1,.6,0.95;green,.25,.6,0.95;yellow,.15,.6,0.95;blue,.55,.6,0.95;purple,.7,.6,0.95;pink,.8,.6,0.95;blue2,.58,.6,0.95;magenta,.9,.6,0.95;green2,.29,.6,0.95}
\definecolor{Brown}{cmyk}{0,0.81,1,0.60}
\definecolor{OliveGreen}{cmyk}{0.64,0,0.95,0.40}
\definecolor{CadetBlue}{cmyk}{0.62,0.57,0.23,0}
\definecolor{lightlightgray}{gray}{0.9}
% Redefine sections
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}
{-1.7ex}{0.7ex}{\normalfont\large\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}
{-1.7ex}{0.5ex}{\normalfont\normalsize\bfseries}}
\makeatother
% No section numbers
\setcounter{secnumdepth}{0}
% No indentation
\setlength{\parindent}{0em}
% No header and footer
\pagestyle{empty}
% A few shortcuts
\newcommand{\cmd}[1] {\texttt{\textbf{{#1}}}}
\newcommand{\cmdline}[1] {
\begin{tabularx}{\hsize}{X}
\texttt{\textbf{{#1}}}
\end{tabularx}
}
\newcommand{\colouredbox}[2] {
\colorbox{#1!40}{
\begin{minipage}{0.95\linewidth}
{
\rowcolors[]{1}{#1!20}{#1!10}
#2
}
\end{minipage}
}
}
\usepackage{color}
\usepackage{xcolor}
\usepackage{listings}
\usepackage[small,bf]{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{yellow}{\parbox{0.95\linewidth}{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing}
\begin{document}
\centerline{\Large{\textbf{ClojureScript Cheat Sheet}}}
\centerline{{\large{\textbf{\textit{http://github.com/clojure/clojurescript}}}}}
\colouredbox{purple}{
\section{Documentation}
\begin{tabularx}{\hsize}{lX}
\textmd{\textrm{http://github.com/clojure/clojurescript/wiki}} & \\
\textmd{\textrm{http://www.clojurescript.net}} & \\
\textmd{\textrm{http://www.clojuredocs.org}} & \\
\end{tabularx}
}
\lstset{
language=Lisp, % Code langugage
basicstyle=\ttfamily,
keywordstyle=\color{OliveGreen},
morekeywords={require, use, ns, require-macros, use-macros, as, only},
frame=none,
}
\colouredbox{yellow}{
\section{Namespace Declaration}
\begin{tabularx}{\hsize}{lX}
\cmd{(ns my-cool-lib } &\\
\cmd{ (:require [some-lib :as lib]) } &\\
\cmd{ (:use [another-lib :only (a-func)]) } &\\
\cmd{ (:require-macros [my.macros :as macs]) } &\\
\cmd{ (:use-macros [mo.macs :only (my-mac)])) } &\\
\end{tabularx}
}
\colouredbox{red}{
\section{Rich Data Literals}
\begin{tabularx}{\hsize}{lX}
Maps: & \cmd{\{:key1 :val1, :key2 :val2\}} \\
Vectors: & \cmd{[1 2 3 4 :a :b :c 1 2]} \\
Sets: & \cmd{\#\{:a :b :c 1 2 3\}} \\
Truth/nullity: & \cmd{true, false, nil} \\
Keywords: & \cmd{:kw, :a-2, :prefix/kw, ::pi} \\
Symbols: & \cmd{sym, sym-2, prefix/sym} \\
Characters: & \cmd{\textbackslash a, \textbackslash u1123, \textbackslash space, \textbackslash newline} \\
Numbers/Strings: & same as in JavaScript \\
RegEx: & \cmd{\#"[Cc]lojure[Ss]cript"} \\
\end{tabularx}
}
\colouredbox{blue}{
\section{Frequently Used Functions \& Macros}
\subsection{Functions}
\begin{tabularx}{\hsize}{lX}
Math:& \cmd{+ - * / quot rem mod inc dec max min}\\
Comparison:& \cmd{= == not= < > <= >=}\\
Tests:& \cmd{nil? identical? zero? pos? neg? even? odd? true? false? nil?} \\
Keywords:& \cmd{keyword keyword?} \\
Symbols:& \cmd{symbol symbol? gensym} \\
Data Processing:& \cmd{map reduce filter partition split-at split-with} \\
Data Create:& \cmd{vector vec hash-map set list list* for} \\
Data Examination:& \cmd{first rest count get nth get get-in contains? find keys vals} \\
Data Manipulation:& \cmd{seq into conj cons assoc assoc-in dissoc zipmap merge merge-with select-keys update-in}\\
Arrays:& \cmd{into-array to-array aget aset amap areduce alength}\\
\end{tabularx}
\subsection{Macros}
\begin{tabularx}{\hsize}{lX}
Defining:& \cmd{defmacro}\\
Implementation:& Must be written in Clojure \\
Emission:& Must emit ClojureScript \\
Macros:& \cmd{if if-let cond and or -> ->> doto when when-let ..}\\
\end{tabularx}
\subsection{Extra ClojureScript Libraries}
\begin{tabularx}{\hsize}{lX}
\cmd{clojure.\{string set zipper\}} & \\
\cmd{clojure.browser.\{dom event net repl\}} & \\
\end{tabularx}
}
\colouredbox{magenta}{
\section{Abstraction (http://clojure.org/protocols)}
\subsection{Protocols}
\begin{tabularx}{\hsize}{lX}
Definition:& \cmd{(defprotocol Slicey} \\
& \cmd{ (slice [at]))} \\
Extend:& \cmd{(extend-type js/String}\\
& \cmd{ Slicey (slice [at] ...))} \\
Extend null:& \cmd{(extend-type nil}\\
& \cmd{ Slicey (slice [\_] nil))} \\
Reify:& \cmd{(reify Slicey (slice [at] ...))}\\
\end{tabularx}
\subsection{Records}
\begin{tabularx}{\hsize}{lX}
Definition:& \cmd{(defrecord Pair [h t])} \\
Access:& \cmd{(:h (Pair. 1 2)) ;=> 1} \\
Constructing:& \cmd{Pair. ->Pair map->Pair} \\
\end{tabularx}
\subsection{Types}
\begin{tabularx}{\hsize}{lX}
Definition:& \cmd{(deftype Pair [h t])} \\
Access:& \cmd{(.-h (Pair. 1 2)) ;=> 1} \\
Constructing:& \cmd{Pair. ->Pair} \\
With Method(s):& \cmd{(deftype Pair [h t] Object (toString [] ...))} \\
\end{tabularx}
\subsection{Multimethods}
\begin{tabularx}{\hsize}{lX}
Definition:& \cmd{(defmulti my-mm dispatch-function)}\\
Method Define:& \cmd{(defmethod my-mm :dispatch-value [args] ...)}\\
\end{tabularx}
}
\colouredbox{green2}{
\section{JS Interop (http://fogus.me/cljs-js)}
\begin{tabularx}{\hsize}{lX}
Method Call: & \cmd{(.meth obj args)} \\
& \cmd{(. obj (meth args))} \\
Property Access: & \cmd{(.-prop obj)} \\
& \cmd{(. obj -prop)} \\
& \cmd{(aget obj prop-str)} \\
Set Property: & \cmd{(set! (.-prop obj) val)} \\
& \cmd{(aset obj prop-str val)} \\
Set Array element: & \cmd{(aset arr idx val)} \\
JS Global Access: & \cmd{js/window} \\
JS \cmd{this}: & \cmd{(this-as me (.method me))} \\
Create JS Object: & \cmd{(js-obj)} \\
Create JS Array: & \cmd{(array var-args) (make-array size)} \\
Transf. JS value: & \cmd{(js->clj js-val)} \\
Transf. CLJ value: & \cmd{(clj->js clj-val)} \\
\end{tabularx}
}
\colouredbox{red}{
\section{Compilation (http://fogus.me/cljsc)}
\begin{tabularx}{\hsize}{lX}
Compile: & \cmdline{cljsc src-home '\{:optimizations :simple :pretty-print true\}'} \\
Adv. Compile: & \cmdline{cljsc src-home '\{:optimizations :advanced\}'} \\
\end{tabularx}
}
\colouredbox{green}{
\section{Other Useful Libraries}
\begin{tabularx}{\hsize}{lX}
Lein build: & https://github.com/emezeske/lein-cljsbuild \\
Client/Server: & http://github.com/cemerick/shoreleave-remote-ring \\
DOM: & http://github.com/levand/domina \\
jQuery: & http://github.com/ibdknox/jayq \\
Templating: & https://github.com/Prismatic/dommy \\
\end{tabularx}
}
\begin{flushright}
\footnotesize
\rule{0.7\linewidth}{0.25pt}
\verb!$Revision: 1.0, $Date: Feb 08, 2012!\\
\verb!Fogus (fogus -at- clojure -dot- com)!
\end{flushright}
\end{document}