Skip to content
Back to formatted view

Raw Message

Message-ID: <e390c0c5c60cfa5855549ed1ef8999b2.squirrel@www.statistik.tu-dortmund.de>
Date: 2010-08-30T00:01:47Z
From: Olaf Mersmann
Subject: Surprising behavior of Negate()

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