標籤:scala
-
集合架構概述
Iterable <--------- Seq, Set, Map
Seq <--------- IndexedSeq, linkedSeq
indexedSeq <------ Vectors, Array ,String
linkedSeq <------- List
2.集合常見操作
map, flatMap, filter
foldLeft, foldRight
3. 實現Map操作
abstract class List[+T] { def map[U](f: T => U): List[U] = this match { case x :: xs => f(x) :: xs.map(f) case Nil => Nil } }
4. ++ 和 :::的不同
package week1object Test { List(1,2) ::: List(3,4) //> res0: List[Int] = List(1, 2, 3, 4) List(1,2) ++ List(3,4) //> res1: List[Int] = List(1, 2, 3, 4) }
:::是右操作符,執行的是prepend操作
++是左操作符,執行的是append操作
5.Scala沒有for迴圈,只有for運算式
scala將for 運算式轉化成map,flatmap或filter來執行
for (x <- e1) yield e2 被轉化成 e1.map(x => e2)
for(x <- e1 if f; s) yield e2 被轉化成 for (x <- e1.withFilter(x =>f); s) yield e2
6.for運算式可以與模式比對一起用
val data: List[JSON] = ... for { JObj(bindings) <- data JSeq(phones) = bindings("phoneNumbers") JObj(phone) <- phones JStr(digits) = phone("number") if digits startsWith "212" } yield (bindings("firstName"), bindings("lastName"))
reactive programming 1.3 Recap- Collections