nth: Returns the nth item of an iterator

Description Usage Arguments Value Examples

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

1
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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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")

Example output

[1] 5
[1] "e"
[1] NA
[1] "foo"

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