Skip to content

Latest commit

 

History

History
105 lines (68 loc) · 1.04 KB

guide.md

File metadata and controls

105 lines (68 loc) · 1.04 KB

basic of clojure

hello world

(println "hello world")

working with sequences

an empty sequence

'()

adding an element to the list

(conj '() 1)

the order of inserting into the list is important:

(conj '() 1 2 3 4)

variables

(def variable 1)
(def other-symbols-as-well true)
(def is-true? true)

let's try mutating the state*:

(def listt '(1 2 3))

(cons 4 listt)

what is the value of listt?

idiomatic use

(def listt (cons 4 listt))

is not very idiomatic.

What do you want to do with it?

Partial functions

(defn add [a b] (+ a b))

(def add2 (partial add 2))

(add2 4)

Functions are first-class citizens

(def o {:a 1 :b identity})

((:b o) 5)
(defn applier[f coll]
 (map f coll))

(applier identity '(1 2 3))

another example:

(applier inc '(1 2 3))

(inc 0)

(applier dec '(1 2 3))

(dec 0)

it could be simplified to this:

(def applier map)