{"id":9365,"date":"2014-08-23T22:11:22","date_gmt":"2014-08-24T02:11:22","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=9365"},"modified":"2014-08-23T22:11:51","modified_gmt":"2014-08-24T02:11:51","slug":"swift-function-currying","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/08\/23\/swift-function-currying\/","title":{"rendered":"Swift Function Currying"},"content":{"rendered":"<p><a href=\"http:\/\/www.russbishop.net\/swift-function-currying\">Russ Bishop<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.russbishop.net\/swift-function-currying\"><p>Now forget all that stuff we just discussed because Swift supports automatic function currying. Just separate the parameters inside their own parens and you&rsquo;re done:<\/p>\n<pre>\nfunc takeFive&lt;T:IntegerArithmeticType&gt;(a:T)(b:T)(c:T)(d:T)(e:T) -&gt; T {\n    return a + b + c + d + e\n}\n<\/pre>\n<p>[&#8230;]<\/p>\n<p>A caller might ask us for a function that runs queries against a certain database. We can use a curried function (possibly private) that takes connection info as its first parameter, followed by query information as its second; the caller gets back a curried function that takes only the query parameters. The caller can then use this returned function without ever being aware of the details. We don&rsquo;t have to create queue or thread specific ambient context information to track some sort of &ldquo;current&rdquo; connection. The storage lifetime of the connection information is tied to the lifetime of the closure&rsquo;s environment. When the closure goes away, so too will the saved context. This also lets us have multiple active connections or pass them between queues and threads. In short, we can <em>hide the details<\/em> without jumping through any hoops or writing boilerplate objects just to carry some state data around.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Russ Bishop: Now forget all that stuff we just discussed because Swift supports automatic function currying. Just separate the parameters inside their own parens and you&rsquo;re done: func takeFive&lt;T:IntegerArithmeticType&gt;(a:T)(b:T)(c:T)(d:T)(e:T) -&gt; T { return a + b + c + d + e } [&#8230;] A caller might ask us for a function that runs queries [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[46,71,901],"class_list":["post-9365","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=9365"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9365\/revisions"}],"predecessor-version":[{"id":9367,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9365\/revisions\/9367"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=9365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=9365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=9365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}