如何在 Scala 2.8 中实现集合? [英] How do I implement a collection in Scala 2.8?

查看:35
本文介绍了如何在 Scala 2.8 中实现集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试编写 API 时,我在 2.8(.0-beta1) 中遇到了 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:

  • 为特定类型的不可变集合添加功能
  • filter 和 map 等所有方法都返回相同类型的集合,而无需覆盖所有内容(这就是我首先选择 2.8 的原因)
  • 通过这些方法获得的所有集合都使用与原始集合相同的参数构造(类似于 SortedSet 如何通过隐式进行排序)
  • 这本身仍然是一个特征,独立于任何集合实现.

此外,我想定义一个默认实现,例如基于 HashSet.特征的伴随对象可能使用这个默认实现.我还不确定是否需要构建器工厂的全部功能来将我的集合类型映射到其他集合类型.

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.

我阅读了 关于集合 API 重新设计的论文 但似乎从那时起事情发生了一些变化,我在那里遗漏了一些细节.我还研究了集合源代码,但我不确定它是否非常一致.

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.

理想情况下,我希望看到的是一个动手教程,它可以一步一步地告诉我我需要的部分,或者是对所有细节的详尽描述,这样我就可以判断自己需要哪些部分.我喜欢Scala 编程"中关于对象相等的章节.:-)
但我感谢任何指向文档或示例的提示,以帮助我更好地理解新的集合设计.

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.

推荐答案

我想看看 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.

这篇关于如何在 Scala 2.8 中实现集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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