理解Currying 概念
简单说就是多元函数变换成一元函数调用链
了解Curring的实现方法
实例1
package com.yzdzy.kotlin.chapter5.currying
fun hello(x: String, y: Int, z: Double): Boolean {
return true
}
fun currideHello(x: String): (y: Int) -> (z: Double) -> Boolean {
}
实例2
package com.yzdzy.kotlin.chapter5.currying
import java.io.OutputStream
fun log(tag: String, target: OutputStream, message: Any?) {
target.write("[$tag] $message\n".toByteArray())
}
fun log(tag: String) = fun(target: OutputStream) = fun(message: Any?) = target.write("[$tag] $message\n".toByteArray())
// fun <P1, P2, P3, R> Function3<P1, P2, P3, R>.curried() =
// fun(p1: P1) = fun(p2: P2) = fun(p3: P3) = this(p1, p2, p3)
fun main(args: Array<String>) {
log("benny", System.out, "Hello world")
log("benny")(System.out)("Hello world again")
}
实例3
package com.yzdzy.kotlin.chapter5.currying
import java.io.OutputStream
fun log(tag: String, target: OutputStream, message: Any?) {
target.write("[$tag] $message\n".toByteArray())
}
//fun log(tag: String) = fun(target: OutputStream) = fun(message: Any?) = target.write("[$tag] $message\n".toByteArray())
fun <P1, P2, P3, R> Function3<P1, P2, P3, R>.curried() =
fun(p1: P1) = fun(p2: P2) = fun(p3: P3) = this(p1, p2, p3)
fun main(args: Array<String>) {
log("benny", System.out, "Hello world")
::log.curried()("benny")(System.out)("Hello world again")
}