Skip to content
Browse files

CLJS-256: fix inconsistencies in nth's behaviour

This fixes a problem in nth's linear traversal helper whereas an
index-out-of-bounds might not be thrown in the appropriate
circumstances.

Also fixes a bug whereby the not-found argument might not be used.
  • Loading branch information...
1 parent 83f9d91 commit 98d84ba670bec624d3a64c196a83160e8bf1de52 @michalmarczyk michalmarczyk committed with David Nolen May 15, 2012
Showing with 12 additions and 7 deletions.
  1. +12 −7 src/cljs/cljs/core.cljs
View
19 src/cljs/cljs/core.cljs
@@ -625,6 +625,7 @@ reduces them without incurring seq initialization"
(defn- linear-traversal-nth
([coll n]
(cond
+ (nil? coll) (throw (js/Error. "Index out of bounds"))
(zero? n) (if (seq coll)
(first coll)
(throw (js/Error. "Index out of bounds")))
@@ -633,11 +634,12 @@ reduces them without incurring seq initialization"
:else (throw (js/Error. "Index out of bounds"))))
([coll n not-found]
(cond
+ (nil? coll) not-found
(zero? n) (if (seq coll)
(first coll)
not-found)
(indexed? coll) (-nth coll n not-found)
- (seq coll) (linear-traversal-nth (next coll) (dec n))
+ (seq coll) (linear-traversal-nth (next coll) (dec n) not-found)
:else not-found)))
(defn nth
@@ -646,13 +648,16 @@ reduces them without incurring seq initialization"
also works for strings, arrays, regex Matchers and Lists, and,
in O(n) time, for sequences."
([coll n]
- (if (satisfies? IIndexed coll)
- (-nth coll (.floor js/Math n))
- (linear-traversal-nth coll (.floor js/Math n))))
+ (when (coercive-not= coll nil)
+ (if (satisfies? IIndexed coll)
+ (-nth coll (.floor js/Math n))
+ (linear-traversal-nth coll (.floor js/Math n)))))
([coll n not-found]
- (if (satisfies? IIndexed coll)
- (-nth coll (.floor js/Math n) not-found)
- (linear-traversal-nth coll (.floor js/Math n) not-found))))
+ (if (coercive-not= coll nil)
+ (if (satisfies? IIndexed coll)
+ (-nth coll (.floor js/Math n) not-found)
+ (linear-traversal-nth coll (.floor js/Math n) not-found))
+ not-found)))
(defn get
"Returns the value mapped to key, not-found or nil if key not present."

0 comments on commit 98d84ba

Please sign in to comment.
Something went wrong with that request. Please try again.