{"id":1128,"date":"2012-01-27T18:55:18","date_gmt":"2012-01-27T18:55:18","guid":{"rendered":"http:\/\/www.navision-blog.de\/?p=1128"},"modified":"2012-01-29T08:32:43","modified_gmt":"2012-01-29T08:32:43","slug":"partial-application-if-f-and-c","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2012\/01\/27\/partial-application-if-f-and-c\/","title":{"rendered":"Partial application in F# and C#"},"content":{"rendered":"<p>Today I had a conversation on twitter about partial application and type inference in F#. Partial application is a very important and useful concept and there are a lot of resources out there. Here is a short list of related material:<\/p>\n<ul>\n<li><a href=\"http:\/\/screencasts.chariotsolutions.com\/webpage\/uncovering-the-unknown-principles-of-type-inference-\">Uncovering the Unknown: Principles of Type Inference (video)<\/a> <\/li>\n<li>Wikipedia: <a href=\"http:\/\/en.wikipedia.org\/wiki\/Currying\">Currying<\/a>, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Partial_application\">Partial application<\/a>, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Type_inference\">Type inference<\/a> <\/li>\n<li>StackOverflow: <a href=\"http:\/\/stackoverflow.com\/questions\/218025\/what-is-the-difference-between-currying-and-partial-application\">What is the difference between currying and partial application<\/a> <\/li>\n<li>HaskellWiki: <a href=\"http:\/\/www.haskell.org\/haskellwiki\/Partial_application\">Partial application<\/a> <\/li>\n<li><a href=\"http:\/\/www.navision-blog.de\/2009\/06\/17\/f-bootcamp-questions-and-answers-part-ii-currying\/\">F# BootCamp Q&amp;A on this blog<\/a> <\/li>\n<\/ul>\n<p>I promised to show a small sample and I hope this clarifies some of my points on twitter. Let&#8217;s consider a multiplication function in C#:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1690023.js\"> <\/script>  <\/p>\n<p>This function allows us to compute the product of two ints. But what can I do when I need a function which doubles its input? The solutions is easy: I just create a new function:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1690127.js\"> <\/script>  <\/p>\n<p>And when I need a function which triples its input? Same thing, just create another method. But can we do better?<\/p>\n<p>Let\u2019s transform the multiplication function into the curryied version. The transformation process is very easy once you see the pattern. It\u2019s actually possible to write a function which curryies another function, but that\u2019s something for another post. Anyway, here\u2019s the curryied version:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1690011.js\"> <\/script>  <\/p>\n<p>It&#8217;s a little bit noisy with all the funky Funcs, but Ok. We can still write the double function in terms of multiply:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1690161.js\"> <\/script>  <\/p>\n<p>And of course we can use multiply directly (which might look a bit weird at first):<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1690171.js\"> <\/script>  <\/p>\n<p>But we can also use it in another way, which we couldn&#8217;t really do before:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1690184.js\"> <\/script>  <\/p>\n<p>How cool is this? We just applied a single parameter and we got a new function without writing any method declarations. Unfortunately we wrote a lot of weird type declarations in order to get here, but hey.<\/p>\n<p>Let\u2019s move to a language which has type inference. Here\u2019s the uncurryied version of multiply in F#:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1690214.js\"> <\/script>  <\/p>\n<p>The type signature tells us, that we have to give it a tuple of ints and then we get an int back. That\u2019s exactly the same as in C#. Notice that we think about x1,x2 as only one parameter \u2013 a tuple. How can we get to the curryied form? It couldn\u2019t be easier: just remove the parentheses and you\u2019re done:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1690235.js\"> <\/script>  <\/p>\n<p>As you can see the type signature changes a little bit. The * is now just another arrow. This means we have basically the same as Func&lt;int, Func&lt;int, int&gt;&gt; but in a much nicer syntax. There is also another way to write this:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1697880.js\"> <\/script>  <\/p>\n<p>This looks like the C# version, but notice how nicely the arrows align to the type signature. If we want to use the multiply function than we can write something like this:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1690255.js\"> <\/script>  <\/p>\n<p>It\u2019s also very easy to create partially applied functions:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1690268.js\"> <\/script>  <\/p>\n<p>And finally the point which made me write this post. There is a simple rule: whenever we have the same parameter as the last parameter of the left and the right side of a function definition, we can remove it. This leads us to:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1690298.js\"> <\/script>  <\/p>\n<p>Isn&#8217;t this beautiful?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I had a conversation on twitter about partial application and type inference in F#. Partial application is a very important and useful concept and there are a lot of resources out there. Here is a short list of related material: Uncovering the Unknown: Principles of Type Inference (video) Wikipedia: Currying, Partial application, Type inference [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[448],"tags":[558,664,559],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1128"}],"collection":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/comments?post=1128"}],"version-history":[{"count":10,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1128\/revisions"}],"predecessor-version":[{"id":1139,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1128\/revisions\/1139"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=1128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=1128"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=1128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}