Skip to content

Commit

Permalink
Merge pull request #92 from tomdl89/check-editable-for-assoc-some
Browse files Browse the repository at this point in the history
Fix assoc-some for non-editable collections
  • Loading branch information
weavejester authored Jul 18, 2024
2 parents 2e727f2 + f019754 commit ff99bb0
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/medley/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
(recur (dissoc-in m ks) ks' kss)
(dissoc-in m ks))))

(defn- editable? [coll]
#?(:clj (instance? clojure.lang.IEditableCollection coll)
:cljs (satisfies? cljs.core/IEditableCollection coll)))

(defn- assoc-some-transient! [m k v]
(if (nil? v) m (assoc! m k v)))

Expand All @@ -45,13 +49,19 @@
([m k v]
(if (nil? v) m (assoc m k v)))
([m k v & kvs]
(loop [acc (assoc-some-transient! (transient (or m {})) k v)
kvs kvs]
(if (next kvs)
(recur (assoc-some-transient! acc (first kvs) (second kvs)) (nnext kvs))
(if (zero? (count acc))
m
(persistent! acc))))))
(if (editable? m)
(loop [acc (assoc-some-transient! (transient (or m {})) k v)
kvs kvs]
(if (next kvs)
(recur (assoc-some-transient! acc (first kvs) (second kvs)) (nnext kvs))
(if (zero? (count acc))
m
(persistent! acc))))
(loop [acc (assoc-some m k v)
kvs kvs]
(if (next kvs)
(recur (assoc-some acc (first kvs) (second kvs)) (nnext kvs))
acc)))))

(defn update-existing
"Updates a value in a map given a key and a function, if and only if the key
Expand Down Expand Up @@ -83,10 +93,6 @@
m)))]
(up m ks f args)))

(defn- editable? [coll]
#?(:clj (instance? clojure.lang.IEditableCollection coll)
:cljs (satisfies? cljs.core/IEditableCollection coll)))

(defn- reduce-map [f coll]
(let [coll' (if (record? coll) (into {} coll) coll)]
(if (editable? coll')
Expand Down

0 comments on commit ff99bb0

Please sign in to comment.