{"id":1798,"date":"2015-05-03T16:33:31","date_gmt":"2015-05-03T22:33:31","guid":{"rendered":"http:\/\/jamesonquave.com\/blog\/?p=1798"},"modified":"2015-05-03T16:36:30","modified_gmt":"2015-05-03T22:36:30","slug":"function-currying-in-swift","status":"publish","type":"post","link":"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/","title":{"rendered":"Function Currying in Swift"},"content":{"rendered":"<h1 id=\"function-currying-in-swift\">Function Currying in Swift<\/h1>\n<p>The concept of currying is that instead of accepting multiple arguments, a function accepts only one, and returns a function which acepts the remaining arguments. The returned function will also accept only one argument, and returns another function. This process continues until all arguments are exhausted and we are left only with a single return value.<\/p>\n<p>For example, usually we define a function that returns the sum of two integers as follows:<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_493367\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"keyword\">func<\/code> <code class=\"plain\">add1(x: <\/code><code class=\"color1\">Int<\/code><code class=\"plain\">, y: <\/code><code class=\"color1\">Int<\/code><code class=\"plain\">) -&gt; <\/code><code class=\"color1\">Int<\/code> <code class=\"plain\">{<\/code><\/div><div class=\"line number2 index1 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">return<\/code> <code class=\"plain\">x + y<\/code><\/div><div class=\"line number3 index2 alt2\"><code class=\"plain\">}<\/code><\/div><div class=\"line number4 index3 alt1\"><code class=\"plain\">add1(1, 2) <\/code><code class=\"comments\">\/\/ Output: 3<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>We can always transform a function taking multiple arguments into a curried one, by separating the function into a series of function that each takes only one argument. The curried version of add1 is as follows:<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_256156\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"keyword\">func<\/code> <code class=\"plain\">add2(x: <\/code><code class=\"color2\">Int<\/code><code class=\"plain\">) -&gt; (<\/code><code class=\"color1\">Int<\/code> <code class=\"plain\">-&gt; <\/code><code class=\"color2\">Int<\/code><code class=\"plain\">) {<\/code><\/div><div class=\"line number2 index1 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">return<\/code> <code class=\"plain\">{ y <\/code><code class=\"keyword\">in<\/code> <code class=\"keyword\">return<\/code> <code class=\"plain\">x + y }<\/code><\/div><div class=\"line number3 index2 alt2\"><code class=\"plain\">}<\/code><\/div><div class=\"line number4 index3 alt1\"><code class=\"plain\">add2(1)(2) <\/code><code class=\"comments\">\/\/ Output: 3<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>This function has this type specified: <code>Int -&gt; Int -&gt; Int<\/code>. This may seem a little strange to newcomers to functional programming. Which part is the argument, and which part is the return type?<\/p>\n<p>Here, add2 is taking an <code>Int<\/code>, and returning a Function which takes another <code>Int<\/code>, which in turn returns a third <code>Int<\/code>. You could say it&#8217;s something like this: <code>Int -&gt; (Int -&gt; Int)<\/code>, or we could use typealias to make <code>(Int -&gt; Int)<\/code> more clear:<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_753577\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"keyword\">typealias<\/code> <code class=\"color2\">IntTransformer<\/code> <code class=\"plain\">= (<\/code><code class=\"color1\">Int<\/code> <code class=\"plain\">-&gt; <\/code><code class=\"color1\">Int<\/code><code class=\"plain\">)<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>Now, any time we see <code>IntTransformer<\/code>, it&#8217;s easier to comprehend that it&#8217;s a function that transforms an Int value. With that, we could redfine add2 like this:<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_66204\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"keyword\">func<\/code> <code class=\"plain\">add2Aliased(x: <\/code><code class=\"color1\">Int<\/code><code class=\"plain\">) -&gt; <\/code><code class=\"color2\">IntTransformer<\/code> <code class=\"plain\">{<\/code><\/div><div class=\"line number2 index1 alt1\"><code class=\"plain\">...<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>This probably looks a little more familiar, but under the hood this is exactly the same as the add2 function we defined with the data type <code>Int -&gt; Int -&gt; Int<\/code>. This is just a more familiar looking way to write it.<\/p>\n<p>Calling add2() with just a single argument returns a function that takes another <code>(Int -&gt; Int)<\/code> function, which means we can store <em>that<\/em> function in a separate variable if we so choose:<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_747991\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">addTwentyTransformer = add2(20)<\/code><\/div><div class=\"line number2 index1 alt1\"><code class=\"plain\">addTwentyTransformer(5) <\/code><code class=\"comments\">\/\/ Output: 25<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>Now, <code>add2(20)<\/code> is a function that takes one integer and returns the value of that integer plus 20.<\/p>\n<p>The -&gt; operator is defined as associativity right. Instead of writing <code>A -&gt; (B -&gt; (C -&gt; D))<\/code>, we can simply write <code>A -&gt; B -&gt; C -&gt; D<\/code>.<\/p>\n<p>Swift also supports another way to define a curried function:<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_963713\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"keyword\">func<\/code> <code class=\"plain\">add3(x: <\/code><code class=\"color1\">Int<\/code><code class=\"plain\">)(y: <\/code><code class=\"color1\">Int<\/code><code class=\"plain\">) -&gt; <\/code><code class=\"color1\">Int<\/code> <code class=\"plain\">{<\/code><\/div><div class=\"line number2 index1 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">return<\/code> <code class=\"plain\">x + y<\/code><\/div><div class=\"line number3 index2 alt2\"><code class=\"plain\">}<\/code><\/div><div class=\"line number4 index3 alt1\"><code class=\"plain\">add3(1)(y: 2) <\/code><code class=\"comments\">\/\/ Output: 3<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>This is helpful if you want named parameters, which can sometimes help your code easier to read. It&#8217;s also easy to  make the syntax the same as <code>add2<\/code> and remove the explicit argument name.<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_886610\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"keyword\">func<\/code> <code class=\"plain\">add4(x: <\/code><code class=\"color1\">Int<\/code><code class=\"plain\">)(_ y: <\/code><code class=\"color1\">Int<\/code><code class=\"plain\">) -&gt; <\/code><code class=\"color1\">Int<\/code> <code class=\"plain\">{<\/code><\/div><div class=\"line number2 index1 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">return<\/code> <code class=\"plain\">x + y<\/code><\/div><div class=\"line number3 index2 alt2\"><code class=\"plain\">}<\/code><\/div><div class=\"line number4 index3 alt1\"><code class=\"plain\">add4(1)(2) <\/code><code class=\"comments\">\/\/ Output: 3<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<h2 id=\"benefits-of-currying\">Benefits of Currying<\/h2>\n<p>What are the benefits currying provides? Let&#8217;s look at the add functions above.<\/p>\n<p>With <code>add1<\/code> the regular function, we cannot apply this function until both of its arguments are ready. With the curried <code>add2<\/code>, we can apply one or two arguments.<\/p>\n<p>Let&#8217;s see an example that uses <code>add1<\/code> and <code>add2<\/code> with <code>map<\/code>to transform an array of integers by adding 7 to each.<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_49828\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"comments\">\/\/ Apply map to the array using the two functions<\/code><\/div><div class=\"line number2 index1 alt1\"><code class=\"keyword\">let<\/code> <code class=\"plain\">xs = [1, 2, 3]<\/code><\/div><div class=\"line number3 index2 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">addSeven1 = { add1($0, 7) }<\/code><\/div><div class=\"line number4 index3 alt1\"><code class=\"functions\">map<\/code><code class=\"plain\">(xs, addSeven1) <\/code><code class=\"comments\">\/\/ Output: [8, 9, 10]<\/code><\/div><div class=\"line number5 index4 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">addSeven2 = add2(7)<\/code><\/div><div class=\"line number6 index5 alt1\"><code class=\"functions\">map<\/code><code class=\"plain\">(xs, addSeven2) <\/code><code class=\"comments\">\/\/ Output: [8, 9, 10]<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>The curried function <code>add2<\/code> is much cleaner in this case.<\/p>\n<p>There is another case when curried functions have obvious advantages. To demonstrate the example, first we define a function (a custom operator) that composes two functions together:<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_607089\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"comments\">\/\/ Define a new operator |&gt; that has left associativity<\/code><\/div><div class=\"line number2 index1 alt1\"><code class=\"keyword\">infix<\/code> <code class=\"keyword\">operator<\/code> <code class=\"plain\">|&gt; { associativity left }<\/code><\/div><div class=\"line number3 index2 alt2\"><code class=\"keyword\">func<\/code> <code class=\"plain\">|&gt; &lt;A, B, C&gt;(f: B -&gt; C, g: A -&gt; B) -&gt; A -&gt; C {<\/code><\/div><div class=\"line number4 index3 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">return<\/code> <code class=\"plain\">{ x <\/code><code class=\"keyword\">in<\/code><\/div><div class=\"line number5 index4 alt2\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"plain\">f(g(x))<\/code><\/div><div class=\"line number6 index5 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"plain\">}<\/code><\/div><div class=\"line number7 index6 alt2\"><code class=\"plain\">}<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>Let&#8217;s say we want to transform an array of integers by adding 7 to each element, and then adding 3 again. We could write:<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_299695\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">xs = [1, 2, 3]<\/code><\/div><div class=\"line number2 index1 alt1\">&nbsp;<\/div><div class=\"line number3 index2 alt2\"><code class=\"comments\">\/\/ Returns a function that adds three to it's only argument<\/code><\/div><div class=\"line number4 index3 alt1\"><code class=\"keyword\">let<\/code> <code class=\"plain\">addThree = add2(3)<\/code><\/div><div class=\"line number5 index4 alt2\">&nbsp;<\/div><div class=\"line number6 index5 alt1\"><code class=\"comments\">\/\/ Apply addSeven1 to every item in xs<\/code><\/div><div class=\"line number7 index6 alt2\"><code class=\"comments\">\/\/ Then apply addThree to every item in that list<\/code><\/div><div class=\"line number8 index7 alt1\"><code class=\"plain\">xs.<\/code><code class=\"functions\">map<\/code><code class=\"plain\">(addSeven1).<\/code><code class=\"functions\">map<\/code><code class=\"plain\">(addThree) <\/code><code class=\"comments\">\/\/ Output: [11, 12, 13]<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>It first adds 7 to each element in <code>xs<\/code>, wraps the results into a temporary array, then add 3 to each in the temporary array, and return the last results in a new array. This creates a temporary array that we never need.<\/p>\n<p>By composing the curried functions, <code>addSeven2<\/code> and <code>addThree<\/code>, we can eliminate the creation of the temporary array.<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_269735\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"plain\">xs.<\/code><code class=\"functions\">map<\/code><code class=\"plain\">(addSeven1 |&gt; addThree) <\/code><code class=\"comments\">\/\/ Output: [11, 12, 13]<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<h2 id=\"builtin-currying-functions-in-swift\">Builtin Currying Functions in Swift<\/h2>\n<p>In the Swift standard library there is a function on the Int type called advancedBy that takes an amount, and returns an Int that has been adjusted by that amount.<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_852808\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"plain\">extension <\/code><code class=\"color1\">Int<\/code> <code class=\"plain\">: <\/code><code class=\"color2\">RandomAccessIndexType<\/code> <code class=\"plain\">{<\/code><\/div><div class=\"line number2 index1 alt1\"><code class=\"undefined spaces\">&nbsp;&nbsp;&nbsp;&nbsp;<\/code><code class=\"keyword\">func<\/code> <code class=\"plain\">advancedBy(amount: <\/code><code class=\"color2\">Distance<\/code><code class=\"plain\">) -&gt; <\/code><code class=\"color1\">Int<\/code><\/div><div class=\"line number3 index2 alt2\"><code class=\"plain\">}<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>It&#8217;s simple enough to use this function on an Int and get the advanced value:<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_261494\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"plain\">5.advancedBy(6) <\/code><code class=\"comments\">\/\/ Output: 11<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>But because of function currying, we could get the partial application of this function by not specifying the initial value to be advanced:<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_442143\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"keyword\">let<\/code> <code class=\"plain\">add6 = <\/code><code class=\"color1\">Int<\/code><code class=\"plain\">.advancedBy(6)<\/code><\/div><div class=\"line number2 index1 alt1\"><code class=\"plain\">add6(5) <\/code><code class=\"comments\">\/\/ Output: 11<\/code><\/div><div class=\"line number3 index2 alt2\"><code class=\"plain\">add6(10) <\/code><code class=\"comments\">\/\/ Output: 10<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>Let&#8217;s look at the following example. To insert another string at the end of the given string, we could call the splice function on an instance of String:<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_272867\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"keyword\">var<\/code> <code class=\"plain\">s = <\/code><code class=\"string\">\"Hello\"<\/code><\/div><div class=\"line number2 index1 alt1\"><code class=\"plain\">s.splice(<\/code><code class=\"string\">\", world\"<\/code><code class=\"plain\">, atIndex: s.endIndex) <\/code><\/div><div class=\"line number3 index2 alt2\"><code class=\"comments\">\/\/ Output: \"Hello, world\"<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>Or we can call it on String data type, and pass the String instance as its only argument:<\/p>\n<pre><code class=\"lang-swift\"><div id=\"highlighter_51420\" class=\"syntaxhighlighter nogutter highlightedCode \"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tbody><tr><td class=\"code\"><div class=\"container\"><div class=\"line number1 index0 alt2\"><code class=\"color1\">String<\/code><code class=\"plain\">.splice(&amp;s)(<\/code><code class=\"string\">\"!!!\"<\/code><code class=\"plain\">, atIndex: s.endIndex)<\/code><\/div><div class=\"line number2 index1 alt1\"><code class=\"plain\">s <\/code><code class=\"comments\">\/\/ Output: \"Hello, world!!!\"<\/code><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/code><\/pre>\n<p>The <code>splice<\/code> function on a String instance is not a curried function. <code>s.splice(\"!!!\")(atIndex: s.endIndex)<\/code> will not compile.<\/p>\n<h2 id=\"related-topics\">Related Topics<\/h2>\n<p>The term <em>partial application<\/em>, is a function that accepts only some of its arguments, and returns another function that takes the rest of arguments. While a curried function takes only one argument.<\/p>\n<p>Don&#8217;t confuse partial application with another term called partial function. <em>Partial function<\/em> is a function that cannot provide a valid result for all its possible inputs. In Objective-C, if we call <code>[[NSString alloc] initWithString:nil]<\/code>, it will compile but throw a NSInvalidArgumentException at runtime. <code>-initWithString:<\/code> is a partial function, because there is no return value for nil.<\/p>\n<h2 id=\"next-steps\">Next Steps<\/h2>\n<p>Is this all making sense? This can all be quite a chunk of new information to take in if you are new to functional programming in general. For that reason we are preparing a free functional programming course that is in private beta testing right now. If you want to be part of the beta, or just want us to let you know when it&#8217;s ready, <a href=\"http:\/\/jamesonquave.us6.list-manage1.com\/subscribe?u=1d2576bf288fe2fd7fa71bd20&amp;id=802e5c7d81\">sign up for the beta here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Function Currying in Swift The concept of currying is that instead of accepting multiple arguments, a function accepts only one, and returns a function which acepts the remaining arguments. The returned function will also accept only one argument, and returns another function. This process continues until all arguments are exhausted and we are left only&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_links_to":"","_links_to_target":""},"categories":[70,50,10,32],"tags":[33,36],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.13 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Function Currying in Swift - Jameson Quave<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Function Currying in Swift - Jameson Quave\" \/>\n<meta property=\"og:description\" content=\"Function Currying in Swift The concept of currying is that instead of accepting multiple arguments, a function accepts only one, and returns a function which acepts the remaining arguments. The returned function will also accept only one argument, and returns another function. This process continues until all arguments are exhausted and we are left only...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/\" \/>\n<meta property=\"og:site_name\" content=\"Jameson Quave\" \/>\n<meta property=\"article:published_time\" content=\"2015-05-03T22:33:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-05-03T22:36:30+00:00\" \/>\n<meta name=\"author\" content=\"Guanshan Liu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Guanshan Liu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/\",\"url\":\"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/\",\"name\":\"Function Currying in Swift - Jameson Quave\",\"isPartOf\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\"},\"datePublished\":\"2015-05-03T22:33:31+00:00\",\"dateModified\":\"2015-05-03T22:36:30+00:00\",\"author\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/d0ef4e53cd95e21e389aefe6277f0811\"},\"breadcrumb\":{\"@id\":\"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jamesonquave.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Function Currying in Swift\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#website\",\"url\":\"https:\/\/jamesonquave.com\/blog\/\",\"name\":\"Jameson Quave\",\"description\":\"Using computer technology to educate, and improve lives.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jamesonquave.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/d0ef4e53cd95e21e389aefe6277f0811\",\"name\":\"Guanshan Liu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8f00053e85112a67cee95968f2f36ab2?s=96&d=retro&r=pg\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8f00053e85112a67cee95968f2f36ab2?s=96&d=retro&r=pg\",\"caption\":\"Guanshan Liu\"},\"description\":\"Guanshan is an iOS Developer currently working as an iOS Engineer at Alibaba Inc. Before working at Alibaba, Guanshan worked with 2K Games on Civilization Revolution 1 and 2 for iOS. He has a Masters in Software Engineering from the University of York, UK as well as a Bachelors of Engineering in Information Security from Nanjing University of Aeronautics and Astronautics. Find him on twitter, @guanshanliu.\",\"sameAs\":[\"https:\/\/guanshanliu.github.io\"],\"url\":\"https:\/\/jamesonquave.com\/blog\/author\/guanshanliu\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Function Currying in Swift - Jameson Quave","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/","og_locale":"en_US","og_type":"article","og_title":"Function Currying in Swift - Jameson Quave","og_description":"Function Currying in Swift The concept of currying is that instead of accepting multiple arguments, a function accepts only one, and returns a function which acepts the remaining arguments. The returned function will also accept only one argument, and returns another function. This process continues until all arguments are exhausted and we are left only...","og_url":"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/","og_site_name":"Jameson Quave","article_published_time":"2015-05-03T22:33:31+00:00","article_modified_time":"2015-05-03T22:36:30+00:00","author":"Guanshan Liu","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Guanshan Liu","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/","url":"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/","name":"Function Currying in Swift - Jameson Quave","isPartOf":{"@id":"https:\/\/jamesonquave.com\/blog\/#website"},"datePublished":"2015-05-03T22:33:31+00:00","dateModified":"2015-05-03T22:36:30+00:00","author":{"@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/d0ef4e53cd95e21e389aefe6277f0811"},"breadcrumb":{"@id":"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jamesonquave.com\/blog\/function-currying-in-swift\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jamesonquave.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Function Currying in Swift"}]},{"@type":"WebSite","@id":"https:\/\/jamesonquave.com\/blog\/#website","url":"https:\/\/jamesonquave.com\/blog\/","name":"Jameson Quave","description":"Using computer technology to educate, and improve lives.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jamesonquave.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/d0ef4e53cd95e21e389aefe6277f0811","name":"Guanshan Liu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jamesonquave.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8f00053e85112a67cee95968f2f36ab2?s=96&d=retro&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8f00053e85112a67cee95968f2f36ab2?s=96&d=retro&r=pg","caption":"Guanshan Liu"},"description":"Guanshan is an iOS Developer currently working as an iOS Engineer at Alibaba Inc. Before working at Alibaba, Guanshan worked with 2K Games on Civilization Revolution 1 and 2 for iOS. He has a Masters in Software Engineering from the University of York, UK as well as a Bachelors of Engineering in Information Security from Nanjing University of Aeronautics and Astronautics. Find him on twitter, @guanshanliu.","sameAs":["https:\/\/guanshanliu.github.io"],"url":"https:\/\/jamesonquave.com\/blog\/author\/guanshanliu\/"}]}},"_links":{"self":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/1798"}],"collection":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/comments?post=1798"}],"version-history":[{"count":1,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/1798\/revisions"}],"predecessor-version":[{"id":1799,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/posts\/1798\/revisions\/1799"}],"wp:attachment":[{"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/media?parent=1798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/categories?post=1798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesonquave.com\/blog\/wp-json\/wp\/v2\/tags?post=1798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}