File: ilimit.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-- 432 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)

ilimit <- function(it, times) {
  it <- iter(it)

  nextEl <- function() {
    if (times > 0)
      times <<- times - 1
    else
      stop('StopIteration')

    nextElem(it)
  }

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

it <- ilimit(icount(Inf), 3)
print(nextElem(it))
print(nextElem(it))
print(nextElem(it))
print(tryCatch(nextElem(it), error=function(e) e))