Documentation

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

Collections

Collections

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?

Lists

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

Vectors

Create [] vector vec vector-of
Examine (my-vec idx)( nth my-vec idx) get peek .indexOf .lastIndexOf
'Change' assoc pop subvec replace conj rseq
Ops (1.4) mapv filterv reduce-kv

Sets

Create #{} set hash-set sorted-set sorted-set-by
Examine (my-set item)( get my-set item) contains?
'Change' conj disj
Set ops (clojure.set/) union difference intersection select See also Relations
Test (clojure.set/) subset? superset?

Maps

Create {} hash-map array-map zipmap sorted-map sorted-map-by bean frequencies group-by (clojure.set/) index
Examine (: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 (clojure.set/) rename-keys map-invert GitHub: Medley
Entry key val
Sorted maps rseq subseq rsubseq

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

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

Transients (clojure.org/transients)

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

Misc

Compare = == identical? not= not compare clojure.data/diff
Test true? false? instance? nil? (1.6) some?

IO

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 (clojure.tools.reader.edn/) read
from reader line-seq (clojure.tools.reader.edn/) read also: (binding [*in* reader] ...) java.io.Reader
from string with-in-str (clojure.tools.reader.edn/) read-string
Open with-open (clojure.java.io/) text: reader writer binary: input-stream output-stream
Binary (.write ostream byte-arr) (.read istream byte-arr) java.io.OutputStream java.io.InputStream GitHub: gloss byte-spec
Misc flush (.close s) file-seq *in* *out* *err* (clojure.java.io/) file copy delete-file resource as-file as-url as-relative-path GitHub: fs
Data readers (1.4) *data-readers* default-data-readers (1.5) *default-data-reader-fn*

Abstractions (Clojure type selection flowchart)

Protocols (clojure.org/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 (clojure.org/datatypes)

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

Types (clojure.org/datatypes)

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

Multimethods (clojure.org/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 isa? parents ancestors descendants make-hierarchy

Reader Macros

' Quote: 'form(quote form)
\ Character literal
; Single line comment
^ Metadata (see Metadata section)
@ Deref: @form(deref form)
` Syntax-quote
~ Unquote
~@ Unquote-splicing
#"p" Regex Pattern p
#' Var quote: #'x(var x)
#() #(...)(fn [args] (...))
#_ Ignore next form

Metadata (clojure.org/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

Loading

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

this cheatsheet is based on https://github.com/jafingerhut/jafingerhut.github.com