ichunk: Iterator that returns elements in fixed-length chunks

Description Usage Arguments Details Value Examples

View source: R/ichunk.r

Description

Constructs an iterator that returns elements of an iterable object in fixed-length chunks. If the length of the iterator is not divisible by chunk_size, the remainder of the last block is filled with the value specified in fill.

Usage

1
ichunk(object, chunk_size = 1, fill = NA)

Arguments

object

an iterable object

chunk_size

the number of elements returned per chunk

fill

the value with which to fill the last chunk if the length of the iterator is not divisble by chunk_size

Details

This function corresponds to Python's grouper function. We chose the name ichunk because it more explicitly defines the function's purpose.

Value

each call to nextElem results in a list of length chunk_size

Examples

1
2
3
4
5
6
7
8
9
it <- ichunk(iterators::iter(1:5), chunk_size=2)
# List: list(1, 2, 3)
iterators::nextElem(it)
# List: list(4, 5, NA)
iterators::nextElem(it)

it2 <- ichunk(levels(iris$Species), chunk_size=4, "weeee")
# Returns: list("setosa", "versicolor", "virginica", "weeee")
iterators::nextElem(it2)

itertools2 documentation built on May 30, 2017, 7:17 a.m.