# Kotlin之高阶函数

### 一、定义高阶函数

``````(String,Int)->Unit
``````

``````fun example(func:(String,Int)->Unit){
func("hello",123)
}
``````

``````fun num1AndNum2(num1:Int,num2:Int,operator:(Int,Int)->Int):Int{
val result=operator(num1,num2)
return result
}
``````

num1AndNum2()函数的前两个参数没有什么需要解释的，第三个参数是一个接收两个整型参数并且返回值也是整型的函数类型参数。在num1AndNum2()函数中，我们并没有进行任何具体的运算操作，只是将num1和num2参数传给了第三个函数类型参数，并获取它的返回值，最终将得到的返回值返回。(函数名用什么都可以，这里主要设计功能是运算，所以我命名为operator)

``````fun plus(num1: Int,num2: Int):Int{
return num1+num2
}
fun minus(num1: Int,num2: Int):Int{
return num1-num2
}
``````

``````fun main(){
val num1=100
val num2=80
val result1= num1AndNum2(num1,num2,::plus)
val result2 = num1AndNum2(num1, num2, ::minus)
println("result1 is \$result1")
println("result2 is \$result2")
}
``````

``````fun main(){
val num1=100
val num2=80
val result1=num1AndNum2(num1,num2){ n1,n2 ->
n1+n2
}
val result2=num1AndNum2(num1,num2){ n1,n2 ->
n1-n2
}
println("result1 is \$result1")
println("result2 is \$result2")
}
``````

``````fun StringBuilder.build(block:StringBuilder.()->Unit):StringBuilder{
block()
return this
}
``````

``````fun main(){
val list=listof("Apple","Banana","Orange","Pear","Grape")
val result=StringBuilder().build{
append("Start eating fruits.n")
for(fruit in list){
append(fruit).append("n")
}
append("Ate all fruits.")
}
println(result.toString)
}
``````

### 二、内联函数的作用

``````fun num1AndNum2(num1:Int,num2:Int,operator:(Int,Int)->Int):Int{
val result=operation(num1,num2)
return result
}
fun main(){
val num1=100
val num2=80
val result=num1AndNum2(num1,num2){n1,n2 ->
n1+n2
}
}
``````

``````public static int num1AndNum2(int num1,int num2,Function operation){
int result=(int) operation.invoke(num1,num2);
return result;
}
public static void main(){
int num1=100;
int num2=80;
int result=num1AndNum2(num1,num2,new Function(){
@Override
public Integer invoke(Integer n1,Integer n2){
return n1+n2;
}
});
}
``````

``````inline fun num1AndNum2(num1:Int,num2:Int,operation:(Int,Int)->Int):Int{
val result=operation(num1,num2)
return result
}
``````

### 三、noinline与crossinline

``````inline fun inlineTest(block1:()->Unit,noinline block2:()->Unit){
}
``````

``````fun printString(str:String,block:(String)->Unit){
println("printString begin")
block(str)
println("println end")
}
``````
``````fun main(){
println("main start")
val str=""
printString(str){s->
println("lambda start")
if(s.isEmpty())
return@printString
println(s)
println("lambda end")
}
println("main end")
}
``````

``````inline fun printString(str:String,block:(String)->Unit){
println("printString begin")
block(str)
println("printString end")
}
``````
``````fun main(){
println("main start")
val str=""
printString(str){s ->
println("Lambda start")
if(s.isEmpty())
return
println(s)
println("lambda end")
}
println("main end")
}
``````

``````inline fun runRunnable(block:()->Unit){
val runnable= Runnable { block() }
runnable.run()
}
``````

``````inline fun runRunnable(crossinline block:()->Unit){
val runnable= Runnable {
block()
}
runnable.run()
}
``````

THE END