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

Comments

Projects
None yet
3 participants
@lionel-
Member

lionel- commented Sep 1, 2016

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

@lionel-

This comment has been minimized.

Member

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-

This comment has been minimized.

Member

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

This comment has been minimized.

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-

This comment has been minimized.

@lionel-

This comment has been minimized.

Member

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 from at_bottom() to Mapping over trees Feb 26, 2017

@hadley hadley added the feature label Mar 3, 2017

@hadley

This comment has been minimized.

Member

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

This comment has been minimized.

Member

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