From olafm at statistik.tu-dortmund.de  Mon Aug 30 02:01:47 2010
From: olafm at statistik.tu-dortmund.de (Olaf Mersmann)
Date: Mon, 30 Aug 2010 02:01:47 +0200 (CEST)
Subject: [Rd] Surprising behavior of Negate()
Message-ID: <e390c0c5c60cfa5855549ed1ef8999b2.squirrel@www.statistik.tu-dortmund.de>

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



[Rd] Surprising behavior of Negate()
Olaf Mersmann