Scala 中 for 循环 和 generator 的使用例子
这个例子是,从每个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 的使用例子的更多相关文章
- scala学习手记2 - scala中的循环
先来看一段Java中的循环: for (int i = 1; i < 4; i++) { System.out.print(i + ","); } 毫无疑问,scala可以让 ...
- scala中停止循环的三种方式
1:使用return关键字 object BreakLoop { //1.使用return关键字 def add():Unit= { for(i <- 1 to 10){ if(i==7){ / ...
- Scala 中使用 akka system 的 scheduler 的例子
这是在scala控制台直接执行的例子. import akka.actor._ import scala.concurrent.duration._ import scala.concurrent ...
- Scala中的If判断&While&For循环
If 判断: object TestScalaIf { def main(args: Array[String]): Unit = { // val resutlt = judge1(-100) // ...
- Scala 中的函数式编程基础(一)
主要来自 Scala 语言发明人 Martin Odersky 教授的 Coursera 课程 <Functional Programming Principles in Scala>. ...
- scala中的call-by-name和call-by-value
http://www.jianshu.com/p/93eefcb61d4f val和def的区别 在scala中,可以用val和def前缀来定义变量,例如: val x = 1 def y = &qu ...
- Scala中 object 和 class的区别
object 在scala中没有静态方法和静态字段,所以在scala中可以用object来实现这些功能,直接用对象名调用的方法都是采用这种实现方式,例如Array.toString.对象的构造器在第一 ...
- Programming In Scala笔记-第七章、Scala中的控制结构
所谓的内建控制结构是指编程语言中可以使用的一些代码控制语法,如Scala中的if, while, for, try, match, 以及函数调用等.需要注意的是,Scala几乎所有的内建控制结构都会返 ...
- scala中option、None、some对象
转载:http://www.jianshu.com/p/95896d06a94d 1.option类型避免对象是空值,造成空指针异常. 2.None对象表示null,在没有对象返回时使用,some在有 ...
随机推荐
- Docker学习之路(二)DockerFile详解
Dockerfile是一个镜像的表示,可以通过Dockerfile来描述构建镜像的步骤,并自动构建一个容器 所有的 Dockerfile 命令格式都是: INSTRUCTION arguments 虽 ...
- C语言实践 输出100以内的素数
int main() { int isprime = 1; for (int i = 2; i < 101; i++) { isprime = 1;//要确保每次循环都要把这个值设置为1,不然上 ...
- 9.TOP 子句--mysql limit
TOP 子句 TOP 子句用于规定要返回的记录的数目. 对于拥有数千条记录的大型表来说,TOP 子句是非常有用的. 注释:并非所有的数据库系统都支持 TOP 子句. MySQL 语法 SELECT c ...
- 在线破解PDF
在线破解PDF密码的6个网站 有很多PDF文件只能查看却不能被编辑和打印,因为它们已被保护.你并不知道被保护的PDF文档的密码却又急着向上司交差,怎么办?让 Ap PDF Password Recov ...
- Qt工程文件Pro介绍(转)
转载请注明:http://blog.163.com/hu_cuit/blog/static/122849143201127104232142/ 我也才开始学习QT的菜鸟.但是前几天有同学叫我给他讲一下 ...
- HTML 5+CSS 3网页设计经典范例 (李俊民,黄盛奎) 随书光盘
<html 5+css 3网页设计经典范例(附cd光盘1张)>共分为18章,涵盖了html 5和css3中各方面的技术知识.主要内容包括html 5概述.html 5与html 4的区别. ...
- HTML5权威指南 中文版 高清PDF扫描版
HTML5权威指南是一本系统学习网页设计的权威参考图书.<HTML5权威指南>分为五部分:第一部分介绍学习本书的预备知识和HTML.CSS和JavaScript的最新进展:第二部分讨论HT ...
- Graphics 小记
1.切图 drowg.DrawImage(productImg1, new System.Drawing.Rectangle(30, 30, 300, 300), new System.Drawing ...
- [python]模块及包
一 .module 通常模块为一个文件,直接使用import来导入就好了.可以作为module的文件类型有".py".".pyo".".pyc&quo ...
- .NET和C#的版本历史
维基百科页面:https://en.wikipedia.org/wiki/.NET_Framework_version_history Versionnumber CLRversion Release ...