= Solution
A single flag represents whether the only element remains:
``
def unitIter[T](t: T): Iter[T] = new Iter[T] {
private var available = true
def hasNext: Boolean = available
def next(): T = {
require(available, "empty iterator")
available = false
t
}
}
def unitCursor[T](t: T): Cursor[T] = new Cursor[T] {
private var available = true
def hasCurrent: Boolean = available
def current: T = {
require(available, "empty cursor")
t
}
def next(): Unit = {
require(available, "empty cursor")
available = false
}
}
``
The abstract <sequence> is `[t]` while the flag is true and empty afterward. `current` leaves it unchanged; either advance consumes its sole element. \b[Renaming both methods to `unit` would leave identical parameter signatures differing only in result type], which cannot define two such Scala overloads.
Back to article page