collect: Iterate over iterator functions

Description Usage Arguments Value See Also Examples

View source: R/iterator-collect.R

Description

loop() and collect() are helpers for iterating over iterator functions such as generators.

Usage

1
2
3
collect(x, n = NULL)

loop(loop)

Arguments

x

An iterator function.

n

The number of elements to collect. If x is an infinite sequence, n must be supplied to prevent an infinite loop.

loop

A for loop expression.

Value

collect() returns a list of values; loop() returns the exhausted() sentinel, invisibly.

See Also

async_collect() for async generators.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
generate_abc <- generator(function() for (x in letters[1:3]) yield(x))
abc <- generate_abc()

# Collect 1 element:
collect(abc, n = 1)

# Collect all remaining elements:
collect(abc)

# With exhausted iterators collect() returns an empty list:
collect(abc)


# With loop() you can use `for` loops with iterators:
abc <- generate_abc()
loop(for (x in abc) print(x))

coro documentation built on Dec. 18, 2020, 1:06 a.m.