26 mai 2016
Clojure core functions - Cheat Sheet
My notes on classic Clojure functions.
Here are some useful Clojure core functions along with examples and MathML notation for fun.
Nothing new under the sun, but these notes had been dragging on for a long time in my drafts, so it was time to share them!
| Syntax quoting brings optional gensym (if # at the end of var name. Useful for macros that create local vars, eg in let or loop ) + namespace qualification of symbols. Produces code to reproduce the form. One can unquote inside a syntax-quoted form with the tilde ~ . |
| Returns nil if arg is nil , otherwise execute method on arg. Thus avoids nil checks. |
| Evaluates ƒ (must not be a macro) on \( x_n \) arguments prepended to the collection. Has similarities with unquote splicing ~@ . |
|
|
| Returns the same type of associative structure, with v the value of nested key reached by \( k1 .. kn \). If any \( k_x \) level does not exist, hash-maps are created. |
| Returns a map with all getters of the java object. |
| Returns a function with undefined arity, applying \( f_x \) (from right to left) on arguments. |
| Returns a sequence including all \( x_k \) elements. Does not flatten nested colls. |
|
|
| Returns a function with undefined arity, that always results in x. |
| Returns true if the key k is present in the indexed collection (map and set ), or if the index exists in a vector . Do not use with list . Prefer some to query for a value. |
| Optionally with implementation of protocols. |
| Opposite of assoc . Returns an associative structure of the same type than map but without the nested key reached by \( k_1 \cdots k_n \) |
| Force evaluation of lazy seqs (side effects). unlike for , doseq never returns a value but nil . doall retains the head and returns it. dorun does not retain the head and returns nil . |
| Runs body \( n \) times, from \( 0 \text{ to } n-1 \). |
|
|
| Flattens the nested sequences. |
| Returns a function that calls ƒ with \( x_k \) as an argument if the original argument is nil . The arity of ƒ must be \( \geq n \). |
| « List comprehension ». Returns a sequence containing the results of the execution of body. Not intended for side effects. |
| Returns with a map that indicates, for each separate element of col , the frequency at with which it appears. |
| Returns a map of col elements, sorted by the return value of ƒ applied. to them. |
| Returns a sequence containing the first element of each \( c_x \), then the second, ... |
| Returns a sequence of elements of the collection separated by the sep separator. |
| Returns a collection of the same type than to , appending all elements of collection from . |
| ƒ must be a pure function. |
| Returns a function that returns a vector whose elements are the application of ƒ on the argument. |
| Returns a sequence made of non nil results of the application of ƒ on every coll elements. false results are included. ƒ must be a pure function. keep-indexed uses a function like fn [idx v] .
|
| Returns a list containing all \( x_n \) args, possibly nil . Unlike literal notation list '(...) , the elements are evaluated before insertion. |
| Returns a list containing all \( x_n \) args, possibly nil , as well as all elements of sequence s (if not empty and not nil ). |
| Returns a sequence containing results of the application of ƒ on each first elements of every collections \( c_n \) then on each second elements, ... ƒ should have as many args as the number of collections. If a collection has too many arguments they will be ignored. With a single collection, map applies the function on every elements. mapv : same thing but returns a vector and is not lazy. |
| Equivalent to (apply concat (map f c1 ... cn)) . Applies concat on the result of the application of map on ƒ and collections. |
| Returns a cached version of ƒ. ƒ must be pure. |
| Alternate docstring. Metadata does not affect equality. |
| Returns supplied-val if not nil , otherwise default-val . |
| Returns a function that takes \( n \) less args that what ƒ requires. |
| Returns a lazy sequence containing lists of \( n \) elements each. If the final list has less than \( n \) elements, it is not added, except with partition-all (see below). The 'step', which is \( n \) by default, is the offset for the creation of each list. 'Pad' is a list designed to complement the latest if less than \( n \) elements. |
| Similar to partition , but also builds the last list even if there are less than \( n \) elements. |
| Similar to partition , but cut the list each times ƒ change its value. |
| ƒ should have a 2 args arity, except when not used or indicated. Returns the accumulator. |
| Returns a sequence of intermediate steps of reduce . |
| ƒ should have no args, possibly impure. Returns an infinite sequence (or size n) of successive calls to ƒ. |
| Returns an infinite sequence (or size n) of value x. If x is a function, only one call is made. |
| Returns a sequence from the collection coll . For an nil or empty collection, seq and sequence behave differently. |
| Returns a sorted sequence. |
| Returns a vector of [(take n coll) (drop n coll)] . |
| Returns a vector of [(take-while pred coll) (drop-while pred coll)] . |
| Takes the first and every nth elements of c . |
| Returns elements of coll as long as predicate pred is true. |
| Used for mutual recursion without consumming the stack. Performs the ping pong as long as what is returned is a function. |
| Returns an associative structure identical to map but with the value of the nested key reached by \( k_1 \cdots k_n \) updated by ƒ (and its optional arguments). If the \( k_x \) level does not exist, hash-maps will be created. |
| Returns a vector containing the elements of c. |
| Returns a vector containing all arguments \( x_\n \). |
| Returns a vector of primitive types (:int :long :float :double :byte :short :char or :boolean) containing all optional \( x_\n \) arguments. |
| return a map with the keys associated with values. |
| keep = map + filter |
Inspect a map
:
(require 'clojure.inspector)
(clojure.inspector/inspect-tree map)
And finally, some other useful functions, from the excellent Jay Fields's blog.
(def jay {:fname "jay" :lname "fields" :employer "drw"})
(def mike {:fname "mike" :lname "jones" :employer "forward"})
(def john {:fname "john" :lname "dydo" :employer "drw"})
; returns a map whose keys are every employers and values are people defined above
(clojure.set/index [jay mike john] [:employer])
; => {{:employer "drw"} #{{:employer "drw" :fname "jay" :lname "fields"}
; {:employer "drw" :fname "john" :lname "dydo"}}
; {:employer "forward"} #{{:employer "forward" :fname "mike" :lname "jones"}}}
; projection
(clojure.set/project [jay mike john] [:fname :lname])
; => #{{:lname "fields", :fname "jay"}
; {:lname "dydo", :fname "john"}
; {:lname "jones", :fname "mike"}}
; key rename
(clojure.set/rename [jay mike john] {:fname :first-name :lname :last-name})
; => #{{:last-name "jones", :first-name "mike", :employer "forward"}
; {:last-name "dydo", :first-name "john", :employer "drw"}
; {:last-name "fields", :first-name "jay", :employer "drw"}}