# 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): _*)
}
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：scala学习之高阶函数 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)