Traversable => Java Iterator

Posted by Andres on Stack Overflow See other posts from Stack Overflow or by Andres
Published on 2012-11-02T09:05:46Z Indexed on 2012/11/02 11:00 UTC
Read the original article Hit count: 227

Filed under:
|

I have a Traversable, and I want to make it into a Java Iterator. My problem is that I want everything to be lazily done. If I do .toIterator on the traversable, it eagerly produces the result, copies it into a List, and returns an iterator over the List.

I'm sure I'm missing something simple here...

Here is a small test case that shows what I mean:

class Test extends Traversable[String] {
      def foreach[U](f : (String) => U) {
         f("1")
         f("2")
         f("3")
         throw new RuntimeException("Not lazy!")
     }
}

val a = new Test
val iter = a.toIterator

© Stack Overflow or respective owner

Related posts about scala

Related posts about scala-collections