File: iseq.R

package info (click to toggle)
r-cran-iterators 1.0.7-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 532 kB
  • sloc: sh: 29; makefile: 1
file content (30 lines) | stat: -rw-r--r-- 682 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
library(iterators)

# return an iterator that returns subvectors of a sequence
# of a specified length.
# can specify either "chunks" or "chunkSize" arguments
# since that is what the "idiv" function supports.
iseq <- function(n, ...) {
 i <- 1
 it <- idiv(n, ...)

 nextEl <- function() {
   n <- nextElem(it)
   x <- seq(i, length=n)
   i <<- i + n
   x
 }

 obj <- list(nextElem=nextEl)
 class(obj) <- c('iseq', 'abstractiter', 'iter')
 obj
}

# create a sequence iterator that returns three subvectors
it <- iseq(25, chunks=3)
print(as.list(it))

# create a sequence iterator that returns subvectors
# with a maximum length of 10
it <- iseq(25, chunkSize=10)
print(as.list(it))