1- # ' Iterator of neverending numeric sequence with initial value and step size
1+ # ' Iterator of numeric sequence with initial value and step size
22# '
3- # ' Constructs an iterator that generates a neverending sequence of evenly spaced
4- # ' values starting with \code{icount}. The step size is given by \code{step}.
3+ # ' Constructs an iterator that generates a sequence of evenly spaced values
4+ # ' starting with \code{icount}. The step size is given by \code{step}. By
5+ # ' default, the sequence is neverending unless the optional \code{stop} is
6+ # ' provided.
57# '
68# ' NOTE: Use a negative \code{step} size to generate decreasing sequences.
79# '
1113# ' @export
1214# ' @param start sequence's initial value
1315# ' @param step sequence's step size
16+ # ' @param stop optional stopping point to sequence
1417# ' @return sequence's iterator
1518# '
1619# ' @examples
2326# ' iterators::nextElem(it2)
2427# ' iterators::nextElem(it2)
2528# ' iterators::nextElem(it2)
29+ # '
30+ # ' it3 <- icount(start=1, stop=3)
31+ # ' iterators::nextElem(it3)
32+ # ' iterators::nextElem(it3)
33+ # ' iterators::nextElem(it3)
34+ # '
2635icount <- function (start = 0 , step = 1 , stop = NULL ) {
2736 start <- as.numeric(start )
2837 step <- as.numeric(step )
29- if (! is.null(stop )) {
30- stop <- as.numeric(stop )
31- }
3238
3339 if (length(start ) != 1 ) {
3440 stop(" 'start' must be a numeric value of length 1" )
@@ -37,6 +43,13 @@ icount <- function(start=0, step=1, stop=NULL) {
3743 stop(" 'step' must be a numeric value of length 1" )
3844 }
3945
46+ if (! is.null(stop )) {
47+ stop <- as.numeric(stop )
48+ if (length(stop ) != 1 ) {
49+ stop(" 'stop' must be a numeric value of length 1" )
50+ }
51+ }
52+
4053 current_val <- start - step
4154 nextElem <- function () {
4255 current_val <<- current_val + step
0 commit comments