我正在阅读一个scala脚本,发现大量使用了iterable [string]。 例如
def wordFeatures(words: Iterable[String]): Iterable[Vector] = words.map(w => Try(word2vecModel.transform(w))).filter(_.isSuccess).map(_.get)
我是Scala的新手,我只会直接使用List [String],Array [String],Vector [String],集合,而不是Iterable [String],Iterable [Int]。
我想也许使用可迭代会减少内存使用和OOM的可能性,但不确定。
因此,问题是:在scala中传递可迭代对象而不是集合作为函数的参数有什么好处?
This convenient because
Iterable
is one of most top level in scala collections hierarchy. AcceptingIterable
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: