延迟评估索引序列类型

发布于 2024-12-01 05:31:38 字数 166 浏览 2 评论 0原文

我需要构建一系列从外部资源加载的对象。这种加载是一项昂贵的操作,需要延迟到需要对象时为止。构建集合后,我需要对所包含的对象进行索引访问。 Scala 标准库是否提供适合此用例的集合?如果没有,实施它的最佳方法是什么?

编辑:
索引查找最好是 O(1) 操作。

I need to build a sequence of objects that are loaded from an external resource. This loading being an expensive operation needs to be delayed until the time the objects are needed. After the collection is built, I need an indexed access to the contained objects. Does Scala standard library provide a collection suited to this use case? If not, what will be the best way to implement it?

Edit:

The indexed lookup should preferably be an O(1) operation.

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

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

发布评论

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

评论(3

鹤舞 2024-12-08 05:31:38

奇怪的是,迈尔斯最近对此发表了推文。然后回复指出需要 位于 scalaz 中 Name.scala 的末尾,另一个指向规范的 LazyParameter

对 Need:

import scalaz._
import Scalaz._
val longOp = { var x = 0; () => {println("longOp"); x += 1; x }}
val seq = Seq(Need(longOp()), Need(longOp()))
val sum = seq.map(_.value).sum
val sum = seq.map(_.value).sum

val v = Need(longOp())
val s = v.map("got " + _)
println(s.value)
println(s.value)

prints: 进行了很少的测试,

longOp: () => Int = <function0>
seq: Seq[scalaz.Name[Int]] = List(
  scalaz.Name$anon$2@1066d88, scalaz.Name$anon$2@1011f1f)
longOp
longOp
sum: Int = 3
sum: Int = 3
v: scalaz.Name[Int] = scalaz.Name$anon$2@133ef6a
s: scalaz.Name[java.lang.String] = scalaz.Name$anon$2@11696ec
longOp
got 3
got 3

因此 longOp 在第一次访问值时仅被调用一次。

Strange, Miles recently tweeted about this. A reply then points out to Need at the end of Name.scala in scalaz and another one points to specs' LazyParameter.

Little testing with Need:

import scalaz._
import Scalaz._
val longOp = { var x = 0; () => {println("longOp"); x += 1; x }}
val seq = Seq(Need(longOp()), Need(longOp()))
val sum = seq.map(_.value).sum
val sum = seq.map(_.value).sum

val v = Need(longOp())
val s = v.map("got " + _)
println(s.value)
println(s.value)

prints:

longOp: () => Int = <function0>
seq: Seq[scalaz.Name[Int]] = List(
  scalaz.Name$anon$2@1066d88, scalaz.Name$anon$2@1011f1f)
longOp
longOp
sum: Int = 3
sum: Int = 3
v: scalaz.Name[Int] = scalaz.Name$anon$2@133ef6a
s: scalaz.Name[java.lang.String] = scalaz.Name$anon$2@11696ec
longOp
got 3
got 3

So longOp is only called once on first access of value.

同展鸳鸯锦 2024-12-08 05:31:38

据我所知,标准库中没有任何内容。解决方案可能是为您的对象使用一种惰性包装器:

class Lazy[A](operation: => A) {
  lazy val get = operation
}

然后您可以使用您想要使用的任何类型的集合来构建您的Collection[Lazy[A]

To my knowledge, there's nothing that fit in the standard library. A solution might be to use a kind of Lazy wrapper for your objects:

class Lazy[A](operation: => A) {
  lazy val get = operation
}

And then you can build your Collection[Lazy[A] with any kind of collection you want to use.

丘比特射中我 2024-12-08 05:31:38

听起来您想要一个Stream

It sounds like you want a Stream.

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