Consume an entire range

Brad Anderson eco at gnuk.net
Wed May 29 21:41:53 PDT 2013


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;
}


More information about the Digitalmars-d-learn mailing list