[Rd] Surprising behavior of Negate()

Olaf Mersmann olafm at statistik.tu-dortmund.de
Mon Aug 30 02:01:47 CEST 2010


Dear R-developers,

I find the current behavior of Negate() somewhat confusing. It does not
match the passed function 'f' until the returned function is called for
the first time. To see an example of what this can do see the following
(contrived) example:

  f <- function(x) is.integer(x)
  not_f <- Negate(f)

  f <- function(x) is.character(x)

  ## Both should, in my mind, return TRUE:
  not_f(1) == !is.integer(1)
  not_f(1L) == !is.integer(1L)

I propose to change Negate() in the following way:

  ## Easy 'fix':
  Negate <- function(f) {
    f <- match.fun(f)
    function(...) !f(...)
  }

This matches 'f' when Negate() is called and not the first time the return
value is used. If the current behavior is desired, maybe a note in the
documentation could be added to clarify this peculiarity.

Cheers,
Olaf Mersmann



More information about the R-devel mailing list