You're viewing version 1.2.0 of range. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

range

clojure.core

  • (range)
  • (range end)
  • (range start end)
  • (range start end step)
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.

3 Examples top

  • ;; 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)
     
    
    
  • ;; Since clojure 1.2:
    
    user=> (take 10 (range))
    (0 1 2 3 4 5 6 7 8 9)
    
  • ;; finite range over java Integers
    user=> (take 5 (range 42 (java.lang.Integer/MAX_VALUE)))
    (42 43 44 45 46)
    
    ;; infinite range starting at a certain point
    user=> (take 5 (drop 42 (range)))
    (42 43 44 45 46)
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:2246 top

(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"}
  ([] (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.