Skip to content

Commit 7977b49

Browse files
frenchy64stuarthalloway
authored andcommitted
Add clojure.core/update, like update-in but takes a single key
Signed-off-by: Stuart Halloway <stu@cognitect.com>
1 parent cf9fb27 commit 7977b49

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/clj/clojure/core.clj

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5830,6 +5830,23 @@
58305830
(assoc m k (apply update-in (get m k) ks f args))
58315831
(assoc m k (apply f (get m k) args)))))
58325832

5833+
(defn update
5834+
"'Updates' a value in an associative structure, where k is a
5835+
key and f is a function that will take the old value
5836+
and any supplied args and return the new value, and returns a new
5837+
structure. If the key does not exist, nil is passed as the old value."
5838+
{:added "1.7"
5839+
:static true}
5840+
([m k f]
5841+
(assoc m k (f (get m k))))
5842+
([m k f x]
5843+
(assoc m k (f (get m k) x)))
5844+
([m k f x y]
5845+
(assoc m k (f (get m k) x y)))
5846+
([m k f x y z]
5847+
(assoc m k (f (get m k) x y z)))
5848+
([m k f x y z & more]
5849+
(assoc m k (apply f (get m k) x y z more))))
58335850

58345851
(defn empty?
58355852
"Returns true if coll has no items - same as (not (seq coll)).
@@ -6269,7 +6286,7 @@
62696286
(let [buckets (loop [m {} ks tests vs thens]
62706287
(if (and ks vs)
62716288
(recur
6272-
(update-in m [(clojure.lang.Util/hash (first ks))] (fnil conj []) [(first ks) (first vs)])
6289+
(update m (clojure.lang.Util/hash (first ks)) (fnil conj []) [(first ks) (first vs)])
62736290
(next ks) (next vs))
62746291
m))
62756292
assoc-multi (fn [m h bucket]

src/clj/clojure/reflect/java.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ the kinds of objects to which they can apply."}
224224
(visitSource [name debug])
225225
(visitInnerClass [name outerName innerName access])
226226
(visitField [access name desc signature value]
227-
(swap! result update-in [:members] (fnil conj #{})
227+
(swap! result update :members (fnil conj #{})
228228
(Field. (symbol name)
229229
(field-descriptor->class-symbol desc)
230230
class-symbol
@@ -233,7 +233,7 @@ the kinds of objects to which they can apply."}
233233
(visitMethod [access name desc signature exceptions]
234234
(when-not (= name "<clinit>")
235235
(let [constructor? (= name "<init>")]
236-
(swap! result update-in [:members] (fnil conj #{})
236+
(swap! result update :members (fnil conj #{})
237237
(let [{:keys [parameter-types return-type]} (parse-method-descriptor desc)
238238
flags (parse-flags access :method)]
239239
(if constructor?

test/clojure/test_clojure/other_functions.clj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,21 @@
335335
; Regex Support
336336
; re-matcher re-find re-matches re-groups re-seq
337337

338+
; update
339+
340+
(deftest test-update
341+
(are [result expr] (= result expr)
342+
{:a [1 2]} (update {:a [1]} :a conj 2)
343+
[1] (update [0] 0 inc)
344+
;; higher-order usage
345+
{:a {:b 2}} (update-in {:a {:b 1}} [:a] update :b inc)
346+
;; missing field = nil
347+
{:a 1 :b nil} (update {:a 1} :b identity)
348+
;; 4 hard-coded arities
349+
{:a 1} (update {:a 1} :a +)
350+
{:a 2} (update {:a 1} :a + 1)
351+
{:a 3} (update {:a 1} :a + 1 1)
352+
{:a 4} (update {:a 1} :a + 1 1 1)
353+
;; rest arity
354+
{:a 5} (update {:a 1} :a + 1 1 1 1)
355+
{:a 6} (update {:a 1} :a + 1 1 1 1 1)))

0 commit comments

Comments
 (0)