delay

clojure.core

  • (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?

1 Example top

  • ;; 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
    
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:681 top

(defmacro delay
  "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?"
  {:added "1.0"}
  [& body]
    (list 'new 'clojure.lang.Delay (list* `^{:once true} fn* [] body)))
Vars in clojure.core/delay:
Used in 0 other vars

Comments top

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