File tree Expand file tree Collapse file tree 2 files changed +13
-0
lines changed
Expand file tree Collapse file tree 2 files changed +13
-0
lines changed Original file line number Diff line number Diff line change 5353 m
5454 (persistent! acc))))))
5555
56+ (defn assoc-in-some
57+ " Associates a value v in a nested associative structure provided that v is not nil."
58+ [m k v]
59+ (cond-> m (some? v) (assoc-in k v)))
60+
5661(defn update-existing
5762 " Updates a value in a map given a key and a function, if and only if the key
5863 exists in the map. See: `clojure.core/update`."
Original file line number Diff line number Diff line change 3333 (is (nil? (m/assoc-some nil :a nil )))
3434 (is (nil? (m/assoc-some nil :a nil :b nil ))))
3535
36+ (deftest test-assoc-in-some
37+ (is (= (m/assoc-in-some {:a 1 :b {:c 2 }} [:b ] 3 ) {:a 1 :b 3 }))
38+ (is (= (m/assoc-in-some [{:a 1 } {:a 2 }] [1 :a ] 3 ) [{:a 1 } {:a 3 }]))
39+ (is (= (m/assoc-in-some [{:a 1 } {:a 2 }] [1 :a ] false ) [{:a 1 } {:a false }]))
40+ (is (= (m/assoc-in-some [{:a 1 } {:a 2 }] [1 :a ] nil ) [{:a 1 } {:a 2 }]))
41+ (is (nil? (m/assoc-in-some nil :a nil )))
42+ (is (nil? (m/assoc-in-some nil [:a :b ] nil ))))
43+
3644(deftest test-update-existing
3745 (is (= (m/update-existing {:a 1 } :a inc) {:a 2 }))
3846 (is (= (m/update-existing {:a 1 :b 2 } :a inc) {:a 2 :b 2 }))
You can’t perform that action at this time.
0 commit comments