Skip to content

Commit

Permalink
Rename argument of detect functions to .f
Browse files Browse the repository at this point in the history
So they are documented with the right semantics. Closes tidyverse#348
  • Loading branch information
lionel- committed Jul 19, 2017
1 parent 41be146 commit 9952b0c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 14 deletions.
32 changes: 26 additions & 6 deletions R/find-position.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,42 @@
#'
#' 3:10 %>% detect(is_even, .right = TRUE)
#' 3:10 %>% detect_index(is_even, .right = TRUE)
detect <- function(.x, .p, ..., .right = FALSE) {
.p <- as_mapper(.p, ...)
#'
#'
#' # Since `.f` is passed to as_mapper(), you can supply a
#' # lambda-formula or a pluck object:
#' x <- list(
#' list(1, foo = FALSE),
#' list(2, foo = TRUE),
#' list(3, foo = TRUE)
#' )
#'
#' detect(x, "foo")
#' detect_index(x, "foo")
detect <- function(.x, .f, ..., .right = FALSE, .p) {
if (!missing(.p)) {
warn("`.p` has been renamed to `.f`", "purrr_2.2.3")
.f <- .p
}
.f <- as_mapper(.f, ...)

for (i in index(.x, .right)) {
if (is_true(.p(.x[[i]], ...))) return(.x[[i]])
if (is_true(.f(.x[[i]], ...))) return(.x[[i]])
}
NULL
}

#' @export
#' @rdname detect
detect_index <- function(.x, .p, ..., .right = FALSE) {
.p <- as_mapper(.p, ...)
detect_index <- function(.x, .f, ..., .right = FALSE, .p) {
if (!missing(.p)) {
warn("`.p` has been renamed to `.f`", "purrr_2.2.3")
.f <- .p
}
.f <- as_mapper(.f, ...)

for (i in index(.x, .right)) {
if (is_true(.p(.x[[i]], ...))) return(i)
if (is_true(.f(.x[[i]], ...))) return(i)
}
0L
}
Expand Down
49 changes: 41 additions & 8 deletions man/detect.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions tests/testthat/test-find-position.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ test_that("has_element checks whether a list contains an object", {
expect_true(has_element(list(1, 2), 1))
expect_false(has_element(list(1, 2), 3))
})

test_that("detect functions still work with `.p`", {
is_even <- function(x) x %% 2 == 0
expect_warning(regex = "renamed to `.f`",
expect_identical(detect(1:3, .p = is_even), 2L)
)
expect_warning(regex = "renamed to `.f`",
expect_identical(detect_index(1:3, .p = is_even), 2L)
)
})

0 comments on commit 9952b0c

Please sign in to comment.