gen {async}R Documentation

Create an iterator using sequential code.

Description

gen({...}) with an expression written in its argument, creates a generator, which acts like a block of code whose execution can pause and resume. From the "inside," a generator looks like you are writing sequential code with loops, branches and such, writing values to the outside world by calling yield(). From the "outside," a generator behaves like an iterator over an indefinite collection.

Usage

gen(expr, ..., split_pipes = FALSE, trace = trace_)

yield(expr)

Arguments

expr

An expression, to be turned into an iterator.

...

Undocumented.

split_pipes

Silently rewrite expressions where "yield" appears in chained calls. See async.

trace

Optional tracing function for debugging. See async.

Details

When nextElem is called on a generator, the generator executes its given expression until it reaches a call to ⁠yield(...).⁠ nextElem returns argument to yield is returne, and the generator's execution state is preserved. The generator will resume on the next call to nextElem().

The generator expression is evaluated in a local environment.

Generators are not based on forking or parallel OS processes; they run in the same thread as their caller. The control flow in a generator is interleaved with that of the R code which queries it.

A generator expression can use any R functions, but a call to yield may only appear in some positions. This package has several built-in pausables, equivalents to R's base control flow functions, such as if, while, tryCatch, ⁠<-⁠, {}, || and so on. A call to yield may only appear in an argument of one of these pausable functions. So this random walk generator:

rwalk <- gen({x <- 0; repeat {x <- yield(x + rnorm(1))}})

is legal, because yield appears within arguments to {}, repeat, and ⁠<-⁠, for which this package has pausable definitions. However, this:

rwalk <- gen({x <- rnorm(1); repeat {x <- rnorm(1) + yield(x)}})

is not legal, because yield appears in an argument to +, which does not have a pausable definition.

Value

An object with class "iterator".


[Package async version 0.2.1 Index]