Skip to content

AHABHGK

Scala

连接 Java 和 FP

强类型,类型推断

FP

immutable

分层

FP 与并发:并发问题在于可变状态,定义后可被多次赋值,两个线程可以在同一时间改变相同数据,很难保证数据始终处于有效状态,测试难,数据库通过事务和锁解决,Java 提供控制访问共享数据的工具

除少数例外,Scala 中一切皆对象

1> 5.+(4.*(3)) // 这里 .+ 表示对象 5 上面的 + 方法,操作符可重载
2Int = 17
3> 1 + 0.8
4Double = 1.8
5> "abc" + 4
6String = abc4
7> "abc".size
8Int = 3

Nil 是个空列表,Nil 和数字都不能转换为 Boolean

1> if (0) {println("hello")}
2error
3> if (Nil) {println("hello")}
4error

强类型,会检查两种类型是否兼容 Java、Ruby;弱类型 C、汇编

静态类型判断鸭子根据基因,动态类型判断鸭子根据叫声和走姿

Range 类型

1val range = 0 until 10
2val r = (0 to 10) by 5
3r.start // 0
4r.end // 10
5r.step // 5
6r.size // 3
7
8val r2 = (0 until 10 by 5)
9r.start // 0
10r.end // 10
11r.step // 5
12r.size // 2
13
14(1 to 10) // 1 是默认 step
15(10 until 0) by -1 // 根据 step 改变方向

元组

1val person = ("ahab", 18) // person: (String, Int) = (ahab,18)
2person._1 // "ahab"
3person._2 // 18
4
5val (a, b) = (1, 2)

类,双构造器

1class Person(name: String, age: Int) {
2 // 主构造器
3 val info = name + age
4 println("outer constructor: " + info) // outer constructor: ahab19
5
6 def this(firstName: String, lastName: String, age: Int) { // 辅助构造器
7 this(firstName + lastName, age) // outer constructor: ahabhgk19
8 println("inner constructor: " + info) // inner constructor: ahabhgk19
9 }
10
11 def talk(): Unit = println("hello")
12}
13
14val p = new Person("ahab", 19)
15p.talk()
16
17val p2 = new Person("ahab", "hgk", 19)
18p.talk()

扩展类

  1. 伙伴对象和类方法
1object TrueRing { // object 声明单例对象
2 def rule(): Unit = println("to rule them all")
3}
4
5TrueRing.rule()
  1. 继承
1class Person(val name: String) {
2 def talk(message: String) {
3 println(name + " says: " + message)
4 }
5 def id = name
6}
7
8class Employee(override val name: String, val number: Int) extends Person(name) {
9 override def id: String = super.id
10
11 override def talk(message: String) {
12 println("employee: " + name + " says: " + message)
13 }
14}
15
16val em = new Employee("yoda", 4)
17
18em.talk("hah") // employee: yoda says: hah
  1. trait (mixin)
1class Person(val name: String)
2
3trait Nice {
4 def greeting(): Unit = println("hello")
5}
6
7class GoodMan(override val name: String) extends Person(name) with Nice
8
9val p = new GoodMan("zkk")
10p.greeting() // "hello"

var 与 val:var is mutable, val is immutable

集合:List、Set、Map

  1. List
1> List(1, 2, 3)
2List[Int] = List(1, 2, 3)
3> List(1, 2, "ha")
4List[Any] = List(1, 2, "ha")
5> List(1, "ha")(0)
6Any = 1
7> List(1, 2, 3) == List(3, 2, 1)
8Boolean = false
  1. Set
1> val animals = Set("lions", "tigers")
2Set[String] = Set("lions", "tigers")
3> val animals2 = animals - "tigers"
4Set("lions")
5> val andAnimals = animals ++ Set("lions", "bears")
6Set("lions", "tigers", "bears")
7> val divAnimals = animals -- Set("lions")
8Set("tigers")
9> Set(1, 2, 3) == Set(3, 2, 1)
10Boolean = true
  1. Map
1> val ordinals = Map(0 -> "zero", 1 -> "one", 2 -> "two")
2Map[Int, String] = Map(0 -> "zero", 1 -> "one", 2 -> "two")
3> ordinals(2)
4String = "two"
5> val map = new HashMap[Int, String]
6> map += 4 -> "four"
7> map += 5 -> "five"
  1. 所有类都继承与 Any,Nothing 继承自所有类。Null 是一个 trait,null 是 Null 的实例,Nil 是个空 List,Nothing 是一个 trait。抛出异常的方法返回类型为 Nothing,表示永不返回

集合方法:foreach lenght size isEmpty head tail last init reverse drop count filter map forall exists sort

foldLeft (reduce)

1> val li = List(1, 2, 3)
2> val sum = (0 /: li) {(sum, i) => sum + i}
3> li.foldLeft(0)((sum, i) => sum + i) // curry

一等结构的 XML

模式匹配

1def factorial(n: Int): Int = n match {
2 case 0 => 1
3 case x if x > 0 => factorial(n - 1) * n
4}
5
6factorial(3)
7factorial(6)

并发

actor 和消息传递,actor 拥有线程池和队列池,当用 ! 给 actor 发消息时,是讲一个对象放到 actor 的队列中,actor 读消息并行动,通常通过模式匹配处理消息

1case object Poke
2case object Feed
3
4class Kid() extends Actor {
5 def act() {
6 loop {
7 react {
8 case Poke => println("Ow")
9 case Feed => println("Lala")
10 }
11 }
12 }
13}
14
15val kk = new Kid()
16val dd = new Kid()
17
18kk ! Poke
19dd ! Poke
20kk ! Feed
21dd ! Feed
22
23// "Ow"
24// "Lala"
25// "Ow"
26// "Lala"
27
28// "Ow"
29// "Ow"
30// "Lala"
31// "Lala"
32
33// 两次顺序不同,是并发的

reactWithin 超时处理、receive 阻塞线程、receiveWithin 超时时间内阻塞线程

feeling

并发

FP + 类

灵活 trait 重载

JVM 强大的社区