scala 函数高级操作

1、匿名函数

(参数:参数类型)=>函数体

匿名函数没有名字,使用
val f=(参数:参数类型)=>函数体
def f=(参数:参数类型)=>函数体

 def test= (x:Int)=>{x*2}
 val test2=(x:Int)=>{x*3}
    println(test(2))
    println(test2(2))

 

 

2、多行字符串与插值

val name="bioamin"
    println(s"user is $name")
    val location=
      """
        |panjiayuan
        |of
        |beijin
      """.stripMargin
    println(location)

3、scala高阶函数reduce、reduceleft、reduceright、fold、foldleft、foldright

  val l=List(1,2,3)
    val l2=l.reduce(_-_)
    val l3=l.reduceRight(_-_)
    val l4=l.reduceLeft(_-_)
    println(l2)
    println(l3)
    println(l4)
    println("-----------------")
    val l5=l.fold(3)((x,y)=>x-y)
    println(l5)
    val l6=l.foldLeft(3)((x:Int,y:Int)=>(x-y))
    println(l6)
    val l7=l.foldRight(3)((x,y)=>{x-y})
    println(l7)
    println(l.min)
    

 

 

posted @ 2021-01-03 20:21  bioamin  阅读(94)  评论(0编辑  收藏  举报