Learn R Programming

container (version 1.0.5)

iterS3: Iterate over Sequences

Description

An Iterator is an object that allows to iterate over sequences. It implements next_iter() and get_value() to iterate and retrieve the value of the sequence it is associated with. For documentation of the methods see Iterator.

Usage

iter(x, ...)

# S3 method for Container iter(x, ...)

# S3 method for default iter(x, ...)

is.iterator(x)

is.iterable(x)

begin(it)

get_value(it)

get_next(it)

has_next(it)

has_value(it)

pos(it)

next_iter(it)

reset_iter(it)

# S3 method for Iterator length(x)

Value

length returns the number of elements that can be iterated over.

Arguments

x

an object of class Iterable or any other R object. In the latter case, x will always be coerced to a base R list prior to creating the Iterator.

...

other parameters passed to or from methods

it

Iterator object

See Also

For the class documentation see Iterator.

Examples

Run this code
# Numeric Vector
v = 1:3
it = iter(v)
it

try(it$get_value())  # iterator does not point at a value

has_value(it)
has_next(it)
next_iter(it)
get_value(it)
get_next(it)
get_next(it)
it
has_next(it)
begin(it)
get_value(it)
reset_iter(it)

# Works on copy of Container
co = container(1, 2, 3)
it = iter(co)
get_next(it)
ref_discard(co, 2)
co
it
get_next(it)
ref_clear(co)
co
it
get_next(it)
begin(it)

Run the code above in your browser using DataLab