Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

hashmap

Hashmaps as a collection benefit from the same syntactic sugar provided by the reader macro with the added support of some generic collection functions, get, set!, and clear!.

For retrieving items from a hashmap prefer the reader syntax (.) or use the get function.

(def m {:one "one", :two "two", "three" 3})
(and (= "one" m.:one)
    (= "two" m.:two)
    (= 3 (get m "three")))

;; => true

For setting key/value pairs on an existing hashmap prefer the reader syntax with the set! function or pass it the output of get.

(def m {:one "one", :two "two", "three" 3, :four 4})
(set! m.:one "changed1")
(set! m.:two "changed2")
(set! (get m "three") "changed3")
(and (= "changed1" m.:one)
    (= "changed2" m.:two)
    (= "changed3" (get m "three")))

(def four :four)
(set! m.~four "changed4")
(= "changed4" (get m :four))

;; => true

Clearing all keys is done with the clear! function.

(def m {:one "one", :two "two", "three" 3})
(clear! m)
(= (make-hash) m)

;; => true

** Hashmap keys that are not keyword types (not prefixed by a colon) can not be used with the reader syntax, which is why in the example the key "three" was used with the get function each time instead of using the reader syntax.

List of symbols:

hash-keys, hash-remove!, make-hash

hash-keys

Usage: (hash-keys hashmap)

Returns a vector of all the hashmaps keys. The keys will be unordered.

Example:

(def tst-hash {:key1  "val one" 'key2 "val two" "key3" "val three" \S "val S"})
(test::assert-equal 4 (len (hash-keys tst-hash)))
(test::assert-true (in? (hash-keys tst-hash) :key1) " Test :key1")
(test::assert-true (in? (hash-keys tst-hash) 'key2) " Test key2")
(test::assert-true (in? (hash-keys tst-hash) \S) " Test S")
(test::assert-true (in? (hash-keys tst-hash) "key3") " Test key3")
(test::assert-false (in? (hash-keys tst-hash) :key4))

hash-remove!

Usage: (hash-remove! hashmap key)

Remove a key from a hashmap. This is a destructive form!

Example:

(def tst-hash {:key1  "val one" 'key2 "val two" "key3" "val three" \S "val S"})
(test::assert-equal 4 (len (hash-keys tst-hash)))
(test::assert-equal "val one" tst-hash.:key1)
(test::assert-equal "val two" (get tst-hash 'key2))
(test::assert-equal "val three" (get tst-hash "key3"))
(test::assert-equal "val S" (get tst-hash \S))
(hash-remove! tst-hash 'key2)
(test::assert-equal 3 (len (hash-keys tst-hash)))
(test::assert-equal "val one" tst-hash.:key1)
(test::assert-equal "val three" (get tst-hash "key3"))
(test::assert-equal "val S" (get tst-hash \S))
(hash-remove! tst-hash :key1)
(test::assert-equal 2 (len (hash-keys tst-hash)))
(test::assert-equal "val three" (get tst-hash "key3"))
(test::assert-equal "val S" (get tst-hash \S))
(hash-remove! tst-hash "key3")
(test::assert-equal 1 (len (hash-keys tst-hash)))
(test::assert-equal "val S" (get tst-hash \S))
(hash-remove! tst-hash \S)
(test::assert-equal 0 (len (hash-keys tst-hash)))

make-hash

Usage: (make-hash associations?)

Make a new hash map.

If associations is provided (makes an empty map if not) then it is a list of pairs (key . value) that populate the initial map. Neither key nor value in the associations will be evaluated.

No Examples