From e53ac24948e113b4ff9429d5919f99faedba32a5 Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Mon, 8 Jan 2018 07:51:29 +0100 Subject: [PATCH 1/3] Fix list_recurse for duplicate names --- R/list-modify.R | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/R/list-modify.R b/R/list-modify.R index 7ec28f36..6e4395b3 100644 --- a/R/list-modify.R +++ b/R/list-modify.R @@ -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 { From d0cfbe6e607d73a1cc2dc5d0b822b80820dd9f73 Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Mon, 8 Jan 2018 07:52:00 +0100 Subject: [PATCH 2/3] Add tests for duplicate names in list_merge and list_modify --- tests/testthat/test-list-modify-update.R | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/testthat/test-list-modify-update.R b/tests/testthat/test-list-modify-update.R index 4b331a7f..06cc92da 100644 --- a/tests/testthat/test-list-modify-update.R +++ b/tests/testthat/test-list-modify-update.R @@ -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 -------------------------------------------------------------- @@ -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 ------------------------------------------------------------ From beabc62945e08c5da9e46d252c4d36f82c9fad62 Mon Sep 17 00:00:00 2001 From: Maximilian Girlich Date: Wed, 7 Feb 2018 08:00:22 +0100 Subject: [PATCH 3/3] Add news bullet for fixing duplicate arguments in list_recurse() --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 46db04ad..37167053 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,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.