On Thursday, 30 May 2013 at 04:30:11 UTC, Brad Anderson wrote:
[snip]
import std.stdio, std.algorithm, std.array;

void eat(R)(R r) { while(!r.empty) { r.front; r.popFront; } }

void main() {
   size_t[dstring] dic;
   stdin.byLine
           .joiner(" ")
           .array
           .splitter(' ')
           .filter!(w => !w.empty && w !in dic)
           .map!(w => writeln(dic[w.idup] = dic.length, '\t', w))
           .eat;
}

I would have prefered to not use joiner() but working with ranges of ranges of ranges (splitter() on each line) got a bit weird and confusing.

Ok, I guess it's not that weird after all:

void main() {
   size_t[string] dic;
   stdin.byLine
           .map!(l => l.splitter(' ')
                                   .filter!(w => !w.empty && w !in dic)
.map!(w => writeln(dic[w.idup] = dic.length, '\t', w)).eat).eat;
}

Reply via email to