diff --git a/R/reduce.R b/R/reduce.R index d491ae1c..9f561318 100644 --- a/R/reduce.R +++ b/R/reduce.R @@ -7,8 +7,22 @@ #' function that takes two values and returns a single value: reducing #' `f` over `1:3` computes the value `f(f(1, 2), 3)`. #' -#' @inheritParams map -#' @param .y For `reduce2()` and `accumulate2()`, an additional +#' @param ... Additional arguments passed on to the reduce function. +#' +#' We now generally recommend against using `...` to pass additional +#' (constant) arguments to `.f`. Instead use a shorthand anonymous function: +#' +#' ```R +#' # Instead of +#' x |> reduce(f, 1, 2, collapse = ",") +#' # do: +#' x |> reduce(\(x) f(x, 1, 2, collapse = ",")) +#' ``` +#' +#' This makes it easier to understand which arguments belong to which +#' function and will tend to yield better error messages. +#' +#' @param .y For `reduce2()` an additional #' argument that is passed to `.f`. If `init` is not set, `.y` #' should be 1 element shorter than `.x`. #' @param .f For `reduce()`, a 2-argument function. The function will be passed diff --git a/man/reduce.Rd b/man/reduce.Rd index 69ea6388..10e49951 100644 --- a/man/reduce.Rd +++ b/man/reduce.Rd @@ -10,8 +10,6 @@ reduce(.x, .f, ..., .init, .dir = c("forward", "backward")) reduce2(.x, .y, .f, ..., .init) } \arguments{ -\item{.x}{A list or atomic vector.} - \item{.f}{For \code{reduce()}, a 2-argument function. The function will be passed the accumulated value as the first argument and the "next" value as the second argument. @@ -23,15 +21,15 @@ second argument, and the next value of \code{.y} as the third argument. The reduction terminates early if \code{.f} returns a value wrapped in a \code{\link[=done]{done()}}.} -\item{...}{Additional arguments passed on to the mapped function. +\item{...}{Additional arguments passed on to the reduce function. We now generally recommend against using \code{...} to pass additional (constant) arguments to \code{.f}. Instead use a shorthand anonymous function: \if{html}{\out{
}}\preformatted{# Instead of -x |> map(f, 1, 2, collapse = ",") +x |> reduce(f, 1, 2, collapse = ",") # do: -x |> map(\\(x) f(x, 1, 2, collapse = ",")) +x |> reduce(\\(x) f(x, 1, 2, collapse = ",")) }\if{html}{\out{
}} This makes it easier to understand which arguments belong to which @@ -46,7 +44,7 @@ is empty. If missing, and \code{.x} is empty, will throw an error.} \code{"forward"} (the default) or \code{"backward"}. See the section about direction below.} -\item{.y}{For \code{reduce2()} and \code{accumulate2()}, an additional +\item{.y}{For \code{reduce2()} an additional argument that is passed to \code{.f}. If \code{init} is not set, \code{.y} should be 1 element shorter than \code{.x}.} } diff --git a/man/reduce_right.Rd b/man/reduce_right.Rd index f3478e4f..e718ba61 100644 --- a/man/reduce_right.Rd +++ b/man/reduce_right.Rd @@ -13,8 +13,6 @@ reduce2_right(.x, .y, .f, ..., .init) accumulate_right(.x, .f, ..., .init) } \arguments{ -\item{.x}{A list or atomic vector.} - \item{.f}{For \code{reduce()}, a 2-argument function. The function will be passed the accumulated value as the first argument and the "next" value as the second argument. @@ -26,15 +24,15 @@ second argument, and the next value of \code{.y} as the third argument. The reduction terminates early if \code{.f} returns a value wrapped in a \code{\link[=done]{done()}}.} -\item{...}{Additional arguments passed on to the mapped function. +\item{...}{Additional arguments passed on to the reduce function. We now generally recommend against using \code{...} to pass additional (constant) arguments to \code{.f}. Instead use a shorthand anonymous function: \if{html}{\out{
}}\preformatted{# Instead of -x |> map(f, 1, 2, collapse = ",") +x |> reduce(f, 1, 2, collapse = ",") # do: -x |> map(\\(x) f(x, 1, 2, collapse = ",")) +x |> reduce(\\(x) f(x, 1, 2, collapse = ",")) }\if{html}{\out{
}} This makes it easier to understand which arguments belong to which @@ -45,7 +43,7 @@ the accumulation, rather than using \code{.x[[1]]}. This is useful if you want to ensure that \code{reduce} returns a correct value when \code{.x} is empty. If missing, and \code{.x} is empty, will throw an error.} -\item{.y}{For \code{reduce2()} and \code{accumulate2()}, an additional +\item{.y}{For \code{reduce2()} an additional argument that is passed to \code{.f}. If \code{init} is not set, \code{.y} should be 1 element shorter than \code{.x}.} }