Learn R Programming

iterors (version 1.0)

nth: Returns the nth item of an iteror

Description

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

Usage

nth(obj, n, or, ...)

Value

The nth element of the iteror or the result of forcing or.

Arguments

obj

an iterable.

n

The index of the desired element to return.

or

If the iteror finishes before retuning n elements, this argument will be forced and returned.

...

passed along to iteror constructor.

See Also

take consume collect

Examples

Run this code
it <- iteror(1:10)
# Returns 5
nth(it, 5, NA)

it2 <- iteror(letters)
# Returns 'e'
nth(it2, 5, NA)

it3 <- iteror(letters)
# Returns default value of NA
nth(it3, 42, NA)

it4 <- iteror(letters)
# Returns default value of "foo"
nth(it4, 42, or="foo")

Run the code above in your browser using DataLab