Skip to content

Commit b20cf2f

Browse files
authored
Merge pull request #3359 from ntrel/aa-wc
Fix AA word count example Signed-off-by: Dennis <[email protected]> Merged-on-behalf-of: Dennis <[email protected]>
2 parents ed7e830 + 03fdfe4 commit b20cf2f

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

spec/hash-map.dd

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -484,20 +484,23 @@ $(P Properties for associative arrays are:)
484484
else evaluates the $(D create) delegate and adds it to the associative array))
485485
)
486486

487-
$(HR)
487+
$(H2 $(LNAME2 examples, Examples))
488+
488489
$(H3 $(LNAME2 aa_example, Associative Array Example: word count))
489490

490491
$(P Let's consider the file is ASCII encoded with LF EOL.
491492
In general case we should use $(I dchar c) for iteration
492493
over code points and functions from $(LINK2 $(ROOT_DIR)phobos/std_uni.html,std.uni).
493494
)
494495

496+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
495497
---------
498+
import std.algorithm : sort;
496499
import std.file; // D file I/O
497500
import std.stdio;
498501
import std.ascii;
499502

500-
void main (string[] args)
503+
void main(string[] args)
501504
{
502505
ulong totalWords, totalLines, totalChars;
503506
ulong[string] dictionary;
@@ -507,7 +510,7 @@ $(H3 $(LNAME2 aa_example, Associative Array Example: word count))
507510
{
508511
ulong wordCount, lineCount, charCount;
509512

510-
foreach(line; File(arg).byLine())
513+
foreach (line; File(arg).byLine())
511514
{
512515
bool inWord;
513516
size_t wordStart;
@@ -552,19 +555,21 @@ $(H3 $(LNAME2 aa_example, Associative Array Example: word count))
552555
totalChars += charCount;
553556
}
554557

558+
const char[37] hr = '-';
555559
if (args.length > 2)
556560
{
557-
writefln("-------------------------------------\n%8s%8s%8s total",
558-
totalLines, totalWords, totalChars);
561+
writeln(hr);
562+
writefln("%8s%8s%8s total", totalLines, totalWords, totalChars);
559563
}
560564

561-
writeln("-------------------------------------");
565+
writeln(hr);
562566
foreach (word; dictionary.keys.sort)
563567
{
564568
writefln("%3s %s", dictionary[word], word);
565569
}
566570
}
567571
---------
572+
)
568573

569574
$(H3 $(LNAME2 aa_example_iteration, Associative Array Example: counting pairs))
570575

0 commit comments

Comments
 (0)