Clojure 1.8 Cheat Sheet (v35)

Download PDF version / Source repo

Many thanks to Steve Tayon for creating it and Andy Fingerhut for ongoing maintenance.


clojure.repl/ doc find-doc apropos dir source pst javadoc ( is namespace for later syms)



Literals Long: 7, hex 0xff, oct 017, base 2 2r1011, base 36 36rCRAZY BigInt: 7N Ratio: -22/7 Double: 2.78 -1.2e-5 BigDecimal: 4.2M
Arithmetic + - * / quot rem mod inc dec max min +' -' *' inc' dec'
Compare == < > <= >= compare
Bitwise bit-and bit-or bit-xor bit-not bit-flip bit-set bit-shift-right bit-shift-left bit-and-not bit-clear bit-test (1.6) unsigned-bit-shift-right (see BigInteger for integers larger than Long)
Cast byte short int long float double bigdec bigint num rationalize biginteger
Test zero? pos? neg? even? odd? number? rational? integer? ratio? decimal? float?
Random rand rand-int
BigDecimal with-precision
Unchecked *unchecked-math* unchecked-add unchecked-dec unchecked-inc unchecked-multiply unchecked-negate unchecked-subtract


Create str format "a string" "escapes \b\f\n\t\r\" octal \377 hex \ucafe" See also section IO/to string
Use count get subs compare (clojure.string/) join escape split split-lines replace replace-first reverse (1.8) index-of last-index-of
Regex #"pattern" re-find re-seq re-matches re-pattern re-matcher re-groups (clojure.string/) replace replace-first re-quote-replacement Note: \ in #"" is not escape char. (re-pattern "\\s*\\d+") can be written #"\s*\d+"
Letters (clojure.string/) capitalize lower-case upper-case
Trim (clojure.string/) trim trim-newline triml trimr
Test string? (clojure.string/) blank? (1.8) starts-with? ends-with? includes?


Characters char char? char-name-string char-escape-string literals: \a \newline (more at link)
Keywords keyword keyword? find-keyword literals: :kw :my.ns/kw ::in-cur-ns
Symbols symbol symbol? gensym literals: my-sym my.ns/foo
Misc literals: true false nil



Generic ops count empty not-empty into conj (clojure.walk/) walk prewalk prewalk-demo prewalk-replace postwalk postwalk-demo postwalk-replace
Content tests distinct? empty? every? not-every? some not-any?
Capabilities sequential? associative? sorted? counted? reversible?
Type tests coll? list? vector? set? map? seq? (1.6) record? (1.8) map-entry?

Lists (conj, pop, & peek at beginning)

Create () list list*
Examine first nth peek .indexOf .lastIndexOf
'Change' cons conj rest pop

Vectors (conj, pop, & peek at end)

Create [] vector vec vector-of mapv filterv (clojure.core.rrb-vector/) vector vec vector-of
Examine (my-vec idx)( nth my-vec idx) get peek .indexOf .lastIndexOf
'Change' assoc assoc-in pop subvec replace conj rseq update-in (1.7) update
Ops reduce-kv


Create unsorted #{} set hash-set
Create sorted sorted-set sorted-set-by ( sorted-set sorted-set-by (flatland.ordered.set/) ordered-set ( int-set dense-int-set
Examine (my-set item)( get my-set item) contains?
'Change' conj disj
Set ops (clojure.set/) union difference intersection select See also section Relations
Test (clojure.set/) subset? superset?
Sorted sets rseq subseq rsubseq


Create unsorted {} hash-map array-map zipmap bean frequencies group-by (clojure.set/) index
Create sorted sorted-map sorted-map-by ( sorted-map sorted-map-by ( ordered-map ( priority-map ( ordering-map ( int-map
Examine (my-map k)( get my-map k) also (:key my-map)( get my-map :key) get-in contains? find keys vals
'Change' assoc assoc-in dissoc merge merge-with select-keys update-in (1.7) update (clojure.set/) rename-keys map-invert GitHub: Medley
Ops reduce-kv
Entry key val
Sorted maps rseq subseq rsubseq

Queues (conj at end, peek & pop from beginning)

Create clojure.lang.PersistentQueue/EMPTY (no literal syntax or constructor fn)
Examine peek
'Change' conj pop

Relations (set of maps, each with same keys, aka rels)

Rel algebra (clojure.set/) join select project union difference intersection index rename

Transients (

Create transient persistent!
Change conj! pop! assoc! dissoc! disj! Note: always use return value for later changes, never original!


Compare = identical? not= not compare
Test true? false? instance? nil? (1.6) some?


to/from ... spit slurp (to writer/from reader, Socket, string with file name, URI, etc.)
to *out* pr prn print printf println newline (clojure.pprint/) print-table
to writer (clojure.pprint/) pprint cl-format also: (binding [*out* writer] ...)
to string format with-out-str pr-str prn-str print-str println-str
from *in* read-line ( read
from reader line-seq ( read also: (binding [*in* reader] ...)
from string with-in-str ( read-string
Open with-open ( text: reader writer binary: input-stream output-stream
Binary (.write ostream byte-arr) (.read istream byte-arr) GitHub: gloss byte-spec
Misc flush (.close s) file-seq *in* *out* *err* ( file copy delete-file resource as-file as-url as-relative-path GitHub: fs
Data readers *data-readers* default-data-readers *default-data-reader-fn*

Abstractions (Clojure type selection flowchart)

Protocols (

Define ( defprotocol Slicey (slice [at]))
Extend ( extend-type String Slicey (slice [at] ...))
Extend null ( extend-type nil Slicey (slice [_] nil))
Reify ( reify Slicey (slice [at] ...))
Test satisfies? extends?
Other extend extend-protocol extenders

Records (

Define ( defrecord Pair [h t])
Access (:h (Pair. 1 2))1
Create Pair. ->Pair map->Pair
Test record?

Types (

Define ( deftype Pair [h t])
Access (.h (Pair. 1 2))1
Create Pair. ->Pair
With methods ( deftype Pair [h t]
  (toString [this] (str "<" h "," t ">")))

Multimethods (

Define ( defmulti my-mm dispatch-fn)
Method define ( defmethod my-mm :dispatch-value [args] ...)
Dispatch get-method methods
Remove remove-method remove-all-methods
Prefer prefer-method prefers
Relation derive underive isa? parents ancestors descendants make-hierarchy

Special Characters (, tutorial)

, Comma reads as white space. Often used between map key/value pairs for readability.
' quote: 'form( quote form)
/ Namespace separator (see Primitives/Other section)
\ Character literal (see Primitives/Other section)
: Keyword (see Primitives/Other section)
; Single line comment
^ Metadata (see Metadata section)
*foo* 'earmuffs' - convention to indicate dynamic vars, compiler warns if not dynamic
@ Deref: @form( deref form)
` Syntax-quote
foo# 'auto-gensym', consistently replaced with same auto-generated symbol everywhere inside same `( ... )
~ Unquote
~@ Unquote-splicing
-> 'thread first' macro ->
->> 'thread last' macro ->>
( List literal (see Collections/Lists section)
[ Vector literal (see Collections/Vectors section)
{ Map literal (see Collections/Maps section)
#' Var-quote: #'x( var x)
#" #"p" reads as regex pattern p (see Strings/Regex section)
#{ Set literal (see Collections/Sets section)
#( Anonymous function literal: #(...)(fn [args] (...))
% Anonymous function argument: %N is value of anonymous function arg N. % short for %1. %& for rest args.
#? (1.7) Reader conditional: #?(:clj x :cljs y) reads as x on JVM, y in ClojureScript, nothing elsewhere. Other keys: :cljr :default
#?@ (1.7) Splicing reader conditional: [1 #?(:clj [x y] :cljs [w z]) 3] reads as [1 x y 3] on JVM, [1 w z 3] in ClojureScript, [1 3] elsewhere.
#foo tagged literal e.g. #inst #uuid
$ JavaContainerClass$InnerClass
foo? conventional ending for a predicate, e.g.: zero? vector? instance? (unenforced)
foo! conventional ending for an unsafe operation, e.g.: set! swap! alter-meta! (unenforced)
_ conventional name for an unused value (unenforced)
#_ Ignore next form

Metadata (, special_forms)

General ^{:key1 val1 :key2 val2 ...}
Abbrevs ^Type^{:tag Type}
^:key^{:key true}
Common ^:dynamic ^:private ^:doc ^:const
Examples (defn ^:private ^String my-fn ...)
(def ^:dynamic *dyn-var* val)
On Vars meta with-meta vary-meta alter-meta! reset-meta! doc find-doc test


Load libs (tutorial) require use import refer
List loaded loaded-libs
Load misc load load-file load-reader load-string