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

Mapping over trees #236

Closed
lionel- opened this issue Sep 1, 2016 · 7 comments
Closed

Mapping over trees #236

lionel- opened this issue Sep 1, 2016 · 7 comments
Labels

Comments

@lionel-
Copy link
Member

@lionel- lionel- commented Sep 1, 2016

at_bottom() would be like at_depth() but at bottom. Useful for tree structures.

@lionel-
Copy link
Member Author

@lionel- lionel- commented Sep 1, 2016

On that note I wonder if the following would be good UI:

array_tree(x, ~last)
array_tree(x, list(1, 2, ~last)

x %>% at_depth(~last, f)
@lionel-
Copy link
Member Author

@lionel- lionel- commented Sep 15, 2016

Other useful idiom for tree structures: visit each node twice. Once on the way in, the second time on the way back. See https://stlab.adobe.com/group__asl__tutorials__forest.html

The mapped function would get a boolean indicating if this is the second time we see the node.

@alistaire47
Copy link

@alistaire47 alistaire47 commented Feb 10, 2017

Would this map over the deepest level or all leaf nodes? The latter (or some other intelligible version of rapply) could be really useful for parsing deeply nested data coming from JSON or the like.

@lionel-
Copy link
Member Author

@lionel- lionel- commented Feb 26, 2017

@alistaire47 yes I was thinking at_bottom() would apply a function to leaves. We could have map_tree() to apply a function to all nodes, then at_bottom() would be implemented as:

x %>% map_tree_if(is_atomic, fn)

And maybe have map_tree_until(), which would not inspect further the nodes for which the predicate returns FALSE, which could be useful for applying a function to list-like S3 objects.

@lionel- lionel- changed the title at_bottom() Mapping over trees Feb 26, 2017
@hadley hadley added the feature label Mar 3, 2017
@hadley
Copy link
Member

@hadley hadley commented Mar 5, 2017

What if instead of at_bottom(.x, .f) you simply did at_depth(.x, -1, .f)?

And what if at_depth() was instead called modify_depth()? And to match the other modify() functions was called modify_depth()?

@hadley
Copy link
Member

@hadley hadley commented Mar 5, 2017

I also wonder about an option to at_depth() that gives you this behaviour:

  recurse <- function(x, depth) {
    if (depth > 1) {
      if (is.atomic(x)) {
        x
      } else {
        lapply(x, recurse, depth = depth - 1)
      }
    } else {
      lapply(x, .f, ...)
    }
  }

So that if the list is ragged, you can choose to only operate on lists at a certain level, ignoring branches that aren't that deep.

@hadley hadley closed this in 3a38083 Mar 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants