English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

Developing a Clojure Edge


Everything can be modeled with vectors and maps.

Clojure collections are functions (associative).

Defining data transforms.


Vectors are used in preference to lists for sequential data.

Order 1 lookup by index



Common transforms


A Clojure hashmap is like a dictionary, but is immutable and shares structure.  Instead of modifying a hashmap in place, associating a key/value returns a new hashmap efficiently.  

Hashmaps have a special form {} to declare them:

(def city {"Seattle" "cloudy"

          "Phoenix" "sunny"

          "New York" "busy"})

Most languages provide a syntax for accessing values in a dictionary by key:


Clojure takes a different approach; hashmaps are functions that you call to get the value associated with a key:

(city "Seattle")

-> "cloudy"

Thus hashmaps can be passed as a function to higher order functions such as map:

(map city ["Seattle" "Phoenix"])

-> ("cloudy" "sunny")

A sequence of values returned from the city hashmap is the result of calling city across a sequence of keys.

All clojure collections are associative and can be called as functions.  Vectors are assoicative by index, Maps are associative by key, Sets are associative by membership.  The idiomatic way to write contains? or one-of? is to use a set:

(#{1 2 3} 2)

=> 2

(returns a truthy value)

The same leverage we experienced with sequences applies to hashmaps.  Hashmaps as functions is a subtle and powerful abstraction.  Hashmap transformation functions provide leverage from composability.

Maps are used in preference to objects for representing structured data.  They are suitable for deeply nested representations.

(def world {:drones [{:location [5 5]} {:location [2 9]}]

                 :hospitals [{:location [3 4]]}]})

We can represent the world as a map containing a vector of drones and a vector of hospitals.  Drones and hospitals themselves are maps with one key, location, which is a vector of coordinates.

assoc associates a key value pair:

(assoc {} :location [5 5])

=> {:location [5 5]}

(assoc world :remotes [])

dissoc disassociates a key:

(dissoc {:location [5 5]} :location)

=> {}

(dissoc world :hospitals)

update-in uses a function to update a value in a nested associated structure:

(update-in {:location [5 5]} [:location 0] inc)

=> {:location [6 5]}

(update-in world [:drones 1 :location 0] inc)



(zipmap [:a :b :c] [1 2 3])

=> {:c 3, :b 2, :a 1}

Rather than define structure, use structure.

PersistentQueue is good to know about, there is no special form syntax, you have to use interop to construct the class directly so it is often overlooked.


(into {} [[:a 1] [:b 2] [:c 3]])

=> {:a 1, :b 2, :c 3}

(into [1 2] [3 4 5])

=> [1 2 3 4 5]



[[1 2 3]

[4 5 6]]


[[1 4]

[2 5]

[3 6]]


(vec (apply map vector _))

There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.