{"id":1144,"date":"2012-01-30T17:00:29","date_gmt":"2012-01-30T17:00:29","guid":{"rendered":"http:\/\/www.navision-blog.de\/?p=1144"},"modified":"2012-01-30T17:05:59","modified_gmt":"2012-01-30T17:05:59","slug":"currying-and-uncurrying-in-c-and-f","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2012\/01\/30\/currying-and-uncurrying-in-c-and-f\/","title":{"rendered":"Currying and uncurrying in C# and F#"},"content":{"rendered":"<p>In the last couple of days I started to write some posts about <a href=\"http:\/\/en.wikipedia.org\/wiki\/Currying\">Currying<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Partial_application\">Partial application<\/a>:<\/p>\n<ul>\n<li><a href=\"http:\/\/www.navision-blog.de\/2012\/01\/27\/partial-application-if-f-and-c\/\">Partial application in F# and C#<\/a> <\/li>\n<li><a href=\"http:\/\/www.navision-blog.de\/2012\/01\/29\/why-do-we-need-partial-application-part-1-of-n-fluent-interfaces-and-piping\/\">Why do we need partial application? \u2013 Part 1 of n \u2013 Fluent interfaces and piping<\/a> <\/li>\n<\/ul>\n<p>This time I want to show you how we can write a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Higher-order_function\">higher-order function<\/a> which allows us to curry another function. Remember the multiplication function from the <a href=\"http:\/\/www.navision-blog.de\/2012\/01\/27\/partial-application-if-f-and-c\/\">first post<\/a> and it\u2019s curried form:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1705219.js\"> <\/script>  <\/p>\n<h5>Currying<\/h5>\n<p>The question is: how can we automate this transformation process? Remember we want to have the curryied form for partial application:<script src=\"https:\/\/gist.github.com\/1705245.js\"> <\/script><\/p>\n<p>Let\u2019s look at the signature of the desired Curry-function: in our case it has to take <font color=\"#333333\"><strong>Func&lt;int, int, int&gt;<\/strong><\/font> and returns <font color=\"#333333\"><strong>Func&lt;int, Func&lt;int, int&gt;&gt;<\/strong><\/font>. <\/p>\n<p>If we generalize the <strong>ints<\/strong> to generic parameters and fix the signature then the implementation is trivial (Compiler Driven Programming). There is exactly one way to make this work:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1705277.js\"> <\/script>  <\/p>\n<p>The F# implementation does exactly the same, but without all the annoying the type hints:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1705298.js\"> <\/script>  <\/p>\n<h5>Uncurrying<\/h5>\n<p>Of course you can undo the currying by applying a generic Uncurry-function:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1705371.js\"> <\/script><script src=\"https:\/\/gist.github.com\/1705372.js\"> <\/script>  <\/p>\n<p>And in F# this looks like this:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1705380.js\"> <\/script>  <\/p>\n<h5>Libraries<\/h5>\n<p>Currying and Uncurrying are two very important concepts in functional programming so they are included in a couple of libraries:<\/p>\n<ul>\n<li>You can find it at the top of the <a href=\"https:\/\/github.com\/fsharp\/fsharpx\/blob\/master\/src\/FSharpx.Core\/Prelude.fs\">Prelude<\/a> in FSharpx (<a href=\"http:\/\/bugsquash.blogspot.com\/2011\/10\/introducing-fsharpx.html\">read more<\/a>). <\/li>\n<li>You can find it in the <a href=\"http:\/\/www.haskell.org\/ghc\/docs\/latest\/html\/libraries\/base\/Prelude.html\">Haskell Prelude<\/a>. <\/li>\n<li>You can find similar functions in <a href=\"https:\/\/github.com\/scalaz\/scalaz\">Scalaz<\/a>. <\/li>\n<li>Adrian Lang wrote a library called <a href=\"https:\/\/github.com\/adrianlang\/partial-js\">partial-js<\/a> which allows to do something similar in JavaScript. <\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In the last couple of days I started to write some posts about Currying and Partial application: Partial application in F# and C# Why do we need partial application? \u2013 Part 1 of n \u2013 Fluent interfaces and piping This time I want to show you how we can write a higher-order function which allows [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,448],"tags":[565,558,664,559],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1144"}],"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=1144"}],"version-history":[{"count":4,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1144\/revisions"}],"predecessor-version":[{"id":1148,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1144\/revisions\/1148"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=1144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=1144"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=1144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}