Clojure Cheatsheet
60 essential methods for coding interviews
Showing 60 of 60 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
map | (map f coll) (map f c1 c2 ...) | Returns lazy sequence of applying f to each element | O(n) | essential |
filter | (filter pred coll) | Returns lazy sequence of elements where pred returns truthy | O(n) | essential |
reduce | (reduce f coll) (reduce f init coll) | Reduces collection to single value by applying f cumulatively | O(n) | essential |
take | (take n coll) | Returns lazy sequence of first n elements | O(n) | essential |
drop | (drop n coll) | Returns lazy sequence with first n elements removed | O(n) | essential |
first | (first coll) | Returns first element of collection, or nil if empty | O(1) | essential |
rest | (rest coll) | Returns sequence of all elements except the first | O(1) | essential |
group-by | (group-by f coll) | Returns map of elements grouped by result of f | O(n) | essential |
frequencies | (frequencies coll) | Returns map of distinct elements to their counts | O(n) | essential |
sort | (sort coll) (sort comparator coll) | Returns sorted sequence of elements | O(n log n) | essential |
some | (some pred coll) | Returns first truthy value of (pred x) for any x in coll | O(n) | essential |
every? | (every? pred coll) | Returns true if pred returns truthy for all elements | O(n) | essential |
apply | (apply f args) (apply f x y z args) | Applies function to argument list | O(1) + f cost | essential |
vector | (vector) (vector a) (vector a b c ...) | Creates a new vector containing the supplied objects | O(n) | essential |
vec | (vec coll) | Converts collection to vector | O(n) | essential |
conj | (conj coll x) (conj coll x y ...) | Returns new collection with items added (at end for vectors) | O(1) for vectors | essential |
nth | (nth coll index) (nth coll index not-found) | Returns element at index, throws if out of bounds | O(1) for vectors, O(n) for lists | essential |
get | (get coll key) (get coll key not-found) | Returns value at key/index, or nil/not-found if missing | O(1) for vectors/maps | essential |
assoc | (assoc coll key val) (assoc coll k v k2 v2 ...) | Returns new collection with value at key/index replaced | O(log32 n) for vectors | essential |
hash-map | (hash-map) (hash-map k1 v1 k2 v2 ...) | Creates hash map from key-value pairs | O(n) | essential |
keys | (keys map) | Returns sequence of map keys | O(n) | essential |
vals | (vals map) | Returns sequence of map values | O(n) | essential |
contains? | (contains? coll key) | Returns true if key is present in collection | O(1) for maps/sets | essential |
set | (set coll) | Creates set from collection elements | O(n) | essential |
str | (str x) (str x y z ...) | Concatenates arguments into string | O(n) | essential |
clojure.string/split | (clojure.string/split s re) | Splits string on regex pattern | O(n) | essential |
clojure.string/join | (clojure.string/join coll) (clojure.string/join sep coll) | Joins collection elements into string with optional separator | O(n) | essential |
count | (count coll) | Returns number of elements in collection | O(1) for counted colls, O(n) for lazy seqs | essential |
empty? | (empty? coll) | Returns true if collection has no elements | O(1) | essential |
into | (into to from) (into to xform from) | Returns new collection with all elements from from added to to | O(n) | essential |
range | (range) (range end) (range start end) (range start end step) | Returns lazy sequence of numbers | O(1) creation | essential |
cons | (cons x coll) | Returns new sequence with x as first element | O(1) | common |
concat | (concat) (concat x) (concat x y) (concat x y & zs) | Returns lazy sequence of concatenated collections | O(n+m) | common |
partition | (partition n coll) (partition n step coll) | Partitions coll into chunks of n elements | O(n) | common |
sort-by | (sort-by keyfn coll) (sort-by keyfn comparator coll) | Sorts collection by result of applying keyfn to each element | O(n log n) | common |
reverse | (reverse coll) | Returns sequence of elements in reverse order | O(n) | common |
distinct | (distinct coll) | Returns lazy sequence with duplicates removed | O(n) | common |
flatten | (flatten coll) | Completely flattens nested collections into single sequence | O(n) | common |
mapcat | (mapcat f coll) | Maps f over coll and concatenates results | O(n*m) | common |
comp | (comp f) (comp f g) (comp f g & more) | Returns composition of functions, right to left | O(1) | common |
partial | (partial f arg1 arg2 ...) | Returns function with some arguments pre-filled | O(1) | common |
update | (update coll key f) (update coll key f args...) | Returns coll with value at key updated by applying f | O(log32 n) | common |
subvec | (subvec v start) (subvec v start end) | Returns subvector from start to end (exclusive) | O(1) | common |
peek | (peek coll) | Returns last element of vector (or first of list) | O(1) | common |
pop | (pop coll) | Returns collection without last element (for vectors) | O(1) | common |
dissoc | (dissoc map key) (dissoc map k1 k2 ...) | Returns map without the specified key(s) | O(log32 n) | common |
merge | (merge map1 map2 ...) | Returns map that combines all maps, later values override | O(n) | common |
select-keys | (select-keys map keyseq) | Returns map containing only the specified keys | O(k) where k is number of keys | common |
update-in | (update-in m ks f & args) | Updates value at nested path by applying f | O(depth) | common |
get-in | (get-in m ks) (get-in m ks not-found) | Returns value at nested path in data structure | O(depth) | common |
assoc-in | (assoc-in m ks v) | Associates value at nested path, creating maps as needed | O(depth) | common |
clojure.set/union | (clojure.set/union s1 s2 ...) | Returns union of sets | O(n+m) | common |
clojure.set/intersection | (clojure.set/intersection s1 s2 ...) | Returns intersection of sets | O(min(n,m)) | common |
clojure.set/difference | (clojure.set/difference s1 s2 ...) | Returns elements in first set not in others | O(n) | common |
disj | (disj set key) (disj set k1 k2 ...) | Returns set with specified elements removed | O(log32 n) | common |
subs | (subs s start) (subs s start end) | Returns substring from start to end (exclusive) | O(1) | common |
repeat | (repeat x) (repeat n x) | Returns lazy sequence of x repeated n times (or infinitely) | O(1) creation | common |
iterate | (iterate f x) | Returns lazy sequence: x, (f x), (f (f x)), ... | O(1) creation | common |
reduce-kv | (reduce-kv f init map) | Reduces map with function receiving accumulator, key, and value | O(n) | common |
zipmap | (zipmap keys vals) | Creates map from parallel sequences of keys and values | O(n) | common |