Collect Statistics efficiently and easily

Paul Backus snarwin at
Tue Sep 17 14:06:41 UTC 2019

On Tuesday, 17 September 2019 at 01:53:39 UTC, Brett wrote:
> Many times I have to get statistical info which is simply 
> compute statistics on a data set that may be generating or 
> already generated.
> The code usually is
> M = max(M, v);
> m = min(m, v);
> but other things like standard deviation, mean, etc might need 
> to be computed.
> This may need to be done on several data sets simultaneously.
> is there any way that one could just compute them in one line 
> that is efficient, probably using ranges? I'd like to avoid 
> having to loop through a data set multiple times as it would be 
> quite inefficient.

You can use `std.algorithm.fold` to compute multiple results in a 
single pass:

auto stats = v.fold!(max, min);
M = stats[0];
m = stats[1];

More information about the Digitalmars-d-learn mailing list