You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have adapted the code below from a Kafka stream processor I am developing, where I discovered the aberrant behavior.
import scala.util.Random
import scala.collection.immutable._
import scala.collection.parallel.CollectionConverters._
def fromBatchGenerator[T](generator: () => Seq[T]): LazyList[T] = {
val generation: LazyList[T] =
LazyList.empty[T].lazyAppendedAll(generator().to(LazyList))
generation.lazyAppendedAll(fromBatchGenerator(generator))
}
def scan[T](mapFn: Int => T): LazyList[T] = {
def batchGenerator(): Seq[T] = {
val records = Range.inclusive(0, Random.nextInt(10)).map(_ => Random.nextInt())
val mappedBatch = records.par.map(mapFn)
mappedBatch.toList
}
fromBatchGenerator(batchGenerator)
}
Say you want to print out continuously. When a reference is maintained in the REPL it works:
val positives = scan(identity).filter(_ > 0)
positives.foreach(println)
Whereas the following just hangs (batchGenerator() is never called):
scan(identity).filter(_ > 0).foreach(println)
Requiring a reference to be maintained to the filtered LazyList means that I cannot write stream processors with LazyList, because they will eventually run out of memory. But I think the problem runs deeper than that, because I actually can get something similar to work without maintaining any REPL references:
I have adapted the code below from a Kafka stream processor I am developing, where I discovered the aberrant behavior.
Say you want to print out continuously. When a reference is maintained in the REPL it works:
Whereas the following just hangs (batchGenerator() is never called):
scan(identity).filter(_ > 0).foreach(println)
Requiring a reference to be maintained to the filtered LazyList means that I cannot write stream processors with LazyList, because they will eventually run out of memory. But I think the problem runs deeper than that, because I actually can get something similar to work without maintaining any REPL references:
Bug?
The text was updated successfully, but these errors were encountered: