Using associative arrays

How to:

In Clojure, creating and manipulating associative arrays (hash maps) is straightforward. Let’s dive in with examples.

To create a hash map:

(def my-map {:name "Alex" :age 30})

You can retrieve a value by specifying its key:

(get my-map :name)
;; "Alex"

Or, more idiomatically, you can use the key as a function:

(:name my-map)
;; "Alex"

Adding or updating entries is simple:

(def updated-map (assoc my-map :location "New York"))
;; {:name "Alex", :age 30, :location "New York"}

(def incremented-age (update my-map :age inc))
;; {:name "Alex", :age 31}

For removing keys, use dissoc:

(def removed-age (dissoc my-map :age))
;; {:name "Alex"}

To iterate over a map:

(doseq [[k v] my-map] (println k "->" v))
;; :name -> Alex
;; :age -> 30

And for conditional access, find returns a key-value pair if the key exists:

(find my-map :age)
;; [:age 30]

Deep Dive

Associative arrays in Clojure, also commonly referred to as hash maps, are incredibly versatile and efficient for managing key-value based data. They are part of Clojure’s rich collection library, deeply rooted in the language’s philosophy of immutability and functional programming. Unlike arrays or lists that require O(n) time complexity for accessing elements, hash maps provide near-constant time complexity for access, making them highly efficient for look-up operations.

One might argue that vectors in Clojure could serve a similar purpose through indexed access, but hash maps shine when it comes to dealing with non-sequential and labeled data, where the key provides a meaningful descriptor rather than an arbitrary index.

Unique to Clojure (and its Lisp heritage), associative arrays are first-class citizens, meaning they can be directly manipulated, passed around functions, and more, without needing special syntax or access methods. This design decision reinforces Clojure’s emphasis on simplicity and power.

While hash maps are incredibly useful, it’s worth mentioning that for very large datasets or scenarios where keys are highly dynamic (constant addition and removal), alternative data structures or databases might offer better performance and flexibility. However, for most typical use cases within the realm of Clojure applications, associative arrays provide a robust and efficient means of data management.