(adjust direction n s)Left or right-adjust a string.
Left or right-adjust a string.
(clean-code code)Recursively unqualifies qualified code in the provided form, fully-qualifying back any unqualified symbol with a namespace.
(clean-code `(a (b c)))
=> '(a (b c))
Recursively unqualifies qualified code in the provided form, fully-qualifying back any unqualified symbol with a namespace. ```clojure (clean-code `(a (b c))) => '(a (b c)) ```
(debug & forms)Evaluates and prints a debug statement for each form. Returns the value of the last.
(debug (+ 1 2)
(- 3 4))
; (+ 1 2): 3
; (- 3 4): -1
=> -1
Evaluates and prints a debug statement for each form.
Returns the value of the last.
```clojure
(debug (+ 1 2)
(- 3 4))
; (+ 1 2): 3
; (- 3 4): -1
=> -1
```(deconstruct binding-form)Returns a flat sequence of the symbols bound in the binding-form in depth-first order.
(deconstruct '[a & {:keys [x] y :_y :or {x 1} :as m}])
=> '[a x y m]
Returns a flat sequence of the symbols bound in the binding-form
in depth-first order.
```clojure
(deconstruct '[a & {:keys [x] y :_y :or {x 1} :as m}])
=> '[a x y m]
```(deep-merge m & more)Deep merges two or more nested maps.
(deep-merge {:x {:a :a :b :b :c :c}}
{:x {:a :aa :b :bb}}
{:x {:a :aaa}})
=> {:x {:a :aaa :b :bb :c :c}}
Deep merges two or more nested maps.
```clojure
(deep-merge {:x {:a :a :b :b :c :c}}
{:x {:a :aa :b :bb}}
{:x {:a :aaa}})
=> {:x {:a :aaa :b :bb :c :c}}
```(deflatten-keys m)Builds a nested map out of a map obtained from flatten-keys.
(deflatten-keys {[:a :b :c] :x
[:a :b :d] :y})
=> {:a {:b {:c :x
:d :y}}}
Builds a nested map out of a map obtained from [[flatten-keys]].
```clojure
(deflatten-keys {[:a :b :c] :x
[:a :b :d] :y})
=> {:a {:b {:c :x
:d :y}}}
```(disentangle binding-form)Parses one level of destructuring.
(disentangle '[a b & [c]])
=> '{:items [a b], :more [c]}
(disentangle '{:keys [a] b :b [c1 c2] :c :or {d 1} :as m})
=> '{:items [a b [c1 c2]],
:as m,
:or {d 1},
:mapping {a :a, b :b, [c1 c2] :c}}
Parses one level of destructuring.
```clojure
(disentangle '[a b & [c]])
=> '{:items [a b], :more [c]}
(disentangle '{:keys [a] b :b [c1 c2] :c :or {d 1} :as m})
=> '{:items [a b [c1 c2]],
:as m,
:or {d 1},
:mapping {a :a, b :b, [c1 c2] :c}}
```(file-eval code)Evaluate code in a temporary file via load-file in the local
lexical context. Keep the temporary file aside if an error is
raised, deleting it on the next run.
(let [a 1]
(file-eval '(+ 1 a)))
Code evaluated this way will be source-mapped in stacktraces.
Evaluate code in a temporary file via `load-file` in the local lexical context. Keep the temporary file aside if an error is raised, deleting it on the next run. ```clojure (let [a 1] (file-eval '(+ 1 a))) ``` Code evaluated this way will be source-mapped in stacktraces.
(flatten-keys m)Transforms a nested map into a map where keys are paths through the original map and values are leafs these paths lead to.
(flatten-keys {:a {:b {:c :x
:d :y}}})
=> {[:a :b :c] :x
[:a :b :d] :y}
(flatten-keys {:a {:b {:c :x
:d :y}}})
=> {[:a :b :c] :x
[:a :b :d] :y}
Transforms a nested map into a map where keys are paths through
the original map and values are leafs these paths lead to.
```clojure
(flatten-keys {:a {:b {:c :x
:d :y}}})
=> {[:a :b :c] :x
[:a :b :d] :y}
```
(flatten-keys {:a {:b {:c :x
:d :y}}})
=> {[:a :b :c] :x
[:a :b :d] :y}(format-code code)Returns a string as formatted by clojure.pprint/code-dispatch.
Returns a string as formatted by `clojure.pprint/code-dispatch`.
(fully-qualified? sym)(fully-qualified? ns sym)Returns true if the symbol constitutes an absolute reference. See [[fully-qualified]].
Handles namespace aliases.
ns defaults to *ns*.
(fully-qualified? 'clojure.lang.IRecord) => true (fully-qualified? 'my-ns/my-var) => true (fully-qualified? 'alias/my-var) => false
Returns true if the symbol constitutes an absolute reference. See [[fully-qualified]]. Handles namespace aliases. `ns` defaults to `*ns*`. (fully-qualified? 'clojure.lang.IRecord) => true (fully-qualified? 'my-ns/my-var) => true (fully-qualified? 'alias/my-var) => false
(fully-qualify sym)(fully-qualify ns sym)Returns the fully-qualified form of the symbol as if resolved from
within a namespace.
Handles namespace aliases.
nsdefaults tp *ns*.
(fully-qualified? 'IRecord) => clojure.lang.IRecord (fully-qualified? 'my-var) => my-ns/my-var (fully-qualified? 'alias/my-var) => actual.namespace/my-var
Returns the fully-qualified form of the symbol as if resolved from within a namespace. Handles namespace aliases. `ns`defaults tp `*ns*`. (fully-qualified? 'IRecord) => clojure.lang.IRecord (fully-qualified? 'my-var) => my-ns/my-var (fully-qualified? 'alias/my-var) => actual.namespace/my-var
(index-by f coll)(index-by f strategy coll)Like group-by except it applies a strategy to each grouped
collection.
A strategy is a function with signature key, entries) -> entry
where entry is the one that will be indexed.
The default strategy asserts there is only one entry for the given
key and returns it.
(def ms [{:a 1 :b 2} {:a 3 :b 4} {:a 5 :b 4}])
(index-by :a ms)
=> {1 {:a 1 :b 2}
3 {:a 3 :b 4}
5 {:a 5 :b 4}}
(index-by :b ms)
=> ; clojure.lang.ExceptionInfo (Duplicate entries for key 4)
(index-by :b (fn [key entries]
(last entries))
ms)
=> {2 {:a 1 :b 2}
4 {:a 5 :b 4}}
Like `group-by` except it applies a strategy to each grouped
collection.
A strategy is a function with signature `key, entries) -> entry`
where `entry` is the one that will be indexed.
The default strategy asserts there is only one entry for the given
key and returns it.
```clojure
(def ms [{:a 1 :b 2} {:a 3 :b 4} {:a 5 :b 4}])
(index-by :a ms)
=> {1 {:a 1 :b 2}
3 {:a 3 :b 4}
5 {:a 5 :b 4}}
(index-by :b ms)
=> ; clojure.lang.ExceptionInfo (Duplicate entries for key 4)
(index-by :b (fn [key entries]
(last entries))
ms)
=> {2 {:a 1 :b 2}
4 {:a 5 :b 4}}
```(is-form? sym code)Determines whether code is a form starting with sym.
(is-form? 'a 1) ; => false
(is-form? 'a '[a :z]) ; => false
(is-form? 'a '(a :z)) ; => true
Determines whether `code` is a form starting with `sym`. ```clojure (is-form? 'a 1) ; => false (is-form? 'a '[a :z]) ; => false (is-form? 'a '(a :z)) ; => true ```
(java-patch & args)Applies a monkey-patch to a java method.
mode can be either :replace, :before or :after.
body can be javassist pseudo-java (a string) or clojure code.
In the latter case, args must be specified before body so as to
intercept the method's parameters. If :after is used args will
include the method's return value. If the method is an instance
method, args will also include the instance it is called on
(this).
args: [this? return-value? & method-args]
(java-patch [clojure.lang.LispReader "read"
[java.io.PushbackReader "boolean" Object "boolean" Object]]
:replace
[reader eof-is-error eof-value _is-recursive opts]
(clojure.tools.reader/read
(merge {:eofthrow eof-is-error
:eof eof-value}
opts)
reader))
;; or
(java-patch [clojure.lang.LispReader$SyntaxQuoteReader "syntaxQuote"
[Object]]
:after
"{
$_ = clojure.lang.RT.list(
clojure.lang.Symbol.intern("clojure.core", "from-syntax-quote"),
$_
);
}")
Applies a monkey-patch to a java method.
`mode` can be either `:replace`, `:before` or `:after`.
`body` can be javassist pseudo-java (a string) or clojure code.
In the latter case, `args` must be specified before `body` so as to
intercept the method's parameters. If `:after` is used `args` will
include the method's return value. If the method is an instance
method, `args` will also include the instance it is called on
(`this`).
`args`: `[this? return-value? & method-args]`
```clojure
(java-patch [clojure.lang.LispReader "read"
[java.io.PushbackReader "boolean" Object "boolean" Object]]
:replace
[reader eof-is-error eof-value _is-recursive opts]
(clojure.tools.reader/read
(merge {:eofthrow eof-is-error
:eof eof-value}
opts)
reader))
;; or
(java-patch [clojure.lang.LispReader$SyntaxQuoteReader "syntaxQuote"
[Object]]
:after
"{
$_ = clojure.lang.RT.list(
clojure.lang.Symbol.intern("clojure.core", "from-syntax-quote"),
$_
);
}")
```(join-lines lines)Glues strings together with newlines.
Glues strings together with newlines.
(join-words words)Glues strings together with spaces.
Glues strings together with spaces.
(lines s)Splits a string around newlines.
Splits a string around newlines.
(macroexpand-do & args)(defmacro abc [] `(println "xyz"))
(macroexpand-do (abc))
; -- Macro expansion -- ; (clojure.core/println "xyz") ; ; -- Running macro -- ; xyz
or alternatively:
(macroexpand-do MODE expr)
Where MODE has the following meaning:
MODE | expansion |
|---|---|
nil (the default) | macroexpand |
:all | macroexpand-all-eager |
| :n <number> | macroexpand-n |
| :depth <number> | macroexpand-depth |
| :some <fn> | macroexpand-some |
(defmacro abc [] `(println "xyz")) (macroexpand-do (abc)) ; -- Macro expansion -- ; (clojure.core/println "xyz") ; ; -- Running macro -- ; xyz or alternatively: (macroexpand-do MODE expr) Where `MODE` has the following meaning: | `MODE` | expansion | |-------------------------------|----------------------------------| | `nil` (the default) | `macroexpand` | | `:all` | `macroexpand-all-eager` | | :n <number> | `macroexpand-n` | | :depth <number> | `macroexpand-depth` | | :some <fn> | `macroexpand-some` |
(macroexpand-n n form)Iteratively call macroexpand-1 on form n times.
Iteratively call `macroexpand-1` on `form` `n` times.
(macroexpand-some pred expr)Recursively macroexpands seqs in expr whose first element matches
pred. Symbols are passed to filter, their namespace stripped.
Quoted forms are not expanded.
(macroexpand-some #{'let}
'(let [a (let [aa 1] aa)
b '(let [bb 2] bb)]
(dosync [a b])))
=> (let* [a (let* [aa 1] aa) ; expanded
b '(let [bb 2] bb)] ; not expanded since quoted
(dosync nil)) ; not expanded b/c pred does not match
Recursively macroexpands seqs in `expr` whose first element matches
`pred`. Symbols are passed to `filter`, their namespace stripped.
Quoted forms are not expanded.
```clojure
(macroexpand-some #{'let}
'(let [a (let [aa 1] aa)
b '(let [bb 2] bb)]
(dosync [a b])))
=> (let* [a (let* [aa 1] aa) ; expanded
b '(let [bb 2] bb)] ; not expanded since quoted
(dosync nil)) ; not expanded b/c pred does not match
```(map-difference m & ms)Returns a map that is the first map without elements of the remaining maps.
Returns a map that is the first map without elements of the remaining maps.
(map-keys f m)Applies f to each key of m.
Applies `f` to each key of `m`.
(map-vals f m)Applies f to each value of m.
Applies `f` to each value of `m`.
(max-by f x)(max-by f x y)(max-by f x y & more)Returns the greatest of the elements by pred.
Returns the greatest of the elements by pred.
(merge-with-plan plan & maps)Like merge-with except that the combination fn of a specific pair
of entries is determined by looking up their key in plan. If not
found, falls back to the function found under key :else or if not
provided to a function that returns the value in the right-most map,
thus providing the behavior of merge.
In addition to a map, plan can also be a function accepting a key
and returning a combination fn for the two values to merge.
Like `merge-with` except that the combination fn of a specific pair of entries is determined by looking up their key in `plan`. If not found, falls back to the function found under key `:else` or if not provided to a function that returns the value in the right-most map, thus providing the behavior of `merge`. In addition to a map, `plan` can also be a function accepting a key and returning a combination fn for the two values to merge.
(method class name parameter-types)Returns a method by reflection.
Returns a method by reflection.
(min-by f x)(min-by f x y)(min-by f x y & more)Returns the least of the elements by pred.
Returns the least of the elements by pred.
(monkey-patch name target args & body)(monkey-patch perfid-incer clojure.core/+ [original & args]
(inc (apply original args)))
(+ 1 2)
=> 4
Supports reload. Name and target can be vars or symbols. Name can also be a keyword.
```clojure (monkey-patch perfid-incer clojure.core/+ [original & args] (inc (apply original args))) (+ 1 2) => 4 ``` Supports reload. Name and target can be vars or symbols. Name can also be a keyword.
(no-print & body)Binds out to an anonymous writer used as /dev/null and returns the value of the last expr in body.
Binds *out* to an anonymous writer used as /dev/null and returns the value of the last expr in body.
(only name & body)Ensures body is executed only once with respect to name.
If name is a symbol or a keyword without a namespace, it will be
prefixed with the value of *ns*.
(only 'foo (println "bar"))
; prints bar
(only 'foo (println "bar"))
; prints nothing
Ensures `body` is executed only once with respect to `name`. If `name` is a symbol or a keyword without a namespace, it will be prefixed with the value of `*ns*`. ```clojure (only 'foo (println "bar")) ; prints bar (only 'foo (println "bar")) ; prints nothing ```
(order array constraints)Order a sequence with a collection of constraints of the form:
Can specify constraints on :all elements.
Raises an exception if constraints are contradictory.
Example:
(order [1 2 3] {2 1 3 :all})
(order [1 2 3] [[2 1] [3 :all]])
(order [1 2 3] [[2 :before 1] [:all :after 3]])
(order [1 2 3] [[2 :> 1] [:all :< 3]])
;; (3 2 1)
Order a sequence with a collection of constraints of the form:
- [a b]
- [a :before b]
- [a :> b]
- [b :after a]
- [b :< a]
Can specify constraints on `:all` elements.
Raises an exception if constraints are contradictory.
Example:
```clojure
(order [1 2 3] {2 1 3 :all})
(order [1 2 3] [[2 1] [3 :all]])
(order [1 2 3] [[2 :before 1] [:all :after 3]])
(order [1 2 3] [[2 :> 1] [:all :< 3]])
;; (3 2 1)
```(prepostwalk pre-fn post-fn form)A combination of clojure.walk's prewalk and postwalk.
Recursively modifies form with pre-fn when walking in and
post-fn when walking out.
A combination of `clojure.walk`'s `prewalk` and `postwalk`. Recursively modifies `form` with `pre-fn` when walking in and `post-fn` when walking out.
(prepostwalk-demo form)Demonstrates the behavior of prepostwalk. Returns form.
Demonstrates the behavior of [[prepostwalk]]. Returns form.
(read-field x name)Returns the value of a field, static if x is a class, or on the passed instance otherwise. All fields, private, protected or public, are accessible.
Returns the value of a field, static if x is a class, or on the passed instance otherwise. All fields, private, protected or public, are accessible.
(refresh-only name)Reset only statements by name. Next time one is called, the
associated code will be evaluated.
If name is a symbol or a keyword without a namespace, it will be
prefixed with *ns*.
(only 'foo (println "bar"))
; prints bar
(refresh-only 'foo)
(only 'foo (println "bar"))
; prints bar
Reset `only` statements by name. Next time one is called, the associated code will be evaluated. If `name` is a symbol or a keyword without a namespace, it will be prefixed with `*ns*`. ```clojure (only 'foo (println "bar")) ; prints bar (refresh-only 'foo) (only 'foo (println "bar")) ; prints bar ```
(restructure binding-form mapping)Undoes what destructure does.
(restructure [x & {:keys [a b] c :cc d :d :or {d 3}}]
{x 0 a 1 b 2 c 3})
=> [0 :a 1 :b 2 :cc 3 :d nil]
Undoes what destructure does.
```clojure
(restructure [x & {:keys [a b] c :cc d :d :or {d 3}}]
{x 0 a 1 b 2 c 3})
=> [0 :a 1 :b 2 :cc 3 :d nil]
```(separate pred coll)Returns a vector of [(filter pred coll) (remove pred coll)].
(let [coll [1 1 0 1 0 0 1 1 0]]
(separate zero? coll)
=> [(1 1 1 1 1) (0 0 0 0)])
Returns a vector of `[(filter pred coll) (remove pred coll)]`. ```clojure (let [coll [1 1 0 1 0 0 1 1 0]] (separate zero? coll) => [(1 1 1 1 1) (0 0 0 0)]) ```
(silence target expr)(silence substitute target expr)Returns substitute if expr raises an exception that matches
target.
If not provided, substitute is nil.
Target can be:
(silence ArithmeticException (/ 1 0))
=> nil
(silence "Divide by zero" (/ 1 0))
=> nil
(silence [ArithmeticException]
(do (println "watch out !")
(/ 1 0)))
;; watch out !
=> nil
(silence :substitute
(fn [x]
(isa? (class x) ArithmeticException))
(/ 1 0))
=> :substitute
Returns `substitute` if `expr` raises an exception that matches
`target`.
If not provided, `substitute` is `nil`.
Target can be:
- a class
- a sequence of classes
- a predicate
- a string
```clojure
(silence ArithmeticException (/ 1 0))
=> nil
(silence "Divide by zero" (/ 1 0))
=> nil
(silence [ArithmeticException]
(do (println "watch out !")
(/ 1 0)))
;; watch out !
=> nil
(silence :substitute
(fn [x]
(isa? (class x) ArithmeticException))
(/ 1 0))
=> :substitute
```(slice delimiter?
coll
&
{:keys [include-delimiter include-empty]
:or {include-delimiter false include-empty false}})Slice a seq using a delimiter predicate. There are two options:
- :include-delimiter false | :left | :right
whether to include the delimiter and where
- :include-empty true | false
whether to create empty seqs between
successive delimiters
(let [coll [1 1 0 1 0 0 1 1]]
;; the default
(slice zero? coll) ;; by default, :include-delimiter false,
:include-empty false
=> ((1 1) (1) (1 1))
(slice zero? coll :include-empty true)
=> ((1 1) (1) () (1 1))
(slice zero? coll :include-delimiter :left)
=> ((1 1) (0 1) (0 1 1))
(slice zero? coll :include-delimiter :right)
=> ((1 1 0) (1 0) (1 1))
(slice zero? coll :include-delimiter :right :include-empty true)
=> ((1 1 0) (1 0) (0) (1 1))
)
Slice a seq using a delimiter predicate. There are two options:
```
- :include-delimiter false | :left | :right
whether to include the delimiter and where
- :include-empty true | false
whether to create empty seqs between
successive delimiters
```
```clojure
(let [coll [1 1 0 1 0 0 1 1]]
;; the default
(slice zero? coll) ;; by default, :include-delimiter false,
:include-empty false
=> ((1 1) (1) (1 1))
(slice zero? coll :include-empty true)
=> ((1 1) (1) () (1 1))
(slice zero? coll :include-delimiter :left)
=> ((1 1) (0 1) (0 1 1))
(slice zero? coll :include-delimiter :right)
=> ((1 1 0) (1 0) (1 1))
(slice zero? coll :include-delimiter :right :include-empty true)
=> ((1 1 0) (1 0) (0) (1 1))
)
```(split-map m & kss)Returns a series of maps built by splitting m along each sequence
of keys in kss: the first map has (first kss) as keys, the second
one (second kss), etc ... while the last map has the remaining keys
from m.
Returns a series of maps built by splitting `m` along each sequence of keys in `kss`: the first map has `(first kss)` as keys, the second one `(second kss)`, etc ... while the last map has the remaining keys from `m`.
(static-method class name parameter-types)Returns a static method by reflection.
Returns a static method by reflection.
(submap? map1 map2)Determines whether map1 is a subset, keys and values wise, of
map2.
Determines whether `map1` is a subset, keys and values wise, of `map2`.
(tabulate s pad)Left-pad a string with pad, taking newlines into account.
Left-pad a string with `pad`, taking newlines into account.
(tree-seq-breadth branch? children root)Like tree-seq, but in breadth-first order
Like tree-seq, but in breadth-first order
(truncate s length)(truncate s length pad)Truncate a string with pad beyond a certain length. By default,
pad is "...".
Truncate a string with `pad` beyond a certain length. By default, `pad` is `"..."`.
(unqualify sym)(unqualify ns sym)Returns the unqualified form of sym.
Handles namespace aliases.
ns defaults to *ns*.
(unqualifiy 'clojure.lang.IRecord) => IRecord (unqualifiy 'my-ns/my-var) => my-var (unqualifiy 'alias/my-var) => alias/my-var (unqualifiy 'some.path.Class/staticMeth => Class/staticMeth
Returns the unqualified form of sym. Handles namespace aliases. `ns` defaults to `*ns*`. (unqualifiy 'clojure.lang.IRecord) => IRecord (unqualifiy 'my-ns/my-var) => my-var (unqualifiy 'alias/my-var) => alias/my-var (unqualifiy 'some.path.Class/staticMeth => Class/staticMeth
(unwrap-form sym code)Unwraps code if it is a form starting with sym. Returns code
otherwise.
(unwrap-form 'a '(a :z)) ; a
(->> '(a :z) (unwrap-form 'a) (unwrap-form 'a)) ; a
Unwraps `code` if it is a form starting with `sym`. Returns `code` otherwise. ```clojure (unwrap-form 'a '(a :z)) ; a (->> '(a :z) (unwrap-form 'a) (unwrap-form 'a)) ; a ```
(with-ns ns & body)Evaluates body in another namespace. ns is either a namespace
object or a symbol. Useful to define functions in namespaces other
than *ns*.
Evaluates body in another namespace. ns is either a namespace object or a symbol. Useful to define functions in namespaces other than `*ns*`.
(without-meta x)Sets meta of x to nil.
Sets meta of `x` to `nil`.
(words s)Splits a string around whitespaces.
Splits a string around whitespaces.
(wrap-form sym code)Wraps code in a form begining with sym unless it is already
the case.
(wrap-form 'a :z) ; => (a :z)
(->> :z (wrap-form 'a) (wrap-form 'a)) ; => (a :z)
Wraps `code` in a form begining with `sym` unless it is already the case. ```clojure (wrap-form 'a :z) ; => (a :z) (->> :z (wrap-form 'a) (wrap-form 'a)) ; => (a :z) ```
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |