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

Use vec_is_list() in map_depth() #926

Merged
merged 6 commits into from
Sep 12, 2022
Merged

Use vec_is_list() in map_depth() #926

merged 6 commits into from
Sep 12, 2022

Conversation

hadley
Copy link
Member

@hadley hadley commented Sep 9, 2022

Fixes #920

@hadley hadley requested a review from lionel- September 9, 2022 17:19
R/map-if-at.R Outdated
Comment on lines 132 to 141
} else {
if (vec_is_list(.x)) {
map(.x, map_depth_rec, .depth - 1, .f, ..., .ragged = .ragged)
} else {
if (.ragged) {
map(.x, .f, ...)
} else {
abort("List not deep enough")
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Would the control flow be easier to read as a flat if-else chain?

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean within the depth > 1 block?

R/map-if-at.R Outdated
} else if (.depth == 1) {
map(.x, .f, ...)
} else {
if (vec_is_list(.x)) {
Copy link
Member

Choose a reason for hiding this comment

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

Probably we still want to recurse into data frames, since map() treats them as lists?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're the one who wanted vec_is_list() here 😆 But yeah, I think the key the switch from is.atomic() to !is.list(). (And probably include expressions for compatibility with pluck_depth()/pluck())

Copy link
Member

Choose a reason for hiding this comment

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

I do want vec_is_list(), but purrr may require special-casing data frames.

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 maybe we should stick with is.list() for now; it feels like we need to ensure that pluck(), pluck_depth(), and map_depth() should all be consistent.

Or maybe we should have purrr_is_list() which would be vec_is_list(x) || is.data.frame(x)? But that doesn't work for pluck() since it makes sense to (e.g.) pluck elements out of a linear model.

Copy link
Member

Choose a reason for hiding this comment

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

yeah, that's probably safest. Ideally you'd either use accessors or unclass before plucking, with the understanding that you're reaching into unexported internals, but I guess it's too late to be this strict.

hmm... But did is_atomic() use to allow reaching into linear models with map_depth()?

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 so, just because of the way we flipped testing for a leaf vs. testing for recursion.

@hadley
Copy link
Member Author

hadley commented Sep 10, 2022

Need to make a similar change to modify_depth()

@hadley hadley requested a review from lionel- September 11, 2022 17:17
@hadley hadley merged commit ff4dfcb into main Sep 12, 2022
@hadley
Copy link
Member Author

hadley commented Sep 12, 2022

I've merged this since it's a big improvement and effectively orthogonal to what the recursion critera should be.

@@ -63,10 +63,6 @@
* Some mapping functions have now a `.progress` argument to create a
progress bar. See `?progress_bars` (#149).

* purrr is now licensed as MIT (#805).
Copy link
Member Author

Choose a reason for hiding this comment

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

Ooops. Fixed on main.

@hadley hadley deleted the map-depth-leaf branch November 1, 2023 18:14
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.

Use vec_is_list() in map_depth_rec()
2 participants