scala初学大杂烩
1.List有个叫“:::”的方法实现叠加功能。你
可以这么用:
val oneTwo = List(1, 2)
val threeFour = List(3, 4)
val oneTwoThreeFour = oneTwo ::: threeFour
println(oneTwo + " and " + threeFour + " were not mutated.")
println("Thus, " + oneTwoThreeFour + " is a new List.")
2.List最常用的操作符是发音为“cons”的‘::’。Cons把一个新元素组合到已有List
的最前端,然后返回结果List。例如,若执行这个脚本:
val twoThree = list(2, 3)
val oneTwoThree = 1 :: twoThree
println(oneTwoThree)
3.由于定义空类的捷径是Nil,所以一种初始化新List的方法是把所有元素用cons操作符串连起来,Nil作为最后一个元素。
比方说,下面的脚本将产生与之前那个同样的输出,“List(1,
2, 3)”:
val oneTwoThree = 1 :: 2 :: 3 :: Nil
println(oneTwoThree)
4.scala>def hf():Int=>Int=x=>x+1
函数hf返回类型是Int=>Int,函数体是x=>x+1