我应该使用List [A]还是Seq [A]还是其他? [英] Should I use List[A] or Seq[A] or something else?

查看:100
本文介绍了我应该使用List [A]还是Seq [A]还是其他?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个包含一些功能类方法的类。首先,我使用List作为参数和返回类型来编写它们。然后我想:嘿,您也可以使用更通用的类型!因此我用Seq替换了Lists,希望有一天我可以通过提供除list以外的东西来使我的东西更快。



我应该为我的方法和算法写什么结构?有我可以坚持的一般模式吗?这是因为将来可能需要对这些方法进行优化,以防它们会形成瓶颈。



Update



我会尝试更精确一些:
知道您正在使用的操作,例如反转,.tail,直接元素访问或用于理解。我可以选择一种可以提高这些操作效率的类型吗?



更新2



<我很清楚各种任务的具体数据结构的性能。我不知道哪个数据结构可能显示为某些超级类型的子类。



例如,我应该使用TraversableOnce还是IndexedSeq代替List或数组?



其他问题



什么是您的默认列表像数据结构签名?你写

  def a(b:List [A]):List [A] 

  def a(b :TraversableOnce [A]):TraversableOnce [A] 

你能解释为什么吗?

解决方案

我认为,通常来说,应该对参数使用 Seq 并设计方法与 List 一起有效地工作。这样,您的方法就可以在大多数 Seq 实现中正常工作,并且在使用方法之前不必转换seq。



编辑



您的问题中有很多问题。


  1. 那么我应该为哪种通用栈状数据结构编写方法和算法?


    • 我认为这里的答案是列表。它是一个堆栈并且非常快


  2. 我可以选择一种可以提高操作效率的类型吗?


  3. 例如,我应该使用TraversableOnce还是IndexedSeq代替列表还是数组?会给我买什么吗?


    • 某些抽象定义了性能特征,而另一些则没有。例如, IndexedSeq scaladoc说:索引序列支持恒定时间或接近恒定时间的元素访问和长度计算。如果您有一个 IndexedSeq 参数,并且有人通过了一个 IndexedSeq 实现,但该实现没有近恒定时间元素访问 ,那么有人违反了合同,这不是您的问题。


  4. 您默认的类似于列表的数据结构签名是什么?


    • Seq



I was writing a class that contained some functional-esque methods. First I wrote them using List as parameters and return types. Then I thought "Hey, you could also use a more generic type!" so I replaced the Lists with Seq, hoping that I could make my stuff faster one day by feeding them something else than lists.

So which general purpose stack-like data-structure shall I write my methods and algorithms for? Is there a general pattern I can stick to? All this is because the methods might need to get optimized in the future in case they will form a bottle-neck.

Update

I'll try to be a bit more precise: Given you know which operations you are using, like reversing, .tail, direct element access, or for comprehensions. Can I choose a type that will force efficiency on those operations?

Update 2

I'm quite aware of the performance of concrete data structures for various tasks. What I'm not aware of is which data structure might appear as a sub-class of some super type.

For example shall I use TraversableOnce or IndexedSeq instead of List or Array? Will it buy me anything?

Additional Question

What is your default List-like data-structure signature? Do you write

def a(b: List[A]): List[A] 

or

def a(b: TraversableOnce[A]): TraversableOnce[A]

Can you explain why?

解决方案

I think, in general, you should use Seq for your parameters and design your methods to work efficiently with List. This way your methods will work ok with most Seq implementations and you will not have to convert your seqs prior to use your methods.

Edit

Your question has many questions inside.

  1. So which general purpose stack-like data-structure shall I write my methods and algorithms for?
    • I think the answer here is List. It's a stack and it's very fast
  2. Can I choose a type that will force efficiency on those operations?
  3. For example shall I use TraversableOnce or IndexedSeq instead of List or Array? Will it buy me anything?
    • Some abstractions have performance characteristics defined, some others don't. For example IndexedSeq scaladoc says "Indexed sequences support constant-time or near constant-time element access and length computation". If you have an IndexedSeq parameter and someone passes an IndexedSeq implementation that does not have "near-constant time element access", then that someone is breaking the contract and it's not your problem.
  4. What is your default List-like data-structure signature?
    • Seq

这篇关于我应该使用List [A]还是Seq [A]还是其他?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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