As an aside:
Fingertrees are an interesting way to keep a collection that can
efficiently compute means over its values, or a window of its values.
https://gist.github.com/672592
--
Dave
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to
Hi,
On Mar 29, 10:43 pm, Alan Malloy a...@malloys.org wrote:
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.
I think this topic is interesting. My guess would be,
I too think this is interesting because because it serves to illustrate
some important general aspects of Clojure with a very simple problem.
I wrote four Clojure programs contrasting different ways of solving the
problem, and then timed the application of each ten times to a
million-item
On Thu, 2012-03-29 at 23:31 -0700, Benjamin Peter wrote:
the sequence would have been traversed completely by the reduce call
and therefore clojure could know it's size and provide a constant time
count.
Could this be implemented?
Yes. You could probably implement it yourself, as a wrapper
Hi Rob,
Appreciate it, I like the code and explanation, great!
Simon
On Thursday, March 29, 2012 6:28:48 PM UTC+8, Rob Nagle wrote:
You can reduce in one pass with a function that tracks both the sum
and the count.
(defn avg [coll]
(apply / (reduce (fn [[sum n] x] [(+ sum x) (inc
On Fri, Mar 30, 2012 at 11:01 AM, Larry Travis tra...@cs.wisc.edu wrote:
user= (time (dotimes [i 10] (average1 mill-float-numbs)))
Elapsed time: 526.674 msecs
user= (time (dotimes [i 10] (average2 mill-float-numbs)))
Elapsed time: 646.608 msecs
user= (time (dotimes [i 10] (average3
The obvious way is like the following, which traverse the sequence 2 times.
Wondering what will be the efficient way...
(defn avg [coll]
(/ (reduce + coll) (count coll)))
--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send
or to increase a counter while reducing it, a function like inc+ returning
{:sum sum :count count} and then take the sum/counter, which is the mean.
The problem is possible to state as a clean map-reduce problem with only
one traversing of the data. It's also possible to remove items form the
The obvious way is like the following, which traverse the sequence 2 times.
Wondering what will be the efficient way...
(defn avg [coll]
(loop [c coll tot 0 cnt 0]
(if (empty? c)
(/ tot cnt)
(recur (rest c) (+ tot (first c)) (inc cnt)
This will loop only once.
It happens
You can reduce in one pass with a function that tracks both the sum
and the count.
(defn avg [coll]
(apply / (reduce (fn [[sum n] x] [(+ sum x) (inc n)]) [0 0] coll)))
This reduce function is somewhat unusual in that its arguments have
different forms. As a result, this one does require the
On Thu, Mar 29, 2012 at 12:18 AM, simon.T simon.j@gmail.com 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
On Mar 29, 10:18 am, David Cabana drcab...@gmail.com wrote:
On Thu, Mar 29, 2012 at 12:18 AM, simon.T simon.j@gmail.com 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
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
Point taken about lazy sequences. But the above was not intended to
suggest the sequence needs to be source code
13 matches
Mail list logo