Skip to content

Commit

Permalink
Fix inconsistent range, refs #1018
Browse files Browse the repository at this point in the history
Signed-off-by: Stuart Halloway <stu@thinkrelevance.com>
  • Loading branch information
devn authored and stuarthalloway committed May 24, 2013
1 parent 8be9b2b commit 950487f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/clj/clojure/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2650,10 +2650,11 @@
:static true}
[f x] (cons x (lazy-seq (iterate f (f x)))))

(defn range
(defn range
"Returns a lazy seq of nums from start (inclusive) to end
(exclusive), by step, where start defaults to 0, step to 1, and end
to infinity."
(exclusive), by step, where start defaults to 0, step to 1, and end to
infinity. When step is equal to 0, returns an infinite sequence of
start. When start is equal to end, returns empty list."
{:added "1.0"
:static true}
([] (range 0 Double/POSITIVE_INFINITY 1))
Expand All @@ -2662,7 +2663,9 @@
([start end step]
(lazy-seq
(let [b (chunk-buffer 32)
comp (if (pos? step) < >)]
comp (cond (or (zero? step) (= start end)) not=
(pos? step) <
(neg? step) >)]
(loop [i start]
(if (and (< (count b) 32)
(comp i end))
Expand Down
4 changes: 3 additions & 1 deletion test/clojure/test_clojure/sequences.clj
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,9 @@
(range -2 -2) ()
(range -2 -5) ()

(range 3 9 0) ()
(take 3 (range 3 9 0)) '(3 3 3)
(take 3 (range 9 3 0)) '(9 9 9)
(range 0 0 0) ()
(range 3 9 1) '(3 4 5 6 7 8)
(range 3 9 2) '(3 5 7)
(range 3 9 3) '(3 6)
Expand Down

0 comments on commit 950487f

Please sign in to comment.