{"id":2775,"date":"2015-01-12T23:07:51","date_gmt":"2015-01-12T23:07:51","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=2775"},"modified":"2015-01-12T23:07:51","modified_gmt":"2015-01-12T23:07:51","slug":"active-patterns-in-f","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/active-patterns-in-f\/","title":{"rendered":"Active Patterns in F#"},"content":{"rendered":"<p>Active patterns have been causing me a few headaches, trying to understand the syntax. Let&#8217;s take a look at how we might declare an active pattern.<\/p>\n<p><em>Note: I will be using the sample from <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd233248.aspx\" title=\"Active Patterns (F#)\" target=\"_blank\">Active Patterns (F#)<\/a><\/em><\/p>\n<p>So, this is the syntax for declaring an active pattern. The Microsoft documentation states the (| .. |) brackets are known as banana clips. So we declare the types within the banana clips and separated by the | operator, such as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nlet (|Even|Odd|) input = if input % 2 = 0 then Even else Odd\r\n<\/pre>\n<p>So you&#8217;ll notice that the syntax does not include a function name, but only declares the type that the code can be matched against. Here&#8217;s an example of using this code within a pattern match<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nlet test input =\r\n   match input with\r\n   | Even -&gt; printfn &quot;%d is even&quot; input\r\n   | Odd -&gt; printfn &quot;%d is odd&quot; input\r\n<\/pre>\n<p>Now what happens is &#8211; when the function <em>test<\/em> is called with an input, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ntest 2     \/\/ this will output 2 is even\r\n<\/pre>\n<p>The line below the match tells F# to find the code which matches against a type <em>Even<\/em> or type <em>Odd<\/em> and run it. So in this case it will output <em>2 is even<\/em> if we had executed the same code with the int 3 <em>Odd<\/em> would be returned from the active pattern and obvious <em>3 is odd<\/em> will be output. <\/p>\n<p>An extremely useful post on how the code can be viewed is on <a href=\"http:\/\/en.wikibooks.org\/wiki\/F_Sharp_Programming\/Active_Patterns\" title=\"F Sharp Programming\/Active Patterns\" target=\"_blank\">F Sharp Programming\/Active Patterns<\/a>.<\/p>\n<p>To save time I&#8217;ll recreate the code here &#8211; so the declaration for the active patterns can be viewed as<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ntype oddOrEven =\r\n    | Even\r\n    | Odd\r\n \r\nlet getChoice input = if input % 2 = 0 then Even else Odd\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Active patterns have been causing me a few headaches, trying to understand the syntax. Let&#8217;s take a look at how we might declare an active pattern. Note: I will be using the sample from Active Patterns (F#) So, this is the syntax for declaring an active pattern. The Microsoft documentation states the (| .. |) [&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-2775","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\/2775","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=2775"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2775\/revisions"}],"predecessor-version":[{"id":2778,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/2775\/revisions\/2778"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=2775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=2775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=2775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}