2016-09-02 3 views
1

스칼라 접근 목록 객체를 평가하고 그들에게스칼라 목록 개체에 액세스하고 그들에게

을 평가 내가 개체에 액세스하려면, 예제 목록 목록에서

val example = List(ItemDesc(6164,6165,6166,6195,The values are correct), ItemDesc(14879,14879,14879,14894,The values are ok), ItemDesc(19682,19690,19682,19694,The values are good)) 

이 'ItemDesc'. 그리고 짝수와 홀수를 얻으십시오. 목록

ItemDesc 상기 제 오브젝트를 가지고 예를 들어

,

I 알아하려는

evenPlacesCount = 6164 (6164,6165,6165,6195, 숫자가 올바른지) 6166

oddPlacesCount = 6165 +

나는 그것이 일반 목록 (6164,6165,6166,6195 경우 평가하는 코드가 6195, 값은 correc 있습니다 티).

위의 '예제'와 같은 List 개체가있는 경우 어떻게 계산합니까?

+0

ItemDesc의 유형은 무엇입니까? 그것은 사례 수업입니까? – Samar

+0

"일반 목록인지 평가할 코드가 있습니다."평가할 코드를보고 싶습니다. – Kakaji

+0

@Phani가 해결 되었습니까? – slouc

답변

1

이와 비슷한?

case class ItemDesc(a: Int, b: Int, c: Int, d: Int, desc: String) 
case class Pair(even: Int, odd: Int) 

val example = List(
    ItemDesc(6164, 6165, 6166, 6195, "The values are correct"), 
    ItemDesc(14879, 14879, 14879, 14894, "The values are ok"), 
    ItemDesc(19682, 19690, 19682, 19694, "The values are good") 
) 

def evenOddCount(item: ItemDesc): Pair = Pair(item.a + item.c, item.b + item.d) 
def addEvenOddCounts(first: Pair, second: Pair): Pair = Pair((first.even + second.even), (first.odd + second.odd)) 

val evenOddPair = evenOddCount(ItemDesc(6164, 6165, 6166, 6195, "The values are correct")) // Pair(12330,12360) 
val allPairs = example.map(evenOddCount) // List(Pair(12330,12360), Pair(29758,29773), Pair(39364,39384)) 
val totalPair = example.map(evenOddCount).reduce(addEvenOddCounts) // Pair(81452,81517) 
+0

매우 멋지게 모델링되었습니다. – Samar