how to get top N distinct elements from range?

bearophile bearophileHUGS at lycos.com
Fri Mar 8 17:12:26 PST 2013


Andrea Fontana:

> Not funny btw :) It's not so easy to find out. And it's not 
> easy to write a "cached map" function if you use classes or 
> some pointers as ElementType...

Not caching _input.front in filter() means that filter() assumes 
the front() of its argument range to act as a pure function (even 
if this isn't enforced by the type system). At least this should 
be documented in std.algorithm ddocs...


Python filter() does not work that way:


from sys import stdout
def fun(x):
     stdout.write("*")
     return x
list(filter(lambda x: True, map(fun, xrange(5))))


Prints:

*****

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list