scala - lazy iterator calls next too many times? -
scala - lazy iterator calls next too many times? -
i'm trying build lazy iterator pulls blocking queue, , have encountered weird problem next()
appears called more times expected. because queue blocking, causes application stuck in cases.
some simplified sample code:
"infinite iterators" should { def mkiter = new iterable[int] { var = 0 override def iterator: iterator[int] = { new iterator[int] { override def hasnext: boolean = true override def next(): int = { = + 1 } } } override def tostring(): string = "lazy" } "return subsets - not lazy" in { val x = mkiter x.take(2).tolist must equal(list(1, 2)) x.take(2).tolist must equal(list(3, 4)) } "return subsets - lazy" in { val x = mkiter x.view.take(2).tolist must equal(list(1, 2)) x.view.take(2).tolist must equal(list(3, 4)) } }
in illustration above, lazy test fails because sec phone call take(2)
returns list(4, 5)
.
given see behaviour both scala 2.10 , 2.11, suspect error mine, i'm not sure i'm missing.
take invalidates iterators. see code illustration @ top of http://www.scala-lang.org/api/2.10.3/index.html#scala.collection.iterator
scala
Comments
Post a Comment