Skip to content

Commit 08a47f2

Browse files
port over latest merge-with, kv-reduce aware, can merge vectors
Co-authored-by: Rich Hickey <richhickey@gmail.com>
1 parent b25755b commit 08a47f2

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

‎src/main/cljs/cljs/core.cljs‎

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9301,16 +9301,33 @@ reduces them without incurring seq initialization"
93019301
the first. If a key occurs in more than one map, the mapping(s)
93029302
from the latter (left-to-right) will be combined with the mapping in
93039303
the result by calling (f val-in-result val-in-latter)."
9304-
[f & maps]
9305-
(when (some identity maps)
9306-
(let [merge-entry (fn [m e]
9307-
(let [k (key e) v (val e)]
9308-
(if (contains? m k)
9309-
(assoc m k (f (get m k) v))
9310-
(assoc m k v))))
9311-
merge2 (fn [m1 m2]
9312-
(reduce merge-entry (or m1 {}) (seq m2)))]
9313-
(reduce merge2 maps))))
9304+
{:added "1.0"
9305+
:static true}
9306+
([f] nil)
9307+
([f x] x)
9308+
([f x y]
9309+
(if (and x y)
9310+
(let [merge-kv
9311+
(fn this [m k v]
9312+
(let [mv (get m k this)]
9313+
(if (identical? mv this)
9314+
(assoc m k v)
9315+
(assoc m k (f mv v)))))
9316+
merge-kv!
9317+
(fn this [m k v]
9318+
(let [mv (-lookup m k this)]
9319+
(if (identical? mv this)
9320+
(assoc! m k v)
9321+
(assoc! m k (f mv v)))))]
9322+
(if (implements? IKVReduce y)
9323+
(if (implements? IEditableCollection x)
9324+
(persistent! (-kv-reduce y merge-kv! (transient x)))
9325+
(-kv-reduce y merge-kv x))
9326+
(reduce (fn [m e] (merge-kv m (key e) (val e))) x y)))
9327+
(or x y)))
9328+
([f x y & maps]
9329+
(let [merge2 (fn [x y] (merge-with f x y))]
9330+
(reduce merge2 (merge2 x y) maps))))
93149331

93159332
(defn select-keys
93169333
"Returns a map containing only those entries in map whose key is in keys"

0 commit comments

Comments
 (0)