Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require strict predicate functions in purrr #591

Merged
merged 6 commits into from
Nov 30, 2018

Conversation

lionel-
Copy link
Member

@lionel- lionel- commented Nov 30, 2018

Closes #470.

keep(), map_if(), and variants now fail with an informative error message when the predicate function dose not return TRUE or FALSE.

This is only soft-deprecated for every() and some() as they were documented to have liberal truthness. Also the unit tests check that these functions return NA when the predicate has returned NA. I think it's best to deprecate this behaviour for consistency with predicate-based functionals within purrr, and because it simplifies the type of these functions (you can count on them returning TRUE/FALSE, missing values have to be dealt beforehand to avoid an early error).

@lionel- lionel- requested a review from hadley November 30, 2018 11:58
@@ -64,6 +64,16 @@ reduce2_right(.x = letters[1:4], .y = paste2, .f = c("-", ".", "-")) # working

## Minor improvements and fixes

* Functions taking predicates (`map_if()`, `keep()`, `some()`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think TRUE or FALSE is probably more accurate and easier to grasp than "scalar logical"

R/detect.R Outdated
@@ -51,7 +51,7 @@ detect <- function(.x, .f, ..., .right = FALSE) {
#' @export
#' @rdname detect
detect_index <- function(.x, .f, ..., .right = FALSE) {
.f <- as_mapper(.f, ...)
.f <- as_predicate(.f, ..., .mapper = TRUE)

for (i in index(.x, .right)) {
if (is_true(.f(.x[[i]], ...))) return(i)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can now remove the is_true()?

@lionel- lionel- force-pushed the fix-predicate-warning branch from 0e9b4d6 to f9ff267 Compare November 30, 2018 14:48
@lionel- lionel- merged commit 7b761a8 into tidyverse:master Nov 30, 2018
@lionel- lionel- deleted the fix-predicate-warning branch November 30, 2018 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants