Learn R Programming

itertools2 (version 0.1.1)

consume: Consumes the first n elements of an iterator

Description

Advances the iterator n-steps ahead without returning anything.

Usage

consume(iterator, n = 0)

Arguments

iterator
an iterator object
n
The number of elements to consume.

Value

Nothing, i.e., invisible(NULL)

Details

If n is 0, the iterator is consumed entirely. Similarly, if n is larger than the length of the iterator, the iterator is consumed entirely.

Examples

Run this code
it <- iterators::iter(1:10)
# Skips the first 5 elements
consume(it, n=5)
# Returns 6
iterators::nextElem(it)

it2 <- iterators::iter(letters)
# Skips the first 4 elements
consume(it2, 4)
# Returns 'e'
iterators::nextElem(it2)

Run the code above in your browser using DataLab