Skip to content

Commit 404bac2

Browse files
authored
Merge pull request #3372 from ntrel/wc
Update word count examples Signed-off-by: Nicholas Wilson <[email protected]> Signed-off-by: Dennis <[email protected]> Merged-on-behalf-of: Dennis <[email protected]>
2 parents b20cf2f + 1880e61 commit 404bac2

File tree

2 files changed

+35
-71
lines changed

2 files changed

+35
-71
lines changed

spec/hash-map.dd

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -488,88 +488,49 @@ $(H2 $(LNAME2 examples, Examples))
488488

489489
$(H3 $(LNAME2 aa_example, Associative Array Example: word count))
490490

491-
$(P Let's consider the file is ASCII encoded with LF EOL.
492-
In general case we should use $(I dchar c) for iteration
493-
over code points and functions from $(LINK2 $(ROOT_DIR)phobos/std_uni.html,std.uni).
494-
)
495-
496-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
491+
$(RUNNABLE_EXAMPLE
492+
$(RUNNABLE_EXAMPLE_STDIN
493+
too many cooks
494+
too many ingredients
495+
)
497496
---------
498-
import std.algorithm : sort;
499-
import std.file; // D file I/O
497+
import std.algorithm;
500498
import std.stdio;
501-
import std.ascii;
502499

503-
void main(string[] args)
500+
void main()
504501
{
505-
ulong totalWords, totalLines, totalChars;
506502
ulong[string] dictionary;
503+
ulong wordCount, lineCount, charCount;
507504

508-
writeln(" lines words bytes file");
509-
foreach (arg; args[1 .. $]) // for each argument except the first one
505+
foreach (line; stdin.byLine(KeepTerminator.yes))
510506
{
511-
ulong wordCount, lineCount, charCount;
512-
513-
foreach (line; File(arg).byLine())
507+
charCount += line.length;
508+
foreach (word; splitter(line))
514509
{
515-
bool inWord;
516-
size_t wordStart;
517-
518-
void tryFinishWord(size_t wordEnd)
519-
{
520-
if (inWord)
521-
{
522-
auto word = line[wordStart .. wordEnd];
523-
++dictionary[word.idup]; // increment count for word
524-
inWord = false;
525-
}
526-
}
527-
528-
foreach (i, char c; line)
529-
{
530-
if (std.ascii.isDigit(c))
531-
{
532-
// c is a digit (0..9)
533-
}
534-
else if (std.ascii.isAlpha(c))
535-
{
536-
// c is an ASCII letter (A..Z, a..z)
537-
if (!inWord)
538-
{
539-
wordStart = i;
540-
inWord = true;
541-
++wordCount;
542-
}
543-
}
544-
else
545-
tryFinishWord(i);
546-
++charCount;
547-
}
548-
tryFinishWord(line.length);
549-
++lineCount;
510+
wordCount += 1;
511+
if (auto count = word in dictionary)
512+
*count += 1;
513+
else
514+
dictionary[word.idup] = 1;
550515
}
551516

552-
writefln("%8s%8s%8s %s", lineCount, wordCount, charCount, arg);
553-
totalWords += wordCount;
554-
totalLines += lineCount;
555-
totalChars += charCount;
517+
lineCount += 1;
556518
}
557519

520+
writeln(" lines words bytes");
521+
writefln("%8s%8s%8s", lineCount, wordCount, charCount);
522+
558523
const char[37] hr = '-';
559-
if (args.length > 2)
560-
{
561-
writeln(hr);
562-
writefln("%8s%8s%8s total", totalLines, totalWords, totalChars);
563-
}
564524

565525
writeln(hr);
566-
foreach (word; dictionary.keys.sort)
526+
foreach (word; sort(dictionary.keys))
567527
{
568528
writefln("%3s %s", dictionary[word], word);
569529
}
570530
}
571531
---------
572532
)
533+
$(P See $(DDLINK wc, wc, wc) for the full version.)
573534

574535
$(H3 $(LNAME2 aa_example_iteration, Associative Array Example: counting pairs))
575536

wc.dd

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ Ddoc
22

33
$(D_S Example: wc,
44

5-
$(P This program is the D version of the classic wc (wordcount) C
5+
$(P This program is the D version of the classic `wc` (wordcount) C
66
program.
77
It serves to demonstrate how to read files, slice arrays,
88
and simple symbol table management with associative arrays.
99
)
1010

11+
$(RUNNABLE_EXAMPLE
1112
----
1213
import std.stdio;
1314
import std.algorithm;
1415

15-
int main(string[] args)
16+
void main(string[] args)
1617
{
1718
ulong wordCount;
1819
ulong lineCount;
@@ -21,20 +22,20 @@ int main(string[] args)
2122
int[string] dictionary;
2223
writeln(" lines words bytes file");
2324

24-
foreach(arg; args[1 .. args.length])
25+
foreach (arg; args[1 .. $])
2526
{
2627
ulong lWordCount;
2728
ulong lCharCount;
2829
ulong lLineCount;
2930

3031
auto file = File(arg);
31-
foreach(line; file.byLine(KeepTerminator.yes))
32+
foreach (line; file.byLine(KeepTerminator.yes))
3233
{
3334
lCharCount += line.length;
34-
foreach(word; splitter(line))
35+
foreach (word; splitter(line))
3536
{
3637
lWordCount += 1;
37-
if(auto count = word in dictionary)
38+
if (auto count = word in dictionary)
3839
*count += 1;
3940
else
4041
dictionary[word.idup] = 1;
@@ -50,22 +51,24 @@ int main(string[] args)
5051
charCount += lCharCount;
5152
}
5253

54+
const char[37] hr = '-';
55+
5356
if (args.length > 2)
5457
{
55-
writefln("--------------------------------------\n%8s%8s%8s total",
56-
lineCount, wordCount, charCount);
58+
writeln(hr);
59+
writefln("%8s%8s%8s total", lineCount, wordCount, charCount);
5760
}
5861

59-
writeln("--------------------------------------");
62+
writeln(hr);
6063

6164
foreach (word; sort(dictionary.keys))
6265
{
6366
writefln("%3s %s", dictionary[word], word);
6467
}
65-
return 0;
6668
}
6769
----
6870
)
71+
)
6972

7073
Macros:
7174
TITLE=Word Count

0 commit comments

Comments
 (0)