var add = {x: Int, y: Int -> x + y}<\/pre>\nKotlin \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}]}}