On Tuesday, 14 January 2020 at 16:39:16 UTC, mark wrote:
I can't help feeling that the foreach loop's block is rather more verbose than it could be?

    WordSet words;
    auto rx = ctRegex!(r"^[a-z]+", "i");
    auto file = File(filename);
    foreach (line; file.byLine) {
        auto match = matchFirst(line, rx);
        if (!match.empty()) {
auto word = match.hit().to!string; // I hope this assumes UTF-8?
            if (word.length == wordsize) {
                words[word.toUpper] = 0;
    return words;

One thing I picked up during Advent of Code last year was
std.file.slurp, which was great for reading 90% of the input
files from that contest. With that, I'd do this more like

  int[string] words;
  slurp!string("input.txt", "%s").each!(w => words[w] = 0);

Where "%s" is what slurp() expects to find on each line, and
'string' is the type it returns from that. With just a list of
words this isn't very interesting. Some of my uses from the
contest are:

auto input = slurp!(int, int, int)(args[1], "<x=%d, y=%d, z=%d>")
      .map!(p => Moon([p[0], p[1], p[2]])).array;

  Tuple!(string, string)[] input =
      slurp!(string, string)("input.txt", "%s)%s");

Of course if you want to validate the input as you're reading
it, you still have to do extra work, but it could be in a

Reply via email to