1.3.0 permalink Arrow_down_16x16

repeatedly

clojure.core

  • (repeatedly f)
  • (repeatedly n f)
Takes a function of no args, presumably with side effects, and
returns an infinite (or length n if supplied) lazy sequence of calls
to it

2 Examples top

  • ;these two functions are equivalent 
    
    user=> (take 5 (repeatedly #(rand-int 11)))
    (6 6 3 9 8)
    
    user=> (repeatedly 5 #(rand-int 11))
    (1 8 6 9 6)
    
    ;compare with repeat
    user=> (repeat 5 (int (rand 100)))
    (94 94 94 94 94)
  • (defn counter []  
      (let [tick (atom 0)]
        #(swap! tick inc)))
    
    (def tick (counter))
    
    user=> (take 10 (repeatedly tick))
    (1 2 3 4 5 6 7 8 9 10)
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:4512 top

(defn repeatedly
  "Takes a function of no args, presumably with side effects, and
  returns an infinite (or length n if supplied) lazy sequence of calls
  to it"
  {:added "1.0"
   :static true}
  ([f] (lazy-seq (cons (f) (repeatedly f))))
  ([n f] (take n (repeatedly f))))
Vars in clojure.core/repeatedly:
Used in 0 other vars

Comments top

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