Nithin Bekal

Posts About Notes Slides

Clojure

Installation:

brew install leiningen
lein repl

Example code:

(ref: Stuart Holloway: Introduction to Clojure)

(println "Hello, world")

(defn hello
  "Returns a greeting"
  [name]
  (str "Hello, " name))

;; vectors
(def v [42 :rabbit [1 2 3]])
(v 1) -> :rabbit
(peek v) -> [1 2 3]
(pop v) -> [42 :rabbit]
(subvec 1) -> [:rabbit [1 2 3]]

;; maps
(def m { :a 1 :b 2 :c 3 })
(m :b) -> 2
(:b m) -> 2
(keys m) -> (:a :b :c)
(assoc m :d 4 :c 42) -> {:d 4, :a 1, :b 2, :c 42}
(dissoc m :d) -> {:a 1 :b 2, :c 42}
(merge-with + m {:a 2 :b 3}) -> {:a 3 :b 5 :c 42}

(def user {:name "John Doe"
           :address {:zip 123456}})
(get-in user [:address :zip])
(assoc-in user [:address :zip] 123567)
-> {:name "John Doe", :address {:zip 123567}}

Clojure related bookmarks:

Books and tutorials

Videos:

Emacs integration