Last active
August 29, 2015 14:00
-
-
Save infotroph/58608607659a5ce7a989 to your computer and use it in GitHub Desktop.
short-circuiting or (not?) in any()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> f <- function() { print('FALSE'); FALSE } | |
# infix logical operators short-circuit as expected | |
> TRUE || f() | |
[1] TRUE | |
> FALSE || f() | |
[1] "FALSE" | |
[1] FALSE | |
> TRUE && f() | |
[1] "FALSE" | |
[1] FALSE | |
> FALSE && f() | |
[1] FALSE | |
# any and all don't seem to short-circuit like I expected | |
> all(FALSE, f()) | |
[1] "FALSE" | |
[1] FALSE | |
> any(TRUE, f()) | |
[1] "FALSE" | |
[1] TRUE | |
# same calls with missing parens on function call | |
> all(FALSE, f) | |
[1] FALSE | |
> any(TRUE, f) | |
[1] TRUE | |
> all(TRUE, f) | |
Error: cannot coerce type 'closure' to vector of type 'logical' | |
In addition: Warning message: | |
In all(TRUE, f) : coercing argument of type 'closure' to logical | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment