如何在 Scala 2.8 中实现集合?
在尝试编写 API 时,我在 2.8(.0-beta1) 中的 Scala 集合中遇到了困难。
基本上我需要的是编写一些东西:
- 向某种类型的不可变集添加功能
- ,其中所有方法(如filter和map)都返回相同类型的集合,而不必覆盖所有内容(这就是我首先选择2.8的原因) )
- 其中通过这些方法获得的所有集合都是使用原始集合具有的相同参数构造的(类似于 SortedSet 如何通过隐式进行排序),
- 这仍然是其本身的一个特征,独立于任何集合实现。
另外,我想定义一个默认实现,例如基于 HashSet。该特征的伴随对象可能使用此默认实现。我还不确定是否需要构建器工厂的全部功能来将我的集合类型映射到其他集合类型。
我读了 关于重新设计集合 API 的论文,但从那时起事情似乎发生了一些变化,我遗漏了其中的一些细节。我还深入研究了集合源代码,但我不确定它是否非常一致。
理想情况下,我希望看到的是一个实践教程,它逐步告诉我我需要的部分,或者是所有细节的广泛描述,以便我可以自己判断我需要哪些部分。我喜欢《Scala 编程》中关于对象相等的章节。 :-)
但我很感谢任何能帮助我更好地理解新系列设计的文档或示例。
In trying to write an API I'm struggling with Scala's collections in 2.8(.0-beta1).
Basically what I need is to write something that:
- adds functionality to immutable sets of a certain type
- where all methods like filter and map return a collection of the same type without having to override everything (which is why I went for 2.8 in the first place)
- where all collections you gain through those methods are constructed with the same parameters the original collection had (similar to how SortedSet hands through an ordering via implicits)
- which is still a trait in itself, independent of any set implementations.
Additionally I want to define a default implementation, for example based on a HashSet. The companion object of the trait might use this default implementation. I'm not sure yet if I need the full power of builder factories to map my collection type to other collection types.
I read the paper on the redesign of the collections API but it seems like things have changed a bit since then and I'm missing some details in there. I've also digged through the collections source code but I'm not sure it's very consistent yet.
Ideally what I'd like to see is either a hands-on tutorial that tells me step-by-step just the bits that I need or an extensive description of all the details so I can judge myself which bits I need. I liked the chapter on object equality in "Programming in Scala". :-)
But I appreciate any pointers to documentation or examples that help me understand the new collections design better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想看看 collection.immutable.BitSet 的实现。它有点分散,重用了 collection.BitSetLike 和 collection.generic.BitSetFactory 中的东西。但它完全按照您指定的方式执行:实现添加新功能的特定元素类型的不可变集。
I'd have a look at the implementation of collection.immutable.BitSet. It's a bit spread out, reusing things from collection.BitSetLike and collection.generic.BitSetFactory. But it does exactly what you specified: implement an immutable set of a certain element type that adds new functionality.