Skip to contents

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, ...)

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.

Value

an iteror recycling the values from the underlying iterable.

Details

Originally from the itertools package.

Examples


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

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

it2 <- i_recycle(1:3, times=2)
as.list(it2)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 3
#> 
#> [[4]]
#> [1] 1
#> 
#> [[5]]
#> [1] 2
#> 
#> [[6]]
#> [1] 3
#>