On Mar 29, 10:18 am, David Cabana <[email protected]> wrote: > On Thu, Mar 29, 2012 at 12:18 AM, simon.T <[email protected]> wrote: > > The obvious way is like the following, which traverse the sequence 2 times. > > ... > > The obvious way does not necessarily traverse the sequence twice. If > a sequence S satisfies the 'counted?' predicate, (count S) takes > constant time. In particular > user=> (counted? [:a :b :c]) > true > > user=> (counted? '(:a :b :c)) > true > > user=> (counted? {:a 1 :b 2 :c 3}) > true > > user=> (counted? #{:a :b :c}) > true > > The examples are stolen > from:http://clojuredocs.org/clojure_core/clojure.core/counted_q > > So it is very likely that (/ (reduce + coll) (count coll)) will not > traverse 'coll' twice, and the natural way is the preferred way. > <Insert standard warning about premature optimization./>
"Very likely" strikes me as a huge overstatement here. Most sequences that you want to average won't be source-code literals, they'll be lazy sequences, and those aren't counted. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to [email protected] Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/clojure?hl=en
