nth {itertools2}R Documentation

Returns the nth item of an iterator

Description

Returns the nth item of an iterator after advancing the iterator n steps ahead. If the iterator is entirely consumed, the default value is returned instead. That is, if either n > length(iterator) or n is 0, then the iterator is consumed.

Usage

nth(iterator, n, default = NA)

Arguments

iterator

an iterator object

n

The location of the desired element to return

default

The value to return if iterable is consumed, default is NA

Value

The nth element of the iterable or the default value

Examples

it <- iterators::iter(1:10)
# Returns 5
nth(it, 5)

it2 <- iterators::iter(letters)
# Returns 'e'
nth(it2, 5)

it3 <- iterators::iter(letters)
# Returns default value of NA
nth(it3, 42)

it4 <- iterators::iter(letters)
# Returns default value of "foo"
nth(it4, 42, default="foo")

[Package itertools2 version 0.1.1 Index]