이 코드는 zipWithIndex와 for-yield 문, if문을 잘 설명하는 코드이다. 


scala> def occurrencesOf[A](elem:A, collection:List[A]):List[Int] = {

     |   for {

     |     (currentElem, index) <- collection.zipWithIndex

     |     if (currentElem == elem)

     |   } yield index

     | }

occurrencesOf: [A](elem: A, collection: List[A])List[Int]


scala>


scala> occurrencesOf(10, List(0,1,2,3,4,10))

res0: List[Int] = List(5)




occurrencesOf(10, List(0,1,2,3,4,10)) 코드를 설명한다.


collection.zipWithIndex 은 다음과 같은 값을 리턴한다.


((List(0), 0), (List(1), 1), (List(2), 2), (List(3), 3), (List(4), 4), (List(10), 10))


그러다다 elem으로 들어온 10 값을 만나면 관련 index 값 10을 리턴한다. 






'scala' 카테고리의 다른 글

[spark] spark streaming job 개발시 유익한 싸이트  (0) 2017.06.07
[spark] parquet 사용 예제  (0) 2017.05.26
[spark] join 예제  (0) 2017.05.23
[spark] where과 filter의 차이  (0) 2017.05.23
[spark2] spark SQL 예제  (0) 2017.05.20
Posted by '김용환'
,