dlang.org redesign -- a better code sample for landing page
bearophile via Digitalmars-d
digitalmars-d at puremagic.com
Fri Jan 23 06:07:58 PST 2015
aldanor:
> auto lines = stdin.byLine.map!(line => line.length);
> writefln("Average line length: %.4f.", 1.0 * lines.sum /
> lines.array.length);
>
> Ofc this is not the greatest piece of code ever and it's not
> lazy,
lines.walklength seems better than lines.array.length, but you
can also use a reduce with two functions (a sum and a counting
one), scanning the input file only once.
> oh wait, that and a lack of a "mean" function in the standard
> library...
Yes, it's worth adding to Phobos.
Bye,
bearophile
More information about the Digitalmars-d
mailing list