You can change or use an invoice-implemented
(defn count-realized [s]
(loop [s s, n 0]
(if (instance? clojure.lang.IPending s)
(if (and (realized? s) (seq s))
(recur (rest s) (inc n))
n)
(if (seq s)
(recur (rest s) (inc n))
n))))
Note that checking for IPending is necessary for cases where non-lazy elements add a sequence, as in an iteration.
(def foo (iterate inc 1))
(take (count-realized foo) foo)
;=> (1)
(dorun (take 5 foo))
;=> nil
(take (count-realized foo) foo)
;=> (1 2 3 4 5)
source
share