Control Flow. Conditionals, loops, evaluation.

Primary

In this section: comment do doto let letfn

comment

Available since version 1.0 (alias of clojure.core/comment)

MACRO (comment & body)

Ignores body, yields nil.

See also: dunaj.dev/scratch

do

Available since version 1.0 (view source)

MACRO (do & exprs)

Evaluates the exprs in order and returns the value of the last. If no expressions are supplied, returns nil.

See also: doto, let

doto

Available since version 1.0 (alias of clojure.core/doto)

MACRO (doto x & forms)

Evaluates x then calls all of the methods and functions from forms with the value of x supplied at the front of the given arguments (as the second item in the form). The forms are evaluated in order. Returns x.

See also: let, do, while, dotimes, loop

let

Available since version 1.0 (alias of clojure.bootstrap/let)

MACRO (let bindings & body)

Evaluates the exprs in a lexical context in which the symbols in the binding-forms are bound to their respective init-exprs or parts therein.

See also: letfn, do

letfn

Available since version 1.0 (alias of clojure.core/letfn)

MACRO (letfn fnspecs & body)

fnspec ⇒ (fname [params*] exprs) or (fname ([params*] exprs)+)

Takes a vector of function specs and a body, and generates a set of bindings of functions to their names. All of the names are available in all of the definitions of the functions, as well as the body.

See also: let, dunaj.function/fn

Conditionals

case

Available since version 1.0 (alias of clojure.core/case)

MACRO (case e & clauses)

Takes an expression e, and a set of clauses.

Each clause can take the form of either:

  • test-constant result-expr

  • (test-constant1 …​​ test-constantN) result-expr

The test-constants are not evaluated. They must be compile-time literals, and need not be quoted. If the expression is equal to a test-constant, the corresponding result-expr is returned. A single default expression can follow the clauses, and its value will be returned if no clause matches. If no default expression is provided and no clause matches, an IllegalArgumentException is thrown.

Unlike cond and condp, case does a constant-time dispatch, the clauses are not considered sequentially. All manner of constant expressions are acceptable in case, including numbers, strings, symbols, keywords, and composites thereof. Note that since lists are used to group multiple constants that map to the same expression, a vector can be used to match a list if needed. The test-constants need not be all of the same type.

See also: cond, condp, if, when

cond

Available since version 1.0 (view source)

MACRO (cond & clauses)

Takes a set of test/expr pairs. It evaluates each test one at a time. If a test returns logical true, cond evaluates and returns the value of the corresponding expr and doesn’t evaluate any of the other tests or exprs. (cond) returns nil.

Treats last argument as an implicit else, if number of arguments is odd. If test is a vector, treats test as input to if-let.

cond supports several modifiers:

  • :let - treats expr as a binding vec and establishes custom locals that will be available for subsequent test/expr pairs

  • :when - immediatelly returns nil if expr does not evaluate to logical true. Continues with the rest of pairs otherwise.

  • :when-let - similar to :when, but treats expr as an input to when-let.

Note that modifiers cannot be at a tail position. Moreover, it is not idiomatic to use modifier as first test.

See also: condp, case, if, when

condp

Available since version 1.0 (alias of clojure.core/condp)

MACRO (condp pred expr & clauses)

Takes a binary predicate pred, an expression expr, and a set of clauses. Each clause can take the form of either:

  • test-expr result-expr

  • test-expr :>> result-fn (Note :>> is an ordinary keyword.)

For each clause, (pred test-expr expr) is evaluated. If it returns logical true, the clause is a match. If a binary clause matches, the result-expr is returned, if a ternary clause matches, its result-fn, which must be a unary function, is called with the result of the predicate as its argument, the result of that call being the return value of condp.

A single default expression can follow the clauses, and its value will be returned if no clause matches. If no default expression is provided and no clause matches, an IllegalArgumentException is thrown.

See also: cond, if, when, case

if

Available since version 1.0 (view source)

MACRO (if & args)

Evaluates test. If not the singular values nil or false, evaluates and yields then, otherwise, evaluates and yields else. If else is not supplied it defaults to nil. All of the other conditionals in are based upon the same logic, that is, nil and false constitute logical falsity, and everything else constitutes logical truth, and those meanings apply throughout. If tests for primitive boolean values, not the boxed ones. If your host supports boxed booleans, do not use them in if.

See also: when, cond, if-let, if-not, if-some

if-let

Available since version 1.0 (view source)

MACRO (if-let bindings then)  (if-let bindings then else & oldform)

binding ⇒ binding-form test

If all tests are logical true, evaluates then with binding-forms bound to the value of tests, if not, yields else. Accepts multiple bindings.

Supports :let modifier as binding-form, in which case it treats a corresponding test as a bindings vec. This is used to introduce locals that are not subject to the trueness comparison.

else clause must not use any binding-form or any custom locals created with :let modifier in any way.

A subset of the bound locals may be visible in the else expr. Do not rely on this behaviour.

See also: when-let, cond, if, if-not, if-some

if-not

Available since version 1.0 (alias of clojure.core/if-not)

MACRO (if-not test then)  (if-not test then else)

Evaluates test. If logical false, evaluates and returns then expr, otherwise else expr, if supplied, else nil.

See also: when-not, cond, if-let, if, if-some

if-some

Available since version 1.0 (alias of clojure.core/if-some)

MACRO (if-some bindings then)  (if-some bindings then else & oldform)

bindings ⇒ binding-form test

If test is not nil, evaluates then with binding-form bound to the value of test, if not, yields else.

when

Available since version 1.0 (alias of clojure.core/when)

MACRO (when test & body)

Evaluates test. If logical true, evaluates body in an implicit do.

when-let

Available since version 1.0 (view source)

MACRO (when-let bindings & body)

binding ⇒ binding-form test

When test is logically true, evaluates body with binding-form bound to the value of test. Accepts multiple bindings. Evaluates body only if all test are logically true.

Supports :let modifier as binding-form, in which case it treats a corresponding test as a bindings vec. This is used to introduce locals that are not subject to the trueness comparison.

See also: if-let, when, if, cond, case

when-not

Available since version 1.0 (alias of clojure.core/when-not)

MACRO (when-not test & body)

Evaluates test. If logical false, evaluates body in an implicit do.

See also: if-not, when, if, cond

when-some

Available since version 1.0 (alias of clojure.core/when-some)

MACRO (when-some bindings & body)

bindings ⇒ binding-form test

When test is not nil, evaluates body with binding-form bound to the value of test.

Iteration

In this section: dotimes loop recur while

dotimes

Available since version 1.0 (alias of clojure.core/dotimes)

MACRO (dotimes bindings & body)

bindings ⇒ name n

Repeatedly executes body (presumably for side-effects) with name bound to integers from 0 through n-1.

See also: doto, while, loop

loop

Available since version 1.0 (view source)

MACRO (loop bindings & body)

Evaluates the body in a lexical context in which the symbols in the bindings are bound to their respective init-exprs or parts therein. Acts as a recur target. bindings vector supports type signatures.

See also: recur, while, dotimes

recur

Available since version 1.0 (view source)

MACRO (recur & args)

Evaluates the args in order, then, in parallel, rebinds the bindings of the recursion point to the values of the args. If the recursion point was a fn method, then it rebinds the params. If the recursion point was a loop, then it rebinds the loop bindings. Execution then jumps back to the recursion point. The recur expression must match the arity of the recursion point exactly. In particular, if the recursion point was the top of a variadic fn method, there is no gathering of rest args - a single seq (or null) should be passed. recur in other than a tail position is an error.

recur is the only non-stack-consuming looping construct in Clojure. There is no tail-call optimization and the use of self-calls for looping of unknown bounds is discouraged. recur is functional and its use in tail-position is verified by the compiler.

while

Available since version 1.0 (alias of clojure.core/while)

MACRO (while test & body)

Repeatedly executes body while test expression is true. Presumes some side-effect will cause test to become false/nil. Returns nil.

See also: loop, doto, dotimes

Evaluation

In this section: delay eval force quote retrying-delay

delay

Available since version 1.0 (alias of clojure.core/delay)

MACRO (delay & body)

Takes a body of expressions and yields an IDelay object that will invoke the body only the first time it is forced (with force or deref/@), and will cache the result and return it on all subsequent force calls.

eval

Available since version 1.0 (alias of clojure.core/eval)

Usage:
  • (eval form)

Type signature:
  • (Any) → Any

Evaluates the form data structure (not text!) and returns the result.

See also: quote

force

Available since version 1.0 (view source)

Usage:
  • (force x)

Type signature:
  • (Any) → Any

If x is a IDelay, returns the (possibly cached) value of its expression, else returns x.

quote

Available since version 1.0 (view source)

MACRO (quote form)

Returns the unevaluated form.

See also: eval

retrying-delay

Available since version 1.0 (view source)

not referred automatically

MACRO (retrying-delay & body)

Takes a body of expressions and yields a IDelay object that will invoke the body only the first time it is forced (with force or deref/@), and will cache the result and return it on all subsequent force calls. Retries body on subsequent call if it throwed previously. Does not clear locals like normal delay does.