Skip to content

as_iterator() is a generic function that transforms its input to an iterator function. The default implementation is as follows:

  • Functions are returned as is.

  • Other objects are assumed to be vectors with length() and [[ methods.

Methods must return functions that implement coro's iterator protocol.

as_iterator() is called by coro on the RHS of in in for loops. This applies within generators, async functions, and loop().

Usage

as_iterator(x)

# S3 method for default
as_iterator(x)

Arguments

x

An object.

Value

An iterable function.

Examples

as_iterator(1:3)
#> function () 
#> {
#>     if (i == n) {
#>         return(exhausted())
#>     }
#>     i <<- i + 1L
#>     x[[i]]
#> }
#> <bytecode: 0x562c34a318d8>
#> <environment: 0x562c34a36d68>

i <- as_iterator(1:3)
loop(for (x in i) print(x))
#> [1] 1
#> [1] 2
#> [1] 3