0% found this document useful (0 votes)
237 views

Clojure Cheat Sheet A4

The document provides a concise overview of Clojure's main primitives, data structures, functions and macros for working with numbers, strings, collections, concurrency, and more. It summarizes how to create and manipulate core Clojure types like vectors, maps and sets as well as work with lazy sequences. The cheat sheet also outlines functions and macros for multimethods, macros, printing, and concurrency constructs like atoms, refs, agents and more.

Uploaded by

Iain Wood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
237 views

Clojure Cheat Sheet A4

The document provides a concise overview of Clojure's main primitives, data structures, functions and macros for working with numbers, strings, collections, concurrency, and more. It summarizes how to create and manipulate core Clojure types like vectors, maps and sets as well as work with lazy sequences. The cheat sheet also outlines functions and macros for multimethods, macros, printing, and concurrency constructs like atoms, refs, agents and more.

Uploaded by

Iain Wood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Clojure Cheat Sheet (Clojure 1.

2) StructMaps
Create defstruct create-struct accessor
Documentation Individual struct-map struct
doc find-doc Use get assoc
clojure.repl source
Transients
Primitives Create transient persistent!
Use conj! pop! assoc! dissoc! disj!
Numbers
‘Change’ conj concat distinct group-by partition
Arithmetic + - * / quot rem mod inc dec max min
partition-all partition-by split-at
Compare = == not= < > <= >=
split-with filter remove replace shuffle
Bitwise bit-{and, or, xor, not, flip, set,
Iteration for doseq map map-indexed mapcat reduce
shift-right, shift-left, and-not, clear,
reductions max-key min-key doall dorun
test}
Cast byte short int long float double bigint Misc
bigdec num rationalize Compare = == identical? not= not
Test nil? identical? zero? pos? neg? even? odd? Test true? false? nil?
Random rand rand-int
BigInt with-precision Sequences
Unchecked unchecked-{add, dec, divide, inc, multiply, Creating a Lazy Seq
negate, remainder,subtract} From collection seq vals keys rseq subseq rsubseq
Strings From producer fn lazy-seq repeatedly iterate
From constant repeat replicate range
Create str print-str println-str pr-str prn-str
From other file-seq line-seq resultset-seq
with-out-str
re-seq tree-seq xml-seq iterator-seq
Use count get subs format
enumeration-seq
Cast/Test char char? string?
From seq keep keep-indexed
Strings (clojure.strings)
Seq in, Seq out
Test blank?
Get shorter distinct filter remove for
Letters capitalize lower-case upper-case
Get longer cons conj concat lazy-cat mapcat
Use join escape split split-lines replace
cycle interleave interpose
replace-first reverse
Tail-items rest fnext nnext drop drop-while
Trim trim trim-newline triml trimr
nthrest for
Other Head-items take take-nth take-while take-last
Characters char char-name-string char-escape-string butlast drop-last for
Keywords keyword keyword? ‘Change’ conj concat distinct group-by
Symbols symbol symbol? gensym partition partition-all partition-by
split-at split-with filter remove
Collections replace shuffle
Rearrange reverse sort sort-by
Collections Process each item map pmap map-indexed mapcat for
Generic ops count empty not-empty into conj
replace seque
Content tests contains? distinct? empty? every?
Un-lazy Seq sequence
not-every? some not-any?
Capabilities sequential? associative? sorted? counted? Using a Seq
reversible? Extract item first second last rest next ffirst
Type tests coll? seq? vector? list? map? set? rfirst nfirst fnext nnext nth nthnext
rand-nth when-first max-key min-key
Lists
Create ’() list list* Construct coll zipmap into reduce reductions set vec
Stack peek pop into-array to-array-2d
Examine first rest peek list? Pass to fn apply
‘Change’ cons conj Search some filter
Force evaluation doseq dorun doall
Vectors
Create [] vector vec vector-of Zippers (clojure.zip)
Examine get nth peek rseq vector? Create zipper
‘Change’ assoc pop subvec replace conj Get zipper seq-zip vector-zip xml-zip
Get location up down left right leftmost rightmost
Sets
Get seq lefts rights path children
Create #{} hash-set sorted-set set conj disj
‘Change’ make-node replace edit insert-child
Examine get
insert-left insert-right append-child
Sets (clojure.set) remove
Rel. algebra join select project union difference Move next, prev
intersection Misc root node branch? end?
Get map index rename-keys rename map-invert
Test subset? superset?
Printing
Print to *out* pr prn print printf println newline
Maps Print to string pr-str prn-str print-str println-str
Create {} hash-map array-map zipmap sorted-map with-out-str
sorted-map-by bean frequencies
‘Change’ assoc assoc-in dissoc zipmap merge Functions
merge-with select-keys update-in Create fn defn defn- definline identity constantly
Examine get get-in contains? find keys vals map? memfn comp complement partial juxt memoize
Entry key val Call -> -» apply
Sorted maps rseq subseq rsubseq Test fn? ifn?
Multimethods Concurrency
Create defmulti defmethod Atoms atom swap! reset! compare-and-set!
Dispatch get-method methods Futures future future-call future-done? future-cancel
Remove remove-method remove-all-methods future-cancelled? future?
Prefer prefer-method prefers Threads bound-fn bound-fn* get-thread-bindings
Relation derive isa? parents ancestors descendants push-thread-bindings pop-thread-bindings
make-hierarchy thread-bound?
Misc locking pcalls pvalues pmap seque promise
Macros deliver
Create defmacro definline macroexpand-1
Refs and Transactions
macroexpand
Create ref
Branch and or when when-not when-let when-first
Examine deref @ (@form → (deref form))
if-not if-let cond condp
Transaction macros sync dosync io!
Loop for doseq dotimes while
In transaction . ensure ref-set alter commute
Arrange .. doto ->
Validators set-validator! get-validator
Scope binding locking time with-in-str
History ref-history-count ref-max-history
with-local-vars with-open with-out-str
ref-min-history
with-precision
Lazy lazy-cat lazy-cons delay Agents and Asynchronous Actions
Document assert comment doc Create agent
Examine agent-error
Reader Macros Change state send send-off restart-agent
’ Quote ’form → (quote form) Block waiting await await-for
\ Character literal Ref validators set-validator get-validator
; Single line comment Watchers add-watch remove-watch
ˆ Meta ˆform → (meta form) Thread handling shutdown-agents
@ Deref @form → (deref form) Error error-handler set-error-handler!
‘ Syntax-quote error-mode set-error-mode
~ Unquote Misc *agent* release-pending-sends
~@ Unquote-splicing
#"p" Regex Pattern p Java Interoperation
#ˆ Metadata General .. doto Classname/ Classname. new
#0 Var quote #0 x → (var x) bean comparator enumeration-seq import
#() #(...) → (fn [args] (...)) iterator-seq memfn set!
#_ Ignore next form Cast int long float double char num boolean
short byte bigdec bigint
Vars and global environment Exceptions catch finally throw throw-if try
Def variants defn defn- definline defmacro defmethod Arrays
defmulti defonce defstruct Create make-array {object, boolean, byte, char,
Interned vars declare intern binding find-var var short, int, long, float, double}-array aclone
Var objects with-local-vars var-get var-set to-array to-array-2d into-array
alter-var-root var? Use aget aset aset-{boolean, char, byte, int,
Var validators set-validator get-validator long, short, float, double} alength amap
Var metadata doc find-doc test areduce
Cast booleans bytes chars ints shorts longs floats
Namespace doubles
Current *ns* Proxy
Create/Switch in-ns ns create-ns Create proxy get-proxy-class construct-proxy
Add alias def import intern refer init-proxy
Find all-ns find-ns Misc proxy-mappings proxy-super update-proxy
Examine ns-name ns-aliases ns-map ns-interns
ns-publics ns-refers ns-imports Other
From symbol resolve ns-resolve namespace Regex #"pattern" re-pattern re-matcher re-find
Remove ns-unalias ns-unmap remove-ns re-matches re-groups re-seq
XML parse (clojure.xml) xml-seq
Loading REPL *1 *2 *3 *e *print-dup* *print-length*
Loading libs require use import refer *print-level* *print-meta* *print-readably*
Listing loaded libs loaded-libs IO *in* *out* *err* flush read-line read
Loading misc load load-file load-reader read-string slurp spit with-in-str with-out-str
load-string with-open
Code *compile-files* *compile-path* *file*
*warn-on-reflection* compile gen-class
Special Forms gen-interface loaded-libs test
def if do let quote var fn loop recur throw try Misc eval force hash name *clojure-version*
monitor-enter monitor-exit clojure-version *command-line-args*

$Revision: 1.21, $Date: November 02, 2010


Steve Tayon (steve.tayon googlemail com)

You might also like