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
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.
Method get_value()
get value where the iterator points to
Method get_next()
get next value
Method has_next()
check if iterator
has more elements
Method has_value()
check if iterator
points at value
Method pos()
get iterator position
Method next_iter()
increment iterator
Method reset_iter()
reset iterator to '0'
Method clone()
The objects of this class are cloneable with this method.
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