Tony Duan
Tony Duan

Reputation: 317

Clojure thread sleep between map evaluation

I have a block of code I need to execute in Clojure that looks like:

    (map function coll)

However, I need to delay the interval of time between each successive function call. That is, I want to call function with the first item, then sleep for 10 seconds, then call with the second item, etc.

How can this be accomplished?

Thanks in advance for your help.

Upvotes: 9

Views: 7158

Answers (1)

juan.facorro
juan.facorro

Reputation: 9920

Just for the sake of completeness, following the discussion in the comments, this is what an implementation using doseq would look like wrapped in a neat little function:

(defn doseq-interval
  [f coll interval]
  (doseq [x coll]
    (Thread/sleep interval)
    (f x)))

And here's how you would call it:

(doseq-interval prn (range 10) 1000)

Upvotes: 15

Related Questions