pjb3 (owner)

Forks

Revisions

gist: 200412 Download_button fork
public
Public Clone URL: git://gist.github.com/200412.git
Embed All Files: show embed
assoc-conj.clj #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(defn
  #^{:test (fn []
      (assert (= {:x 1} (assoc-conj {} :x 1)))
      (assert (= {:x [1 2]} (assoc-conj {:x 1} :x 2)))
      (assert (= {:x [1 2 3]} (assoc-conj {:x [1 2]} :x 3)))
      (assert (= {:x [1 2 3] :y 1} (assoc-conj {:x [1 2 3]} :y 1)))
      (assert (= {:x [1 2 3] :y [1 2]} (assoc-conj {:x [1 2 3] :y 1} :y 2)))
      (assert (= {:x [1 2 3] :y [1 2 3]} (assoc-conj {:x [1 2 3] :y [1 2]} :y 3))))
     :doc "This is similar to assoc but if you pass it a key that already has a value,
it will conj the value onto the existing value. If the existing value isn't
a collection, it will make it into a vector first."}
  assoc-conj
  [map key val]
  (if (contains? map key)
    (if (coll? (map key))
      (merge-with conj map {key val})
      (assoc map key [(map key) val]))
    (assoc map key val)))