Skip to contents

Returns an iterator that counts starting from one.

icountn(vn) takes a vector specifying an array size, and returns an iterator over array indices. Each returned element is a vector the same length as vn, with the first index varying fastest. If vn has a names attribute the output will have the same names.

Usage

icount(count = Inf, ..., recycle = FALSE, chunkSize, chunks)

icountn(vn, ..., recycle = FALSE, chunkSize, chunks, rowMajor = TRUE)

Arguments

count

number of times that the iterator will fire. Use NA or Inf to make an iterator that counts forever.

...

Undocumented

recycle

Whether to restart the count after finishing.

chunkSize

How many valies to return from each call to nextOr().

chunks

How many chunks to split the input. Either chunks or chunkSize may be given but not both.

vn

A vector of integers.

rowMajor

If TRUE (default), the earliest indices will cycle fastest; if FALSE, last indices cycle fastest.

Value

The counting iterator.

Details

Originally from the iterators package.

See also

For more control over starting number and step size, see iseq.

Examples


# create an iterator that counts from 1 to 3.
it <- icount(3)
nextOr(it)
#> [1] 1
nextOr(it)
#> [1] 2
nextOr(it)
#> [1] 3
nextOr(it, NULL)  # expect NULL
#> NULL

x <- icount(5)
repeat print(nextOr(x, break))
#> [1] 1
#> [1] 2
#> [1] 3
#> [1] 4
#> [1] 5

it2 <- icount(100)
all.equal(as.numeric(it2), 1:100)
#> [1] TRUE
as.list(icountn(c(2, 3)))
#> [[1]]
#> [1] 1 1
#> 
#> [[2]]
#> [1] 2 1
#> 
#> [[3]]
#> [1] 1 2
#> 
#> [[4]]
#> [1] 2 2
#> 
#> [[5]]
#> [1] 1 3
#> 
#> [[6]]
#> [1] 2 3
#>