是否可以使用'yield'生成'Iterator'而不是Scala中的列表? [英] Is it possible to use 'yield' to generate 'Iterator' instead of a list in Scala?

查看:135
本文介绍了是否可以使用'yield'生成'Iterator'而不是Scala中的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不评估每个值的情况下将yield用作迭代器?

Is it possible to use yield as an iterator without evaluation of every value?

当易于实现复杂的列表生成时,这是一项常见任务,然后您需要将其转换为Iterator,因为您不需要某些结果...

It is a common task when it is easy to implement complex list generation, and then you need to convert it into Iterator, because you don't need some results...

推荐答案

好的.实际上,我在下面列出了三个非限制性的选项.对于示例,假设:

Sure. Actually, there are three options for non-strictness, which I list below. For the examples, assume:

val list = List.range(1, 10)
def compute(n: Int) = {
    println("Computing "+n)
    n * 2
}

  1. Stream. Stream是一个延迟计算的列表.它将按需计算值,但是一旦计算出值就不会重新计算.如果您将多次重用部分流,这将非常有用.例如,运行下面的代码将分别打印计算1",计算2"和计算3".

  1. Stream. A Stream is a lazily evaluated list. It will compute values on demand, but it will not recompute values once they have been computed. It is most useful if you'll reuse parts of the stream many times. For example, running the code below will print "Computing 1", "Computing 2" and "Computing 3", one time each.

val stream = for (n <- list.toStream) yield compute(n)
val third = stream(2)
println("%d %d" format (third, stream(2)))

  • 视图.视图是基本集合上的操作的组合.检查视图时,将按需计算每个检查的元素.如果您将随机访问视图,但永远只看其中的一小部分,这将非常有用.例如,运行下面的代码将两次打印"Computing 3",而没有其他结果(除了结果).

  • A view. A view is a composition of operations over a base collection. When examining a view, each element examined is computed on-demand. It is most useful if you'll randomly access the view, but will never look but at a small part of it. For example, running the code below will print "Computing 3" two times, and nothing else (well, besides the result).

    val view = for (n <- list.view) yield compute(n)
    val third = view(2)
    println("%d %d" format (third, view(2)))
    

  • Iterator. Iterator是用于懒惰地遍历集合的东西.可以这么说,人们可以将其视为一次性"收藏.它既不会重新计算也不会存储任何元素-一旦计算"了元素,就不能再次使用它.因此,使用起来有些棘手,但是鉴于这些限制,它是最有效的方法.例如,下面的示例需要有所不同,因为Iterator不支持索引访问(如果以此方式编写,视图将表现不佳),并且下面的代码显示"Computing 1","Computing 2","Computing 3" ",计算4",计算5"和计算6".另外,它在末尾打印两个不同的数字.

  • Iterator. An Iterator is something that is used to lazily walk through a collection. One can think of it as a "one-shot" collection, so to speak. It will neither recompute nor store any elements -- once an element has been "computed", it cannot be used again. It is a bit more tricky to use because of that, but it is the most efficient one given these constraints. For example, the following example needs to be different, because Iterator does not support indexed access (and view would perform badly if written this way), and the code below prints "Computing 1", "Computing 2", "Computing 3", "Computing 4", "Computing 5" and "Computing 6". Also, it prints two different numbers at the end.

    val iterator = for (n <- list.iterator) yield compute(n)
    val third = iterator.drop(2).next
    println("%d %d" format (third, iterator.drop(2).next))
    

  • 这篇关于是否可以使用'yield'生成'Iterator'而不是Scala中的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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