Skip to content

Commit 9458266

Browse files
devnstuarthalloway
authored andcommitted
Fix inconsistent range, refs #1018
Signed-off-by: Stuart Halloway <[email protected]>
1 parent 64f38a4 commit 9458266

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/clj/clojure/core.clj

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,10 +2650,11 @@
26502650
:static true}
26512651
[f x] (cons x (lazy-seq (iterate f (f x)))))
26522652

2653-
(defn range
2653+
(defn range
26542654
"Returns a lazy seq of nums from start (inclusive) to end
2655-
(exclusive), by step, where start defaults to 0, step to 1, and end
2656-
to infinity."
2655+
(exclusive), by step, where start defaults to 0, step to 1, and end to
2656+
infinity. When step is equal to 0, returns an infinite sequence of
2657+
start. When start is equal to end, returns empty list."
26572658
{:added "1.0"
26582659
:static true}
26592660
([] (range 0 Double/POSITIVE_INFINITY 1))
@@ -2662,7 +2663,9 @@
26622663
([start end step]
26632664
(lazy-seq
26642665
(let [b (chunk-buffer 32)
2665-
comp (if (pos? step) < >)]
2666+
comp (cond (or (zero? step) (= start end)) not=
2667+
(pos? step) <
2668+
(neg? step) >)]
26662669
(loop [i start]
26672670
(if (and (< (count b) 32)
26682671
(comp i end))

test/clojure/test_clojure/sequences.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,9 @@
944944
(range -2 -2) ()
945945
(range -2 -5) ()
946946

947-
(range 3 9 0) ()
947+
(take 3 (range 3 9 0)) '(3 3 3)
948+
(take 3 (range 9 3 0)) '(9 9 9)
949+
(range 0 0 0) ()
948950
(range 3 9 1) '(3 4 5 6 7 8)
949951
(range 3 9 2) '(3 5 7)
950952
(range 3 9 3) '(3 6)

0 commit comments

Comments
 (0)