From 2956e6275afa2618499d52355e21f090d4582ff0 Mon Sep 17 00:00:00 2001 From: Lionel Henry Date: Wed, 5 Aug 2020 09:31:56 +0200 Subject: [PATCH] Add `.simplify` argument in `accumulate2()` Closes #774 --- NEWS.md | 4 ++++ R/reduce.R | 10 ++++++++-- man/accumulate.Rd | 4 ++-- tests/testthat/test-reduce.R | 8 ++++---- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index 8ffd670a..6034ee9d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/R/reduce.R b/R/reduce.R index ea969464..f01bfd4b 100644 --- a/R/reduce.R +++ b/R/reduce.R @@ -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) { diff --git a/man/accumulate.Rd b/man/accumulate.Rd index 14c85525..185d4a7e 100644 --- a/man/accumulate.Rd +++ b/man/accumulate.Rd @@ -5,8 +5,6 @@ \alias{accumulate2} \title{Accumulate intermediate results of a vector reduction} \usage{ - -accumulate2(.x, .y, .f, ..., .init) accumulate( .x, .f, @@ -15,6 +13,8 @@ accumulate( .dir = c("forward", "backward"), .simplify = TRUE ) + +accumulate2(.x, .y, .f, ..., .init, .simplify = TRUE) } \arguments{ \item{.x}{A list or atomic vector.} diff --git a/tests/testthat/test-reduce.R b/tests/testthat/test-reduce.R index 6bde58fa..e15f2172 100644 --- a/tests/testthat/test-reduce.R +++ b/tests/testthat/test-reduce.R @@ -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", { @@ -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)", {