iterate

clojure.core

  • (iterate f x)
Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of side-effects

3 Examples top

  • ;; iterate Ad Infinitum starting at 5 using the inc (increment) function
    user=> (iterate inc 5)
    (5 6 7 8 9 10 11 12 13 14 15 ... n
    
    ;; limit results
    user=> (take 5 (iterate inc 5))
    (5 6 7 8 9)
    
    user=> (take 10 (iterate (partial + 2) 0))
    (0 2 4 6 8 10 12 14 16 18)
    
    user=> (take 20 (iterate (partial + 2) 0))
    (0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38)
    
    
  • user=> (def powers-of-two (iterate (partial * 2) 1))
    #'user/powers-of-two
    
    user=> (nth powers-of-two 10)
    1024
    user=> (take 10 powers-of-two)
    (1 2 4 8 16 32 64 128 256 512)
    
  • ;; demonstrating the power of iterate
    ;; to generate the Fibonacci sequence
    ;; uses +' to promote to BigInt
    user=> (def fib (map first (iterate (fn [[a b]] [b (+' a b)]) [0 1])))
    #'user/fib
    
    user=> (take 10 fib)
    (0 1 1 2 3 5 8 13 21 34)
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:2592 top

(defn iterate
  "Returns a lazy sequence of x, (f x), (f (f x)) etc. f must be free of side-effects"
  {:added "1.0"
   :static true}
  [f x] (cons x (lazy-seq (iterate f (f x)))))
Vars in clojure.core/iterate:
Used in 0 other vars

Comments top

No comments for iterate. Log in to add a comment.