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

Standardise list_modify() #918

Merged
merged 3 commits into from
Sep 7, 2022
Merged

Standardise list_modify() #918

merged 3 commits into from
Sep 7, 2022

Conversation

hadley
Copy link
Member

@hadley hadley commented Sep 6, 2022

  • NULL now sets value
  • Don't recurse into non-lists

Fixes #810

* `NULL` now sets value
* Don't recurse into non-lists

Fixes #810
@hadley hadley requested a review from lionel- September 6, 2022 22:21
R/list-modify.R Outdated Show resolved Hide resolved
}
#' @export
#' @rdname list_modify
list_merge <- function(.x, ...) {
list_recurse(.x, list2(...), c)
y <- dots_list(..., .named = NULL, .homonyms = "error")
list_recurse(.x, y, c)
Copy link
Member

Choose a reason for hiding this comment

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

Should we try switching to vec_c()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's a good idea.

x[[i]] <- NULL
} else if (vec_is_list(x_i) && vec_is_list(y_i)) {
list_slice2(x, i) <- list_recurse(x_i, y_i, base_f)
} else {
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 one thing that has always bothered me in these functions is this very flexible base case, i.e. that it accepts mismatches between lists and atoms. Should we require that the list structure of the lhs and rhs match, and fail otherwise?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, that's a good idea. I'll explore.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is ok — it lets you override vectors with lists and vice versa:

library(purrr)

str(list_modify(list(x = list(1, 2)), x = 2))
#> List of 1
#>  $ x: num 2
str(list_modify(list(x = 2), x = list(1, 2)))
#> List of 1
#>  $ x:List of 2
#>   ..$ : num 1
#>   ..$ : num 2

Created on 2022-09-07 with reprex v2.0.2

@hadley hadley marked this pull request as ready for review September 7, 2022 12:23
@hadley hadley merged commit c958c72 into main Sep 7, 2022
@hadley hadley deleted the list-modify branch September 7, 2022 18:22
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.

list_modify removes elements when replacement is NULL (aka zaps)
2 participants