On Jun 13, 4:39 pm, Laurent PETIT <laurent.pe...@gmail.com> wrote: > Hi, > > Well, the array is iterated once by map, the new seq created by map is > iterated once by filter, and the new seq created by filter is iterated > once by count, so right, I should have written : 3 walks of seqs of > the size of the array.
Hi Laurent, I'm probably misunderstanding what you mean by size of the array here, but since all of this is lazy map will only walk as far as take-while lets it, right? I.e., just far enough to make the take-while happy and not the whole array. (defn f [x] (print (str "x: " x "\n")) (inc x)) user> (count (take-while #(< % 10) (map f (iterate inc 0)))) x: 0 x: 1 x: 2 x: 3 x: 4 x: 5 x: 6 x: 7 x: 8 x: 9 9 > While reduce would create no new seq, and one walk ? I guess it's a matter of perspective as to whether there's one walk or multiple as the combined lazy-seq gets "pulled" by count -- I tend to think of it as an assembly line with multiple stations on it (one, lengthy "walk" :-). Yes, like you said, there's double the consing (or triple with a filter as in the original example). However, isn't the JVM supposed to excel at allocating/collecting just this kind of very short-lived data/ garbage? The (count (take-while (filter (map)))) just feels very natural in clojure to me. -Sudish --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---