Scala 学习手册(Learning Scala)
表达式
- 定义:
在执行后会返回一个值的代码单元
scala> "hello world"
res53: String = hello world
scala> "hel" + "l" + "o"
res54: String = hello
定义值和变量
- 定义:
val <identifier>[:<type>] = <experssion>
val <identifier>[:<type>] = <experssion>
- 例子
scala> val value:Double = 3.43454
value: Double = 3.43454
scala> var data:String = "333Pi"
data: String = 333Pi
表达块
可以使用大括号结合多个表达式创建一个表达式块。
scala> val x = 5*20;val amount = x + 20
x: Int = 100
amount: Int = 120
scala> val amount = {val x = 5*20;x+10}
amount: Int = 110
scala> {val a = 1;{val b = a*2;{val c=b+4;c}}}
res55: Int = 6
注释:第二个以"x+10"作为返回值,第三个以"c"作为返回值
- 分行
scala> val amount = {
val x = 5*20
x + 10
}
amount: Int = 110
if else表达块
Scala只支持一个"if"和可选的"else"块
if (<Boolean experssion>) <expression>
- 例子
scala> if(47 % 3 > 0) println("Not a multiple of 3")
Not a multiple of 3
scala> val x = 10;val y = 20
x: Int = 10
y: Int = 20
scala> val max = if(x>y) x else y
max: Int = 20
注意:如果if表达式没有相应的else表达式,则必须使用大括号。
匹配
匹配表达式
<expression> match{
case <pattern> match => <expression>
[case...]
}
- 例子
scala> val x = 10;val y = 20
x: Int = 10
y: Int = 20
scala> val max = x > y match {
case true => x
case false => y
}
max: Int = 20
scala> val status = 500
status: Int = 500
scala> val message = status match {
case 200 => "ok"
case 400 => {
println("ERROR - we called the service incorrectly")
"error"
}
case 500 => {
println("ERROR - the service encountered an error")
"error"
}
}
ERROR - the service encountered an error
message: String = error
注意:=> 表示传递,后面跟多个表达式。如果一个case块中有多个表达式,可以通过大括号把它们转换为一个表达式块。
匹配替换式
case <pattern 1> | <pattern 2>.. => <one or more expression>
- 例子
scala> val day = "MON"
day: String = MON
scala> val kind = day match {
case "MON" | "TUE" | "WED" | "THU" | "FRI" =>
"weekday"
case "SAT" | "SUN" =>
"weekend"
}
kind: String = weekday
注意:候选匹配必须能匹配上,不然就会报错误。
scala> "match me" match {case "nope" => "sorry"}
scala.MatchError: match me (of class java.lang.String)
... 28 elided
因为没有囊括所有的可能,导致匹配不上的错误。
用通配模式匹配
- 值绑定
分为值绑定、变量绑定:将把匹配表达式的输入绑定到一个局部值,然后可以在case块的体中使用。值绑定也是通配模式。
scala> val message = "OK"
message: String = OK
scala> val status = message match {
case "OK" => 200
case other => {
println(s"Couldn't parse $other")
}
}
status: AnyVal = 200
scala> val message = "werwere"
message: String = werwere
scala> val status = message match {
case "OK" => 200
case other => {
println(s"Couldn't parse $other")
}
}
Couldn't parse werwere
status: AnyVal = ()
可以看出,绑定和字符串内插相似。
- 通配符(_)
case _ => <one or more expressions>
- 例子
scala> val message = "Unauthorized"
message: String = Unauthorized
scala> val status = message match {
case "OK" => 200
case _ => {
println(s"Couldn't parse $message")
-1
}
}
Couldn't parse Unauthorized
status: Int = -1
不能像值匹配一样访问通配符
模式哨位匹配
值绑定模式增加一个if表达式,即case if 模式,从而可以为匹配表达式增加条件逻辑,只有当表达式返回true时模式才匹配。
case <pattern> if <Boolean expression> => <one or more expressions>
- 例子
scala> val response :String = null
response: String = null
scala> response match {
case s if s != null => println(s"Received '$s'")
case s => println("ERROR! Received a null response")
}
ERROR! Received a null response
匹配变量类型
case <identifier>:<type> => <one or more expressions>
用于匹配类型
scala> val x:Int = 12180
x: Int = 12180
scala> val y:Any = x
y: Any = 12180
scala>
scala> y match {
case x:String => s"'x'"
case x:Double => f"$x%.2f"
case x:Float => f"$x%.2f"
case x:Long => s"${x}l"
case x:Int => s"${x}i"
}
res60: String = 12180i
循环
主要是for循环
- 定义范围
scala> val x= 1 to 7
x: scala.collection.immutable.Range.Inclusive = Range 1 to 7
- for循环
定义:
for (<idenifier> <- <iterator> [yield] [<expression>])
yield为可选,若是使用了yield,则将产生一个集合作为返回。若是没有,但是指定了表达式,调用时返回表达式,不能访问他的返回值。
例子:
scala> for (x <- 1 to 7) {println(s"Day: $x:")}
Day: 1:
Day: 2:
Day: 3:
Day: 4:
Day: 5:
Day: 6:
Day: 7:
带有yield关键词的
scala> for (x <- 1 to 7) yield {s"Day $x:"}
res62: scala.collection.immutable.IndexedSeq[String] = Vector(Day 1:, Day 2:, Day 3:, Day 4:, Day 5:, Day 6:, Day 7:)
迭代哨位
即for if模式
for (<identifier> <- <iterator> if <Boolean expression>)
- 例子
scala> val threes = for(i <- 1 to 20 if i % 3 == 0) yield i
threes: scala.collection.immutable.IndexedSeq[Int] = Vector(3, 6, 9, 12, 15, 18)
scala> val quote = "Faith,Hope,Charity"
quote: String = Faith,Hope,Charity
scala> for {
t <- quote.split(",")
if t != null
if t.size > 0
}
{println(t)}
Faith
Hope
Charity
嵌套迭代
即多个迭代器
scala> for {x <- 1 to 2
y <- 1 to 6}
{println(s"($x,$y)")}
(1,1)
(1,2)
(1,3)
(1,4)
(1,5)
(1,6)
(2,1)
(2,2)
(2,3)
(2,4)
(2,5)
(2,6)
while 和 Do/while
- while
scala> var x = 10;while(x > 0) x -= 1
x: Int = 0
- Do/while
scala> val x = 0
x: Int = 0
scala> do println(s"Here I am,x = $x") while (x > 0)
Here I am,x = 0