Permalink
Browse files

make get-in faster #256

Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information...
1 parent c19ce81 commit eb4502cb1e9cbc7a9a9378af1f8b8c1cd8cc6507 @stuarthalloway stuarthalloway committed Jun 5, 2010
Showing with 11 additions and 4 deletions.
  1. +11 −4 src/clj/clojure/core.clj
@@ -4910,12 +4910,19 @@
"Returns the value in a nested associative structure,
where ks is a sequence of ke(ys. Returns nil if the key is not present,
or the not-found value if supplied."
+ {:added "1.2"}
([m ks]
- (reduce get m ks))
+ (reduce get m ks))
([m ks not-found]
- (if (seq ks)
- (get (reduce get m (butlast ks)) (last ks) not-found)
- m)))
+ (loop [sentinel (Object.)
+ m m
+ ks (seq ks)]
+ (if ks
+ (let [m (get m (first ks) sentinel)]
+ (if (identical? sentinel m)
+ not-found
+ (recur sentinel m (next ks))))
+ m))))
(defn assoc-in
"Associates a value in a nested associative structure, where ks is a

0 comments on commit eb4502c

Please sign in to comment.