File tree Expand file tree Collapse file tree 3 files changed +38
-3
lines changed
test/clojure/test_clojure Expand file tree Collapse file tree 3 files changed +38
-3
lines changed Original file line number Diff line number Diff line change 5830
5830
(assoc m k (apply update-in (get m k) ks f args))
5831
5831
(assoc m k (apply f (get m k) args)))))
5832
5832
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))))
5833
5850
5834
5851
(defn empty?
5835
5852
" Returns true if coll has no items - same as (not (seq coll)).
6269
6286
(let [buckets (loop [m {} ks tests vs thens]
6270
6287
(if (and ks vs)
6271
6288
(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)])
6273
6290
(next ks) (next vs))
6274
6291
m))
6275
6292
assoc-multi (fn [m h bucket]
Original file line number Diff line number Diff line change @@ -224,7 +224,7 @@ the kinds of objects to which they can apply."}
224
224
(visitSource [name debug])
225
225
(visitInnerClass [name outerName innerName access])
226
226
(visitField [access name desc signature value]
227
- (swap! result update-in [ :members ] (fnil conj #{})
227
+ (swap! result update :members (fnil conj #{})
228
228
(Field. (symbol name)
229
229
(field-descriptor->class-symbol desc)
230
230
class-symbol
@@ -233,7 +233,7 @@ the kinds of objects to which they can apply."}
233
233
(visitMethod [access name desc signature exceptions]
234
234
(when-not (= name " <clinit>" )
235
235
(let [constructor? (= name " <init>" )]
236
- (swap! result update-in [ :members ] (fnil conj #{})
236
+ (swap! result update :members (fnil conj #{})
237
237
(let [{:keys [parameter-types return-type]} (parse-method-descriptor desc)
238
238
flags (parse-flags access :method )]
239
239
(if constructor?
Original file line number Diff line number Diff line change 335
335
; Regex Support
336
336
; re-matcher re-find re-matches re-groups re-seq
337
337
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 )))
You can’t perform that action at this time.
0 commit comments