Scala的for表达式进阶

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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28912557/viewspace-2088057/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值