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