Skip to content

Commit

Permalink
011: Maps - conj
Browse files Browse the repository at this point in the history
A simple look at joining collections to create a map.  Includes looking as some
of the potential errors that may arise and what they mean.
  • Loading branch information
practicalli-johnny committed Dec 13, 2018
1 parent 7d153f3 commit c0c720e
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/four_clojure/011-maps-conj.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
;; # 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

0 comments on commit c0c720e

Please sign in to comment.