3 回答
TA貢獻1811條經驗 獲得超6個贊
List(1, 2, 3).reduceLeft{_ + _} // valid, single Function2[Int,Int] parameterList{1, 2, 3}.reduceLeft(_ + _) // invalid, A* vararg parameter使用Parens增加編譯檢查
method {
1 +
2
3}method(
1 +
2
3)error: ')' expected but integer literal found1 + 2 + 3.
滔滔不絕
…關閉大括號緊跟在函數的最后一行之后。
內定表示法
List(1,2,3) indexOf (2)List(1, 2, 3) indexOf 2
x + 2a => a % 2 == 0
元組
((1, 2))(1, 2)
函數/部分函數文字 case
{
case pattern if guard => statements case pattern => statements}casematchcatch
object match {
case pattern if guard => statements case pattern => statements}try {
block} catch {
case pattern if guard => statements case pattern => statements} finally {
block}casecase
表達式和塊
{
import stuff._
statement ; // ; optional at the end of the line
statement ; statement // not optional here
var x = 0 // declaration
while (x < 10) { x += 1 } // stuff
(x % 5) + 1 // expression}( expression )import
( { var x = 0; while (x < 10) { x += 1}; x } % 5) + 11 // literal(1) // expression{1} // block of code({1}) // expression with a block of code{(1)} // block of code with an expression({(1)}) // you get the drift...在那里它們是不可互換的
{}()
while (x < 10) { x += 1 }condition
while ({x < 10}) { (x += 1) }TA貢獻1842條經驗 獲得超13個贊
list.map(_ * 2)list.map({_ * 2})list.foldLeft(0)(_ + _)list.foldLeft(0) { _ + _ }list.foldLeft(0)({_ + _})
caselist.map(case x => x * 2)list.map({case x => 2 * 2})list.map { case x => x * 2 }
添加回答
舉報
