柯里化函數(shù)
定義:數(shù)學(xué)上的一種概念
簡(jiǎn)單說就是多元函數(shù)變換一元函數(shù)調(diào)用鏈
fun hello(x: String,y: Int):Boolean{
print("x==$x,y==$y \n")
return true
}
fun curriedHello(x: String):(y: Int) -> Boolean{
return fun (y):Boolean{
print("x==$x,y==$y \n")
return true
}
}
fun main() {
hello("a",123)
curriedHello("a")(123)
}
利用擴(kuò)展函數(shù)對(duì)該類函數(shù)進(jìn)行擴(kuò)展
fun <P1, P2, R> Function2<P1, P2, R>.curried()
= fun(p1: P1) = fun(p2: P2) = this(p1, p2)
fun main() {
hello("a",123)
curriedHello("a")(123)
::hello.curried()("a")(123)
}
偏函數(shù)
1.偏函數(shù)是在柯里化的基礎(chǔ)上得來
2.原函數(shù)傳入部分參數(shù)后得到的新函數(shù)就叫偏函數(shù)
fun main() {
var printY=::hello.curried()("a")
printY(1)
printY(2)
}