什么是集合视图?您何时想使用它们?

发布于 2024-09-11 22:52:23 字数 65 浏览 2 评论 0原文

在 Scala 中,您可以为许多(所有?)类型的集合创建视图。

视图到底是什么?视图可用于哪些目的?

In Scala, for many (all?) types of collections you can create views.

What exactly is a view and for which purposes are views useful?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

再可℃爱ぅ一点好了 2024-09-18 22:52:23

视图是集合的非严格版本。这意味着元素是在访问时计算的,而不是像普通集合中那样急切地计算。

以下面的代码为例:

val xs = List.tabulate(5)(_ + 1)
val ys = xs.view map { x => println(x); x * x }

这不会打印任何内容,但每次访问列表都会执行计算并打印值,即每次调用ys.head都会得到1正在打印。如果您想再次获得集合的严格版本,您可以对其调用 force 。在这种情况下,您将看到打印出的所有数字。

视图的一个用途是当您需要遍历一组值时,这些值的计算成本很高,并且一次只需要一个值。此外,视图还允许您通过调用 toStream 来构建惰性序列,这也将缓存计算的元素。

Views are non-strict versions of collections. This means that the elements are calculated at access and not eagerly as in normal collections.

As an example take the following code:

val xs = List.tabulate(5)(_ + 1)
val ys = xs.view map { x => println(x); x * x }

Just this will not print anything but every access to the list will perform the calculation and print the value, i.e. every call to ys.head will result in 1 being printed. If you want to get a strict version of the collection again you can call force on it. In this case you will see all numbers printed out.

One use for views is when you need to traverse a collection of values which are expensive to compute and you only need one value at a time. Also views let you build lazy sequences by calling toStream on them that will also cache the evaluated elements.

孤城病女 2024-09-18 22:52:23

一个用例是当您需要收集元素转换的第一个结果时:

    case class Transform(n: Int) { println("Transform "+n)}
    val list = List(1,2,3,4,5)
    list.view.map(v => Transform(v)).collectFirst{case Transform(3) => println("found")}

打印:

Transform 1
Transform 2
Transform 3
found

While:

    list.map(v => Transform(v)).collectFirst{case Transform(3) => println("found")}

打印:

Transform 1
Transform 2
Transform 3
Transform 4
Transform 5
found

One use case is when you need to collect first result of elements transformation:

    case class Transform(n: Int) { println("Transform "+n)}
    val list = List(1,2,3,4,5)
    list.view.map(v => Transform(v)).collectFirst{case Transform(3) => println("found")}

Prints:

Transform 1
Transform 2
Transform 3
found

While:

    list.map(v => Transform(v)).collectFirst{case Transform(3) => println("found")}

Prints:

Transform 1
Transform 2
Transform 3
Transform 4
Transform 5
found
落日海湾 2024-09-18 22:52:23

请参阅 视图,来自 Scala 2.8 集合 API

默认情况下,Scala 集合的所有转换器都是严格的,但 Stream 除外,它延迟实现了所有转换器方法。然而,有一种基于集合视图的系统方法可以将每个集合转变为惰性集合,反之亦然视图是一种特殊的集合,它代表一些基本集合,但延迟实现所有转换器。

...

您可能想要考虑使用视图有两个原因。首先是性能。您已经看到,通过将集合切换到视图,可以避免构建中间结果。这些节省可能非常重要。

...

第二个用例适用于可变序列的视图。此类视图上的许多转换器函数提供了一个进入原始序列的窗口,然后可以使用该窗口有选择地更新该序列的某些元素。

See Views from Scala 2.8 Collections API.

Scala collections are by default strict in all their transformers, except for Stream, which implements all its transformer methods lazily. However, there is a systematic way to turn every collection into a lazy one and vice versa, which is based on collection views. A view is a special kind of collection that represents some base collection, but implements all transformers lazily.

...

There are two reasons why you might want to consider using views. The first is performance. You have seen that by switching a collection to a view the construction of intermediate results can be avoided. These savings can be quite important.

...

The second use case applies to views over mutable sequences. Many transformer functions on such views provide a window into the original sequence that can then be used to update selectively some elements of that sequence.

东北女汉子 2024-09-18 22:52:23

视图用于惰性计算,但不用于节省内存。

当您针对集合创建视图时,已经为该集合分配了内存。

当使用 val view = Range(1,9).view. 创建视图时,集合已经分配了内存,如果太大,例如Range(1,1000000000 ),OOM无法避免

view is used for lazy computation,but not for saving memory.

When you create a view against a collection, the memory has already been allocated forthe collection.

When creating the view with val view = Range(1,9).view., the collection has already been allocated the memory, if it is too large,say,Range(1,1000000000), OOM can't be avoid

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文