recursiveiter {combiter} | R Documentation |
Factory of Iterators defined by Recursive Transition Functions
Description
This is a constructor for custom iterator objects. It requires four functions, "next", "prev", "first", and "last", and additional parameters.
The state of the constructor is characterized by the variable i
.
The "next" and "prev" function must take i
and the parameters
and return the next and previous state variables respectively. The behavior where there is no more state left is arbitrary.
The "first" and "last" functions must take the additional parameters and return the initial and last state variables respectively.
The created object is an iterator of class recursiveiter
, which inherits
abstractiter
and iter
.
It can be used with foreach
and accepts as.list
conversion.
Usage
recursiveiter(nextFunc, prevFunc, firstFunc, lastFunc, ...)
Arguments
nextFunc , prevFunc |
Functions that take the iterator state and the parameters |
firstFunc , lastFunc |
Functions that take the parameters |
... |
additional parameters of the iterator |
Value
iterator object
Examples
fibiter <- recursiveiter(
nextFunc = function(i) if (length(i)==1 && i==0) 1 else
if (length(i)==1 && i==1) c(1,1) else
c(sum(i), i[1]),
prevFunc = NULL, firstFunc = function() 0, lastFunc = function() Inf)
for (k in 1:20) cat(nextElem(fibiter)[1], "")