Calculating mean and standard deviation with std.algorithm.reduce

jerro a at a.com
Wed Feb 13 06:25:49 PST 2013


> ... where k represents the index count 1, 2, 3, ...  However, 
> it's not evident to me how you could get reduce() to know this 
> counting value.

You would use zip and sequence to add indices to x, like this:

reduce!reducer(initial, zip(x, sequence!"n"))

> Where calculating Q[k] is concerned, you need to know both the 
> index value _and_ the value of the corresponding M[k].  Again, 
> it's not evident to me how you'd indicate to reduce() what is 
> needed.

I guess reduce would need to operate on tuples of M and Q, so you 
would have something like:

alias Tuple!(float, float) MQ;

MQ reducer(MQ mq, Tuple!(float, int) xi)
{
     return MQ(
         // compute new values or M and Q here
     );
}

And you would then call reduce like this:

reduce!reducer(MQ(x.front, 0), zip(x, sequence!"n"))

You could also use Tuple of M, Q and k instead of using zip and 
sequence. You would then pass MQk(x.front, 0, 0) as first 
argument to reduce (I'm assuming zero based indexing here) and 
simply compute the k component of the return value in reducer as 
mqk[2] + 1.


More information about the Digitalmars-d-learn mailing list