> This is a common misconception: passing a seq to apply doesn't force its > evaluation. Ahh, is this because the [& more] portion is itself a lazy sequence? That's very cool =)
Hmm, the (reduce + ...) approach works just fine, but if it is already implemented as reduce, it seems like it would be very easy to allow (+) to take a sequence. I still think the > (< (range 10)) ... use case is really worthwhile though, and I don't see a way to accomplish it with reduce. Thanks, Stu On Tue, Feb 3, 2009 at 1:36 PM, Christophe Grand <christo...@cgrand.net>wrote: > > Hello, > > stuhood a écrit : > > Functions like (+), (*), (-), (and probably more) should support > > sequences as parameters. > > > > The current way to accomplish this (without implementing your own sum > > using reduce) seems to be: > > > >> (apply + (map #(. Math pow 2 %) (range 10))) > >> > > ... which has to generate the sequence first. > > > This is a common misconception: passing a seq to apply doesn't force its > evaluation. > You can test this fact by passing an infinite seq to a function: > (defn second-arg [& args] > (second args)) > user=> (apply second-arg (iterate inc 0)) > 1 > > > and (apply + some-seq) is really equivalent to (reduce + some-seq), see > the def of + in core.clj: > (defn + > ... > ([x y & more] > (reduce + (+ x y) more))) > > 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 clojure@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---