On Thu, Apr 4, 2013 at 8:44 AM, Tassilo Horn <t...@gnu.org> wrote:
> > However, the what's bad with the juxt approach above is that the > collection is iterated twice. If somebody comes up with a version that > returns a vector of two lazy seqs and which iterates the input > collection only once, she'd get my warmest thank you, and my vote for > putting it into core. > > here's a first shot, kind of ugly but seems to work: user> (defn seqmr [f g a bs] (lazy-seq (if (seq bs) (g (f (first bs)) (seqmr f g a (rest bs))) a))) #'user/seqmr user> (defn lazy-partition [p xs] (seqmr (fn [x] [(p x) x]) (fn [[t? x] r] (if t? [(lazy-seq (cons x (first r))) (lazy-seq (second r))] [(lazy-seq (first r)) (lazy-seq (cons x (second r)))])) [() ()] xs)) #'user/lazy-partition user> (def r (lazy-partition even? (map #(do (print % "") %) (range 129)))) #'user/r user> (first (first r)) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 0 user> (first (second r)) 1 user> (nth (second r) 10) 21 user> (nth (second r) 20) 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 41 user> (nth (first r) 20) 40 -- Ben Wolfson "Human kind has used its intelligence to vary the flavour of drinks, which may be sweet, aromatic, fermented or spirit-based. ... Family and social life also offer numerous other occasions to consume drinks for pleasure." [Larousse, "Drink" entry] -- -- 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 --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.