Skip to content

Commit d96bb1b

Browse files
mfikesswannodette
authored andcommitted
CLJS-2113: nth function produces different results from clojure when using a negative index on a sequence
Fix range’s implementation of IIndexed for negative values of n
1 parent d6f0ec1 commit d96bb1b

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/main/cljs/cljs/core.cljs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9174,15 +9174,15 @@ reduces them without incurring seq initialization"
91749174

91759175
IIndexed
91769176
(-nth [rng n]
9177-
(if (< n (-count rng))
9177+
(if (and (<= 0 n) (< n (-count rng)))
91789178
(+ start (* n step))
9179-
(if (and (> start end) (zero? step))
9179+
(if (and (<= 0 n) (> start end) (zero? step))
91809180
start
91819181
(throw (js/Error. "Index out of bounds")))))
91829182
(-nth [rng n not-found]
9183-
(if (< n (-count rng))
9183+
(if (and (<= 0 n) (< n (-count rng)))
91849184
(+ start (* n step))
9185-
(if (and (> start end) (zero? step))
9185+
(if (and (<= 0 n) (> start end) (zero? step))
91869186
start
91879187
not-found)))
91889188

src/test/cljs/cljs/core_test.cljs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,12 @@
13371337
"unreachable")
13381338
"cljs-2104"))))
13391339

1340+
(deftest test-cljs-2113
1341+
(is (thrown? js/Error (nth (range 2) -2)))
1342+
(is (thrown? js/Error (nth (range 2 1 0) -2)))
1343+
(is (= ::not-found (nth (range 2) -2 ::not-found)))
1344+
(is (= ::not-found (nth (range 2 1 0) -2 ::not-found))))
1345+
13401346
(comment
13411347
;; ObjMap
13421348
;; (let [ks (map (partial str "foo") (range 500))

0 commit comments

Comments
 (0)