Maps In Clojure

I was planning on looking into Clojure’s map function, the second of the Big Three Higher-Order Functions. But looking over my Clojure API page, I noticed there were a few functions with the word “map” in it that I had not gone over yet that deal with the map data structure.

Just as “map” is a noun and a verb in Clojure, the map data structure goes by different names in different programming languages: associative array, map, dictionary, symbol table, hash, hash table.

Anyway, I have looked at some of these functions earlier, but here are some more functions dealing with maps:

user> (sorted-map :a "value a" :b "value b" :c 33)
{:a "value a", :b "value b", :c 33}
user> (sorted-map :a "value a" :c "value c" :b 33)
{:a "value a", :b 33, :c "value c"}
user> (hash-map :a "value a" :c "value c" :b 33)
{:c "value c", :b 33, :a "value a"}
user> (array-map :a "value a" :c "value c" :b 33)
{:a "value a", :c "value c", :b 33}
user> (array-map :a "value a" :c "value c" :b 33 :c "more c")
{:a "value a", :c "more c", :b 33}
; honestly, I am not clear what he order is
user> (reverse (array-map :a "value a" :c "value c" :b 33 :c "more c"))
([:b 33] [:c "more c"] [:a "value a"])
user> (first (array-map :a "value a" :c "value c" :b 33 :c "more c"))
[:a "value a"]
user> (first (reverse (array-map :a "value a" :c "value c" :b 33 :c "more c")))
[:b 33]

user> (hash-map :a "value a" :d "what is d?" :c "value c" :b 33)
{:c "value c", :b 33, :d "what is d?", :a "value a"}
user> (hash-map :a "value a" :c "value c" :b 33 :b "again")
{:c "value c", :b "again", :a "value a"}
user> (hash-map :a "value a" :c "value c" :b 33 :b "again" :b "Third b")
{:c "value c", :b "Third b", :a "value a"}
user> (sorted-map-by > :a "value a" :c "value c" :b 33)
Execution error (ClassCastException) at user/eval5630 (form-init12885185727845486110.clj:148).                                                                           
class clojure.lang.Keyword cannot be cast to class java.lang.Number (clojure.lang.Keyword is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap')   
user> (sorted-map-by compare :a "value a" :c "value c" :b 33)
{:a "value a", :b 33, :c "value c"}
user> (sorted-map-by compare :a "value a" :c "value c" :b 33 :c "more c")
{:a "value a", :b 33, :c "more c"}

user> (reverse (sorted-map-by compare :a "value a" :c "value c" :b 33))
([:c "value c"] [:b 33] [:a "value a"])
user> (sorted-map-by < 2 "value a" 33 "value c" 4 "v4")
{2 "value a", 4 "v4", 33 "value c"}
user> (sorted-map-by > 2 "value a" 33 "value c" 4 "v4")
{33 "value c", 4 "v4", 2 "value a"}
user> (sorted-map-by > 2 "value a" 33 "value c" 4 "v4" 33 "more 33")
{33 "more 33", 4 "v4", 2 "value a"}


user> (def empty-hm (hash-map))
#'user/empty-hm
user> (def empty-am (array-map))
#'user/empty-am
user> (def empty-sm (sorted-map))
#'user/empty-sm
user> 
; zipmap will create a map from a collection of keys and a collection of values
user> (zipmap [:a :b :c] [1 2 3])
{:a 1, :b 2, :c 3}
user> (zipmap  '(:a :b :c) '(1 2 3))
{:a 1, :b 2, :c 3}
; does not work with sets
user> (zipmap  #{:a :b :c} #{1 2 3})
{:c 1, :b 3, :a 2}
user> 
user> (map? (zipmap  '(:a :b :c) '(1 2 3)))
true
user> (map? (zipmap  #{:a :b :c} #{1 2 3}))
true
user> (map? #{1 2 3})
false
user>

You’re welcome.

There are 536 words in this article.