update sprint1
This commit is contained in:
parent
042bef2dcb
commit
899b3a272b
9 changed files with 333 additions and 12 deletions
|
@ -1,14 +1,100 @@
|
|||
## 1.列举Scala的主要特性
|
||||
|
||||
### 1. 伴生对象以及apply()方法
|
||||
### 2. 变长参数
|
||||
### 3. 柯里化函数与部分应用
|
||||
### 4. Option
|
||||
### 5. 函数组合子
|
||||
### 6. 偏函数
|
||||
### 7. 类型推断
|
||||
### 8. 函数是一等公民(函数和其他基本类型地位相等,可以做为参数传入,也可以作为返回值返回)
|
||||
```scala
|
||||
class Person(_name: String) {
|
||||
val name = _name
|
||||
}
|
||||
|
||||
object Person {
|
||||
def apply(_name: String): Person = new Person(_name)
|
||||
}
|
||||
|
||||
val person = Person("Rhyme")
|
||||
person.name
|
||||
```
|
||||
### 2. 变长参数
|
||||
```scala
|
||||
def addAll(args: Int*): Int = {
|
||||
var sum = 0
|
||||
for (arg <- args) {
|
||||
sum += arg
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
addAll(1, 2, 3, 4)
|
||||
```
|
||||
### 3. 柯里化函数与部分应用
|
||||
```scala
|
||||
def multiBy(a: Int)(b: Int): Int = {
|
||||
a * b
|
||||
}
|
||||
|
||||
multiBy(2)(3)
|
||||
|
||||
val a = multiBy(2) _
|
||||
a(3)
|
||||
```
|
||||
### 4. Option
|
||||
```scala
|
||||
val map = Map("Rhyme" -> 10, "Chiang" -> 20)
|
||||
map.get("Rhyme")
|
||||
map.get("Rhyme2")
|
||||
```
|
||||
### 5. 函数组合子
|
||||
|
||||
```scala
|
||||
val list = List(1, 2, 3, 4, 5)
|
||||
list map (_ * 2)
|
||||
```
|
||||
### 6. 偏函数
|
||||
|
||||
```scala
|
||||
val one: PartialFunction[Int, String] = {
|
||||
case 1 => "one"
|
||||
}
|
||||
one.isDefinedAt(1)
|
||||
one.isDefinedAt(2)
|
||||
one(1)
|
||||
|
||||
val two: PartialFunction[Int, String] = {
|
||||
case 2 => "two"
|
||||
}
|
||||
val three: PartialFunction[Int, String] = {
|
||||
case 3 => "three"
|
||||
}
|
||||
|
||||
val particial = one orElse two orElse three
|
||||
particial(1)
|
||||
particial(2)
|
||||
particial(3)
|
||||
```
|
||||
### 7. 类型推断
|
||||
```scala
|
||||
val i: Int = 3
|
||||
val l = List(1, 2, 3, 4)
|
||||
l.map(i => i * 2)
|
||||
```
|
||||
>1.Scala是通过类型推断来缓解静态类型的语法开销
|
||||
|
||||
>2.Scala类型推断的核心思想是:推断约束,统一类型
|
||||
|
||||
>3.Scala的类型推断是局部的,一次只对一个表达式进行类型推断
|
||||
```scala
|
||||
def id[A](i: A) = i
|
||||
val x = id(23)
|
||||
val s = id("Rhyme")
|
||||
```
|
||||
### 8. 函数是一等公民(函数和其他基本类型地位相等,可以做为参数传入,也可以作为返回值返回)
|
||||
```scala
|
||||
val l2 = List(1, 2, 3, 4)
|
||||
l2.sortWith(_ > _)
|
||||
def multiBy2(x: Int) = {
|
||||
y: Int => x * y
|
||||
}
|
||||
val m = multiBy2(1)
|
||||
```
|
||||
## 2.静态类型、动态类型、强类型、弱类型如何理解
|
||||
|
||||
### 静态类型
|
||||
|
@ -22,7 +108,7 @@
|
|||
|
||||
## 3.什么是纯函数/引用透明性
|
||||
|
||||
纯函数值的是函数和外界沟通的方式只有参数和返回值
|
||||
纯函数值的是函数和外界沟通的方式只有参数和返回值,具备引用透明性(相同的参数可以得到相同的结果)的函数,一个表达式在程序中可以被它等价的值替换,而对源程序不会有任何的影响,相反,带有副作用的函数称为非纯函数
|
||||
|
||||
以下的foo函数不是纯函数
|
||||
|
||||
|
@ -37,6 +123,8 @@ class A {
|
|||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 4.简述数学意义上集合、映射、函数、域和值域的概念,以及编程意义上函数与其对应关系
|
||||
|
||||
### 4.1 数学意义上
|
||||
|
@ -59,7 +147,7 @@ class A {
|
|||
|
||||
### 5.1 函数式编程
|
||||
|
||||
>将运算过程定义为一系列嵌套的函数调用
|
||||
>函数式编程关心的是类型(代数类型之间的关系),更多的是一种演绎和推导,面向对象编程更多的是一种归纳总结,因此从这个角度来分析,函数式编程更侧重于一种数学上的逻辑推理,更能保证程序的正确性
|
||||
|
||||
#### 1. 函数是一等公民
|
||||
>函数和其他基本类型一样,地位是相等的,可以作为参数传入和返回
|
||||
|
@ -69,7 +157,7 @@ class A {
|
|||
>函数式编程强调纯函数,就是函数设计要独立,不能对外界造成影响
|
||||
#### 4. 不修改状态
|
||||
>函数不修改变量,变量负责保存状态
|
||||
#### 5. 依赖透明
|
||||
#### 5. 引用透明
|
||||
>函数的运行只与传入的参数相关,与外部不相关,相同的参数,应该得到相同的结果
|
||||
|
||||
#### 6. 函数式编程的好处
|
||||
|
@ -82,11 +170,29 @@ class A {
|
|||
|
||||
### 6.1 高阶函数
|
||||
>接受函数参数或返回一个函数的函数,简单来说就是能够操纵其他函数
|
||||
### 6.2 高阶类型
|
||||
```scala
|
||||
import scala.math._
|
||||
|
||||
def valueAtOneQuarter(f: Double => Double) = f(0.25)
|
||||
valueAtOneQuarter(ceil)
|
||||
valueAtOneQuarter(sqrt)
|
||||
|
||||
def mulBy(factor: Double) = (x: Double) => x * factor
|
||||
mulBy(3)(4)
|
||||
```
|
||||
### 6.2 高阶类型[Higher Kinded Types]
|
||||
>和高阶函数同理,高阶类型值的是接受其他类型构造出来的新类型,可以用于简化类型
|
||||
### 6.3 类型构造器
|
||||
>类型构造器原理和函数相似,只不过类型构造器处理的是类型,在函数中,我传入一个a,返回一个b,在类型构造器中,我传入一个类型,返回一个类型
|
||||
|
||||
```scala
|
||||
List[_]
|
||||
List[String]
|
||||
List[Int]
|
||||
Map[_,_]
|
||||
Map[String,Int]
|
||||
Functor[F[_]]
|
||||
Functor[List]
|
||||
```
|
||||
## 7. 什么是non-strict/strict
|
||||
|
||||
### 7.1 strict
|
||||
|
@ -99,6 +205,39 @@ class A {
|
|||
|
||||
|
||||
|
||||
## 8.代换模型与惰性求值
|
||||
|
||||
在真正被用到的时候再执行计算,以正则序对表达式进行求值
|
||||
|
||||
## 9.副作用
|
||||
|
||||
你的存在对外界存在影响,例如一个函数改变了一个成员变量的值或状态,可以用零件来形象的比喻函数,没有副作用的函数就像一个个单独的小零件,可以轻松的被维护与更换
|
||||
|
||||
### 9.1 如何组合副作用
|
||||
|
||||
使用函数对副作用进行包装,并将原先的副作用以函数的形式返回
|
||||
|
||||
```scala
|
||||
object Pure {
|
||||
def println(str: String) = () => Predef.println(str)
|
||||
}
|
||||
|
||||
Pure.println("Rhyme")
|
||||
```
|
||||
|
||||
## 10. 解构
|
||||
|
||||
典型的一个例子是使用模式匹配对元组进行解构赋值操作
|
||||
|
||||
```scala
|
||||
val a = ("Rhyme", 20)
|
||||
val (first, second) = a
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1 +1,53 @@
|
|||
// 模式匹配和函数组合
|
||||
// compose and andThen
|
||||
def f(s: String): String = {
|
||||
"f(" + s + ")"
|
||||
}
|
||||
def g(s: String): String = {
|
||||
"g(" + s + ")"
|
||||
}
|
||||
val compose = f _ compose g _
|
||||
|
||||
compose("Rhyme")
|
||||
|
||||
val andThen = f _ andThen g _
|
||||
andThen("Rhyme")
|
||||
|
||||
// 柯里化和偏函数
|
||||
|
||||
val one: PartialFunction[Int, String] = {
|
||||
case 1 => "one"
|
||||
}
|
||||
one.isDefinedAt(1)
|
||||
one.isDefinedAt(2)
|
||||
one(1)
|
||||
|
||||
val two: PartialFunction[Int, String] = {
|
||||
case 2 => "two"
|
||||
}
|
||||
|
||||
val three: PartialFunction[Int, String] = {
|
||||
case 2 => "two"
|
||||
}
|
||||
|
||||
val wildcard: PartialFunction[Int, String] = {
|
||||
case _ => "something else"
|
||||
}
|
||||
|
||||
val partial = one orElse two orElse three orElse wildcard
|
||||
|
||||
partial(1)
|
||||
partial(2)
|
||||
partial(3)
|
||||
partial(4)
|
||||
|
||||
object Pure {
|
||||
def println(str: String) = () => Predef.println(str)
|
||||
}
|
||||
|
||||
Pure.println("Rhyme")
|
||||
//解构
|
||||
|
||||
val a = ("Rhyme", 20)
|
||||
val (first, second) = a
|
||||
|
||||
|
|
10
src/main/scala/Chapter6-type-polymorphism-basics.sc
Normal file
10
src/main/scala/Chapter6-type-polymorphism-basics.sc
Normal file
|
@ -0,0 +1,10 @@
|
|||
//类型和多态
|
||||
// 过于繁华
|
||||
// 所谓的一级多态,意思是不能多级多态,Scala只有一级多态
|
||||
def toList[A](a: A) = List(a)
|
||||
// B,A的类型只能在调用的上下文中才能被确定
|
||||
//def foo[A, B](f: A => List[A], b: B) = f(b)
|
||||
// 简单的来说不能在类型变量未在上下文中确定的清况下调用,这就是Scala的一级多态
|
||||
def foo[A, B](f: A => List[A], b: Int) = f(b)
|
||||
|
||||
// 类型推断
|
5
src/main/scala/Chapter7-Variance.sc
Normal file
5
src/main/scala/Chapter7-Variance.sc
Normal file
|
@ -0,0 +1,5 @@
|
|||
// 类型的变性
|
||||
// 函数的参数类型是逆变的,函数的返回类型是协变的,说的简单一点,就会参数类型允许传比它要到的父类,
|
||||
// 函数的返回类型允许返回子类型
|
||||
// 这样为什么合理,我们可以仔细试想一下,当我要传入一个男人时,我的入参允许逆变,也几乎是说我可以传入一个人,当我要返回一个人时,我可以返回一个男人,这个没问题
|
||||
// 其实参数的这种逆变可以理解为java的
|
11
src/main/scala/Chapter8-advanced-typed.sc
Normal file
11
src/main/scala/Chapter8-advanced-typed.sc
Normal file
|
@ -0,0 +1,11 @@
|
|||
// 高级类型
|
||||
|
||||
//视界View bounds type class
|
||||
|
||||
// 类型类
|
||||
|
||||
class Container[A <% Int] {
|
||||
def addIt(x: A): Int = 123 + x
|
||||
}
|
||||
|
||||
(new Container[String]).addIt("123")
|
12
src/main/scala/Chapter9.sc
Normal file
12
src/main/scala/Chapter9.sc
Normal file
|
@ -0,0 +1,12 @@
|
|||
val names = List("Rhyme", "Chiang")
|
||||
def toUpperCase(s: String) = Vector(s.toUpperCase, s.toLowerCase())
|
||||
names map toUpperCase
|
||||
|
||||
// map flatMap 翻译for表达式
|
||||
|
||||
for (i <- 0 to 10) yield i * i
|
||||
(0 to 10) map (i => i * i)
|
||||
|
||||
for (i <- 1 to 10; j <- 1 to i) yield i * j
|
||||
|
||||
(1 to 10) flatMap (i => (1 to i) map (j => j * i))
|
75
src/main/scala/Example.sc
Normal file
75
src/main/scala/Example.sc
Normal file
|
@ -0,0 +1,75 @@
|
|||
//伴生对象以及apply()方法
|
||||
class Person(_name: String) {
|
||||
val name = _name
|
||||
}
|
||||
|
||||
object Person {
|
||||
def apply(_name: String): Person = new Person(_name)
|
||||
}
|
||||
|
||||
val person = Person("Rhyme")
|
||||
person.name
|
||||
|
||||
// 变长参数
|
||||
def addAll(args: Int*): Int = {
|
||||
var sum = 0
|
||||
for (arg <- args) {
|
||||
sum += arg
|
||||
}
|
||||
sum
|
||||
}
|
||||
|
||||
addAll(1, 2, 3, 4)
|
||||
|
||||
// 柯里化函数与部分应用
|
||||
def multiBy(a: Int)(b: Int): Int = {
|
||||
a * b
|
||||
}
|
||||
|
||||
multiBy(2)(3)
|
||||
|
||||
val a = multiBy(2) _
|
||||
a(3)
|
||||
|
||||
// Option
|
||||
val map = Map("Rhyme" -> 10, "Chiang" -> 20)
|
||||
map.get("Rhyme")
|
||||
map.get("Rhyme2")
|
||||
|
||||
// 函数组合子
|
||||
val list = List(1, 2, 3, 4, 5)
|
||||
list map (_ * 2)
|
||||
|
||||
//偏函数
|
||||
val one: PartialFunction[Int, String] = {
|
||||
case 1 => "one"
|
||||
}
|
||||
one.isDefinedAt(1)
|
||||
one.isDefinedAt(2)
|
||||
one(1)
|
||||
|
||||
val two: PartialFunction[Int, String] = {
|
||||
case 2 => "two"
|
||||
}
|
||||
val three: PartialFunction[Int, String] = {
|
||||
case 3 => "three"
|
||||
}
|
||||
|
||||
val particial = one orElse two orElse three
|
||||
particial(1)
|
||||
particial(2)
|
||||
particial(3)
|
||||
|
||||
// 类型推断
|
||||
val i: Int = 3
|
||||
val l = List(1, 2, 3, 4)
|
||||
l.map(i => i * 2)
|
||||
|
||||
//函数时一等公民,和其他基本类型是相同的,可以做为参数传入,也可以作为返回值返回
|
||||
val l2 = List(1, 2, 3, 4)
|
||||
l2.sortWith(_ > _)
|
||||
def multiBy2(x: Int) = {
|
||||
y: Int => x * y
|
||||
}
|
||||
val m = multiBy2(1)
|
||||
|
14
src/main/scala/Example2.sc
Normal file
14
src/main/scala/Example2.sc
Normal file
|
@ -0,0 +1,14 @@
|
|||
// 高阶函数
|
||||
// (3+2)*1
|
||||
import scala.math._
|
||||
|
||||
def valueAtOneQuarter(f: Double => Double) = f(0.25)
|
||||
valueAtOneQuarter(ceil)
|
||||
valueAtOneQuarter(sqrt)
|
||||
|
||||
def mulBy(factor: Double) = (x: Double) => x * factor
|
||||
mulBy(3)(4)
|
||||
|
||||
def id[A](i: A) = i
|
||||
val x = id(23)
|
||||
val s = id("Rhyme")
|
|
@ -1,5 +1,8 @@
|
|||
object Test {
|
||||
def main(args: Array[String]): Unit = {
|
||||
println("pppp")
|
||||
|
||||
val list = Seq(1,4,3,6)
|
||||
list.sortWith()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue