Skip to content

Commit

Permalink
Fix for duplicate names in list_recurse() (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgirlich authored and hadley committed Feb 8, 2018
1 parent 2948a60 commit d66b626
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

* `map()` and `modify()` now work with calls and pairlists (#412).

* `list_modify()`, `update_list()` and `list_merge()` now handle duplicate
duplicate argument names correctly (#441, @mgirlich).

# purrr 0.2.4

* Fixes for R 3.1.
Expand Down
9 changes: 5 additions & 4 deletions R/list-modify.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ list_recurse <- function(x, y, base_case) {
}
}
} else if (is_names(x_names) && is_names(y_names)) {
for (nm in y_names) {
if (has_name(x, nm) && is_list(x[[nm]]) && is_list(y[[nm]])) {
x[[nm]] <- list_recurse(x[[nm]], y[[nm]], base_case)
for (i in seq_along(y_names)) {
nm <- y_names[[i]]
if (has_name(x, nm) && is_list(x[[nm]]) && is_list(y[[i]])) {
x[[nm]] <- list_recurse(x[[nm]], y[[i]], base_case)
} else {
x[[nm]] <- base_case(x[[nm]], y[[nm]])
x[[nm]] <- base_case(x[[nm]], y[[i]])
}
}
} else {
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-list-modify-update.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ test_that("lists are replaced recursively", {
)
})

test_that("duplicate names works", {
expect_equal(list_modify(list(x = 1), x = 2, x = 3), list(x = 3))
})



# list_merge --------------------------------------------------------------

Expand All @@ -64,6 +69,9 @@ test_that("list_merge returns the non-empty list", {
expect_equal(list_merge(list(), 2), set_names(list(2), ""))
})

test_that("list_merge handles duplicate names", {
expect_equal(list_merge(list(x = 1), x = 2, x = 3), list(x = 1:3))
})

# update_list ------------------------------------------------------------

Expand Down

0 comments on commit d66b626

Please sign in to comment.