From @tevem@rt|n041 @end|ng |rom gm@||@com  Thu Sep  9 06:05:26 2021
From: @tevem@rt|n041 @end|ng |rom gm@||@com (Steve Martin)
Date: Thu, 9 Sep 2021 00:05:26 -0400
Subject: [Rd] Should Position() use match.fun()?
Message-ID: <CAP=dwz9FY4j77S0Qm+grtX+S=Hjud6UF5+csewX6J4mG2HUHnQ@mail.gmail.com>

Hello,

All of the funprog functions except Position() use match.fun() early
in the body of the function. (Filter() seems to rely on lapply() for
this, but the effect is the same.) In most cases this isn't a problem,
but I can't see why Position() shouldn't look something like

Position2 <- function(f, x, right = FALSE, nomatch = NA_integer_) {
    f <- match.fun(f) # the only difference from Position()
    ind <- if (right) rev(seq_along(x)) else seq_along(x)
    for (i in ind) {
        if (f(x[[i]])) return(i)
    }
    nomatch
}

This would make it consistent with the other funprog functions, and
would mean that Find() and Position() give the same result when
expected

> equals3 <- function(x) x == 3
> Position("equals3", 1:5)
Error in f(x[[i]]) : could not find function "f"
> Position2("equals3", 1:5)
[1] 3
> Find("equals3", 1:5)
[1] 3

Thanks,
Steve



[Rd] Should Position() use match.fun()?
Steve Martin