{"id":160,"date":"2019-02-23T21:33:26","date_gmt":"2019-02-23T13:33:26","guid":{"rendered":"http:\/\/132.145.126.25\/?p=160"},"modified":"2020-05-25T15:43:07","modified_gmt":"2020-05-25T07:43:07","slug":"closure-zh","status":"publish","type":"post","link":"https:\/\/www.tsihyoung.com\/index.php\/2019\/02\/23\/closure-zh\/","title":{"rendered":"\u7a0b\u5f0f\u8a9e\u8a00\u4e2d\u7684\u6cdb\u51fd"},"content":{"rendered":"\n<p>\u5728\u6578\u5b78\u91cc\uff0c\u7c21\u55ae\u4f86\u8aaa\u6cdb\u51fd\u5c31\u662f\u51fd\u6578\u7684\u51fd\u6578\u3002\u6cdb\u51fd\u7684\u81ea\u8b8a\u6578\u662f\u51fd\u6578\uff0c\u5728\u6578\u5b78\u91cc\u7a31\u70ba\u5b97\u91cf\uff0c\u800c\u5728\u7a0b\u5f0f\u8a9e\u8a00swift\u4e2d\u7a31\u70ba\u9589\u5305\u2018closure\u2019\u3002<\/p>\n\n\n\n<p>\u5728swift\u4e2d\uff0c\u5b97\u91cf\u5beb\u6cd5\u8207\u51fd\u6578\u985e\u4f3c\uff0c\u4f46\u9700\u8981\u5c07\u81ea\u8b8a\u6578\u548c\u8fd4\u56de\u503c\u8072\u660e\u90fd\u653e\u7f6e\u5230\u82b1\u62ec\u865f\u5c0d\u4e4b\u4e2d\uff0c\u4e26\u8207\u5269\u9918\u90e8\u5206\u7528\u2018in\u2019\u9694\u958b\uff0c\u5982\u679c\u6c92\u6709\u8fd4\u56de\u503c\uff0c\u4e5f\u9808\u7528\u7a7a\u7684\u5713\u62ec\u865f\u4f54\u4f4d\u3002\u800c\u6cdb\u51fd\u7684\u5b9a\u7fa9\u5247\u8207\u666e\u901a\u51fd\u6578\u7121\u7570\uff0c\u53ea\u662f\u8981\u5728\u8b8a\u6578\u8072\u660e\u88e1\u5beb\u51fa\u5b97\u91cf\u7684\u81ea\u8b8a\u6578\u548c\u8fd4\u56de\u503c\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\u6211\u5011\u5b9a\u7fa9\u5169\u7a2e\u5e73\u5747\u6578\uff0c\u8abf\u548c\u5e73\u5747\u8207\u5e7e\u4f55\u5e73\u5747\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">import Foundation\n\nlet harmonic = { (_ corrected: [Double]) -> Double in\n    return Double(corrected.count)\/corrected.reduce(0.0,{$0+1.0\/$1})\n}\n\nlet geometric = { (_ corrected: [Double]) -> Double in\n    return pow(corrected.reduce(1.0,*),1.0\/Double(corrected.count))\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u5f8c\u6211\u5011\u5beb\u4e00\u500b\u6cdb\u51fd\u7528\u4f86\u8a08\u7b97\u4e00\u7d44\u6578\u64da\u53bb\u6389\u57fa\u7dda\u503c\u5f8c\u7684\u5e73\u5747\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">func average(_ nums: [Double], _ baseLine: Double, _ ave: (_ corrected: [Double]) -> Double) -> Double {\n    var nums = nums\n    \n    \/\/\u6e1b\u53bb\u57fa\u7dda\n    for i in 0..&lt;nums.count {\n        nums[i] -= baseLine\n    }\n    \n    return ave(nums)\n}<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\u4e0a\u9762average\u6cdb\u51fd\u4e2d\u5b97\u91cfave\u7684\u5199\u6cd5\u3002<\/p>\n\n\n\n<p>\u6211\u5011\u73fe\u5728\u4ee5\u5e7e\u4f55\u5e73\u5747\u503c\u70ba\u57fa\u7dda\u8a08\u7b97\u8abf\u548c\u5e73\u5747\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"swift\" class=\"language-swift\">let nums = [1.0,2.0,3.0,4.0]\nlet baseLine = average(nums,0.0,geometric)\nlet result = average(nums,baseLine,harmonic)\nprint(baseLine,result)<\/code><\/pre>\n\n\n\n<p>\u5728\u8abf\u7528\u6cdb\u51fd\u6642\uff0c\u5b97\u91cf\u7684\u5beb\u6cd5\u8207\u666e\u901a\u81ea\u8b8a\u6578\u7121\u7570\u3002<\/p>\n\n\n\n<p>\u9664\u4e86\u81ea\u8b8a\u6578\u53ef\u4ee5\u662f\u51fd\u6578\u5916\uff0c\u51fd\u6578\u7684\u8fd4\u56de\u503c\u4e5f\u53ef\u4ee5\u662f\u51fd\u6578\uff0c\u9019\u88e1\u5c31\u4e0d\u8209\u4f8b\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6578\u5b78\u91cc\uff0c\u7c21\u55ae\u4f86\u8aaa\u6cdb\u51fd\u5c31\u662f\u51fd\u6578\u7684\u51fd\u6578\u3002\u6cdb\u51fd\u7684\u81ea\u8b8a\u6578\u662f\u51fd\u6578\uff0c\u5728\u6578\u5b78\u91cc\u7a31\u70ba\u5b97\u91cf\uff0c\u800c\u5728\u7a0b\u5f0f\u8a9e\u8a00swift\u4e2d\u7a31\u70ba\u9589\u5305\u2018closure\u2019\u3002 \u5728swift\u4e2d\uff0c\u5b97\u91cf\u5beb\u6cd5\u8207\u51fd\u6578\u985e\u4f3c\uff0c\u4f46\u9700\u8981\u5c07\u81ea\u8b8a\u6578\u548c\u8fd4\u56de\u503c\u8072\u660e\u90fd\u653e\u7f6e\u5230\u82b1\u62ec\u865f\u5c0d\u4e4b\u4e2d\uff0c\u4e26\u8207\u5269\u9918\u90e8\u5206\u7528\u2018in\u2019\u9694\u958b\uff0c\u5982\u679c\u6c92\u6709\u8fd4\u56de\u503c\uff0c\u4e5f\u9808\u7528\u7a7a\u7684\u5713\u62ec\u865f\u4f54\u4f4d\u3002\u800c\u6cdb\u51fd\u7684\u5b9a\u7fa9\u5247\u8207\u666e\u901a\u51fd\u6578\u7121\u7570\uff0c\u53ea\u662f\u8981\u5728\u8b8a\u6578\u8072\u660e\u88e1\u5beb\u51fa\u5b97\u91cf\u7684\u81ea\u8b8a\u6578\u548c\u8fd4\u56de\u503c\u3002 \u4f8b\u5982\u6211\u5011\u5b9a\u7fa9\u5169\u7a2e\u5e73\u5747\u6578\uff0c\u8abf\u548c\u5e73\u5747\u8207\u5e7e\u4f55\u5e73\u5747\uff1a \u7136\u5f8c\u6211\u5011\u5beb\u4e00\u500b\u6cdb\u51fd\u7528\u4f86\u8a08\u7b97\u4e00\u7d44\u6578\u64da\u53bb\u6389\u57fa\u7dda\u503c\u5f8c\u7684\u5e73\u5747\u503c\uff1a \u6ce8\u610f\u4e0a\u9762average\u6cdb\u51fd\u4e2d\u5b97\u91cfave\u7684\u5199\u6cd5\u3002 \u6211\u5011\u73fe\u5728\u4ee5\u5e7e\u4f55\u5e73\u5747\u503c\u70ba\u57fa\u7dda\u8a08\u7b97\u8abf\u548c\u5e73\u5747\uff1a \u5728\u8abf\u7528\u6cdb\u51fd\u6642\uff0c\u5b97\u91cf\u7684\u5beb\u6cd5\u8207\u666e\u901a\u81ea\u8b8a\u6578\u7121\u7570\u3002 \u9664\u4e86\u81ea\u8b8a\u6578\u53ef\u4ee5\u662f\u51fd\u6578\u5916\uff0c\u51fd\u6578\u7684\u8fd4\u56de\u503c\u4e5f\u53ef\u4ee5\u662f\u51fd\u6578\uff0c\u9019\u88e1\u5c31\u4e0d\u8209\u4f8b\u4e86\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"post_formats":[],"languages":[6],"post_translationss":[],"class_list":["post-160","post","type-post","status-publish","format-standard","hentry","category-programme"],"guten_post_layout_featured_media_urls":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"guten_post_layout_landscape_large":false,"guten_post_layout_portrait_large":false,"guten_post_layout_square_large":false,"guten_post_layout_landscape":false,"guten_post_layout_portrait":false,"guten_post_layout_square":false,"sciencexlite-blog-thumb":false},"_links":{"self":[{"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/posts\/160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/comments?post=160"}],"version-history":[{"count":0,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/posts\/160\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/media?parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/categories?post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/tags?post=160"},{"taxonomy":"post_format","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/post_formats?post=160"},{"taxonomy":"language","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/languages?post=160"},{"taxonomy":"post_translations","embeddable":true,"href":"https:\/\/www.tsihyoung.com\/index.php\/wp-json\/wp\/v2\/post_translationss?post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}