Given a class from a legacy library that behaves like an Iterator such that you can traverse it's content with hasNext and next but it does not implement the Iterator interface:
class LegacyIterator[T](iterable: Iterable[T]) {
val iterator: Iterator[T] = iterable.iterator
def hasNext: Boolean = iterator.hasNext
def next(): T = iterator.next()
}
val lagIter: LegacyIterator[Int] = new LegacyIterator(List(1, 2, 3))
What would be your opinion on the most elegant way to traverse such a data structure - idially inside the scope of the function? I've come up with two versions:
One using @tailrec
@tailrec
def lagIterList(accu: List[Int]): List[Int] =
if (lagIter.hasNext) lagIterList(lagIter.next() :: accu) else accu
val res = lagIterList(Nil).reverse
And one using LazyList
def ll: LazyList[Int] = if(lagIter.hasNext) lagIter.next() #:: ll else LazyList.empty
val res = ll.toList
What would you think would be more Scala idiomatic? The tailrec version is probably slightly faster on larger data structures but the LazyList version makes better use of the collections library.
Or any other ideas?