Skip to content

Commit aeef897

Browse files
committed
Updated docs about icount's stop argument. #39
1 parent 9dade44 commit aeef897

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# itertools 0.1.3
2+
3+
## Miscellaneous
4+
5+
* `icount()` now has an optional `stop` argument. By default, the sequence is
6+
neverending.
7+
18
# itertools 0.1.2
29

310
## Iterators

R/icount.r

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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
#'
@@ -11,6 +13,7 @@
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
@@ -23,12 +26,15 @@
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+
#'
2635
icount <- 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

man/icount.Rd

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,25 @@
22
% Please edit documentation in R/icount.r
33
\name{icount}
44
\alias{icount}
5-
\title{Iterator of neverending numeric sequence with initial value and step size}
5+
\title{Iterator of numeric sequence with initial value and step size}
66
\usage{
77
icount(start = 0, step = 1, stop = NULL)
88
}
99
\arguments{
1010
\item{start}{sequence's initial value}
1111
1212
\item{step}{sequence's step size}
13+
14+
\item{stop}{optional stopping point to sequence}
1315
}
1416
\value{
1517
sequence's iterator
1618
}
1719
\description{
18-
Constructs an iterator that generates a neverending sequence of evenly spaced
19-
values starting with \code{icount}. The step size is given by \code{step}.
20+
Constructs an iterator that generates a sequence of evenly spaced values
21+
starting with \code{icount}. The step size is given by \code{step}. By
22+
default, the sequence is neverending unless the optional \code{stop} is
23+
provided.
2024
}
2125
\details{
2226
NOTE: Use a negative \code{step} size to generate decreasing sequences.
@@ -34,5 +38,10 @@ it2 <- icount(start=5.5, step=1.5)
3438
iterators::nextElem(it2)
3539
iterators::nextElem(it2)
3640
iterators::nextElem(it2)
41+
42+
it3 <- icount(start=1, stop=3)
43+
iterators::nextElem(it3)
44+
iterators::nextElem(it3)
45+
iterators::nextElem(it3)
3746
}
3847

0 commit comments

Comments
 (0)