有人能解释我**之间的界限吗?我不明白地图是如何工作的

时间:2013-05-17 11:24:41

标签: scala map

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)
}

1 个答案:

答案 0 :(得分: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))