Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add editable? check for collections for assoc-some fast path #92

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading