关闭

scala学习之高阶函数

2390人阅读 评论(0) 收藏 举报
分类:
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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:278476次
    • 积分:4087
    • 等级:
    • 排名:第7597名
    • 原创:124篇
    • 转载:110篇
    • 译文:0篇
    • 评论:8条
    最新评论