Skip to content

Commit

Permalink
Add .simplify argument in accumulate2()
Browse files Browse the repository at this point in the history
  • Loading branch information
lionel- committed Aug 5, 2020
1 parent 5601db2 commit 2956e62
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
possible to disable the simplification by setting `.simplify` to
`FALSE`.

* `accumulate2()` gains a `.simplify` argument as well. It did not use
to simplify its output, but we've changed this default to
automatically simplify for consistency with `accumulate()`.

* `accumulate()` now uses vctrs for simplifying the output. This
ensures a more principled and flexible coercion behaviour.

Expand Down
10 changes: 8 additions & 2 deletions R/reduce.R
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,14 @@ accumulate <- function(.x,
}
#' @rdname accumulate
#' @export
accumulate2 <- function(.x, .y, .f, ..., .init) {
reduce2_impl(.x, .y, .f, ..., .init = .init, .acc = TRUE)
accumulate2 <- function(.x, .y, .f, ..., .init, .simplify = TRUE) {
res <- reduce2_impl(.x, .y, .f, ..., .init = .init, .acc = TRUE)

if (.simplify) {
acc_simplify(res)
} else {
res
}
}

accumulate_names <- function(nms, init, dir) {
Expand Down
4 changes: 2 additions & 2 deletions man/accumulate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/testthat/test-reduce.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ test_that("basic accumulate2() works", {
paste2 <- function(x, y, sep) paste(x, y, sep = sep)

x <- c("a", "b", "c")
expect_equal(accumulate2(x, c("-", "."), paste2), list("a", "a-b", "a-b.c"))
expect_equal(accumulate2(x, c(".", "-", "."), paste2, .init = "x"), list("x", "x.a", "x.a-b", "x.a-b.c"))
expect_equal(accumulate2(x, c("-", "."), paste2), c("a", "a-b", "a-b.c"))
expect_equal(accumulate2(x, c(".", "-", "."), paste2, .init = "x"), c("x", "x.a", "x.a-b", "x.a-b.c"))
})

test_that("can terminate accumulate2() early", {
Expand All @@ -187,8 +187,8 @@ test_that("can terminate accumulate2() early", {
}

x <- c("a", "b", "c")
expect_equal(accumulate2(x, c("-", "."), paste2), list("a", "a-b"))
expect_equal(accumulate2(x, c(".", "-", "."), paste2, .init = "x"), list("x", "x.a", "x.a-b"))
expect_equal(accumulate2(x, c("-", "."), paste2), c("a", "a-b"))
expect_equal(accumulate2(x, c(".", "-", "."), paste2, .init = "x"), c("x", "x.a", "x.a-b"))
})

test_that("accumulate2() forces arguments (#643)", {
Expand Down

0 comments on commit 2956e62

Please sign in to comment.