基本的に私はこれを変換したい:
def data(block: T => Unit)
ストリームへ(dataToStreamは、この変換を行う架空の関数です):
val dataStream: Stream[T] = dataToStream(data)
この問題は継続することで解決できると思います。
// let's assume that we don't know how data is implemented
// we just know that it generates integers
def data(block: Int => Unit) { for (i <- 0 to 10) block(i) }
// here we can print all data integers
data { i => println(i) }
// >> but what we really want is to convert data to the stream <<
// very dumb solution is to collect all data into a list
var dataList = List[Int]()
data { i => dataList = i::dataList }
// and make a stream from it
dataList.toStream
// but we want to make a lazy, CPU and memory efficient stream or iterator from data
val dataStream: Stream[Int] = dataToStream(data)
dataStream.foreach { i => println(i) }
// and here a black magic of continuations must be used
// for me this magic is too hard to understand
// Does anybody know how dataToStream function could look like?
ありがとう、Dawid