Sie sind auf Seite 1von 2

Clojure Cheat Sheet Sequences

Functions and Macros Overview Seq in, Seq out


Documentation Get shorter seq: distinct filter remove for
doc find-doc source (clojure.contrib.repl-utils) Get longer seq: cons concat lazy-cat
mapcat cycle interleave
Data Structures interpose
Head-items missing: rest frest rrest drop
Numbers drop-while nthrest for
Computation: + - * / inc dec quot rem min
Tail-items missing: take take-nth take-while
max rationalize
butlast drop-last for
Comparison: == < > <= >= zero? pos? neg?
Rearrangment: reverse sort sort-by
Bitwise ops: bit-{and, or, xor, not, flip,
Nested seqs: split-at split-with
set, shift-right, shift-left,
partition
and-not, clear, test}
Process each item: map pmap mapcat for
Integer ops: odd even
replace seque
Random num: rand rand-int
BigInt ops: with-precision Using a Seq
Unchecked: unchecked-{add, dec, divide, Extract special item: first ffirst rfirst
inc, multiply, negate, second nth when-first
remainder,subtract} last
Construct a coll: zipmap into reduce
Strings
set vec into-array
str string? pr-str prn-str print-str
to-array-2d
println-str with-out-str
Pass items to fn: apply
Get a boolean: empty? not-empty
Characters some not-any? every?
char char-name-string char-escape-string not-every? reduce
seq? counted? sorted?
contains? reversible?
Lists
sequential? associative?
Create a list: ’() list list*
Search a seq: some filter
List as stack: peek pop
Force evaluation: doseq dorun doall
Examine a list: list?
Creating a Lazy Seq
Vectors
Create a vector: [] vector vec From collection: seq vals keys rseq subseq
Examine a vector: get nth peek rseq vector? rsubseq
’Change’ a vector: assoc pop subvec replace From producer fn: lazy-seq repeatedly
iterate
Maps From constant: repeat replicate range
Create: {} hash-map sorted-map From other objects: line-seq resultset-seq
sorted-map-by re-seq tree-seq file-seq
’Change’: assoc dissoc select-keys merge xml-seq iterator-seq
merge-with zipmap enumeration-seq
Examine: get contains? find keys vals map?
Entry: key val
Reader Macros
StructMaps ’ Quote ’form → (quote form)
Setup: create-struct defstruct accessor \ Character literal
Individual: struct-map struct ; Single line comment
ˆ Meta ˆform → (meta form)
ArrayMaps @ Deref @form → (deref form)
Create: array-map ‘ Syntax-quote
~ Unquote
Sets
~@ Unquote-splicing
Create a set: #{} hash-set sorted-set set get
#"p" Regex Pattern p
conj disj contains? count seq
#ˆ meta data
Operations: union difference intersection
#’ Var quote #’x → (var x)
Rel. algebra: select index rename join project
#() #(...) → (fn [args] (...))
map-invert rename-keys
Misc.
Collections: count conj seq
Special Forms
Keywords: keyword keyword? def if do let quote var fn loop recur throw
Symbols: symbol symbol? gensym try monitor-enter monitor-exit
Macros Namespace
Creation: defmacro definline Current: *ns*
macroexpand-1 macroexpand Creating: in-ns ns create-ns
Branching: and or when when-not Switching: in-ns ns create-ns
when-let when-first if-not Adding: alias def import intern refer
if-let cond condp Finding: all-ns find-ns
Looping: for doseq dotimes while Examining: ns-name ns-aliases ns-map
Arranging code: .. doto -> ns-interns ns-publics ns-refers
Dynamic scopes: binding locking time ns-imports
with-in-str with-local-vars From symbol: resolve ns-resolve namespace
with-open with-out-str Removing: ns-unalias ns-unmap remove-ns
with-precision
Lazy things: lazy-cat lazy-cons delay Java Interoperation
Documenting: assert comment doc Misc: . .. Classname/ Classname. new
bean comparator enumeration-seq
Multimethods import iterator-seq memfn
Creation: defmulti defmethod add-classpath set!
Remove: remove-method Proxys: construct-proxy get-proxy-class
Prefer: prefer-method proxy proxy-mappings proxy-super
Relationship: derive isa? parents ancestors update-proxy
descendants make-hierarchy Arrays: aclone alength aget aset
aset-<type > amap <type >-array
Vars and global environment areduce make-array to-array
Def variants: defn defn- definline defmacro into-array to-array-2d
defmethod defmulti defonce Primitives: int long float double char num
defstruct boolean short byte bigdec bigint
Interned vars: declare intern binding Exceptions: catch finally throw throw-if try
find-var var
Var objects: with-local-vars var-get Other
var-set alter-var-root var? Boolean and = == identical? not= not
Var validators: set-validator get-validator comparison: true? false? nil?
Var metadata: doc find-doc test Creating fns: fn #() partial comp complement
constantly
Refs and Transactions Regex: #"pattern" re-matcher re-find
Create a ref: ref re-matches re-groups re-seq
Examine a ref: deref XML: parse xml-seq
Transaction macros: dosync io! Inspector: inspect inspect-table
In transaction only: ensure ref-set alter inspect-tree
commute Misc.: identity assert with-open eval
Ref validators: set-validator compile force hash name
get-validator
Zippers
Agents and Asynchronous Actions Create: zipper
Creation: agent Get zipper: seq-zip vector-zip xml-zip
Examine: agent-errors Get location: up down left right leftmost
Change state: send send-off rightmost
clear-agent-errors Get seq: lefts rights path children
Block waiting: await await-for ’Change’: make-node replace edit
Ref validators: set-validator get-validator insert-child insert-left
Watchers: add-watch remove-watch insert-right append-child
Thread handling: shutdown-agents remove
Move: next, prev
Loading Misc: root node branch? end?
Loading libs: require use Parallel
Listing loaded libs: loaded-libs Aggregate: pany pmax pmin psummary
Loading misc: load load-file load-reader preduce
load-string Get collection: psort pvec pdistinct
pfilter-dupes pfilter-nils
Printing Array par
Print to *out*: pr prn print println newline
Print to string: pr-str prn-str print-str
println-str with-out-str $Revision: 1.02, $Date: July 10, 2009
Steve Tayon (steve.tayon googlemail com)

Das könnte Ihnen auch gefallen