--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
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./>
Point taken about lazy sequences. But the above was not intended to
suggest the sequence needs to be source code literal to satisfy
'counted?', rather that vectors, lists, maps, and sets do so. That
covers a fair bit of ground.
Yes. You could probably implement it yourself, as a wrapper sequence
type, though this wouldn't make all other sequence types automatically
countable.
> Is it?
It won't be, in general, because you would have to "hold onto the head"
until you got to the end, or go through a costly half-broken weak
reference dance.
It is worth considering that even for some uncounted sequences, the cost
of a second traversal for "count" may be less than the bookkeeping cost
of keeping a count as you traverse once.
--
Stephen Compall
^aCollection allSatisfy: [:each|aCondition]: less is better
I can understand the first one being "slow" but I'm a bit surprised
about the loop being the slowest of the four options. Can someone shed
some light on that?
Nice to see the accumulating reduce being faster since that's how I've
settled in to solving this kind of problem in our code, without really
wondering about performance (it's "fast enough" and I think it's the
more elegant solution).
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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