ITPub博客

首页 > 应用开发 > Java > Scala的for表达式进阶

Scala的for表达式进阶

原创 Java 作者:541732025 时间:2016-04-25 17:44:28 0 删除 编辑
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/,如需转载,请注明出处,否则将追究法律责任。

上一篇: MySQL锁机制
下一篇: Akka之WordCount
请登录后发表评论 登录
全部评论

注册时间:2013-05-23

  • 博文量
    127
  • 访问量
    479253