Clojure core environment and runtime library providing a dynamic, general-purpose programming language on the JVM.
—
Advanced control structures, conditional execution, and macro system for metaprogramming. Clojure provides both special forms and macros for controlling program flow.
Control structures for conditional program execution.
(if test then else?)
;; Special form: evaluates test, returns then if truthy, else if falsy
;; else is optional (defaults to nil)
;; Returns: Object
(if-not test then else?)
;; Macro: inverted conditional, returns then if test is falsy
;; Returns: Object
(when test & body)
;; Macro: executes body when test is truthy
;; Returns: result of last body expression or nil
(when-not test & body)
;; Macro: executes body when test is falsy
;; Returns: result of last body expression or nil
(when-let bindings & body)
;; Macro: binds and executes body if binding value is truthy
;; bindings: [symbol test-expr]
;; Returns: result of last body expression or nil
(when-first bindings & body)
;; Macro: binds first item of sequence and executes body
;; bindings: [symbol sequence-expr]
;; Returns: result of last body expression or nil
(cond & clauses)
;; Macro: multi-branch conditional
;; clauses: test1 expr1 test2 expr2 ... :else default-expr
;; Returns: result of first matching expression
(condp pred expr & clauses)
;; Macro: conditional with predicate applied to expr
;; clauses: test-val result-expr test-val result-expr ... default-expr?
;; Returns: result expression for first (pred test-val expr) that's truthy
(case expr & clauses)
;; Macro: constant-time dispatch on expr value
;; clauses: const1 result1 const2 result2 ... default-expr?
;; Returns: result for matching constantConstructs for repetitive execution and iteration.
(loop [bindings*] & body)
;; Special form: establishes recursion point with local bindings
;; bindings: [symbol init-expr symbol init-expr ...]
;; Use recur to loop back with new values
;; Returns: result of last body expression
(recur & exprs)
;; Special form: recursively calls enclosing fn or loop
;; Must be in tail position
;; exprs become new parameter/binding values
;; Returns: never returns (transfers control)
(while test & body)
;; Macro: executes body repeatedly while test is truthy
;; Returns: nil
(dotimes [name n] & body)
;; Macro: executes body n times with name bound to 0, 1, ..., n-1
;; Returns: nil
(doseq [seq-exprs*] & body)
;; Macro: executes body for each item in sequences
;; seq-exprs: [name coll name coll :let [binding] :when test :while test]
;; Returns: nil
(for [seq-exprs*] body-expr)
;; Macro: list comprehension returning lazy sequence
;; seq-exprs: same as doseq but returns sequence of body-expr results
;; Returns: clojure.lang.LazySeqConstructs for handling exceptions and errors.
(try expr* catch* finally?)
;; Special form: exception handling
;; catch: (catch ExceptionClass binding-symbol catch-expr)
;; finally: (finally finally-expr)
;; Returns: result of expr or catch-expr
(throw expr)
;; Special form: throws exception
;; expr should evaluate to Throwable
;; Returns: never returns
(assert expr)
(assert expr message)
;; Macro: throws AssertionError if expr is falsy
;; Returns: true if assertion passes
(with-open [bindings*] & body)
;; Macro: ensures resources are closed after use
;; bindings: [name init-expr name init-expr ...]
;; Resources must implement java.io.Closeable
;; Returns: result of last body expressionMacros for readable function composition and data transformation pipelines.
(-> x & forms)
;; Macro: thread-first - inserts x as first argument to each form
;; (-> x (f a) (g b)) expands to (g (f x a) b)
;; Returns: result of final form
(->> x & forms)
;; Macro: thread-last - inserts x as last argument to each form
;; (->> x (f a) (g b)) expands to (g b (f a x))
;; Returns: result of final form
(as-> expr name & forms)
;; Macro: thread with explicit binding - binds result to name in each form
;; (as-> x $ (f $ y) (g z $)) where $ is explicitly positioned
;; Returns: result of final form
(some-> expr & forms)
;; Macro: thread-first with nil safety - stops if any form returns nil
;; Returns: result of final form or nil
(some->> expr & forms)
;; Macro: thread-last with nil safety - stops if any form returns nil
;; Returns: result of final form or nil
(cond-> expr & clauses)
;; Macro: conditional threading - only applies forms when test is truthy
;; clauses: test1 form1 test2 form2 ...
;; Returns: final transformed expr
(cond->> expr & clauses)
;; Macro: conditional thread-last
;; Returns: final transformed exprTools for creating and working with macros.
(defmacro name doc-string? attr-map? [params*] body)
;; Special form: defines a macro
;; Macro receives unevaluated arguments and returns code to execute
;; Returns: clojure.lang.Var
(macroexpand form)
;; Function: recursively expands all macros in form
;; Returns: expanded form
(macroexpand-1 form)
;; Function: expands outermost macro in form once
;; Returns: expanded form or original if no macro
(quote form)
;; Special form: returns form without evaluation (same as 'form)
;; Returns: unevaluated form
(unquote form)
;; Special form: evaluates form within syntax-quote (same as ~form)
;; Only valid within syntax-quote
;; Returns: evaluated form
(unquote-splicing form)
;; Special form: splices sequence into containing form (same as ~@form)
;; Only valid within syntax-quote
;; Returns: spliced sequence
;; Syntax-quote (backtick `) and unquote
`(form ~expr ~@seq-expr) ; Template with unquoting
'(form expr seq-expr) ; Simple quote - no unquotingFunctions for runtime evaluation and code generation.
(eval form)
;; Function: evaluates form in current namespace context
;; Returns: result of evaluation
(load-string s)
;; Function: loads and evaluates Clojure code from string
;; Returns: result of last expression
(read-string s)
;; Function: reads one object from string (doesn't evaluate)
;; Returns: parsed object
(compile namespace-symbol)
;; Function: compiles namespace to class files
;; Returns: namespace symbolUsage Examples:
;; Conditional execution
(if (> 5 3) "yes" "no") ; => "yes"
(when (even? 4) "four is even") ; => "four is even"
(cond
(< 5 3) "less"
(> 5 3) "greater"
:else "equal") ; => "greater"
;; Loops
(loop [i 0 acc []]
(if (< i 3)
(recur (inc i) (conj acc i))
acc)) ; => [0 1 2]
(dotimes [i 3] (println i)) ; Prints 0, 1, 2
(doseq [x [1 2 3]] (println (* x x))) ; Prints 1, 4, 9
(for [x [1 2 3] y [10 20]]
(+ x y)) ; => (11 21 12 22 13 23)
;; Threading macros
(-> 5
(+ 3)
(* 2)) ; => 16, equivalent to (* (+ 5 3) 2)
(->> [1 2 3 4]
(map inc)
(filter even?)) ; => (2 4), equivalent to (filter even? (map inc [1 2 3 4]))
;; Exception handling
(try
(/ 1 0)
(catch ArithmeticException e
"Cannot divide by zero")) ; => "Cannot divide by zero"
;; Macro definition
(defmacro unless [test & body]
`(when (not ~test)
~@body))
(unless false (println "This prints")) ; Prints "This prints"
;; Macro expansion
(macroexpand '(when true (println "hi")))
; => (if true (do (println "hi")))
;; Evaluation
(eval '(+ 1 2 3)) ; => 6
(read-string "(+ 1 2)") ; => (+ 1 2) [not evaluated]
(load-string "(+ 1 2)") ; => 3 [evaluated]Install with Tessl CLI
npx tessl i tessl/maven-org-clojure--clojure