Utilities for writing iterators

Share:

Description

is.iterator indicates if an object is an iterator. end_iteration throws an exception to signal that there are no more values available in an iterator. iteration_has_ended tests an exception to see if it indicates that iteration has ended. new_iterator returns an iterator object.

Usage

1
2
3
4

Arguments

x

any object.

e

a condition object.

nextElem

a function object that takes no arguments.

...

not currently used.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# Manually iterate using the iteration_has_ended function to help
it <- iter(1:3)
tryCatch({
  stopifnot(is.iterator(it))
  repeat {
    print(nextElem(it))
  }
},
error=function(e) {
  if (!iteration_has_ended(e)) {
    stop(e)
  }
})