how to get top N distinct elements from range?

Andrea Fontana nospam at example.com
Fri Mar 8 08:52:41 PST 2013


On Friday, 8 March 2013 at 16:12:37 UTC, Ivan Kazmenko wrote:
>>>> I wonder if exists a way to get top n distinct elements from 
>>>> a range (infinite too!)
>>>
>>> It's impossible to do that for infinite ranges
>>
>> Why?
>>
>> sequence!"n*2".myTopDistinct!"a==b"(3);
>>
>> will give [2,4,6]
>
> I don't quite get it, do you want the least n elements?  What
> result would you want if the sequence was the following:
>
> sequence!"-n*2".myTopDistinct!"a==b"(3);

Infinite loop as:

sequence!"n*2".filter!"a>1";
and a lot of other cases.

>
> The sequence goes like [-2, -4, -6, -8, -10, ...] ad infinitum.
>
> To clarify a doubt, do you mean "top" as in "front", or "top" 
> as the "greatest" or "least" in terms of comparison?  Or maybe 
> "the first n distinct elements found"?  If so, why "sort" in 
> the original post?  It could bring middle elements to front.

I mean "the first n distinct elements found". Example was a 
mistake. Uniq works with sorted range (if you want to get 
distinct element) so I sort it to make it works. But I should not 
:) Whoops. Bearophile got it btw.

So, is there any lazy way to do it?




More information about the Digitalmars-d-learn mailing list