reactive programming 1.3 Recap- Collections

來源:互聯網
上載者:User

標籤:scala

  1. 集合架構概述

     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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.