Skip to content

Instantly share code, notes, and snippets.

@infotroph
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infotroph/58608607659a5ce7a989 to your computer and use it in GitHub Desktop.
Save infotroph/58608607659a5ce7a989 to your computer and use it in GitHub Desktop.
short-circuiting or (not?) in any()
> 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