scala中iterable [string]与list [string]的用法

我正在阅读一个scala脚本,发现大量使用了iterable [string]。 例如

def wordFeatures(words: Iterable[String]): Iterable[Vector] = => Try(word2vecModel.transform(w))).filter(_.isSuccess).map(_.get)

我是Scala的新手,我只会直接使用List [String],Array [String],Vector [String],集合,而不是Iterable [String],Iterable [Int]。



  • eet
    eet 回复

    This convenient because Iterable is one of most top level in scala collections hierarchy. Accepting Iterable functions can handle most number specific types independent of concrete realization, functions don't need to know it. It's enough to know what collection can be iterated. So, it's not about memory optimization, it's just implementation of Barbara Liskov substitution principle. helpfull links: