Calculating mean and standard deviation with std.algorithm.reduce

jerro a at a.com
Wed Feb 13 07:12:50 PST 2013


> See enumerate():
> http://d.puremagic.com/issues/show_bug.cgi?id=5550

I like this enumerate() thing. Is there any particular reason why
it isn't in phobos, or is it just that no one has added it yet?

> I think with enumerate it becomes:
> MQ(x.front, 0).enumerate(1).reduce!reducer()

I think the argument to enumerate here should be 2 (and the x
range is missing, of course).

Another way to do this is:

MQk(x.front, 0, 2).reduce!reducer(x.drop(1))

Or using a lambda instead of reducer():

auto mqk = tuple(x.front, 0.to!double, 2).reduce!
      ((mqk, x) => tuple(
          mqk[0] + (x - mqk[0]) / mqk[2],
          mqk[1] + (mqk[2] - 1) * ((x - mqk[0]) ^^ 2) / mqk[2],
          mqk[2] + 1))
      (x.drop(1));


More information about the Digitalmars-d-learn mailing list