inst/examples/itimer.R

library(iterors)

# Returns an iterator that limits another iterator based on time
itimer <- function(it, time) {
  it <- iteror(it)
  start <- proc.time()[[3]]

  nextOr <- function(or) {
    current <- proc.time()[[3]]
    if (current - start >= time)
      or
    else nextOr(it, or)
  }

  iteror(nextOr_)
}

# Create a iterator that counts for one second
it <- itimer(icount(Inf), 1)
repeat {
  print(nextOr(it, break))
}
cat('timer expired\n')

Try the iterors package in your browser

Any scripts or data that you put into this service are public.

iterors documentation built on May 31, 2023, 5:36 p.m.