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 bc1dec5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ of `[<-`. `modify.default()` is thus a shorthand for `x[] <- map(x, f)`.
* `transpose()` now matches by name if available (#164). You can
override the default choice with the new `.names` argument.

* The function argument of `detect()` and `detect_index()` have been
renamed from `.p` to `.f`. This is because they have mapper
semantics rather than predicate semantics.


# purrr 0.2.2.1

Expand Down
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 bc1dec5

Please sign in to comment.