From 432eb1ba66f1ed8d8e521a175235efbe95c91ffa Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Sat, 17 Jun 2017 18:07:20 +0300 Subject: [PATCH 1/2] Make average line length example more concise --- index.dd | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/index.dd b/index.dd index 744927ff0a..8b87898ee2 100644 --- a/index.dd +++ b/index.dd @@ -243,20 +243,17 @@ Modern convenience. Modeling power. Native efficiency.) ---- -#!/usr/bin/env rdmd -import std.range, std.stdio; - // Compute average line length for stdin void main() { - ulong lines = 0, sumLength = 0; - foreach (line; stdin.byLine()) - { - ++lines; - sumLength += line.length; - } + import std.range, std.stdio; + + auto sum = 0.0; + auto count = stdin.byLine + .tee!(l => sum += l.length).walkLength; + writeln("Average line length: ", - lines ? cast(double) sumLength / lines : 0.0); + count ? sum / count : 0); } ---- )) From 634f9a3269f1afdfadea00566db4fbac80479213 Mon Sep 17 00:00:00 2001 From: ZombineDev Date: Sat, 17 Jun 2017 17:02:39 +0300 Subject: [PATCH 2/2] Copy the average length example to the top And also make it the default. The primary motivation is that we need to have a simple example for first time visitors. --- index.dd | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/index.dd b/index.dd index 8b87898ee2..05fee17917 100644 --- a/index.dd +++ b/index.dd @@ -30,6 +30,27 @@ $(DIVC intro, $(DIV, $(DIV, ) $(DIVID your-code-here-default, $(RUNNABLE_EXAMPLE +$(RUNNABLE_EXAMPLE_STDIN +The D programming language +Modern convenience. +Modeling power. +Native efficiency.) +---- +// Compute average line length for stdin +void main() +{ + import std.range, std.stdio; + + auto sum = 0.0; + auto count = stdin.byLine + .tee!(l => sum += l.length).walkLength; + + writeln("Average line length: ", + count ? sum / count : 0); +} +---- +) +$(EXTRA_EXAMPLE $(RUNNABLE_EXAMPLE_STDIN 2.4 plus 2.4 equals 5 for sufficiently large values of 2.) ---- // Round floating point numbers