Scala
连接 Java 和 FP
强类型,类型推断
FP
immutable
分层
FP 与并发:并发问题在于可变状态,定义后可被多次赋值,两个线程可以在同一时间改变相同数据,很难保证数据始终处于有效状态,测试难,数据库通过事务和锁解决,Java 提供控制访问共享数据的工具
除少数例外,Scala 中一切皆对象
1> 5.+(4.*(3)) // 这里 .+ 表示对象 5 上面的 + 方法,操作符可重载2Int = 173> 1 + 0.84Double = 1.85> "abc" + 46String = abc47> "abc".size8Int = 3
Nil 是个空列表,Nil 和数字都不能转换为 Boolean
1> if (0) {println("hello")}2error3> if (Nil) {println("hello")}4error
强类型,会检查两种类型是否兼容 Java、Ruby;弱类型 C、汇编
静态类型判断鸭子根据基因,动态类型判断鸭子根据叫声和走姿
Range 类型
1val range = 0 until 102val r = (0 to 10) by 53r.start // 04r.end // 105r.step // 56r.size // 378val r2 = (0 until 10 by 5)9r.start // 010r.end // 1011r.step // 512r.size // 21314(1 to 10) // 1 是默认 step15(10 until 0) by -1 // 根据 step 改变方向
元组
1val person = ("ahab", 18) // person: (String, Int) = (ahab,18)2person._1 // "ahab"3person._2 // 1845val (a, b) = (1, 2)
类,双构造器
1class Person(name: String, age: Int) {2 // 主构造器3 val info = name + age4 println("outer constructor: " + info) // outer constructor: ahab1956 def this(firstName: String, lastName: String, age: Int) { // 辅助构造器7 this(firstName + lastName, age) // outer constructor: ahabhgk198 println("inner constructor: " + info) // inner constructor: ahabhgk199 }1011 def talk(): Unit = println("hello")12}1314val p = new Person("ahab", 19)15p.talk()1617val p2 = new Person("ahab", "hgk", 19)18p.talk()
扩展类
- 伙伴对象和类方法
1object TrueRing { // object 声明单例对象2 def rule(): Unit = println("to rule them all")3}45TrueRing.rule()
- 继承
1class Person(val name: String) {2 def talk(message: String) {3 println(name + " says: " + message)4 }5 def id = name6}78class Employee(override val name: String, val number: Int) extends Person(name) {9 override def id: String = super.id1011 override def talk(message: String) {12 println("employee: " + name + " says: " + message)13 }14}1516val em = new Employee("yoda", 4)1718em.talk("hah") // employee: yoda says: hah
- trait (mixin)
1class Person(val name: String)23trait Nice {4 def greeting(): Unit = println("hello")5}67class GoodMan(override val name: String) extends Person(name) with Nice89val p = new GoodMan("zkk")10p.greeting() // "hello"
var 与 val:var is mutable, val is immutable
集合:List、Set、Map
- 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 = 17> List(1, 2, 3) == List(3, 2, 1)8Boolean = false
- 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
- 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"
- 所有类都继承与 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 => 13 case x if x > 0 => factorial(n - 1) * n4}56factorial(3)7factorial(6)
并发
actor 和消息传递,actor 拥有线程池和队列池,当用 ! 给 actor 发消息时,是讲一个对象放到 actor 的队列中,actor 读消息并行动,通常通过模式匹配处理消息
1case object Poke2case object Feed34class Kid() extends Actor {5 def act() {6 loop {7 react {8 case Poke => println("Ow")9 case Feed => println("Lala")10 }11 }12 }13}1415val kk = new Kid()16val dd = new Kid()1718kk ! Poke19dd ! Poke20kk ! Feed21dd ! Feed2223// "Ow"24// "Lala"25// "Ow"26// "Lala"2728// "Ow"29// "Ow"30// "Lala"31// "Lala"3233// 两次顺序不同,是并发的
reactWithin 超时处理、receive 阻塞线程、receiveWithin 超时时间内阻塞线程
feeling
并发
FP + 类
灵活 trait 重载
JVM 强大的社区