{"id":1140,"date":"2012-01-29T10:34:35","date_gmt":"2012-01-29T10:34:35","guid":{"rendered":"http:\/\/www.navision-blog.de\/?p=1140"},"modified":"2012-01-29T17:32:32","modified_gmt":"2012-01-29T17:32:32","slug":"why-do-we-need-partial-application-part-1-of-n-fluent-interfaces-and-piping","status":"publish","type":"post","link":"http:\/\/www.navision-blog.de\/blog\/2012\/01\/29\/why-do-we-need-partial-application-part-1-of-n-fluent-interfaces-and-piping\/","title":{"rendered":"Why do we need partial application? &#8211; Part 1 of n &#8211; Fluent interfaces and piping"},"content":{"rendered":"<p>My last <a href=\"http:\/\/www.navision-blog.de\/2012\/01\/27\/partial-application-if-f-and-c\/\">blog post<\/a> was yet another introduction to <a href=\"http:\/\/en.wikipedia.org\/wiki\/Currying\">Currying<\/a> and <a href=\"http:\/\/en.wikipedia.org\/wiki\/Partial_application\">Partial application<\/a>. Now I want to put the focus more on the <font color=\"#333333\"><strong>why<\/strong><\/font> part. Why do we want to have our functions in curryied form most of the time? This is the first part of a small blog post series and shows partial application in F# pipelines.<\/p>\n<p>Using \u201c<a href=\"http:\/\/en.wikipedia.org\/wiki\/Fluent_interface\">Fluent interfaces<\/a>\u201d is a popular technique to write code in a more readable form. In languages like C# they also provide a way to create the code much faster. On every . we get IntelliSense and this gives us a \u201cfluid\u201d way of writing.<\/p>\n<p>Let\u2019s consider the following task: we want to compute the sum of the square roots of all odd numbers between 1 and 100. In C# we can use the LINQ method chaining approach in order to do something like this:<\/p>\n<p><a href=\"http:\/\/www.navision-blog.de\/images\/ffe2881d8ffb_81AF\/image.png\"><img loading=\"lazy\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.navision-blog.de\/images\/ffe2881d8ffb_81AF\/image_thumb.png\" width=\"494\" height=\"248\" \/><\/a><\/p>\n<p>Now how does this look in F#? It\u2019s basically the same. We replace every . with the |&gt; operator and use the analogous Seq.* functions:<\/p>\n<p><a href=\"http:\/\/www.navision-blog.de\/images\/ffe2881d8ffb_81AF\/image_3.png\"><img loading=\"lazy\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.navision-blog.de\/images\/ffe2881d8ffb_81AF\/image_thumb_3.png\" width=\"494\" height=\"85\" \/><\/a><\/p>\n<p>Oups! What happened here? The F# compiler noticed a type error. <strong>Math.Sqrt<\/strong> needs a float as input but we gave it an int. C# uses implicit casts between int and float so we didn\u2019t noticed the problem there. Implicit casts are a little bit problematic, at least if you want to have proper <a href=\"http:\/\/en.wikipedia.org\/wiki\/Type_inference\">type inference<\/a> so F# doesn\u2019t have this feature. No problem, we are programmers so let\u2019s add the conversion manually:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1697997.js\"> <\/script>  <\/p>\n<p>Notice that <strong>float<\/strong> is a function from int to float and not a cast. <\/p>\n<p>Now you might ask: how does this all relate to partial application? The answer is simple: In every pipeline step we use a higher-order function (Seq.*) and apply the first parameter with a lambda. The second parameter is given via the |&gt; operator from the line above.<\/p>\n<p><a href=\"http:\/\/www.navision-blog.de\/images\/ffe2881d8ffb_81AF\/image_4.png\"><img loading=\"lazy\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.navision-blog.de\/images\/ffe2881d8ffb_81AF\/image_thumb_4.png\" width=\"494\" height=\"99\" \/><\/a><\/p>\n<p>By applying our rule of thumb from the last post were are able to remove the x parameters:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1698054.js\"> <\/script>  <\/p>\n<p>Now let\u2019s step back to C#. Keeping this knowledge in mind we try to apply the same rule in order to get rid of the x parameters:<\/p>\n<p><a href=\"http:\/\/www.navision-blog.de\/images\/ffe2881d8ffb_81AF\/image_5.png\"><img loading=\"lazy\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.navision-blog.de\/images\/ffe2881d8ffb_81AF\/image_thumb_5.png\" width=\"494\" height=\"302\" \/><\/a><\/p>\n<p>Oups again! Now we see same error in C#. In this case it doesn\u2019t know how to apply the implicit cast. As I said they are \u201cproblematic\u201d, but we know how to fix this:<\/p>\n<p> <script src=\"https:\/\/gist.github.com\/1698185.js\"> <\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>My last blog post was yet another introduction to Currying and Partial application. Now I want to put the focus more on the why part. Why do we want to have our functions in curryied form most of the time? This is the first part of a small blog post series and shows partial application [&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":[558,664,559],"_links":{"self":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1140"}],"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=1140"}],"version-history":[{"count":3,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1140\/revisions"}],"predecessor-version":[{"id":1143,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/posts\/1140\/revisions\/1143"}],"wp:attachment":[{"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/media?parent=1140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/categories?post=1140"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.navision-blog.de\/blog\/wp-json\/wp\/v2\/tags?post=1140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}