\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{melbatoast} \pdfinfo{ /Title (basic-clojure.pdf) /Creator (Cheatography) /Author (melbatoast) /Subject (Basic Clojure Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{456ECC} \definecolor{LightBackground}{HTML}{F3F5FB} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Basic Clojure Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{melbatoast} via \textcolor{DarkBackground}{\uline{cheatography.com/59474/cs/15589/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}melbatoast \\ \uline{cheatography.com/melbatoast} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 25th April, 2018.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{General Info}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{;Everything is a function}} \newline that means everything is enclosed in brackets \newline \newline {\bf{;multiple pieces of data often need to be passed in a vector}} \newline (get-in myFunc {[}:ok :go{]}) \newline \newline {\bf{;commas are considered whitespace in clojure}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Primitives}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{Integer}} \newline • corresponds to long int \newline \newline {\bf{Float}} \newline • corresponds to double \newline \newline {\bf{String}} \newline • Always enclosed by double quotes \newline • "My big dog" \newline \newline {\bf{Boolean}} \newline • true or false \newline \newline {\bf{nil}} \newline • is used as the NULL value} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Binding}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{; general form}} \newline ; similar to type variable\_name = value \newline (def name value) \newline \newline {\bf{; or int x = 4}} \newline (def x 4)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{IF (Control Flow)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{; general syntax}} \newline (if boolean-form \newline then-form \newline optional-else-form) \newline \newline {\bf{; an example (if else)}} \newline (if (\textgreater{} 5 0) \newline "positive" \newline "negative") \newline {\bf{; -\textgreater{} "positive"}} \newline \newline {\bf{; optional else}} \newline (if (\textgreater{} 5 0) \newline "positive") \newline {\bf{; -\textgreater{} "positive"}} \newline \newline (if (\textless{} 5 0) \newline "negative") \newline {\bf{; -\textgreater{} nil}} \newline \newline {\bf{; Maximum of two terms per if. The following is an invalid form}} \newline (if (\textgreater{} 5 0) \newline "positive" ; then 1 \newline "good" ; then 2 \newline "negative") ; else} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Arity (or Number of Arguments)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{; basic arity overloading}} \newline (defn func\_name \newline ({[}arg1{]} ; one parm version \newline (expression)) \newline \newline ({[}arg1 arg2{]} ; two parm version \newline (expression))) \newline \newline {\bf{; using other arities}} \newline ; similar to constructors in java \newline ( defn func\_name \newline ({[}arg1{]} \newline (expression)) \newline ({[}arg1 arg2{]} \newline ( func\_name arg1 ) ; call the with one arg \newline (expression) ))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Lists (Composite Data Strucutres)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{;basic strucutre is '(elem1 elem2 elem3)}} \newline (1 2 3) ; =\textgreater{} error: 1 is not a function \newline '(1 2 3) ; =\textgreater{} valid list \newline (+ 2 2) ; =\textgreater{} function call \newline '(+ 2 2) ; =\textgreater{} valid list \newline \newline {\bf{;very similar to vectors, but have to use nth instead of get}} \newline (nth '(1 2 3) 1) {\bf{; =\textgreater{} 2}} \newline (nth '(1 2 3) 10) {\bf{; error}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Basic Maths \& Logic}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{; simple Math}} \newline (+ 1 1) \newline (- 2 1) \newline (* 1 2) \newline (/ 2 1) \newline \newline {\bf{; Equality testing}} \newline (= 1 1) ; =\textgreater{} true \newline (= 2 1) ; =\textgreater{} false \newline \newline {\bf{; Negation}} \newline (not true) ; =\textgreater{} false} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Function Syntax}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{;best way to bind functions}} \newline (defn hello {[}{]} "hello world") \newline \newline {\bf{;anonymous function}} \newline (fn {[}parm list{]} body) \newline \newline {\bf{;example}} \newline (fn {[}{]} "hello world") \newline \newline {\bf{;without the defn keyword have to do this}} \newline (def hello (fn {[}{]} "hello world"))} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{DO (Control Flow)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{;used for executing more than one line}} \newline ;will return the last value of the line \newline (if (\textgreater{} 5 0) \newline (do \newline (println "looks good") \newline "positive") \newline (do \newline (println "looks bad") \newline "negative")) \newline {\bf{;-\textgreater{} "positive"}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{When (Control Flow)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{;when is a combination of IF and DO}} \newline (when (\textgreater{} 5 0) \newline (println "looks good") \newline "positive") \newline {\bf{;-\textgreater{} "positive"}} \newline \newline ;always returns nil on false} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Immutability and Adding}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{;Clojure data structures are immutable. \newline ;That means once you assign data to a variable, you can't change it \newline (def foo1 '(1 2 3)) \newline (conj foo1 0) {\bf{; =\textgreater{} (0 1 2 3)}} \newline foo1 {\bf{; =\textgreater{} (1 2 3)}}, no change \newline \newline (def foo2 (conj foo1 0)) \newline foo1 {\bf{; =\textgreater{} (1 2 3)}} \newline foo2 {\bf{; =\textgreater{} (0 1 2 3)}} \newline \newline (def foo {[}1 2 3{]}) \newline foo ; =\textgreater{} {[}1 2 3{]} \newline \newline (assoc foo 1 "dog") ; =\textgreater{} {[}1 "dog" 3{]} \newline foo ; =\textgreater{} {[}1 2 3{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Vectors (Composite Data Structure)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{;Vectors are essentially arrays indexed at 0}} \newline ;two forms are \newline (def myVec {[}1 2 3{]}) \newline (def myVec (vector 1 2 3)) \newline \newline {\bf{;again can put functions inside}} \newline (def things {[} \newline "dog" \newline 2 \newline (fn {[}{]} "do something") \newline {]}) \newline \newline {\bf{;and of course we can use get to access the data}} \newline (get things 0) ; =\textgreater{} "dog" \newline (get things 1) : =\textgreater{} 2 \newline (get things 10) : =\textgreater{} nil} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Truthey, Falsey \& Nil}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{;Can test for nil}} \newline – (nil? "boo") ; =\textgreater{} false \newline – (nil? nil) ; =\textgreater{} true \newline \newline ;false and nil are considered falsey \newline ;everything else is considered truthey} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Printing}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{(print "woo") \newline {\bf{; =\textgreater{} "woo"}} \newline \newline (print "woo" "hoo") \newline {\bf{; =\textgreater{} "woo hoo"}} \newline \newline (print "Number:" 4) \newline {\bf{; =\textgreater{} "Number: 4"}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Nested Maps (Composite Data Structure)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{can nest a map in a map}} \newline (def nest \{ \newline :a "eh" \newline :b \{:dog "fido" :cat "fluffy"\} \newline \}) \newline \newline (get nest :a) {\bf{; =\textgreater{} "eh"}} \newline (get nest :b) {\bf{; =\textgreater{} \{:dog "fido", :cat "fluffy"\}}} \newline \newline {\bf{use the get-in function to access inner maps}} \newline (get-in nest {[}:b :cat{]}) ; =\textgreater{} "fluffy"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Set (Composite Data Structure)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{;like in Python, the Clojure set maintains a collection of unique values}} \newline \#\{1 2 "dog" "cat"\} \newline (hash-set 1 2 "dog" :c) \newline \newline {\bf{;sets will automatically discard duplicates}} \newline \#\{1 2 "dog" 2\} ; =\textgreater{} {\bf{\#\{1 2 "dog"\}}} \newline \newline {\bf{;can also create sets from lists and vectors}} \newline ;use the set keyword \newline (set '( 1 1 2 2)) ; =\textgreater{} \#\{1 2\} \newline (set {[}"dog" "cat" "dog"{]}) ; =\textgreater{} \#\{"dog" "cat"\} \newline \newline {\bf{;searching the set}} \newline ;can use {\bf{get}}, a {\bf{keyword}} (:john) or {\bf{contains?}} \newline (def foo \#\{ 1 2 3 :a\} ) \newline (get foo 3) ; =\textgreater{} 3 \newline (get foo 4) ; =\textgreater{} nil \newline (:a foo) ; =\textgreater{}:a, returns itself \newline (contains? foo 2) ; =\textgreater{} true} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Maps (Composite Data Structures)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{; maps are key value pairs}} \newline {\bf{; like dictionaries from python}} \newline \newline (def jobs \{ \newline :john "professor" \newline :sue "doctor" \newline :ahmed "astronaut" \newline \}) \newline \newline {\bf{;keywords}} \newline ; {\bf{:john}} is a {\emph{keyword}}, which is basically a string \newline ; it just has faster internal lookup \newline \newline {\bf{;the get function}} \newline ;returns the value associated /w a key \newline (get jobs :sue) {\bf{; =\textgreater{} "doctor"}} \newline (get jobs :Sue) {\bf{; =\textgreater{} nil}} \newline \newline {\bf{; function lookup table}} \newline (def funcs \{ \newline :func1 (fn {[}{]} (println "func1")) \newline :func2 (fn {[}{]} (println "func2")) \newline :func3 (fn {[}{]} (println "func3")) \newline \}) \newline {\bf{; execute the third function}} \newline ((get funcs :func3)) ; =\textgreater{} func3} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}