Clojure Quick Reference

The basic Clojure syntax and a few common functions you should probably learn first.

The examples are editable (using an embedded REPL) so feel free to experiment and watch as the return value changes as you change the code. Reload the page if you want to reset all the code back to the starting point.

Install Clojure on your computer if you want to experiment even further.

Want to go deeper already?

Watch the Clojure language video series by Brian Will for a good introduction to key parts of the language. Or practice Clojure by completing simple challenges on and then watching how Practicalli solved them.

Calling functions

The first element in a list, (), is treated as a call to a function. The examples show how to call functions with multiple arguments.

(+ 1 2)
(+ 3 (* 2 (- 7 2) 4) (/ 16 4))
(str "Clojure is " (- 2020 2007) " years old")
(inc 1)
(map inc [1 2 3 4 5])
(filter odd? (range 11))

Prefix notation and parens

Hugging code with () is a simple syntax to define the scope of code expressions. No additional ;, , or spaces are required.

Treating the first element of a list as a function call is referred to as prefix notation, which greatly simplifies Clojure syntax. Prefix notation makes mathematical expressions completely deterministic, eliminating the need for operator precedence.

Understanding functions

Functions contain doc-strings describing what that function does. The doc function returns the doc-string of a particular function. Most editors also support viewing of doc-strings as well as jumping to function definitions to view the source code

(doc doc)

Modeling data with Collection types

Clojure has 4 main collection types, all immutable (cannot change once created) and can contain any Clojure types.

(str "lists used mainly " (* 2 2) " " :code)
[0 "indexed" :array (* 2 2) "random-access"]
{:key "value" "hash-map" "also referred to as dictionary"}
#{1 2 3 4 "unique" "set" "of" "values" "unordered" (* 3 9)}

Persistent data types

To change data in Clojure new copies are created rather than changing existing values. The copies of data will share values from the original data that are common in both. This sharing is called persistent data types and enables immutable data to be used efficiently.

Defining names for values (vars)

Names can be bound to any values, from simple values like numbers, collections or even function calls. Using def is convenient way to create names for values that are shared in your code.

evaluating a name will return the value it is bound to.

(def public-health-data
  [{:date "2020-01-01" :confirmed-cases 23814 :recovery-percent 15}
   {:date "2020-01-02" :confirmed-cases 24329 :recovery-percent 14}
   {:date "2020-01-03" :confirmed-cases 25057 :recovery-percent 12}])


def for shared values, let for locally scoped values

let function is used to bind names to values locally, such as within a function definition. Names bound with def have namespace scope so can be used with any code in that namespace.

Using data structures

Using the map and inc function, increment all the numbers in a vector

(map inc [1 2 3 4 5])

The above map function is roughly equivalent to the following expression

(conj [] (inc 1) (inc 2) (inc 3) (inc 4) (inc 5))

The conj function creates a new collection by combining a collecion and one or more values.

map reduce filter are common functions for iterating through a collection / sequence of values

(map * [1 3 5 8 13 21] [3 5 8 13 21 34])
(filter even? [1 3 5 8 13 21 34])
(reduce + [31 28 30 31 30 31])
(empty? [])

Defining custom functions

(defn square-of
  "Calculates the square of a given number"
  (* number number))

(square-of 9)

Function definitions can also be used within other expressions, useful for mapping custom functions over a collection

(map (fn [x] (* x x)) [1 2 3 4 5])

Host Interoperability

The REPL in this web page is running inside a JavaScript engine, so JavaScript functions can be used from within ClojureScript code (ClojureScript is Clojure that runs in JavaScript environments).

In the box below, replace () with (js/alert "I am a pop-up alert")


Java libraries in Clojure

java.lang library is available in Clojure by default and many other Java methods can be included by using their full name, e.g. (java.lang.Date.) will return the current date.

Next steps

Install Clojure on your computer if you want to experiment even further or keep on reading more about Clojure.

results matching ""

    No results matching ""