Clojure Day 1

Today I started reading through this book to learn some Clojure. It’s called Clojure for the Brave and True.

I thought I would make myself a little cheatsheet that grows as I progress through the book.

Forms

These are like the building blocks of the files.

1
"This string"
["Vectors" "of" "strings" "are" "a" "thing"]

Operators

All operations use the same structure. Given an operator operator and operands op1, op2, ... opN, you use the following structure:

(operator op1 op2 ... opN)

Comments

; This is a single line comment

Control Flow

if

(if some-boolean-form
    then-form
    an-optional-else form)

do

(if some-boolean-form
    (do "Some truthy thing 1"
        "Some truthy thing 2")
    (do "Some falsey thing 1"
        "Some falsey thing 2"))

when

(when some-true-form
    (println "True thing found"))

Booleans

Some operators:

Save state

(def varName value)

Data Structures

Numerical Things

Strings

Maps

; An empty map
{}

; A map with keywords for keys
{:firstname "Andy"
 :lastname "Lu"}
 
; Mapping a string to a function
{"Totally-legal" +}

; Nested Maps
{:name {:first "Andy" :last "Lu"}}

; Hash Map
(hash-map :firstname "Andy" :lastname "Lu")

Reading from maps

Keywords

Vectors

Lists

Sets


Thanks for reading this post! Comments, questions, and feedback are always welcome.