I'm trying to understand why the following code using Scala Streams doesn't work:
def main(args: Array[String]): Unit = {
lazy val y : SimNumericStream = y.shift
y.scalstream.take(10).print
}
class SimNumericStream( ss : Stream[Double] ) {
lazy val scalstream = ss
lazy val shift = new SimNumericStream( 0 #:: scalstream )
}
and yet replacing
lazy val y : SimNumericStream = y.shift
by
lazy val y : SimNumericStream = new SimNumericStream( 0 #:: y.scalstream )
works just fine.
I'm looking for a solution that allows to me wrap up operations on Streams inside functions without breaking the lazy evaluation when the streams are self-referential.
在你的第一个版本,您的通话实例SimNumericStream
是一个实例内SimNumericStream
,所以你可以从来没有真正intantiate一个,除非你有一个已经。
I have achieved the effect I would like through the following:
class SimNumericStream(str: =>Stream[Double]) {
def ::(hd: Double) = Stream.cons(hd, str)
def shift = 0.0 :: this
}
implicit def streamToSimNumericStream(str: =>Stream[Double]) = new SimNumericStream(str)
lazy val y: Stream[Double] = y.shift
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.