Skip to contents

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 class 'Container'
iter(x, ...)

# Default S3 method
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 class 'Iterator'
length(x)

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

Value

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

See also

For the class documentation see Iterator.

Examples

# Numeric Vector
v = 1:3
it = iter(v)
it
#> <Iterator> at position 0 / 3 

try(it$get_value())  # iterator does not point at a value
#> Error in it$get_value() : iterator does not point at a value

has_value(it)
#> [1] FALSE
has_next(it)
#> [1] TRUE
next_iter(it)
#> <Iterator> at position 1 / 3 
get_value(it)
#> [1] 1
get_next(it)
#> [1] 2
get_next(it)
#> [1] 3
it
#> <Iterator> at position 3 / 3 
has_next(it)
#> [1] FALSE
begin(it)
#> <Iterator> at position 1 / 3 
get_value(it)
#> [1] 1
reset_iter(it)
#> <Iterator> at position 0 / 3 

# Works on copy of Container
co = container(1, 2, 3)
it = iter(co)
get_next(it)
#> [[1]]
#> [1] 1
#> 
ref_discard(co, 2)
co
#> [1, 3]
it
#> <Iterator> at position 1 / 3 
get_next(it)
#> [[1]]
#> [1] 2
#> 
ref_clear(co)
co
#> []
it
#> <Iterator> at position 2 / 3 
get_next(it)
#> [[1]]
#> [1] 3
#> 
begin(it)
#> <Iterator> at position 1 / 3