延迟评估索引序列类型
我需要构建一系列从外部资源加载的对象。这种加载是一项昂贵的操作,需要延迟到需要对象时为止。构建集合后,我需要对所包含的对象进行索引访问。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
奇怪的是,迈尔斯最近对此发表了推文。然后回复指出需要 位于 scalaz 中 Name.scala 的末尾,另一个指向规范的 LazyParameter。
对 Need:
prints: 进行了很少的测试,
因此
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:
prints:
So
longOp
is only called once on first access of value.据我所知,标准库中没有任何内容。解决方案可能是为您的对象使用一种惰性包装器:
然后您可以使用您想要使用的任何类型的集合来构建您的
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:
And then you can build your
Collection[Lazy[A]
with any kind of collection you want to use.听起来您想要一个Stream。
It sounds like you want a Stream.