Search code examples
clojuredelay

Nested dereference of delay


@(delay (delay 1)) ; equals to unrealized delay object, containing 1.

Is there an easy (without a head-first macross which tests each block with realized?) way to deref all inner delays recursively?


Solution

  • Arthur's code certainly works for the example you've posted, but here is an example of how you could handle mixed sequences:

    (defn deref-delays [x]
      (cond
        (sequential? x) (map deref-delays x)
        (= clojure.lang.Delay (class x)) (recur @x)
        :else x))
    
    (deref-delays [1 2 (delay 3) (delay [4 (delay (delay 5)) 6]) 7])
    ;(1 2 3 (4 5 6) 7)