-
-
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
any(pred, itr) and all(pred, itr) do not short-circuit. #11750
Comments
I think there's an argument for having two functions. The current |
Bikeshed on the name, or do we make it a default argument |
|
Also, this might just be a bug... if it was written to call |
Yeah. Given the way it's being called, I'm not sure the 'evaluate everything' behavior is intentional. I think @lindahua might be able to provide some insight. ETA: given https://groups.google.com/d/msg/julia-users/uGVQvhY64S4/OlztaVGQjW4J, I'm pretty sure this counts as "unintended behavior". |
A possible complication is that
whereas, if it were to call
This might be another advantage since it is enforcing that the predicate return a |
Couldn't |
To relax |
Yes, precisely, and I imagine that that would perform better than having to add a wrapper around the function, just to do |
I don't like the idea of "allowing" non-bool results in the predicate. It appears to violate the documentation:
and it also requires I think it's better to error out if the predicate does not return Bool. You can always typecast f(x) if you need to. |
I think it boils down to an argument over keeping things pure, or making them easier to use... |
Ref #11540 |
It seems to me that predicates should not have side effects, so it's a reasonable optimization to evaluate past the first |
I think I've read several times that it's your responsibility to write Regarding the issue at stake: +1 for short-circuiting |
Did you mean "it's a reasonable optimization to NOT evaluate..."? My
which does the same thing, but it'd be much cleaner with |
Ok, that's a pretty convincing case that this should short-circuit. |
I would very surprised if anyone relied on the non-short-circuit behavior. Having the predicate do some sort of data logging is the only case I can think of. |
... in which case they'd be able to use a standard iterator, right? The thing is this: any() actually apparently DOES short-circuit in some cases: when you have an AbstractArray of Bool with more than 16 values, it will short-circuit (see https://groups.google.com/d/msg/julia-users/uGVQvhY64S4/OlztaVGQjW4J). Surely this wasn't the intended behavior. |
Just so that people are convinced:
|
Since it's uncommon to call If the non-short-circuiting behavior is desired still, would an optional and general mechanism to break from reduce calls be useful? Like clojure's |
@fcard a tip - if you hit |
@tkelman That's neat! I will do that from now on, thanks. |
I'm wondering how many people have learnt this trick from @tkelman. I certainly did not too long ago. Edit: proof #11395 (comment) |
@yuyichao count me in that figure... that's a really great trick. |
I learned it from @hayd originally I think. |
Cool! Thanks for the tip! |
IMO semantically "any" means using ....Or is the issue that you may not want to run this iteratively/in order for performance e.g. it's faster with SIMD but you may run over a few items (which are evaluated after the short-circuit). So not advertising it as short-circuit may be preferable?? :s For some reason (not quite sure why) I'm reminded of python code like:
|
I'm totally sold. If someone wants to make a PR to change this I think it's pretty clear this should short circuit. |
I already know what to change, so let me go ahead and... |
Nice! Test to demonstrate it does short-circuit now? |
@ScottPJones Worked on my local tests. If the tests here pass, it should be alright. Here's the PR: #11774 |
confirmed this is working via #11774. |
From https://groups.google.com/d/msg/julia-users/uGVQvhY64S4/KrBEYsMTUUIJ:
It would be great to be able to short circuit any() and all().
The text was updated successfully, but these errors were encountered: