Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure thread sleep between map evaluation

Tags:

clojure

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.

like image 288
Tony Duan Avatar asked Sep 08 '14 21:09

Tony Duan


1 Answers

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)
like image 199
juan.facorro Avatar answered Sep 17 '22 13:09

juan.facorro