Clojure Quick Referenceλ︎
The basic Clojure syntax and a few common functions you should probably learn first.
Also see the Clojure.org cheat-sheet
The first element in a list,
(), is treated as a call to a function. This is known as prefix notation which greatly simplifies Clojure syntax and makes mathematical expressions completely deterministic, eliminating the need for operator precedence.
Functions contain doc-strings and you can ask for a functions documentation, or show the source code.
Clojure is a dynamically typed language, it is also strongly typed (everything is a type, but you dont have to express the type in your code). The type of anything in Clojure can be returned.
Modeling data with Collection typesλ︎
Clojure has 4 main collection types, all immutable (cannot change once created) and can contain any Clojure types.
Defining names for values (vars)λ︎
Names can be bound to any values, simple values like numbers, collections or functions. A convenient way to refer to value in your code.
map reduce filterλ︎
Common functions for iterating through a collection / sequence of values
Using data structuresλ︎
inc function, increment all the numbers in a vector
map function is roughly equivalent to the following expression
conj function creates a new collection by combining a collection and one or more values.
Defining custom functionsλ︎
Function definitions can also be used within other expressions, useful for mapping custom functions over a collection
; Using the division function (/ ) shows another interesting characteristic of Clojure, the fact that it is lazy. This is not lazy in a bad way, but lazy evaluation of data structures. This actually helps to make clojure more efficient at dealing with data, especially very large data sets.
Using a Ratio means that the mathematical division is not evaluated when using whole numbers (Integers) that would produce a decimal number. If you do return a decimal number then what precision of decimal are you expecting. By specifying one or more of the numbers as a decimal value you are giving Clojure a precision to infer and can therefore provide a specific decimal result.
new are Clojure functions that create a Java object. This allows you to use values from Java constants, i.e. PI is a static double from the java.lang.Math object
Also call static and instance methods from Java objects.