{"id":2209,"date":"2023-12-08T21:28:10","date_gmt":"2023-12-08T21:28:10","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2209"},"modified":"2023-12-08T21:28:10","modified_gmt":"2023-12-08T21:28:10","slug":"currying-and-partial-applications-in-f","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/currying-and-partial-applications-in-f\/","title":{"rendered":"Currying and Partial applications in F#"},"content":{"rendered":"<p><em>Note: I&#8217;m going through draft posts that go back to 2014 and publishing where they still may have value. They may not be 100% upto date but better published late than never.<\/em><\/p>\n<p><strong>Currying<\/strong><\/p>\n<p>Currying leads to the ability to create partial applications. <\/p>\n<p>Currying is the process of taking a function with more than one arguments and turning it into single argument functions, for example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet add a b = a + b\r\n\r\n\/\/ becomes\r\n\r\nlet add a = \r\n    let add' b = \r\n        a + b\r\n    add'\r\n<\/pre>\n<p>This results in a function syntax which looks like this<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nval add : a:int -&gt; (int -&gt; int)\r\n<\/pre>\n<p>The bracketed <em>int -> int<\/em> shows the function which takes an <em>int<\/em> and returns and <em>int<\/em>.<\/p>\n<p><strong>Partial Applications<\/strong><\/p>\n<p>A partial application is a way of creating functions which have some of their arguments supplied and thus creating new functions. For example in it&#8217;s simplest form we might have<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet add a b = a + b\r\n\r\nlet partialAdd a = add a 42\r\n<\/pre>\n<p>So, nothing too exciting there but what we can also do is, if we supply the first <em>n<\/em> arguments as per the following example<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\nlet add a b = a + b\r\n\r\nlet partialAdd = add 42\r\n<\/pre>\n<p>notice how we&#8217;ve removed the arguments to <em>partialAdd<\/em> but we can call the function thus<\/p>\n<pre class=\"brush: fsharp; title: ; notranslate\" title=\"\">\r\npartialAdd 10 \r\n|&gt; printfn &quot;%d&quot;\r\n<\/pre>\n<p>we&#8217;re still supplying an argument because the function being called (the add function) requires an extra argument. The <em>partialAdd<\/em> declaration creates a new function which is partially implemented.<\/p>\n<p>By declaring functions so that the last element(s) are to be supplied by a calling function we can better combine and\/reuse functions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note: I&#8217;m going through draft posts that go back to 2014 and publishing where they still may have value. They may not be 100% upto date but better published late than never. Currying Currying leads to the ability to create partial applications. Currying is the process of taking a function with more than one arguments [&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":[6],"tags":[],"class_list":["post-2209","post","type-post","status-publish","format-standard","hentry","category-f"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2209","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=2209"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2209\/revisions"}],"predecessor-version":[{"id":9534,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2209\/revisions\/9534"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}