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.
;; default value of 'end' is infinity user=> (range) (0 1 2 3 4 5 6 7 8 9 10 ... 12770 12771 12772 12773 ... n ;; Since clojure 1.2: user=> (take 10 (range)) (0 1 2 3 4 5 6 7 8 9) user=> (range 10) (0 1 2 3 4 5 6 7 8 9) user=> (range -5 5) (-5 -4 -3 -2 -1 0 1 2 3 4) user=> (range -100 100 10) (-100 -90 -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 70 80 90) user=> (range 0 4 2) (0 2) user=> (range 0 5 2) (0 2 4) user=> (range 0 6 2) (0 2 4) user=> (range 0 7 2) (0 2 4 6) user=> (range 100 0 -10) (100 90 80 70 60 50 40 30 20 10) user=> (range 10 -10 -1) (10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9)
(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."
{:added "1.0"
:static true}
([] (range 0 Double/POSITIVE_INFINITY 1))
([end] (range 0 end 1))
([start end] (range start end 1))
([start end step]
(lazy-seq
(let [b (chunk-buffer 32)
comp (if (pos? step) < >)]
(loop [i start]
(if (and (< (count b) 32)
(comp i end))
(do
(chunk-append b i)
(recur (+ i step)))
(chunk-cons (chunk b)
(when (comp i end)
(range i end step)))))))))
Comments top
No comments for range. Log in to add a comment.