何时准确评估Stream的负责人? [英] When exactly is the head of a Stream evaluated?

查看:62
本文介绍了何时准确评估Stream的负责人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,如果您创建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的负责人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆