Scala的for表示式進階

541732025發表於2016-04-25
for表示式強大而簡潔的表現力

點選(此處)摺疊或開啟

  1. case class Person(name: String, isMale: Boolean, children: Person*)
  2. object For_Expressive {
  3.     def main(args: Array[String]) {
  4.         val lauren = Person("Lauren", false)
  5.         val rocky = Person("Rocky", true)
  6.         val vivian = Person("Vivian", false, lauren, rocky)
  7.         val persons = List(lauren, rocky, vivian)
  8.        
  9.         val result = persons filter (person => !person.isMale) flatMap(person => 
  10.         (person.children map (child => (person.name, child.name))))
  11.         println(result)
  12.        
  13.         val forResult = for (person <- persons; if !person.isMale; child <- person.children) //scala實質上也會將for迴圈變為map的方式
  14.         yield (person.name, child.name)
  15.         println(forResult)
  16.     }
  17. }

Scala中For表示式的生成器、定義和過濾器

點選(此處)摺疊或開啟

  1. object ForInAction {
  2.     def main(args: Array[String]) {
  3.         val lauren = Person("Lauren", false)
  4.         val rocky = Person("Rocky", true)
  5.         val vivian = Person("Vivian", false, lauren, rocky)
  6.         val persons = List(lauren, rocky, vivian)
  7.    
  8.         val forResult = for {person <- persons; name = person.name; if !person.isMale; child <- person.children} //生成器、定義、過濾器
  9.         yield (person.name, child.name)
  10.         println(forResult)
  11.    
  12.         val content =for(x <- List(1,2,3); y <- List("Hadoop","Spark","Flink")) yield(x,y) //兩個生成器
  13.         println(content)
  14.     }
  15. }



使用For表示式做查詢

點選(此處)摺疊或開啟

  1. case class Book(title : String , authors : List[String])
  2. object For_Query {
  3.     def main(args: Array[String]) {
  4.         val books: List[Book] = List(
  5.         Book("Structure and Interpretation ", List("Abelson , Harold", "Sussman")),
  6.         Book("Principles of Compiler Design",
  7.         List("Aho, Alfred", "Ullman, Jeffrey")),
  8.         Book("Programming in Modula-2", List("Wirth, Niklaus")),
  9.         Book("Introduction to Functional Programming", List("Bird, Richard")),
  10.         Book("The Java Language Specification",
  11.         List("Gosling, James", "Joy, Bill", "Steele, Guy", "Bracha, Gilad")))
  12.        
  13.         //    val result = for(b <- books ; a <- b.authors if a startsWith "Gosling") yield b.title
  14.         val result = for(b <- books if (b.title indexOf "Programming") >= 0 ) yield b.title
  15.         println(result)
  16.     }
  17. }

使用For表示式實現map、flatMap、filter

點選(此處)摺疊或開啟

  1. object For_Advanced {
  2.     def main(args: Array[String]) {}
  3.     def map[A, B](list: List[A], f: A => B): List[B] =
  4.         for(element <- list) yield f(element)
  5.     def flatMap[A, B](list: List[A], f: A => List[B]): List[B] =
  6.         for(x <- list; y <- f(x)) yield y
  7.     def filter[A](list: List[A], f: A => Boolean): List[A] =
  8.         for(elem <- list if f(elem)) yield elem
  9. }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28912557/viewspace-2088057/,如需轉載,請註明出處,否則將追究法律責任。

相關文章