Skip to content

Day 90 - What will be my next role

Considering my options for my next role. There have been a lot of really interesting roles I have interviewed for. There is one that seems to require the widest use of my skills.

4Clojure Challenge #11 - conj with mapsλ︎

Exploring the joining of maps and vectors into a map.

Also explored some possible error conditions that can occur when using maps, such as incomplete pairs of keys and values.

Design Journal: 4Clojure #11 - conj with maps

src/four_clojure/011-maps-conj.clj
;; # 011 - Maps: conj
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Difficulty:  Elementary
;; Topics:

;; When operating on a map, the conj function returns a new map with one or more key-value pairs "added".

;; (= {:a 1, :b 2, :c 3} (conj {:a 1} __ [:c 3]))


;; Deconstruct the problem
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Simply fill in a collection that contains the right values.

;; Maps need to include values in pairs, a key and a value that the key is associated with.
;; Maps can conj from values in a vector, so long as the vector contains key/value pairs

;; So this expression works

(conj {:a 1} [:b 2] [:c 3])
;; => {:a 1, :b 2, :c 3}

;; and of course using a map with a key value pair would also work

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


;; Using a vector that contains an a key without a value will cause and error.

(conj {:a 1} [:b 2 :d] [:c 3])
;; java.lang.IllegalArgumentException
;; Vector arg to map conj must be a pair

;; The same expression with a map this time does return something, which is surprising.
;; Although evaluating it also throws a reader exception.
;; This is because second map in the expression has a syntax error,
;; {:b 2 :d} is not valid syntax.

(conj {:a 1} {:b 2 :d} [:c 3])
;; => [:c 3]
;; clojure.lang.LispReader$ReaderException
;; java.lang.RuntimeException: Unmatched delimiter: )


;; Trying to construct a map using the hash-map function and an incorrect set of key value pairs
;; also creates an error.  However, this time its much clearer as to the error.
(conj {:a 1} (hash-map :b 2 :d) [:c 3])

(hash-map :b 2 :d)
;; java.lang.IllegalArgumentException
;; No value supplied for key: :d

;; NOTE: The above code has been evaluated with Clojure 1.9.  Version 1.10 may have improved error messages when using ill-formed maps.

;; Answers summary
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Simplest answers

[:b 2]
{:b 2}
(hash-map :b 2)

;; Overthought answers

;; Least valuable answer

Thank you.

🌐 Practical.li Website

Practical.li GitHub Org practicalli-johnny profile

@practicalli@clj.social @practical_li