scala学习之高阶函数

object High_order_fun{
def main(args:Array[String]){
//(1 to 9).map("*"*_).foreach(println _)
(1 to 9).map(2*_).foreach(println( _))
(1 to 9).filter(_%2==0).foreach(println)
println((1 to 9).reduceLeft(_*_))
"spark is the most exciting thing happening in big data today".split(" ").
sortWith(_.length<_.length).foreach(println)


def high_order_functions(f:(Double)=>Double)=f(0.25)
println(high_order_functions(ceil _))
println(high_order_functions(sqrt _))


}

}


object High_fun{
    def function1(n: Int): Int = {
        val multiplier = (i: Int, m: Int) => i * m
        multiplier.apply(n, 2)
    }

    def function2(m: Int => Int) = m

    def function3(f: (Int, Int) => Int) = f

    val function4 = (x: Int) => x + 1

    val function5 = (x:Int) => {
       if(x > 1){
           //...
       }else{
           //...
       }
    }

    val function6 = (_ : Int) + ( _ :Int)

    def function7(a:Int, b:Int, c:Int) = a+b+c

    val function8 = function7 _

    val function9 = function7(1, _:Int, 3)

    def function10(x:Int) = (y:Int) => x+y

    def function11(args: Int*) = for (arg <- args) println(arg)

    def function12(x: Int): Int = {
        if (x == 0) {
            throw new Exception("bang!")
        }
        else{
            function12(x -1)
        }
    }

    def hello1(m: Int): Int = m

    def hello2(m: Int, n: Int): Int = m * n

    def main(args: Array[String]) {
        println(function1(2))
        println(function2(hello1)(2))
        println(function3(hello2)(2, 3))
        println(function4(4))
        println(function4(5))
        function6(1, 2)
        function8(1, 2, 3)
        function8.apply(1, 2, 3)
        function9.apply(1)
        function10(1)(2)
        function11(1, 2, 3, 4)
        function11(Array(1, 2, 3): _*)
    }
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值