ichunk: Iterator that returns elements in fixed-length chunks

Description Usage Arguments Details Value Examples

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)

Example output

[[1]]
[1] 1

[[2]]
[1] 2

[[1]]
[1] 3

[[2]]
[1] 4

[[1]]
[1] "setosa"

[[2]]
[1] "versicolor"

[[3]]
[1] "virginica"

[[4]]
[1] "weeee"

itertools2 documentation built on May 2, 2019, 3:37 p.m.