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 found
1 + 2 + 3
.
滔滔不绝
…关闭大括号紧跟在函数的最后一行之后。
内定表示法
List(1,2,3) indexOf (2)
List(1, 2, 3) indexOf 2
x + 2
a => a % 2 == 0
元组
((1, 2))
(1, 2)
函数/部分函数文字 case
{ case pattern if guard => statements case pattern => statements}
case
match
catch
object match { case pattern if guard => statements case pattern => statements}
try { block} catch { case pattern if guard => statements case pattern => statements} finally { block}
case
case
表达式和块
{ 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) + 1
1 // 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条经验 获得超12个赞
list.map(_ * 2)
list.map({_ * 2})
list.foldLeft(0)(_ + _)
list.foldLeft(0) { _ + _ }
list.foldLeft(0)({_ + _})
case
list.map(case x => x * 2)
list.map({case x => 2 * 2})
list.map { case x => x * 2 }
添加回答
举报