On Mon, Dec 8, 2008 at 5:56 PM, Stephen C. Gilardi <[EMAIL PROTECTED]> wrote:
> I think I finally see the problem. The "rest expression" in filter's call to
> lazy-cons has a reference to "coll" in it. That's all it takes for coll to
> be retained during the entire calculation of the rest.
>
Well, I had already tried this, eagerly evaluating the first and rest,
and it didn't help:
(defn map2
[f coll]
(when (seq coll)
(let [fcoll (first coll) rcoll (rest coll) ffcoll (f fcoll)]
(lazy-cons ffcoll (map2 f rcoll)))))
(defn filter2
"Returns a lazy seq of the items in coll for which
(pred item) returns true. pred must be free of side-effects."
[pred coll]
(when (seq coll)
(let [fcoll (first coll) rcoll (rest coll)]
(if (pred fcoll)
(lazy-cons fcoll (filter2 pred rcoll))
(recur pred rcoll)))))
(defn splode [n]
(doseq [document (filter2 #(= % 20) (map2 inc (range n)))]))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---