1.传入部分参数得到新函数
val twly = fun(guide: String, name: String): String {
return "$guide,我系$name"
}
fun <P1, P2, R> Function2<P1, P2, R>.partial(p1: P1) = fun(p2: P2) = this(p1, p2)
fun main(args: Array<String>) {
val test = twly.partial("大扎好")
println(test("渣渣晖"))
println(test("古田罗"))
}
本文展示了一个使用Kotlin语言实现函数偏应用的例子。通过定义一个接受两个参数并返回字符串的函数,展示了如何将其中一个参数固定,从而创建一个新的只接受剩余参数的函数。这种方法可以提高代码的复用性和简洁性。
1041

被折叠的 条评论
为什么被折叠?



