Scala学习第四天
来源:互联网 发布:java开发实战入门pdf 编辑:程序博客网 时间:2024/05/21 00:48
参数类型推断
-----------------------------------------------------------------------------
当你将一个匿名函数作为参数传给另一函数时,scala会尽可能帮你推断出类型信息
-------------------------------------------------------------------------
def valueAtOneQuarter(f: (Double) => Double): Double = {f(2) + 2;};
传入匿名函数作为类型,匿名函数的参数类型可以省略
valueAtOneQuarter((x) = > 3 * x);
valueAtOneQuarter(x = > 3 * x);
valueAtOneQuarter(3 * _);
其他匿名函数定义形式
val fun = 3 * (_ :Double);
val fun:(Double) => Double = 3 * _;
有用的高阶函数
-----------------------------------------------------------------------------
(1 to 10).map("*" * _).foreach(println _);
reduceLeft() //接受二元参数的函数,
(1 to 4).reduceLeft(_ - _);//相当于1*2*3*4...*9,第一次调用的参数为集合的元素,下次函数第一个参数为计算的结果;
闭包:
可以在任何作用域内定义函数:包、类和函数中;
-------------------------------------------------------------------------
def mulBy(factor: Double) = (x: Double) => factor * x;
val triple = mulBy(3);
val half = mulBy(0.5);
println(triple(14) + ":" + half(14));//返回42 和 7;
SAM转换:
接口只有单个抽象方法
-------------------------------------------------------------------------
def makeAction(action:(ActionEvent) => Unit) = {
new ActionListener {
override def actionPerformed(event: ActionEvent) = {action(event);}
}
}
Carrying化
将原来接受两个参数的变成接收一个函数参数的过程,新函数以原函数的第二个参数为参数
-------------------------------------------------------------------------
def multBy(x: Int, y: Int) = x * y 柯里化为 def mulOneTime(x:Int)(y: Int) = {x *y}
或 def mulOneTime(x:Int): {(y: Int) => {x *y}}
抽象控制
-------------------------------------------------------------------------
将一系列语句归组成没有参数和返回值的函数
def runInThread(block: () => Unit) {
new Thread {
override def run() {
block();
}
}.start();
}
//无参数时圆括号可以省略
def runInThread(block: => Unit) {//冒号后空格必须有
new Thread {
override def run() {
block();
}
}.start();
}
调用是写成
runInThread(println("hello"));
// until函数定义
def until(condition: => Boolean)(opertion: => Unit) {
if(!condition) {
opertion; //省略参数圆括号后函数调用不能加圆括号
until(condition)(opertion);
}
}
// until函数调用
var x = 10; until(x == 0)({x -= 1;println(x);})
等价于
var x = 10; until(x == 0){x -= 1;println(x);}
until(x == 0)(x -= 1;println(x);)//错误,多条语句需要使用{}包起来
Return表达式
-----------------------------------------------------------
def indexOf(str: String, ch: Char): Int = {
var x = 0;
until(x == str.length)({
if(str(x) == ch) return x;
x += 1;
})
return -1;
}
集合
Seq
-------------------------------------------------------------------------
不可变序列树
Seq
|
--------------------------------------------------------------
|
IndexedSeq List Stream Stack Queue
LinkedList(过时)
Set
-------------
Set是有哈希集实现的,
val s = Set(1,2,3,5);//创建一个集合;
Union操作
val s1 = Set(1, 2, 6);
s.union(s1); 等价于 s ++ s1; 或者 s | s1;//并集操作 (5, 1, 6, 2, 3, 4)
s.diff(s1); 等价于 s -- s1; 或者 s &~ s1//差集 (5, 3, 4)
s.intersect(s1);等价于 s & s1; //交集(1, 2)
-----------------------------------------------------------------------------
当你将一个匿名函数作为参数传给另一函数时,scala会尽可能帮你推断出类型信息
-------------------------------------------------------------------------
def valueAtOneQuarter(f: (Double) => Double): Double = {f(2) + 2;};
传入匿名函数作为类型,匿名函数的参数类型可以省略
valueAtOneQuarter((x) = > 3 * x);
valueAtOneQuarter(x = > 3 * x);
valueAtOneQuarter(3 * _);
其他匿名函数定义形式
val fun = 3 * (_ :Double);
val fun:(Double) => Double = 3 * _;
有用的高阶函数
-----------------------------------------------------------------------------
(1 to 10).map("*" * _).foreach(println _);
reduceLeft() //接受二元参数的函数,
(1 to 4).reduceLeft(_ - _);//相当于1*2*3*4...*9,第一次调用的参数为集合的元素,下次函数第一个参数为计算的结果;
闭包:
可以在任何作用域内定义函数:包、类和函数中;
-------------------------------------------------------------------------
def mulBy(factor: Double) = (x: Double) => factor * x;
val triple = mulBy(3);
val half = mulBy(0.5);
println(triple(14) + ":" + half(14));//返回42 和 7;
SAM转换:
接口只有单个抽象方法
-------------------------------------------------------------------------
def makeAction(action:(ActionEvent) => Unit) = {
new ActionListener {
override def actionPerformed(event: ActionEvent) = {action(event);}
}
}
Carrying化
将原来接受两个参数的变成接收一个函数参数的过程,新函数以原函数的第二个参数为参数
-------------------------------------------------------------------------
def multBy(x: Int, y: Int) = x * y 柯里化为 def mulOneTime(x:Int)(y: Int) = {x *y}
或 def mulOneTime(x:Int): {(y: Int) => {x *y}}
抽象控制
-------------------------------------------------------------------------
将一系列语句归组成没有参数和返回值的函数
def runInThread(block: () => Unit) {
new Thread {
override def run() {
block();
}
}.start();
}
//无参数时圆括号可以省略
def runInThread(block: => Unit) {//冒号后空格必须有
new Thread {
override def run() {
block();
}
}.start();
}
调用是写成
runInThread(println("hello"));
// until函数定义
def until(condition: => Boolean)(opertion: => Unit) {
if(!condition) {
opertion; //省略参数圆括号后函数调用不能加圆括号
until(condition)(opertion);
}
}
// until函数调用
var x = 10; until(x == 0)({x -= 1;println(x);})
等价于
var x = 10; until(x == 0){x -= 1;println(x);}
until(x == 0)(x -= 1;println(x);)//错误,多条语句需要使用{}包起来
Return表达式
-----------------------------------------------------------
def indexOf(str: String, ch: Char): Int = {
var x = 0;
until(x == str.length)({
if(str(x) == ch) return x;
x += 1;
})
return -1;
}
集合
Seq
-------------------------------------------------------------------------
不可变序列树
Seq
|
--------------------------------------------------------------
|
IndexedSeq List Stream Stack Queue
LinkedList(过时)
Set
-------------
Set是有哈希集实现的,
val s = Set(1,2,3,5);//创建一个集合;
Union操作
val s1 = Set(1, 2, 6);
s.union(s1); 等价于 s ++ s1; 或者 s | s1;//并集操作 (5, 1, 6, 2, 3, 4)
s.diff(s1); 等价于 s -- s1; 或者 s &~ s1//差集 (5, 3, 4)
s.intersect(s1);等价于 s & s1; //交集(1, 2)
0 0
- Scala学习第四天
- Scala学习第四天 Scala的For与Function进阶实战、Lazy的使用
- Scala学习第四天 Scala的For与Function进阶实战、Lazy的使用
- scala学习第四弹:基本语法
- Scala学习第四弹 访问修饰符
- Scala学习第四天:For与Function进阶实战、Lazy的使用
- Scala学习整理[第四-六章 使用Scala运行类/对象]<Programming In Scala>
- scala学习第四章 映射和元组
- Scala学习第二天
- 第四课、Scala集合
- scala第四讲:数据类型
- java学习第四天
- COBOL学习第四天
- ajax学习第四天
- 学习Java第四天
- 程序学习第四天
- 学习jsp第四天
- Java学习第四天
- hammer.js
- 使用JSTL标签库
- 我提莫复习了假题 努力再努力
- Storm1.0 集群部署(HA)
- Dubbo+spring+springmvc+mybatis+Maven+Jetty+Mysql+Ajax多框架融合学习网址收藏
- Scala学习第四天
- Mac 使用&技巧
- 【leetcode】【Easy】【461. Hamming Distance】【bit manipulation】
- 【tcpdump】Linux抓包工具
- Caused by: java.lang.NumberFormatException: For input string: "[objectHTMLDivElement]"
- 【c语言】模拟实现strcat()、strncat()函数
- (四十一)线程——线程原语
- Note04
- org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supporte