Learn R Programming

container (version 0.3.0)

Iterator: Iterator

Description

An Iterator is an object that allows to iterate over sequences. It implements _next and get to iterate and retrieve the value of the sequence it is associated with.

Usage

Iterator

iter(x)

is.iterator(x)

itbegin(it)

itget(it)

itget_next(it)

itpos(it)

ithas_next(it)

itnext(it)

Arguments

x

iterable object, e.g., list, vector, Container

it

Iterator object

Format

An object of class R6ClassGenerator of length 24.

Constructor

Iterator$new(x)

Iterator interface

begin()

Reset iterator position to 1.

get()

Get value at current iterator position.

get_next()

Get value after incrementing by one.

pos()

Return current iterator position.

has_next()

Return TRUE if there is a next element.

next()

Increment iterator to point at next element.

S3 method interface

itbegin(it)

Reset iterator position to 1.

itget(it)

Get value at current iterator position.

itget_next()

Get value after incrementing by one.

itpos()

Return current iterator position.

ithas_next(it)

Return TRUE if there is a next element.

itnext(it)

Increment iterator to point at next element.

See Also

Iterable, Container, container

Examples

Run this code
# NOT RUN {
# Iterator on primitive list
it <- Iterator$new(list("A", 1, 2))
while(it$has_next()) {
print(it$get_next())
}
it$has_next()   # FALSE
print(it)       # <Iterator> at position 3
it$begin()
print(it)       # <Iterator> at position 0

# Iterator from Container object
d <- deque(1:3)
it <- iter(d)
sum <- 0
while(it$has_next()) {
sum <- sum + it$get_next()
}
print(sum)

# S3 method interface
it <- iter(list("A", 1, 2))
while(ithas_next(it)) {
print(itget_next(it))
}
ithas_next(it)   # FALSE
print(it)       # <Iterator> at position 3
itbegin(it)
print(it)       # <Iterator> at position 0
# }

Run the code above in your browser using DataLab