Clojure Cheat Sheet

Clojure is a modern programming language that is designed to be simple, efficient, and highly expressive. It is a dialect of Lisp, which is a family of programming languages that are known for their powerful macro system and functional programming features. Clojure is a dynamic language that runs on the Java Virtual Machine (JVM), which means that it can take advantage of the vast ecosystem of Java libraries and tools.

Clojure also has a strong emphasis on simplicity and elegance. The language is designed to be concise and expressive, which makes it easy to write and read code. Clojure also provides a number of powerful abstractions, such as macros and higher-order functions, that allow developers to write code that is both concise and expressive.

This cheat sheet provides a quick reference for Clojure syntax and functions.

Data Types

Data TypeDescription
nilRepresents the absence of a value
booleanRepresents true or false
numberRepresents numeric values
stringRepresents text
keywordRepresents a unique identifier
symbolRepresents a reference to a value
listRepresents a sequence of values
vectorRepresents an ordered collection of values
mapRepresents a collection of key-value pairs
setRepresents a collection of unique values

Variables

SyntaxDescription
(def name value)Defines a new variable with the given name and value
(defn name [args] body)Defines a new function with the given name, arguments, and body
(let [bindings] body)Defines local variables with the given bindings and evaluates the body

Control Flow

SyntaxDescription
(if condition true-branch false-branch)Evaluates the true-branch if the condition is true, otherwise evaluates the false-branch
(when condition body)Evaluates the body if the condition is true
(cond [condition1 body1] [condition2 body2] ... [else body])Evaluates the body corresponding to the first condition that is true, or the else body if no conditions are true
(case value [pattern1 body1] [pattern2 body2] ... [default body])Evaluates the body corresponding to the first pattern that matches the value, or the default body if no patterns match

Functions

SyntaxDescription
(fn [args] body)Defines an anonymous function with the given arguments and body
(#(body %) arg)Defines an anonymous function with a single argument and body, and immediately applies it to the given argument
(apply function args)Calls the function with the given arguments as a list
(map function sequence)Applies the function to each element of the sequence and returns a new sequence
(reduce function sequence)Applies the function to the first two elements of the sequence, then to the result and the next element, and so on, returning the final result

Sequences

SyntaxDescription
(conj collection element)Adds the element to the collection
(first sequence)Returns the first element of the sequence
(rest sequence)Returns the sequence without the first element
(nth sequence index)Returns the element at the given index
(take n sequence)Returns the first n elements of the sequence
(drop n sequence)Returns the sequence without the first n elements
(sort sequence)Returns a new sequence with the elements sorted

References