I'd be careful about writing my own version of mapcat. You might save a call
or two on the f function, but you'll probably lose in the general. Try some
experiments with larger take numbers. You might be better off concentrating on
your f function. I've found that trying to special-case
On Wed, Aug 24, 2011 at 3:49 PM, Asim Jalis asimja...@gmail.com wrote:
I used take 0 as a simple example to illustrate the problem. But in general
the standard mapcat evaluates terms than are needed. My f function does a web
call and processes the JSON records produced by this call, so each
user= (defn f [[x]] (println computing x: (inc x)) (vector (inc x)))
#'user/f
user= (- (iterate f [0]) (take 0))
()
user= (- (iterate f [0]) (apply concat) (take 0))
computing x: 1
computing x: 2
computing x: 3
()
user= (- (iterate f [0]) (mapcat identity) (take 0))
computing x: 1
computing x: 2
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
This is my attempt to take Meikel's code and extend it to accept arbitrarily
many collections as 'mapcat' does:
(defn lazy-mapcat [f colls]
(lazy-seq
(when (every? seq colls)
(concat (apply f (map first colls))
(apply lazy-mapcat f (map rest colls))
user= (- (iterate
Nice. Are there any technical reasons this isn't the default
implementation of mapcat in Clojure.core?
On Mon, Aug 22, 2011 at 9:27 AM, Meikel Brandmeyer m...@kotka.de wrote:
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