delay

added
1.0

ns
clojure.core

type
macro

(delay & body)

Takes a body of expressions and yields a Delay object that will
invoke the body only the first time it is forced (with force or deref/@), and
will cache the result and return it on all subsequent force
calls. See also - realized?

                ;; In this example you can see that the first time the delay is forced
;; the println is executed however the second dereference shows just the
;; precomputed value.

user=> (def my-delay (delay (println "did some work") 100))
#'user/my-delay

user=> @my-delay
did some work
100

user=> @my-delay
100

            
                ;; Note that the implementation of deref for delays makes it impossible for the
;; body of the delay to be executed more than once, even if the derefs occur
;; from multiple threads near the same time, because it is a synchronized method
;; in Java.
            
                ;; test example