Skip to content

Commit 432eb1b

Browse files
committed
Make average line length example more concise
1 parent 36ff9d1 commit 432eb1b

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

index.dd

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -243,20 +243,17 @@ Modern convenience.
243243
Modeling power.
244244
Native efficiency.)
245245
----
246-
#!/usr/bin/env rdmd
247-
import std.range, std.stdio;
248-
249246
// Compute average line length for stdin
250247
void main()
251248
{
252-
ulong lines = 0, sumLength = 0;
253-
foreach (line; stdin.byLine())
254-
{
255-
++lines;
256-
sumLength += line.length;
257-
}
249+
import std.range, std.stdio;
250+
251+
auto sum = 0.0;
252+
auto count = stdin.byLine
253+
.tee!(l => sum += l.length).walkLength;
254+
258255
writeln("Average line length: ",
259-
lines ? cast(double) sumLength / lines : 0.0);
256+
count ? sum / count : 0);
260257
}
261258
----
262259
))

0 commit comments

Comments
 (0)