Reading a file of words line by line

mark mark at qtrac.eu
Wed Jan 15 15:34:06 UTC 2020


Thanks for the ideas, I've now reduced the size of the getWords() 
function (even allowing for moving the imports to the top of the 
file) to this:

WordSet getWords(string filename, int wordsize) {
     string bareWord(string line) {
	auto rx = ctRegex!(r"^([a-z]+)", "i");
	auto match = matchFirst(line, rx);
	return match.empty ? "" : match.hit.to!string;
     }
     WordSet words;
     slurp!string(filename, "%s")
	.map!(line => bareWord(line))
	.filter!(word => word.length == wordsize)
	.each!(word => words[word.toUpper] = 0);
     return words;
}

Is this as compact as it _reasonably_ can be?


More information about the Digitalmars-d-learn mailing list