Iterator Factory Functions

Share:

Description

iter is a generic function used to create iterator objects.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
iter(obj, ...)

## Default S3 method:
iter(obj, checkFunc=function(...) TRUE, recycle=FALSE,
...)
## S3 method for class 'iter'
iter(obj, ...)
## S3 method for class 'matrix'
iter(obj, by=c('column', 'cell', 'row'), chunksize=1L,
checkFunc=function(...) TRUE, recycle=FALSE, ...)
## S3 method for class 'data.frame'
iter(obj, by=c('column', 'row'),
checkFunc=function(...) TRUE, recycle=FALSE, ...)
## S3 method for class 'function'
iter(obj, checkFunc=function(...) TRUE,
recycle=FALSE, ...)

Arguments

obj

an object from which to generate an iterator.

by

how to iterate.

chunksize

the number of elements of by to return with each call to nextElem.

checkFunc

a function which, when passed an iterator value, return TRUE or FALSE. If FALSE, the value is skipped in the iteration.

recycle

a boolean describing whether the iterator should reset after running through all it's values.

...

additional arguments affecting the iterator.

Value

The iterator.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  # a vector iterator
  i1 <- iter(1:3)
  nextElem(i1)
  nextElem(i1)
  nextElem(i1)

  # a vector iterator with a checkFunc
  i1 <- iter(1:3, checkFunc=function(i) i %% 2 == 0)
  nextElem(i1)

  # a data frame iterator by column
  i2 <- iter(data.frame(x=1:3, y=10, z=c('a', 'b', 'c')))
  nextElem(i2)
  nextElem(i2)
  nextElem(i2)

  # a data frame iterator by row
  i3 <- iter(data.frame(x=1:3, y=10), by='row')
  nextElem(i3)
  nextElem(i3)
  nextElem(i3)

  # a function iterator
  i4 <- iter(function() rnorm(1))
  nextElem(i4)
  nextElem(i4)
  nextElem(i4)