On Wednesday, 15 January 2020 at 19:50:31 UTC, mark wrote:
I really do need a set for the next part of the program, but
taking your code and ideas I have now reduced the function to
this:
WordSet getWords(string filename, int wordsize) {
WordSet words;
File(filename).byLine
.map!(line => line.until!(not!isAlpha))
.filter!(word => word.count == wordsize)
.each!(word => words[word.to!string.toUpper] = 0);
return words;
}
This is also 4x faster than my version that used a regex --
thanks!
Why did you use string.count rather than string.length?
Your solution is fine, but also
void main () {
auto file = ["word one", "my word", "word"] ;
writeln (uniqueWords(file, 4));
}
auto uniqueWords(string[] file, uint wordsize) {
import std.algorithm, std.array, std.conv, std.functional,
std.uni;
return file
.map!(line => line.until!(not!isAlpha))
.filter!(word => word.count == wordsize)
.map!(word => word.to!string.toUpper)
.array
.sort
.uniq
.map!(x => tuple (x, 0))
.assocArray ;
}