字符串的高级操作
var name ="li"
println(s"Hello::$name")
val b =
"""
|这是一个
|sss
|ssss
""".stripMargin
匿名函数
val sayHello = (name:String) => {}
def sayHello2 = () => {}
颗粒化currying
def sum(a:Int, b: Int): Unit = {
a + b
}
颗粒化
def sum2(a:Int)(b:Int) = a + b
高阶函数
val l = List(1,2,3,4,5)
l.map(x => x * 2)
l.map(_ * 2)
// 1+2
l.reduce(_ + _)
// 1 -2 -3 - 4 -5
l.reduce(_ - _)
// 可以参考这篇文字https://blog.csdn.net/next__one/article/details/77650135
l.reduceLeft(_-_) // 1 -2 -3 - 4 -5
l.foldLeft(0)(_ - _) // 0 -1 -2 -3 -4 -5 等价于 val a3 = (0 /: l)(_ - _)
l.foldRight(0)(_ - _) // (1-(2-(3-(4-(5 -0))))) val a5 = (a :\ 0)(_ - _)
l.reduceRight(_-_) // 1-(2-(3-(4-5))))
- flatten 压扁一层
- flatMap 先map后flat
偏函数
/**
* 偏函数,没有match的case语句
*/
object PartitalFunctionApp extends App{
// A 输入参数类型 B输出参数类型
def sayChinese: PartialFunction[String,String] = {
case "1" => "da"
case "2" => "dd"
case _ => "不知道"
}
println(sayChinese("ak"))
}
网友评论