1
- # ' Iterator of neverending numeric sequence with initial value and step size
1
+ # ' Iterator of numeric sequence with initial value and step size
2
2
# '
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.
5
7
# '
6
8
# ' NOTE: Use a negative \code{step} size to generate decreasing sequences.
7
9
# '
11
13
# ' @export
12
14
# ' @param start sequence's initial value
13
15
# ' @param step sequence's step size
16
+ # ' @param stop optional stopping point to sequence
14
17
# ' @return sequence's iterator
15
18
# '
16
19
# ' @examples
23
26
# ' iterators::nextElem(it2)
24
27
# ' iterators::nextElem(it2)
25
28
# ' iterators::nextElem(it2)
29
+ # '
30
+ # ' it3 <- icount(start=1, stop=3)
31
+ # ' iterators::nextElem(it3)
32
+ # ' iterators::nextElem(it3)
33
+ # ' iterators::nextElem(it3)
34
+ # '
26
35
icount <- function (start = 0 , step = 1 , stop = NULL ) {
27
36
start <- as.numeric(start )
28
37
step <- as.numeric(step )
29
- if (! is.null(stop )) {
30
- stop <- as.numeric(stop )
31
- }
32
38
33
39
if (length(start ) != 1 ) {
34
40
stop(" 'start' must be a numeric value of length 1" )
@@ -37,6 +43,13 @@ icount <- function(start=0, step=1, stop=NULL) {
37
43
stop(" 'step' must be a numeric value of length 1" )
38
44
}
39
45
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
+
40
53
current_val <- start - step
41
54
nextElem <- function () {
42
55
current_val <<- current_val + step
0 commit comments