Skip to content

Commit eb4502c

Browse files
make get-in faster #256
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
1 parent c19ce81 commit eb4502c

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/clj/clojure/core.clj

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4910,12 +4910,19 @@
49104910
"Returns the value in a nested associative structure,
49114911
where ks is a sequence of ke(ys. Returns nil if the key is not present,
49124912
or the not-found value if supplied."
4913+
{:added "1.2"}
49134914
([m ks]
4914-
(reduce get m ks))
4915+
(reduce get m ks))
49154916
([m ks not-found]
4916-
(if (seq ks)
4917-
(get (reduce get m (butlast ks)) (last ks) not-found)
4918-
m)))
4917+
(loop [sentinel (Object.)
4918+
m m
4919+
ks (seq ks)]
4920+
(if ks
4921+
(let [m (get m (first ks) sentinel)]
4922+
(if (identical? sentinel m)
4923+
not-found
4924+
(recur sentinel m (next ks))))
4925+
m))))
49194926

49204927
(defn assoc-in
49214928
"Associates a value in a nested associative structure, where ks is a

0 commit comments

Comments
 (0)