-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify iterator behaviour (filter is half-lazy) #13712
Comments
I don't understand the multiple evaluation problem. Why is it surprising that collecting a filter of a 1-element collection 5 times calls the function 5 times? That's exactly what I would expect. |
Because it's not symmetric with remove_non_primes(iterable) = filter(is_prime, iterable)
numbers_without_primes = remove_non_primes(numbers)
# `is_prime` will be called twice for each number in `numbers`
variance = mean(map(x->x^2, numbers_without_primes)) - mean(numbers_without_primes)^2 I had an (iterator over an) array of objects that got filtered by an expensive function, then my iterative algorithm went over the filtered array N times, and I did not realize that the expensive function was called N times per object until I profiled. Granted, the above code only has an issue if |
Ok, that example makes it clearer. In fact I think |
Fixed by #18839 |
Filter does not evaluate the filtering function immediately:
However it may evaluate it multiple times,
This is not specified in the docstring:
Jeff mentions in a comment that iterators should be side-effect free. Fair enough. However, my filtering functions take a long time per-element, and I do not want them called many times needlessly. I can put a
collect
in front, but this was a surprise to me, sincemap
is not lazy. Is there a general policy to know where (half-)laziness is to be expected? Thank you.The text was updated successfully, but these errors were encountered: