Re: What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-31 Thread David Powell
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

Re: What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-30 Thread Benjamin Peter
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,

Re: What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-30 Thread Larry Travis
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

Re: What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-30 Thread Stephen Compall
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

Re: What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-30 Thread simon.T
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

Re: What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-30 Thread Sean Corfield
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

What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-29 Thread simon.T
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

Re: What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-29 Thread Linus Ericsson
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

Re: What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-29 Thread Rick Beerendonk
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

Re: What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-29 Thread Rob Nagle
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

Re: What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-29 Thread David Cabana
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

Re: What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-29 Thread Alan Malloy
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

Re: What's the efficient functional way to computing the average of a sequence of numbers?

2012-03-29 Thread David Cabana
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