这个例子是,从每个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. Spark的job调优(1)

    本文翻译之cloudera的博客,本系列有两篇,第二篇看心情了 概论 当我们理解了transformation,action和rdd后,我们就可以写一些基础的spark的应用了,但是如果需要对应用进行 ...

  2. [GO]数组做函数参数

    package main import "fmt" //数组为函数参数,实际上是值传递//实参数据里的每个元素,给形参数组拷贝一份//这里形参的数组其实就是实参的复制品 func ...

  3. LightOJ 1248 Dice (III) (水题,期望DP)

    题意:给出一个n面的色子,问看到每个面的投掷次数期望是多少. 析:这个题很水啊,就是他解释样例解释的太...我鄙视他,,,,, dp[i] 表示 已经看到 i 面的期望是多少,然后两种选择一种是看到新 ...

  4. poj3080 Blue Jeans(暴枚+kmp)

    Description The Genographic Project is a research partnership between IBM and The National Geographi ...

  5. Oracle 定时任务讲解

    前几天,公司的job调度出现了问题,由于权限管的严,没有查看oracle 一些重要的数据字典,后面联系DBA,是由于数据库切换到备机时,参数设置不对,导致db job没有正常调度. 今天刚好有时间,想 ...

  6. react-native-echarts构建的图表出现滚动条并且可以滑动的问题

    前段时间做echarts饼状图,按照官方提供的写法完成以后图表可以出来,但是虚拟机上演示出现了滚动条,并且拖动时就会出现空白,双击会缩小像这样       参考GitHub上给出的方法修改成功: no ...

  7. 微信Token验证

    /// <summary> /// 微信验证 /// </summary> /// <param name="echostr"></par ...

  8. rsync 备份服务搭建(完成)

    rsync服务守护进程 服务器端配置过程: 1. 检查rsync是否安装: rpm -qa rsync 2.添加rsync服务的用户,管理本地目录 useradd-s /sbin/nologin -M ...

  9. CentOS 6.9下PXE+Kickstart无人值守安装操作系统

    一.简介 1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持 ...

  10. 51 nod 1267 4个数和为0

    1267 4个数和为0 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  取消关注 给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出& ...