通常,如果您创建Stream对象,则会急切地评估头部:
Normally if you create a Stream object, the head will be eagerly evaluated:
scala> Stream( {println("evaluating 1"); 1} , 2, 3) evaluating 1 res63: scala.collection.immutable.Stream[Int] = Stream(1, ?)如果我们在同一条语句中创建要在其之前添加的Stream,那么在连接之前未对头部进行求值似乎有点令人惊讶.即
If we create a Stream to which we prepend in the same statement, it seems slightly surprising that the head is not evaluated prior to the concatenation. i.e.
scala> 0 #:: Stream( {println("evaluating 1"); 1} , 2, 3) res65: scala.collection.immutable.Stream[Int] = Stream(0, ?)(#::是右关联的,是ConsWrapper的前置方法,它是Stream的隐式类.)
(#:: is right-associative and is the prepend method on ConsWrapper, which is an implicit class of Stream.)
在设置0之前如何不评估其头?直到我们从生成的Stream中获取值之后,堆上才不存在尾部Stream(或cons单元格)吗?但是,如果是这样,我们如何在尚不存在的对象上调用#::方法?
How does this not evaluate its head before prepending the 0? Is it that the tail Stream (or cons cell) does not exist on the heap until we take values from the resultant Stream? But if so, how do we call the #:: method on an object that doesn't exist yet?
推荐答案-Xprint:typer是您的朋友,任何时候您都想确切地了解如何评估某些代码或推断类型.
-Xprint:typer is your friend, any time you want to understand exactly how some code is evaluated or types are inferred.
scala -Xprint:typer -e '0 #:: Stream( {println("evaluating 1"); 1} , 2, 3)' val x$1: Int = 0; Stream.consWrapper[Int](Stream.apply[Int]({ println("evaluating 1"); 1 }, 2, 3)).#::(x$1)consWrapper 是名字.因此,即使这样有效:
The parameter of consWrapper is by-name. So even this works:
scala> (1 #:: (sys.error("!!"): Stream[Int])).head res1: Int = 1更多推荐
何时准确评估Stream的负责人?
发布评论