何时准确评估Stream的负责人?

编程入门 行业动态 更新时间:2024-10-24 08:20:17
本文介绍了何时准确评估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的负责人?

本文发布于:2023-11-13 10:37:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:负责人   准确   Stream

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!