Learn R Programming

iterors (version 1.0)

i_recycle: Create a recycling iterator

Description

Create an iterator that recycles a specified iterable. On the first repeat the iterable is buffered into memory until it finishes, then we repeat the same sequence of values.

Usage

i_recycle(iterable, times = Inf, ...)

Value

an iteror recycling the values from the underlying iterable.

Arguments

iterable

The iterable to recycle.

times

integer. Number of times to recycle the values . Default value of Inf means to recycle indefinitely.

...

Further arguments will be passed along to iteror.

Details

Originally from the itertools package.

Examples

Run this code

# Recycle over 'a', 'b', and 'c' three times
i <- i_recycle(letters[1:3], 3)
as.character(i)

it <- i_recycle(1:3)
nextOr(it, NA) # 1
nextOr(it, NA) # 2
nextOr(it, NA) # 3
nextOr(it, NA) # 1
nextOr(it, NA) # 2
nextOr(it, NA) # 3
nextOr(it, NA) # 1

it2 <- i_recycle(1:3, times=2)
as.list(it2)

Run the code above in your browser using DataLab