hasNext {iterors} | R Documentation |
Does This Iterator Have A Next Element
Description
wrapped <- ihasnext(obj)
wraps an iteror object with the
ihasNext
class. Then hasNext(wrapped)
will indicate if the
iterator has another element.
Usage
hasNext(obj, ...)
ihasNext(obj, ...)
Arguments
obj |
an iterable |
... |
extra arguments may be passed along to iteror. |
Details
A class ihasNext
was introduced in the itertools
package to try to reduce the boilerplate around extracting the
next value using iterators::nextElem. ihasNext
is included
in iterors
for backward compatibility with iterator code; however,
it is less needed when using the nextOr iteration method, as you can
directly give an action to take at end of iteration.
Value
Logical value indicating whether the iterator has a next element.
Examples
# The bad old style of consuming an iterator in a loop with `nextElem`:
it <- ihasNext(iteror(c('a', 'b', 'c')))
tryCatch(repeat {
print(iterators::nextElem(it))
}, error=function(err) {
if (conditionMessage(err) != "StopIteration")
stop(err)
})
# with ihasNext, this became:
it <- ihasNext(iteror(c('a', 'b', 'c')))
while (hasNext(it))
print(iterators::nextElem(it))
# But using `nextOr` all you need is:
iteror(c('a', 'b', 'c'))
repeat print(nextOr(it, break))
[Package iterors version 1.0 Index]