Hi,

Am 22.08.2011 um 16:32 schrieb Asim Jalis:

> Is there a way to rewrite mapcat (or apply concat) so that they don't
> evaluate the incoming seq unnecessarily?

user=> (defn f [[x]] (println "Computing x:" x) [(inc x)])
#'user/f
user=> (defn lazy-mapcat
  [f coll]
  (lazy-seq
    (when-let [s (seq coll)]
      (concat (f (first s)) (lazy-mapcat f (rest s))))))
#'user/lazy-mapcat
user=> (->> [0] (iterate f) (take 0))
()
user=> (->> [0] (iterate f) (apply concat) (take 0))
Computing x: 0
Computing x: 1
Computing x: 2
()
user=> (->> [0] (iterate f) (mapcat identity) (take 0))
Computing x: 0
Computing x: 1
Computing x: 2
()
user=> (->> [0] (iterate f) (lazy-mapcat identity) (take 0))
()

Sincerely
Meikel

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to