how to get top N distinct elements from range?

bearophile bearophileHUGS at lycos.com
Fri Mar 8 09:05:23 PST 2013


Andrea Fontana:

> So, is there any lazy way to do it?

Take my code and put it inside a struct, add empty, popFront and 
front.

Otherwise return an impure take of a filter closure from a 
function that keeps the set. Something like (untested):

auto firstDistinct(Range)(Range r, size_t n) {
     bool[ForeachType!(Range)] mySet;
     return f.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