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

Implemented accumulate for recursive folding #145

merged 5 commits into from
Dec 7, 2015


Copy link

Following up on #46 and #11, I implemented a version of accumulate, which adds recursive folding to the package.

The differences between accumulate and reduce are minor. I simply added another argument (accumulate = TRUE) to the call of Reduce. Nonetheless, this change provides a lot of utility.

I have two examples in the documentation. The first shows flooring and capping a dataset, which is an extension of an example in Haskell's scanl documentation. The second example shows a simple simulation of a random walk with drift.

#' rerun(5, rnorm(100)) %>% setNames(paste0("sim", 1:5)) %>%
#' map(~ accumulate(., function(.x, .y) .05 + .x + .y)) %>%
#' map(~ data.frame(value = .x, step = 1:100)) %>%
Copy link

Choose a reason for hiding this comment

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

This could now be map_df(~ data_frame(value = .x, step = 1:100), .id = "simulation")

Copy link

hadley commented Dec 6, 2015

Looks good - thanks! Can you please respond to my super nitpicky comments and add a bullet to NEWS?

Copy link
Contributor Author

Super nitpicky is super helpful! I didn't notice the addition of map_df. That's a great little tool.

I also did a little additional formatting to the examples. Hopefully they'll be a little easier to read. In retrospect, one of my examples didn't illustrate what I expected (the flooring and capping). It's dropped.

I also went ahead and added as_function() for all four. Please let me know if I missed anything.

hadley added a commit that referenced this pull request Dec 7, 2015
Implemented accumulate for recursive folding
@hadley hadley merged commit 6d4efbe into tidyverse:master Dec 7, 2015
Copy link

hadley commented Dec 7, 2015

Looks great - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

None yet

2 participants