Mark Engelberg a écrit :
> Let's imagine that you are using map on a collection for which it is
> very computation intensive to generate the rest, but trivial to
> generate the first.
>
I don't think that's that simple: it depends on what is in the cons. For
example, if the input seq is the result of mapping a computation
intensive function (a common source of computation intensive seqs) then
taking the rest of this seq is cheap because the rest itslef is a
lazy-seq (as per map definition):
user=> (def b (map #(do (println "mapping" %) %) (range 10)))
#'user/b
user=> (doall (take 3 (map inc b)))
mapping 0
mapping 1
mapping 2
(1 2 3)
When you really want to delay the computation of rest, you can replace
(rest s) by (drop 1 s) but that holds on s (so does your lazier-map).
Your lazier-map can be written:
(defn lazier-map
([f coll]
(lazy-seq
(when-let [s (seq coll)]
(cons (f (first s)) (lazier-map f (drop 1 s))))))
Hope this helps
Christophe
--
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---