Iterator: Iterator Class

IteratorR Documentation

Iterator Class

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 the standard S3 interface, see iter().

Methods

Public methods


Method new()

Iterator constructor

Usage
Iterator$new(x, .subset = .subset2)
Arguments
x

object to iterate over

.subset

accessor function

Returns

invisibly returns the Iterator object


Method begin()

set iterator to the first element of the underlying sequence unless length of sequence is zero, in which case it will point to nothing.

Usage
Iterator$begin()
Returns

invisibly returns the Iterator object


Method get_value()

get value where the iterator points to

Usage
Iterator$get_value()
Returns

returns the value the Iterator is pointing at.


Method get_next()

get next value

Usage
Iterator$get_next()
Returns

increments the iterator and returns the value the Iterator is pointing to.


Method has_next()

check if iterator has more elements

Usage
Iterator$has_next()
Returns

TRUE if iterator has next element else FALSE


Method has_value()

check if iterator points at value

Usage
Iterator$has_value()
Returns

TRUE if iterator points at value otherwise FALSE


Method length()

iterator length

Usage
Iterator$length()
Returns

number of elements to iterate


Method pos()

get iterator position

Usage
Iterator$pos()
Returns

integer if iterator has next element else FALSE


Method next_iter()

increment iterator

Usage
Iterator$next_iter()
Returns

invisibly returns the Iterator object


Method print()

print method

Usage
Iterator$print()

Method reset_iter()

reset iterator to '0'

Usage
Iterator$reset_iter()
Returns

invisibly returns the Iterator object


Method clone()

The objects of this class are cloneable with this method.

Usage
Iterator$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Author(s)

Roman Pahl

Examples


# Numeric Vector
v = 1:3
it = Iterator$new(v)
it

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

it$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()

# Works by reference for Container
co = Container$new(1, 2, 3)
it = co$iter()
it$get_next()
co$discard(2)
it
it$get_value()
co$discard(1)
it
it$get_value()
it$begin()

container documentation built on Dec. 11, 2022, 5:19 p.m.