how to get top N distinct elements from range?
bearophile
bearophileHUGS at lycos.com
Fri Mar 8 15:36:44 PST 2013
> You expect that output to contain only 5 stars.
Better seen with this code:
import std.stdio, std.range, std.algorithm, std.traits,
std.random;
auto distinct(Range)(Range r) if (isInputRange!Range) {
bool[ForeachType!Range] mySet;
return r.filter!((k) {
if (k in mySet)
return false;
mySet[k] = true;
return true;
});
}
void main() {
5.iota.map!((_) {
auto x = uniform(0, 10);
write("*");
return x;
}).distinct.array;
}
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list