2013-05-17 1 views
1
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) 
} 

답변

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)) 
관련 문제