On Friday, 8 March 2013 at 18:17:22 UTC, bearophile wrote:
Otherwise return an impure take of a filter closure from a
function that keeps the set. Something like (untested):
It seems to work:
import std.stdio, std.range, std.algorithm, std.traits;
auto firstDistinct(Range)(Range r, in size_t n) {
bool[ForeachType!Range] mySet;
return r.filter!((k) {
if (k in mySet)
return false;
mySet[k] = true;
return true;
}).take(n);
}
I have to say, that's a great example of beautiful simplicity in
programming. Bravo.