File: irecycle.R

package info (click to toggle)
r-cran-iterators 1.0.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 532 kB
  • sloc: sh: 29; makefile: 1
file content (24 lines) | stat: -rw-r--r-- 511 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
library(iterators)

# This functions returns an iterator that recycles the values of
# the specified iterator
irecycle <- function(it) {
  values <- as.list(iter(it))
  i <- length(values)
  if (i == 0) stop('iterator must have at least one value')

  nextEl <- function() {
    i <<- i + 1
    if (i > length(values))
      i <<- 1
    values[[i]]
  }

  obj <- list(nextElem=nextEl)
  class(obj) <- c('irecycle', 'abstractiter', 'iter')
  obj
}

it <- irecycle(icount(3))
for (i in 1:9)
  print(nextElem(it))