{"id":71189,"date":"2023-12-23T05:58:12","date_gmt":"2023-12-22T21:58:12","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=71189"},"modified":"2023-12-23T05:58:12","modified_gmt":"2023-12-22T21:58:12","slug":"kotlin-function-analysis","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/kotlin-function-analysis.html","title":{"rendered":"\u89e3\u6790Kotlin\u00a0\u51fd\u6570\u7528\u6cd5\u4e0e\u51fd\u6570\u5f0f\u7f16\u7a0b"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecdKotlin\u51fd\u6570\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u81ea\u5df1\u5bf9\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4e00\u4e9b\u7406\u89e3\u3002\u5e76\u4e14\u4f1a\u548cPython\uff0cC++\u505a\u4e00\u4e9b\u6bd4\u8f83\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u81ea\u4eceGoogle\u7238\u7238\u5ba3\u5e03Kotlin<\/a>\u4e3a\u81ea\u5df1\u7684\u5e72\u513f\u5b50\u4e4b\u540e\uff0cKotlin\u88ab\u5404\u5927\u793e\u533a\u7092\u7684\u706b\u70ed\u3002
\n\u5982\u679c\u4f60\u5bf9Kotlin\u8bed\u6cd5\u4e00\u65e0\u6240\u77e5\uff0c\u63a8\u8350\u5148\u9605\u8bfb\u5b98\u65b9\u6587\u6863\u6216\u8005\u4e2d\u6587\u7ad9\uff08https:\/\/www.kotlincn.net\/docs\/reference\/\uff09\u4e4b\u540e\u518d\u770b\u8fd9\u7bc7\u6587\u7ae0\u4f1a\u6709\u66f4\u6df1\u523b\u7684\u7406\u89e3\u3002<\/p>\n

\u4e0b\u9762\u662f\u7ef4\u57fa\u767e\u79d1\u4e0a\u5bf9\u4e8e\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u5b9a\u4e49\uff1a<\/p>\n

\u51fd\u6570\u5f0f\u7f16\u7a0b\uff08\u82f1\u8bed\uff1afunctional programming\uff09\u6216\u79f0\u51fd\u6570\u7a0b\u5e8f\u8bbe\u8ba1\uff0c\u53c8\u79f0\u6cdb\u51fd\u7f16\u7a0b\uff0c\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u578b\uff0c\u5b83\u5c06\u7535\u8111\u8fd0\u7b97\u89c6\u4e3a\u6570\u5b66\u4e0a\u7684\u51fd\u6570\u8ba1\u7b97\uff0c\u5e76\u4e14\u907f\u514d\u4f7f\u7528\u7a0b\u5e8f\u72b6\u6001\u4ee5\u53ca\u6613\u53d8\u5bf9\u8c61\u3002\u51fd\u6570\u7f16\u7a0b\u8bed\u8a00\u6700\u91cd\u8981\u7684\u57fa\u7840\u662f\u03bb\u6f14\u7b97\uff08lambda calculus\uff09\u3002\u800c\u4e14\u03bb\u6f14\u7b97\u7684\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u51fd\u6570\u5f53\u4f5c\u8f93\u5165\uff08\u5f15\u6570\uff09\u548c\u8f93\u51fa\uff08\u4f20\u51fa\u503c\uff09\u3002<\/p><\/blockquote>\n

\u4e0b\u9762\u662f\u5173\u4e8e\u9ad8\u9636\u51fd\u6570\u7684\u5b9a\u4e49\uff1a<\/p>\n

\u5728\u6570\u5b66\u548c\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\uff0c\u9ad8\u9636\u51fd\u6570\u662f\u81f3\u5c11\u6ee1\u8db3\u4e0b\u5217\u4e00\u4e2a\u6761\u4ef6\u7684\u51fd\u6570\uff1a\u63a5\u53d7\u4e00\u4e2a\u6216\u591a\u4e2a\u51fd\u6570\u4f5c\u4e3a\u8f93\u5165\uff0c\u8f93\u51fa\u4e00\u4e2a\u51fd\u6570<\/p><\/blockquote>\n

\u4e0d\u96be\u63a8\u65ad\u51fa\u51fd\u6570\u5f0f\u7f16\u7a0b\u6700\u91cd\u8981\u7684\u57fa\u7840\u662f\u9ad8\u9636\u51fd\u6570\u3002\u4e5f\u5c31\u662f\u652f\u6301\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u51fd\u6570\u5f53\u4f5c\u8f93\u5165\uff08\u5f15\u6570\uff09\u548c\u8f93\u51fa\uff08\u4f20\u51fa\u503c\uff09\u3002<\/p>\n

\u51fd\u6570\u4f5c\u4e3aKotlin\u4e2d\u7684\u4e00\u7ea7\u516c\u6c11\u53ef\u4ee5\u50cf\u5176\u4ed6\u5bf9\u8c61\u4e00\u6837\u4f5c\u4e3a\u51fd\u6570\u7684\u8f93\u5165\u4e0e\u8f93\u51fa\uff0c\u8fd9\u4e5f\u5c31\u662fJava\u7a0b\u5e8f\u5458\u8f6c\u5230Kotlin\u89c9\u5f97\u53d8\u5316\u6700\u5927\uff0c\u6700\u96be\u7406\u89e3\u7684\u4e00\u70b9\u3002\u5982\u679c\u4f60\u4e4b\u524d\u5b66\u8fc7Python\u6216\u8005C++11\u53ef\u80fd\u4f1a\u5bf9\u6b64\u6bd4\u8f83\u5bb9\u6613\u63a5\u53d7\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u672c\u6587\u4ee5\u4ecb\u7ecdKotlin\u7684\u51fd\u6570\u53ca\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e3a\u4e3b\u3002<\/p>\n

Kotlin \u51fd\u6570<\/strong><\/div>\n

\u4e0b\u9762\u662fKotlin\u4e2d\u4e00\u822c\u7684\u51fd\u6570\u5b9a\u4e49\uff0c\u548cJava\u4e0d\u540c\u7684\u662f\u51fd\u6570\u5f62\u53c2\uff0c\u8fd4\u56de\u503c\u7c7b\u578b\u7f6e\u540e\u3002\u51fd\u6570\u4f53\u53ef\u4ee5\u7528\u7b49\u53f7\u8d4b\u503c\u7ed9\u51fd\u6570\u5b9a\u4e49\uff0c\u8fd9\u91cc\u4e5f\u53ef\u4ee5\u770b\u51fa\u51fd\u6570\u548c\u53d8\u91cf\u7684\u5e73\u7b49\u6027\u3002<\/p>\n

fun main(args: Array) {\r\n    var s = sum(1,2)\r\n    var m = multi(2,3)\r\n    var x = maxOf(3,4)\r\n}\r\n\r\nfun sum(a: Int, b: Int): Int {\r\n    return a + b\r\n}\r\n\r\nfun multi(a: Int, b: Int): Int = a * b\r\n\r\nfun maxOf(a: Int, b: Int): Int = if (a > b) a else b<\/pre>\n

\u53e6\u5916Kotlin\u8fd8\u652f\u6301\u51fd\u6570\u9ed8\u8ba4\u53c2\u6570\uff0c\u62d3\u5c55\u51fd\u6570\uff0c\u4e2d\u7f00\u8868\u8fbe\u5f0f\uff0c\u4e0b\u9762\u662f\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n

fun main(args: Array) {\r\n    isBiggerThan(2)\r\n    isBiggerThan(2, 5)\r\n    var s = \"a\".isLetter()\r\n    var a = 1 add 2\r\n}\r\n\r\nfun isBiggerThan(a: Int, b: Int = 0) {\r\n    return a > b\r\n}\r\n\r\n\/\/\u62d3\u5c55\u51fd\u6570\r\nfun String.isLetter(): Boolean {\r\n    return matches(Regex(\"^[a-z|A-Z]$\"))\r\n}\r\n\r\n\/\/\u62d3\u5c55\u51fd\u6570\uff0c\u4e2d\u7f00\u8868\u8fbe\u5f0f\r\ninfix fun Int.add(x: Int): Int {\r\n    return this + x\r\n}<\/pre>\n

\u652f\u6301\u9ed8\u8ba4\u53c2\u6570\u7684\u51fd\u6570\u53ef\u4ee5\u51cf\u5c0f\u51fd\u6570\u7684\u91cd\u8f7d\u3002<\/p>\n

String\u5bf9\u8c61\u4e2d\u672c\u6ca1\u6709\u5224\u65ad\u662f\u5426\u662f\u5b57\u6bcd\u7684\u65b9\u6cd5\uff0c\u5728Java\u4e2d\u6211\u4eec\u4e00\u822c\u4f1a\u5b9a\u4e49\u4e00\u4e9bUtils\u65b9\u6cd5\uff0c\u800c\u5728Kotlin\u4e2d\u53ef\u4ee5\u5b9a\u4e49\u7c7b\u7684\u62d3\u5c55\u51fd\u6570\u3002
\n\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u662f\u7ed9Int\u7c7b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u62d3\u5c55\u51fd\u6570\uff0c\u5e76\u4e14\u8be5\u62d3\u5c55\u51fd\u6570\u4ee5\u4e2d\u7f00\u8868\u8fbe\u5f0f\u8868\u793a\uff0c\u7ed9\u4e88\u4e86\u5f00\u53d1\u8005\u5b9a\u4e49\u7c7b\u4f3c\u5173\u952e\u5b57\u7684\u6743\u5229\u3002<\/p>\n

\u6bd4\u5982\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u521b\u5efa\u4e00\u4e2amap\u5bf9\u8c61\uff1a<\/p>\n

val kv = mapOf(\"a\" to 1, \"b\" to 2)<\/pre>\n

\u8fd9\u91cc\u7684to\u5c31\u662f\u4e00\u4e2a\u4e2d\u7f00\u8868\u8fbe\u5f0f\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n

public infix fun<A, B> A.to(that: B): Pair<A, B> = Pair(this, that)<\/pre>\n

Pair\u5c31\u662fMap\u4e2d\u5b58\u7684\u5bf9\u8c61\uff0c\u6240\u4ee5\u4f60\u4e5f\u53ef\u4ee5\u8fd9\u6837\u521b\u5efa<\/p>\n

val kv = mapOf(Pair(\"a\", 1), Pair(\"b\", 2))<\/pre>\n

\u5728Python\u4e2d\u5982\u679c\u6211\u4eec\u60f3\u8ba9\u51fd\u6570\u8fd4\u56de\u591a\u4e2a\u503c\uff0c\u53ef\u4ee5\u8fd4\u56de\u4e00\u4e2a\u5143\u7ec4\uff0cKotlin\u57fa\u4e8e\u89e3\u6784\u539f\u5219\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u529f\u80fd\uff1a<\/p>\n

fun main(args: Array) {\r\n    val (index, count) = findWhere(\"abcabcabcabc\", 'c')\r\n}\r\n\r\nfun findWhere(str: String, findChar: Char): Pair<Int, Int> {\r\n    var index = -1\r\n    var count = 0\r\n    for ((i, v) in str.withIndex()) {\r\n        if (v == findChar) {\r\n            if (index == -1) {\r\n                index = i\r\n            }\r\n            ++count\r\n        }\r\n    }\r\n    return Pair(index, count)\r\n}<\/pre>\n

\u81ea\u5b9a\u4e49\u5bf9\u8c61\u5982\u4f55\u652f\u6301\u89e3\u6784\u8bf7\u67e5\u770b\u5b98\u65b9\u6587\u6863\uff0cmap\u652f\u6301\u89e3\u6784\uff0c\u6240\u4ee5\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u904d\u5386\uff1a<\/p>\n

for ((k, v) in map) {\r\n    print(\"$k -> $v, \")\r\n}<\/pre>\n
\u9ad8\u9636\u51fd\u6570\u4e0e Lambda \u8868\u8fbe\u5f0f<\/strong><\/div>\n

\u201cLambda \u8868\u8fbe\u5f0f\u201d(lambda expression)\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0cLambda\u8868\u8fbe\u5f0f\u57fa\u4e8e\u6570\u5b66\u4e2d\u7684\u03bb\u6f14\u7b97\u5f97\u540d\uff0c\u76f4\u63a5\u5bf9\u5e94\u4e8e\u5176\u4e2d\u7684lambda\u62bd\u8c61(lambda abstraction)\uff0c\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\uff0c\u5373\u6ca1\u6709\u51fd\u6570\u540d\u7684\u51fd\u6570\u3002Lambda\u8868\u8fbe\u5f0f\u53ef\u4ee5\u8868\u793a\u95ed\u5305<\/a>\uff08\u6ce8\u610f\u548c\u6570\u5b66\u4f20\u7edf\u610f\u4e49\u4e0a\u7684\u4e0d\u540c\uff09\u3002<\/p><\/blockquote>\n

Python\u4e2d\u7684lambda\u8868\u8fbe\u5f0f\uff1a<\/p>\n

add = lambda x, y:x+y<\/pre>\n

C++\u4e2d\u7684lambda\uff1a<\/p>\n

[](int x, int y) -> int{ return x + y; }<\/pre>\n

Kotlin\u4e2d\u7684lambda:<\/p>\n

var add = {x: Int, y: Int -> x + y}<\/pre>\n

Kotlin \u4f5c\u4e3a\u4e00\u4e2a\u5f3a\u7c7b\u578b\u8bed\u8a00\u8fd8\u662f\u6bd4\u8f83\u7b80\u6d01\u7684\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\u4e00\u4e2alambda\u8868\u8fbe\u5f0f\uff1a<\/p>\n

fun main(args: Array) {\r\nval sumLambda = {a: Int, b: Int -> a + b}\r\nsumLambda(1, 2)\r\n}<\/pre>\n

\u5b83\u53ef\u4ee5\u50cf\u51fd\u6570\u4e00\u6837\u4f7f\u7528()\u8c03\u7528\uff0c\u5728kotlin\u4e2d\u64cd\u4f5c\u7b26\u662f\u53ef\u4ee5\u91cd\u8f7d\u7684\uff0c()\u64cd\u4f5c\u7b26\u5bf9\u5e94\u7684\u5c31\u662f\u7c7b\u7684\u91cd\u8f7d\u51fd\u6570invoke()\u3002<\/p>\n

\u4f60\u8fd8\u53ef\u4ee5\u60f3\u4e0b\u9762\u8fd9\u6837\u5b9a\u4e49\u4e00\u4e2a\u53d8\u91cf\uff1a<\/p>\n

val numFun: (a: Int, b: Int) -> Int<\/pre>\n

\u5b83\u4e0d\u662f\u4e00\u4e2a\u666e\u901a\u7684\u53d8\u91cf\uff0c\u5b83\u5fc5\u987b\u6307\u5411\u4e00\u4e2a\u51fd\u6570\uff0c\u5e76\u4e14\u51fd\u6570\u7b7e\u540d\u5fc5\u987b\u4e00\u81f4\uff1a<\/p>\n

fun main(args: Array) {\r\n    val sumLambda = {a: Int, b: Int -> a + b}\r\n    var numFun: (a: Int, b: Int) -> Int\r\n    numFun = {a: Int, b: Int -> a + b}\r\n    numFun = sumLambda\r\n    numFun = ::sum\r\n    numFun(1,2)\r\n}\r\n\r\nfun sum(a: Int, b: Int): Int {\r\n    return a + b\r\n}<\/pre>\n

\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a\u53d8\u91cf\u53ef\u4ee5\u7b49\u4e8e\u4e00\u4e2alambda\u8868\u8fbe\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u7b49\u4e8e\u53e6\u4e00\u4e2alambda\u8868\u8fbe\u5f0f\u53d8\u91cf\uff0c\u8fd8\u53ef\u4ee5\u7b49\u4e8e\u4e00\u4e2a\u666e\u901a\u51fd\u6570\uff0c\u4f46\u662f\u5728\u51fd\u6570\u540d\u524d\u9700\u8981\u52a0\u4e0a(::)\u6765\u83b7\u53d6\u51fd\u6570\u5f15\u7528\u3002<\/p>\n

\u8fd9\u4e2a\u7c7b\u4f3cC++\u4e2d\u7684\u51fd\u6570\u6307\u9488\uff0c\u7136\u800c\u5728Python\u4e2d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u51fd\u6570\u540d\u4f5c\u4e3a\u51fd\u6570\u5f15\u7528\uff0c\u4e0b\u9762\u662fc++\u51fd\u6570\u6307\u9488\u7684\u4f8b\u5b50\uff1a<\/p>\n

#include \r\n\r\nusing namespace std;\r\n\r\nvoid swap(int &x, int &y);\r\n\r\nint main(int arg, char* args[]) {\r\n\tint x = 10;\r\n\tint y = 20;\r\n\r\n\tvoid (*methodPtr)(int &x, int &y);\/\/\u58f0\u660e\u4e00\u4e2a\u51fd\u6570\u6307\u9488\r\n\tmethodPtr = &swap; \/\/\u51fd\u6570\u6307\u9488\u8d4b\u503c\r\n\tmethodPtr = swap;\/\/\u53d6\u5730\u5740\u7b26\u53ef\u7701\u7565,\u6548\u679c\u548c\u4e0a\u9762\u4e00\u81f4\r\n\tmethodPtr(x, y); \/\/\u50cf\u7ed9\u51fd\u6570\u8d77\u4e86\u4e00\u4e2a\u522b\u540d\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\uff08\uff09\u8c03\u7528\r\n\tcout << \"x:\" << x << \" y:\" << y << endl; \/\/x:20 y:10\r\n}\r\n\r\nvoid swap(int &x, int &y) {\r\n\tint tmp = x;\r\n\tx = y;\r\n\ty = tmp;\r\n}<\/pre>\n

\u56de\u5230Kotlin\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5c06\u4e00\u4e2a\u51fd\u6570\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u6bd4\u5982\uff1a<\/p>\n

\/\/\u51fd\u6570\u53c2\u6570\r\nfun  doMap(list: List, function: (it: T) -> Any) {\r\n    for (item in list) {\r\n        function(item)\r\n    }\r\n}<\/pre>\n
\n

\u00a0\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e00\u4e2aList\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u76ee\u7684\u5c31\u662f\u5c06List\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd\u6267\u884c\u4e00\u6b21\u7b2c\u4e8c\u4e2a\u51fd\u6570\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n

val strList = listOf(\"h\" ,\"e\", \"1\", \"a\", \"b\", \"2\", \" \", \"\", \"c\", \"5\", \"7\", \"F\")\r\ndoMap(strList, {item ->print(\"item: ${upperLetter(item)}, \") })\r\n\r\nfun upperLetter(item: String): String {\r\n    if (item.isLetter()) {\r\n        return item.toUpperCase()\r\n    }\r\n    return item\r\n}<\/pre>\n<\/div>\n

\u7b2c\u4e8c\u4e2a\u53c2\u6570\u76f4\u63a5\u4f20\u8fdb\u53bb\u4e86\u4e00\u4e2alambda\u8868\u8fbe\u5f0f\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u4f20\u4e00\u4e2a\u51fd\u6570\u5f15\u7528\uff1a<\/p>\n

val strList = listOf(\"h\" ,\"e\", \"1\", \"a\", \"b\", \"2\", \" \", \"\", \"c\", \"5\", \"7\", \"F\")\r\ndoMap(strList, ::printUpperLetter)\r\n\r\nfun printUpperLetter(item: String) {\r\n    print(\"item: ${upperLetter(item)}, \")\r\n}\r\n\r\nfun upperLetter(item: String): String {\r\n    if (item.isLetter()) {\r\n        return item.toUpperCase()\r\n    }\r\n    return item\r\n}<\/pre>\n

\u6548\u679c\u548c\u4e0a\u9762\u7684\u4ee3\u7801\u4e00\u6837\u3002<\/p>\n

\u5728C++\u4e2d\u4f7f\u7528\u51fd\u6570\u6307\u9488\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\uff1a<\/p>\n

using namespace std;\r\n\r\nvoid mMap(vector list, void (*fun)(int item));\r\n\r\nint main(int arg, char* args[]) {\r\n\tvector list = {2,3,4,3,2,1,2};\r\n\tmMap(list, [](int item) -> void { cout << item << endl; });\r\n}\r\n\r\nvoid mMap(vector list, void (*fun)(int item)) {\r\n\tfor(int it : list) {\r\n\t    fun(it);\r\n\t}\r\n}<\/pre>\n

\u518d\u6b21\u56de\u5230Kotlin\uff0c\u5982\u679c\u51fd\u6570\u4f5c\u4e3a\u5165\u53c2\u5728\u5165\u53c2\u5217\u8868\u7684\u6700\u540e\u4e00\u4e2a\uff0c\u4f60\u8fd8\u53ef\u4ee5\u8fd9\u6837\u505a\uff0c\u76f4\u63a5\u5199\u5728\u5927\u62ec\u53f7\u5185\uff1a<\/p>\n

fun main(args: Array) {\r\n    log { sum(1,2) }\r\n}\r\n\r\nfun  log(function: () -> T) {\r\n    val result = function()\r\n    println(\"result -> $result\")\r\n}<\/pre>\n

\u662f\u4e0d\u662f\u6709\u70b9\u50cfgradle\u914d\u7f6e\u6587\u4ef6\u7684\u5199\u6cd5\uff0c\u6240\u4ee5Kotlin\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u7f16\u5199 \u9886\u57df\u4e13\u7528\u8bed\u8a00(DSL)<\/p>\n

\u53e6\u5916Kotlin\u8fd8\u652f\u6301\u5c40\u90e8\u51fd\u6570\u548c\u51fd\u6570\u4f5c\u4e3a\u8fd4\u56de\u503c\uff0c\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n

fun main(args: Array) {\r\n    val addResult = lateAdd(2, 4)\r\n    addResult()\r\n}\r\n\/\/\u5c40\u90e8\u51fd\u6570\uff0c\u51fd\u6570\u5f15\u7528\r\nfun lateAdd(a: Int, b: Int): Function0 {\r\n    fun add(): Int {\r\n        return a + b\r\n    }\r\n    return ::add\r\n}<\/pre>\n

\u5728lateAdd\u5185\u90e8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5c40\u90e8\u51fd\u6570\uff0c\u6700\u540e\u8fd4\u56de\u4e86\u8be5\u5c40\u90e8\u51fd\u6570\u7684\u5f15\u7528\uff0c\u5bf9\u7ed3\u679c\u4f7f\u7528()\u64cd\u4f5c\u7b26\u62ff\u5230\u6700\u7ec8\u7684\u7ed3\u679c\uff0c\u8fbe\u5230\u5ef6\u8fdf\u8ba1\u7b97\u7684\u76ee\u7684\u3002<\/p>\n

\u51fd\u6570\u4f5c\u4e3a\u4e00\u7ea7\u516c\u6c11\u5f53\u7136\u53ef\u4ee5\u50cf\u666e\u901a\u5bf9\u8c61\u4e00\u6837\u653e\u8fdbmap\u4e2d\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n

val funs = mapOf(\"sum\" to ::sum)\r\nval mapFun = funs[\"sum\"]\r\nif (mapFun != null) {\r\n   val result = mapFun(1,2)\r\n   println(\"sum result -> $result\")\r\n}\r\n\r\nfun sum(a: Int, b: Int): Int {\r\n    return a + b\r\n}<\/pre>\n

\u5c06\u4e00\u4e2a\u51fd\u6570\u5f15\u7528\u4f5c\u4e3avalue\u653e\u8fdb\u4e86map\u4e2d\uff0c\u53d6\u51fa\u6765\u4e4b\u540e\u4f7f\u7528()\u64cd\u4f5c\u7b26\u8c03\u7528\uff0c\u53ef\u4ee5\u7b80\u5316\u4e00\u4e9bif,else\u7684\u573a\u666f\u3002<\/p>\n

\u57fa\u4e8e\u4ee5\u4e0a\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u7279\u6027\uff0cKotlin\u53ef\u4ee5\u50cfRxJava\u4e00\u6837\u5f88\u65b9\u4fbf\u7684\u8fdb\u884c\u76f8\u5e94\u5f0f\u7f16\u7a0b\uff0c\u6bd4\u5982\uff1a<\/p>\n

fun printUpperLetter(list: List) {\r\n    list\r\n            .filter (fun(item):Boolean {\r\n                return item.isNotEmpty()\r\n            })\r\n            .filter { item -> item.isNotBlank()}\r\n            .filter {\r\n                item ->\r\n                if (item.isNullOrEmpty()) {\r\n                    return@filter false\r\n                }\r\n                return@filter item.matches(Regex(\"^[a-z|A-Z]$\"))\r\n            }\r\n            .filter { it.isLetter() }\r\n            .map(String::toUpperCase)\r\n            .sortedBy { it }\r\n            .forEach { print(\"$it, \") }\r\n    println()\r\n}<\/pre>\n

\u4e0a\u9762\u7684\u4ee3\u7801\u53ea\u662f\u505a\u6f14\u793a\uff0c\u5e76\u65e0\u5b9e\u9645\u610f\u4e49\u3002\u5177\u4f53\u8bed\u6cd5\u8bf7\u67e5\u770b\u5b98\u65b9\u6587\u6863\u3002<\/p>\n

\u6211\u76f8\u4fe1Kotlin\u4f5c\u4e3a\u4e00\u79cd\u5f3a\u7c7b\u578b\u7684\u73b0\u4ee3\u5316\u8bed\u8a00\u53ef\u4ee5\u5728\u4fdd\u8bc1\u7a33\u5b9a\u6027\u7684\u540c\u65f6\u6781\u5927\u5730\u63d0\u9ad8\u5f00\u53d1\u8005\u7684\u5f00\u53d1\u6548\u7387\u3002<\/p>\n

\n

\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.codeceo.com\/article\/kotlin-function.html<\/a><\/p>\n

\u672c\u6587\u5730\u5740\uff1ahttp:\/\/lrxjmw.cn\/kotlin-function-analysis.html<\/a>\u7f16\u8f91\u5458\uff1a\u534e\u4e16\u53d1\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

\u5bfc\u8bfb \u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecdKotlin\u51fd\u6570\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u81ea\u5df1\u5bf9\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u4e00\u4e9b\u7406\u89e3\u3002\u5e76\u4e14\u4f1a\u548cPython\uff0cC++\u505a\u4e00 […]<\/p>\n","protected":false},"author":63,"featured_media":85674,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-71189","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/71189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=71189"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/71189\/revisions"}],"predecessor-version":[{"id":85681,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/71189\/revisions\/85681"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/85674"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=71189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=71189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=71189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}