how to get top N distinct elements from range?

bearophile bearophileHUGS at lycos.com
Fri Mar 8 10:40:49 PST 2013


It needs a constraint:


auto firstDistinct(Range)(Range r, in size_t n)
if (isInputRange!Range) {
     bool[ForeachType!Range] mySet;

     return r.filter!((k) {
         if (k in mySet)
             return false;
         mySet[k] = true;
         return true;
     }).take(n);
}


Bye,
bearophile



More information about the Digitalmars-d-learn mailing list