这个例子是,从每个list中,找到age最大的那个node。

class Node(vName: String, vAge: Int) {
// Entity class
var name: String = vName
var age: Int = vAge
} object TestGenerator { def main(args: Array[String]): Unit = {
test()
} def test(): Unit = {
// This test case is to find out the max age node from each node's list // First, define the node 1,2,3
val node1 = new Node("name-1", 1)
val node2 = new Node("name-2", 2)
val node3 = new Node("name-3", 3) // Second, def some List containing nodes
val list1: List[Node] = List(node1, node2, node3)
val list2: List[Node] = List(node1)
val list3: List[Node] = List(node1, node2)
val list4: List[Node] = List()
val list5: List[Node] = Nil
val list6: List[Node] = null // ==== Test Case 1 ====
// In this test, the generator excluded the Nil and List() and null, and take the "node" out of headOption" which is Option[Node]
// The returns are collected into node as Node val allList: Seq[List[Node]] = Seq(list1, list2, list3, list4, list5, list6) val result1 = for {
list: List[Node] <- allList // The type List[Node] is necessary for this situation, it can help to filter out list6 (null)
node <- list.sortWith(_.age > _.age).headOption
} yield node for (r <- result1) {
println(r.name)
} println("======================================================") // ***************************************************************************** // ==== Test Case 2 ====
// In this test, use get() function to get back the list instead of Seq[List[Node]] def get(i: Int): List[Node] = {
i match {
case 1 => list1;
case 2 => list2;
case 3 => list3;
case 4 => list4;
case 5 => list5;
case 6 => list6;
}
} // Define the array to contain the test lists
// List 1-5 will be used for this test, but list6 (null) cannot be handled in this approach
val arr = List(1, 2, 3, 4, 5) // list6 (null) cannot be handled thus only 1-5 here val result2 = for {
i <- arr
node <- get(i).sortWith(_.age > _.age).headOption
} yield node for (r <- result2) {
println(r.name)
} }
}

Scala 中 for 循环 和 generator 的使用例子的更多相关文章

  1. scala学习手记2 - scala中的循环

    先来看一段Java中的循环: for (int i = 1; i < 4; i++) { System.out.print(i + ","); } 毫无疑问,scala可以让 ...

  2. scala中停止循环的三种方式

    1:使用return关键字 object BreakLoop { //1.使用return关键字 def add():Unit= { for(i <- 1 to 10){ if(i==7){ / ...

  3. Scala 中使用 akka system 的 scheduler 的例子

    这是在scala控制台直接执行的例子.   import akka.actor._ import scala.concurrent.duration._ import scala.concurrent ...

  4. Scala中的If判断&While&For循环

    If 判断: object TestScalaIf { def main(args: Array[String]): Unit = { // val resutlt = judge1(-100) // ...

  5. Scala 中的函数式编程基础(一)

    主要来自 Scala 语言发明人 Martin Odersky 教授的 Coursera 课程 <Functional Programming Principles in Scala>. ...

  6. scala中的call-by-name和call-by-value

    http://www.jianshu.com/p/93eefcb61d4f val和def的区别 在scala中,可以用val和def前缀来定义变量,例如: val x = 1 def y = &qu ...

  7. Scala中 object 和 class的区别

    object 在scala中没有静态方法和静态字段,所以在scala中可以用object来实现这些功能,直接用对象名调用的方法都是采用这种实现方式,例如Array.toString.对象的构造器在第一 ...

  8. Programming In Scala笔记-第七章、Scala中的控制结构

    所谓的内建控制结构是指编程语言中可以使用的一些代码控制语法,如Scala中的if, while, for, try, match, 以及函数调用等.需要注意的是,Scala几乎所有的内建控制结构都会返 ...

  9. scala中option、None、some对象

    转载:http://www.jianshu.com/p/95896d06a94d 1.option类型避免对象是空值,造成空指针异常. 2.None对象表示null,在没有对象返回时使用,some在有 ...

随机推荐

  1. 在Asp.Net中使用amChart统计图

    怎么在自己的ASP.NET页面插入可动态更新的数据统计图呢?网上的资源倒是不少(Fusioncharts.amCharts……),在这些资源中有一个比较好用:amChart,这个工具很炫,还能与用户交 ...

  2. 2.8.2 并发下的ArrayList,以及源码分析

    package 第二章.并发下的ArrayList; import java.util.ArrayList;import java.util.List; /** * Created by zzq on ...

  3. Java 集合框架必记框架图

  4. c++基础之引用reference

    1.何为引用 简单来说就是,比如你换了个新名字,用新名字叫你,你也会答应 2.引用vs指针 -引用没有null,好比你说你换了个新名字,但是新名字是啥总得有点东西 -一旦引用被初始化后就不可以指到另外 ...

  5. .net 特性 Attribute

    public sealed class RemarkAttribute : Attribute { public string Remark { get; set; } // 构造函数 public ...

  6. c# Include 与 用户控件

    <!-- #Include File="~/App_UC/head.bootstrap.aspx --> 这个路径文件可以是你html代码,也可以是应用脚本文件, 原理:跟用户控 ...

  7. 2张图简单分析count(0)与count(*)

    以前一直以为count(0)查询效率比count(*)比较高,原因大概是这么认为count(0)只是第一列进行统计,而count(*)所有列放在一起统计(亲,不要误会,这里不是所有列累加哦) 结果真的 ...

  8. sql server 简单语句整合

    1.去重distinct , group by select distinct userid,username from 表名 select userid,username from 表名 group ...

  9. WinForm中TabControl的使用

    TabControl和TabPage之间有一个默认颜色的边框,很难去除,需要重写TabControl控件重绘区域 public class FullTabControl : TabControl { ...

  10. 编码原则实例------c++程序设计原理与实践(进阶篇)

    编码原则: 一般原则 预处理原则 命名和布局原则 类原则 函数和表达式原则 硬实时原则 关键系统原则 (硬实时原则.关键系统原则仅用于硬实时和关键系统程序设计) (严格原则都用一个大写字母R及其编号标 ...