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

Author

Roman Pahl

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.

Examples


# Numeric Vector
v = 1:3
it = Iterator$new(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

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

# Works by reference for Container
co = Container$new(1, 2, 3)
it = co$iter()
it$get_next()
#> [[1]]
#> [1] 1
#> 
co$discard(2)
#> [1, 3]
it
#> <Iterator> at position 1 / 2 
it$get_value()
#> [[1]]
#> [1] 1
#> 
co$discard(1)
#> [3]
it
#> <Iterator> at position 1 / 1 
it$get_value()
#> [[1]]
#> [1] 3
#> 
it$begin()
#> <Iterator> at position 1 / 1