【发布时间】:2018-06-05 14:01:06
【问题描述】:
我想避免使用 zipped 来创建中间序列,例如:
def flat(as: Seq[Int], bs: Seq[Int], cs: Seq[Int], ds: Seq[Int]): Seq[Int] = for{
(a,b) <- (as, bs).zipped
(c,d) <- (cs, ds).zipped
} yield a + b + c + d
但是 zipped (Tuple2Zipped) flatMap 默认返回 Traversable,而不是 Seq。如果我使用显式 map 和 flatMap 编写相同的代码,我可以给它合适的 canBuildFrom 来输出 Seq。
那么,for-comprehension 如何为 flatMap 选择 canBuildFrom 以及是否可以告诉它使用哪一个?
【问题讨论】:
-
这可能与super-detailed answer here 重复,但tl;博士,对不起。 this here 也很相似,但对于
Maps。