Skip to content

Commit

Permalink
Return missing when output is length 0
Browse files Browse the repository at this point in the history
Fixes #254
  • Loading branch information
hadley committed Mar 4, 2017
1 parent 8658c40 commit d24d1b5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* Removed `LinkingTo:` dependency on `dplyr` (#247, @krlmlr).

* Recursive index via `as_function()` now returns `missing` when first element
is `NULL`
is `NULL`, or when the element has length 0 (#254).

* When indexing elements (e.g. `map(x, "field")`), `NULL` values are
replaced with `.null` (#231).
Expand Down
5 changes: 1 addition & 4 deletions src/extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,5 @@ SEXP extract_impl(SEXP x, SEXP index, SEXP missing) {
}
}

if (TYPEOF(x) == NILSXP)
return missing;
else
return x;
return (Rf_length(x) > 0) ? x : missing;
}
8 changes: 4 additions & 4 deletions tests/testthat/test-as-function.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ test_that(".null replaces absent values", {
expect_equal(map_dbl(x, "b", .null = NA), c(2, NA, NA))
})

test_that(".null replaces actual NULL values", {
test_that(".null replaces elements with length 0", {
x <- list(
list(a = 1),
list(a = NULL)
list(a = NULL),
list(a = numeric())
)

expect_equal(map_dbl(x, "a", .null = NA), c(1, NA))
expect_equal(map_dbl(x, "a", .null = NA), c(1, NA, NA))
})


Expand Down

0 comments on commit d24d1b5

Please sign in to comment.