Another page exploring the Clojure API.
Today, it’s conj.
Added 2017-10-04_22.55.23
(def my-map (hash-map :a 1 :b 2 :c 3)) my-map => {:c 3, :b 2, :a 1} (conj my-map :d 4) => java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Keyword ;; with assoc, you can list key/val pairs as separate args ;; with conj, they have to be in a map format (conj my-map {:d 4}) => {:c 3, :b 2, :d 4, :a 1} ;; our original map stays the same my-map => {:c 3, :b 2, :a 1} ;; we can work with vectors too (def my-vec [1 2 3 4]) ;; element goes at the end (conj my-vec 5) => [1 2 3 4 5] ;;conj-ing won't change our original vector my-vec => [1 2 3 4] (def my-list '(1 2 3 4)) ;; conj puts new element at beginning of list (conj my-list 5) => (5 1 2 3 4) my-list => (1 2 3 4) (def my-set (set '(1 2 3 4 4))) my-set => #{1 4 3 2} (conj my-set 5) => #{1 4 3 2 5} ;; trying to add an element in the set is ignored (conj my-set 3) => #{1 4 3 2} ;; original not changed my-set => #{1 4 3 2} ;; it seems to put elements in a set in random places (conj my-set 66) => #{1 4 3 2 66} (conj my-set 22) => #{1 4 22 3 2}
;; you can conj nil, true and false (conj my-vec nil) => [1 2 3 4 nil] (conj my-set nil) => #{nil 1 4 3 2} (conj my-set false) => #{1 4 3 2 false} (conj my-set true) => #{1 4 true 3 2} (conj my-list true) => (true 1 2 3 4)
You’re welcome.