Introduction to
A bit about clojure
● Since 2007. Rich Hikey
● LISP - Code represented as lists
○ (+ 1 1)
○ (println (if (= 0 0)“Right” “Wrong”))
(operator operand1 operand2 ... operandn)
● Dynamically Typed, Runs on JVM
Setting up Clojure
Java (1.6+)
Leiningen (Clojure build tool, automate projects)
sudo apt-get install leiningen
brew install leiningen
REPL - Read-Eval-Print-Loop
$ lein repl
=> (println “Hello World!”)
=> (+ 1 3)
=> (import [java.util Date])
=> (Date.)
Data types
● Lists ( 1 2 3 4 5)
● Vectors [ 1 2 3 4 5]
● Maps {:a 1 :b 2 :c 3 “d” 4 :e “val”}
● Sets #{1 2 c 3 d}
Defining Variable and Functions
(def counties [“Nairobi”, “Kiambu”, “Nyeri”] )
(def total-number 47)
(defn square[y] (* y y))
(square 2) ; => 4
((fn [x] (* y y)) 3) ; => 4
(#(* % %) 3) ; => 9
Syntax Comparison
myObj.doSomething(anArg);
(do-something my-obj an-arg)
Common Data Structure Functions
Clojure
(conj [1 2 3] 1)
; => [1 2 3 1]
(map inc [1 2 3])
; => [2 3 4]
Python
x = [1 2 3]
x.append(1)
for i in range(len([1 2 3])):
test[i] += 1
Common Functions
Clojure
(filter even? [1 2 3 4])
; => (2 4)
(sort-by count [“aaa” “bb” “c”])
; => (“c” “bb” “aaa” )
sorted([“aaa” “bb” “c”], key=lambda x: x.count, reverse=True)
Python
[x for x in [1 2 3 4] if x % 2 == 0]
Common Functions
Clojure
(assoc {:a 1 :b 2 :c 3} :c 4)
; => {:a 1 :b 2 :c 4}
(group-by count [“aa” “bb” “c”])
; => {1 [“c”] 2 [“bb” “aa”] }
Python
mydict = {“a” 1 “b” 2 “c” 3}
for key in mydict.keys():
if key == “d”
mydict[key] = 4
…...
Common Functions
Clojure
(println (if (= 1 1) “correct” “wrong”) )
Python
if 1 == 1:
print (“correct”)
print (“wrong”)
merge
empty?
sorted?
if-not
max
max-key
loop
doseq
let
when
max
max-key
sorted-map
sorted-map-by
let
zipmap
get
zero?
Interoperability
Java
(def a (ArrayList.))
(.add a 1)
(.add a 2)
Javascript
(def a (js/Array.))
(.push a 1)
(.push a 2)
(.log js/console “Hello”)
(.alert js/window “Alert!”)
Useful Resources
● Docs https://clojuredocs.org
● The Joy of Clojure
● Clojure for the brave and true - http://www.
braveclojure.com/
● Om - A ClojureScript interface to Facebook's
React.
● How to be a frontend Ninja https://github.
com/om-ninja
References
Zach Oakes - Introduction to Clojure http://oakleaf.info/intro-to-clojure.odp
Clojure for the brave http://www.braveclojure.com/getting-started/
Joy of Clojure

Brief intro to clojure