Consume an entire range
    bearophile 
    bearophileHUGS at lycos.com
       
    Thu May 30 04:38:57 PDT 2013
    
    
  
Brad Anderson:
> 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.
Maybe here it's better to work on lines. Alternatively I don't 
know if you can read the whole input there.
It's usually better to give only pure functions to filter/map, 
because in Bugzilla I've shown those higher order functions don't 
work well otherwise.
So I prefer a terminal function that takes an impure function and 
returns nothing, something like:
...
.filter!(w => !w.empty && w !in dic)
.forEach!((w) { writeln(dic[w.idup] = dic.length, '\t', w); });
Bye,
bearophile
    
    
More information about the Digitalmars-d-learn
mailing list