def combinations(occurrences: Occurrences): List[Occurrences] = occurrences match {
case List() => List(List())
case occ :: occs =>
for {
**occSub <- (0 to occ._2).map((occ._1, _)).toList**
occsCombination <- combinations(occs)
} yield (occSub :: occsCombination).filter(x => x._2 != 0)
}
问问题
264 次
1 回答
8
.map((occ._1, _))
是 的缩写.map(i => (occ._1, i))
。
对于 0 和 之间的每个元素occ._2
,它创建一个Tuple
如上。所以这会返回一个元组列表,其中第一个元素固定,第二个元素从 0 到occ._2
.
例如:
scala> val occ = (42,5)
occ: (Int, Int) = (42,5)
scala> (0 to occ._2).map(i => (occ._1, i)).toList
res0: List[(Int, Int)] = List((42,0), (42,1), (42,2), (42,3), (42,4), (42,5))
scala> (0 to occ._2).map((occ._1, _)).toList
res1: List[(Int, Int)] = List((42,0), (42,1), (42,2), (42,3), (42,4), (42,5))
于 2013-05-17T11:40:18.477 回答