Learn R Programming

async (version 0.3.2)

run: Execute a generator expression immediately, collecting yielded values.

Description

run(expr) with an expression directly writen, will parse that expression as a coroutine, but then run it without pausing.

Usage

run(
  expr,
  type = list(),
  ...,
  split_pipes = FALSE,
  debugR = FALSE,
  debugInternal = FALSE,
  trace = getOption("async.verbose")
)

Value

If expr contains any yield calls, a vector of the same mode as type; otherwise the return value of expr.

Arguments

expr

A generator expression, same as you would write in gen.

type

A value whose mode will determine the output vector mode (as in vapply.)

...

Undocumented.

split_pipes

See async; defaults to FALSE.

debugR

Will open a browser at the first and subsequent R evaluations allowing single-stepping through user code.

debugInternal

Will set a breakpoint at the implementation level, allowing single-stepping through async package code.

trace

a tracing function.

Details

If the expression contains any calls to yield(), run() will collect all the values passed to yield() and return a list. If the expression contains a yield() but it is never called, run() returns an empty list. If the expression does not contain a yield at all, run returns the expression's final return value.

run(expr) is similar to as.list(gen(expr)), except run(expr) evaluates its expression directly in the calling environment, while gen creates a new enclosed environment to run in.

run is useful if you want to take advantage of coroutine language extensions, such as using for loops over iterators, or using goto() in switch statements, in otherwise synchronous code. If you want to collect a variable-length sequence of values but don't need those features, using collect directly will have better performance.

Examples

Run this code

run(type=0, {
  for (i in iterors::iseq(2, Inf, by=5)) {
    if (i %% 37 == 0) break
    else yield(i)
  }
})

Run the code above in your browser using DataLab