Debian Codegraph @

man/promise_reduce.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/utils.R
3 \name{promise_reduce}
4 \alias{promise_reduce}
5 \title{Promise-aware version of Reduce}
6 \usage{
7 promise_reduce(.x, .f, ..., .init)
8 }
9 \arguments{
10 \item{.x}{A vector or list to reduce. (Not a promise.)}
11
12 \item{.f}{A function that takes two parameters. The first parameter will be
13 the "result" (initially \code{.init}, and then set to the result of the most
14 recent call to \code{func}), and the second parameter will be an element of \code{.x}.}
15
16 \item{...}{Other arguments to pass to \code{.f}}
17
18 \item{.init}{The initial result value of the fold, passed into \code{.f} when it
19 is first executed.}
20 }
21 \value{
22 A promise that will resolve to the result of calling \code{.f} on the last
23 element (or \code{.init} if \code{.x} had no elements). If any invocation of \code{.f}
24 results in an error or a rejected promise, then the overall
25 \code{promise_reduce} promise will immediately reject with that error.
26 }
27 \description{
28 Similar to \code{\link[purrr:reduce]{purrr::reduce}} (left fold), but the function \code{.f} is permitted
29 to return a promise. \code{promise_reduce} will wait for any returned promise to
30 resolve before invoking \code{.f} with the next element; in other words, execution
31 is serial. \code{.f} can return a promise as output but should never encounter a
32 promise as input (unless \code{.x} itself is a list of promises to begin with, in
33 which case the second parameter would be a promise).
34 }
35 \examples{
36 # Returns a promise for the sum of e1 + e2, with a 0.5 sec delay
37 slowly_add <- function(e1, e2) {
38 promise(\(resolve, reject) {
39 later::later(\() resolve(e1 + e2), delay = 0.5)
40 })
41 }
42
43 # Prints 55 after a little over 5 seconds
44 promise_reduce(1:10, slowly_add, .init = 0) |>
45 then(print)
46
47 }