{"id":5909,"date":"2018-02-20T19:54:12","date_gmt":"2018-02-20T19:54:12","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5909"},"modified":"2018-02-20T19:54:12","modified_gmt":"2018-02-20T19:54:12","slug":"f-currying-in-a-little-more-depth","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/f-currying-in-a-little-more-depth\/","title":{"rendered":"F# currying in a little more depth"},"content":{"rendered":"<p>Unlike Scala, for example, F# does not use an alternate syntax to differentiate a &#8220;curry-able&#8221; function from a &#8220;non-curry-able&#8221; function. I was therefore interested in what the F# compiler does to when supplied with a function like the following<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet add a b c = a + b + c\r\n<\/pre>\n<p>Does it really generate something like the following code? <\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet add a = \r\n    let add1 b = \r\n        let add2 c = \r\n            a + b + c\r\n        add2\r\n    add1\r\n<\/pre>\n<p>Obviously this wouldn&#8217;t be great as the code requires closures and sub-functions to be created and so would be less performant in situations where we do not require the currying capabilities.<\/p>\n<p>Thankfully the F# compiler takes care of everything for us so that if we have code in our application which solely uses the <em>add<\/em> function, like this<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet r = add 1 2 3\r\n<\/pre>\n<p>then the compiler simply generates the following, this is taken from ILSpy from the compiled code and obviously reproduced in C# which I think demonstrates pretty clearly what happens.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static int add(int a, int b, int c)\r\n{\r\n   return a + b + c;\r\n}\r\n\r\n\/\/ therefore our call to the \r\n\/\/ function becomes\r\nint r = Program.add(1, 2, 3);\r\n<\/pre>\n<p>Now, what happens if we start currying our functions, with code like this<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet r = add 1 2 \r\nlet r1 = r 3\r\n<\/pre>\n<p>Here the compiler still creates the <em>add<\/em> method as before (as one would expect) but it also creates the following internal class (I&#8217;ve removed attributes to make it all a little cleaner)<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ninternal sealed class r@9\r\n{\r\n   public int a;\r\n   public int b;\r\n\r\n   internal r@9(int a, int b)\r\n      : this()\r\n   {\r\n      this.a = a;\r\n      this.b = b;\r\n   }\r\n\r\n   public override int Invoke(int c)\r\n   {\r\n      return Program.add(this.a, this.b, c);\r\n   }\r\n}\r\n\r\n\/\/ now our calling \r\n\/\/ code looks like this\r\nint a = 1;\r\nint b = 2;\r\nFSharpFunc r2 = new r@9(a, b);\r\nint r = (int)r2.Invoke((!0)3);\r\n<\/pre>\n<p>So in conclusion, if we&#8217;re not using currying on our functions then we get a &#8220;standard&#8221; function and once we start currying only then do we get the equivalent of sub-functions being created.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unlike Scala, for example, F# does not use an alternate syntax to differentiate a &#8220;curry-able&#8221; function from a &#8220;non-curry-able&#8221; function. I was therefore interested in what the F# compiler does to when supplied with a function like the following let add a b c = a + b + c Does it really generate something [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[199,6],"tags":[],"class_list":["post-5909","post","type-post","status-publish","format-standard","hentry","category-concepts","category-f"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5909"}],"version-history":[{"count":9,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5909\/revisions"}],"predecessor-version":[{"id":5922,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5909\/revisions\/5922"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}