Scala 学习笔记之集合(2)
class StudentTT extends StudentT{
def sayBye(name: String, age: Int)(address: String){
println("Hello, " + name + ":" + age + ":" + address)
}
}
object CollectionDemo3 {
def main(args: Array[String]): Unit = {
val s = new StudentTT()
//Currying
s.sayBye("leo", 1)("shanghai")
//给定一个起始值0,使用规约函数规约列表,这里其实就是所有元素求和
println(List(1,2,3).fold(0)(_+_))
//给定一个起始值0,使用规约函数从左至右规约列表,这里其实就是所有元素求和
println(List(1,2,3).foldLeft(0)(_+_))
//给定一个起始值0,使用规约函数从右至左规约列表,这里其实就是所有元素求和
println(List(1,2,3).foldLeft(0)(_+_))
//给定一个规约函数,从第一个元素开始规约
println(List(1,2,3) reduce(_+_))
//给定一个规约函数,第一个元素从左到右开始规约
println(List(1,2,3) reduceLeft(_+_))
//给定一个规约函数,第一个元素从右到左开始规约
println(List(1,2,3) reduceRight(_+_))
//给定一个起始值0,使用规约函数,生成一个归约值得列表
println(List(1,2,3).scan(0)(_+_))
//给定一个起始值0,使用规约函数从左至右,生成一个归约值得列表
println(List(1,2,3).scanLeft(0)(_+_))
//给定一个起始值0,使用规约函数从右至左,生成一个归约值得列表
println(List(1,2,3).scanRight(0)(_+_))
//复杂的归约,如果你一看就懂,说明你真的掌握了,我当时认真看了一会才看明白。。。
println(List(1,2,3).foldLeft(false){(a, i) => if(a) a else (i ==2)})
}
}
运行结果:
Hello, leo:1:shanghai
6
6
6
6
6
6
List(0, 1, 3, 6)
List(0, 1, 3, 6)
List(6, 5, 3, 0)
true
Scala 学习笔记之集合(2)的更多相关文章
- Scala 学习笔记之集合(3)
建立一个Java类,为了演示Java集合类型向Scala集合的转换: import java.util.ArrayList; import java.util.List; public class S ...
- scala学习笔记:集合
scala> 1 to 10 res9: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9 ...
- Scala 学习笔记之集合(1)
package com.citi.scala object CollectionDemo { def main(args: Array[String]): Unit = { /** * List */ ...
- Scala 学习笔记之集合(7) Option
object CollectionDemo8 { def main(args: Array[String]): Unit = { //Option集合的使用,可以用来安全的判断null或非null,放 ...
- Scala 学习笔记之集合(6)
object CollectionDemo7 { def main(args: Array[String]): Unit = { //数组使用 val arr = Array("red&qu ...
- Scala 学习笔记之集合(5)
import collection.mutable.Buffer object CollectionDemo6 { def main(args: Array[String]): Unit = { // ...
- Scala 学习笔记之集合(4)
集合的模式匹配操作: object CollectionDemo5 { def main(args: Array[String]): Unit = { //集合模式匹配1 val ls = List( ...
- Scala 学习笔记之集合(9) 集合常用操作汇总
object CollectionDemo10 { def main(args: Array[String]): Unit = { var ls = List[Int](1, 2, 3) //向后增加 ...
- Scala 学习笔记之集合(8) Try和Future
import util._ import concurrent.ExecutionContext.Implicits.global import concurrent.Future import co ...
随机推荐
- HDU - 4358 Boring counting (dsu on tree)
Boring counting: http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意: 求一棵树上,每个节点的子节点中,同一颜色出现k次 的 个数. 思 ...
- hihocoder #1617 : 方格取数(dp)
题目链接:http://hihocoder.com/problemset/problem/1617 题解:一道递推的dp题.这题显然可以考虑两个人同时从起点出发这样就不会重复了设dp[step][i] ...
- lightoj 1158 - Anagram Division(记忆化搜索+状压)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1158 题解:这题看起来就像是记忆搜索,由于s很少最多就10位所以可以考虑用状压 ...
- hdu 1540 Tunnel Warfare(线段树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1540 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. ...
- 九度 题目1454:Piggy-Bank 完全背包
题目1454:Piggy-Bank 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1584 解决:742 题目描述: Before ACM can do anything, a budg ...
- Java之多线程(一)
一,前言 今天总结一些关于线程方面的知识,说到线程可谓是无人不知,毕竟这东西不管是在工作开发中,还是实际生活中都时时存在着.关于线程方面的内容非常多,从简单的单线程,多线程,线程安全以及到高并发等 ...
- 【Offer】[59-2] 【队列的最大值】
题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 请定义一个队列并实现函数max得到队列里的最大值,要求函数max.push_back和 pop_front 的时间复杂度都是0(1). ...
- Maven项目使用Nexus作为远程仓库的settings.xml配置
Maven项目使用Nexus作为远程仓库的settings.xml配置(转) 在自己电脑C:\Users\hanmm\.m2\下的setting.xml. 1.服务器配置 <server> ...
- pageable多字段排序问题
Sort sort = new Sort(Sort.Direction.DESC, "createdate") .and(new Sort(Sort.Direction.AES, ...
- Python Flask高级编程之RESTFul API前后端分离精讲 (网盘免费分享)
Python Flask高级编程之RESTFul API前后端分离精讲 (免费分享) 点击链接或搜索QQ号直接加群获取其它资料: 链接:https://pan.baidu.com/s/12eKrJK ...