how to get top N distinct elements from range?
Chris Cain
clcain at uncg.edu
Fri Mar 8 10:34:21 PST 2013
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.
More information about the Digitalmars-d-learn
mailing list