Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To answer your question about active patterns, let me use a simpler example:</p> <pre><code>let (|Odd|Even|) n = if n % 2 = 0 then Even else Odd </code></pre> <p>When you declare a pattern that has multiple options using <code>(|Odd|Even|)</code>, then the compiler understands it as a function that returns a value of type <code>Choice&lt;unit, unit&gt;</code>. So, the active pattern that you can work with is the whole combination <code>|Odd|Even|</code> and not just two constructs that you could use independently (such as <code>|Odd|</code> and <code>|Even|</code>).</p> <p>It is possible to treat active patterns as first class functions, but if you're using patterns with multiple options, you cannot do much with it:</p> <blockquote> <p>let pattern = (|Odd|Even|);; val pattern : int -> Choice</p> </blockquote> <p>You can write function that tests whether a value matches a specified pattern, but you'd need a lot of functions (because there are many <code>Choice</code> types overloaded by the number of type parameters):</p> <pre><code>let is1Of2 pattern item = match pattern item with | Choice1Of2 _ -&gt; true | _ -&gt; false &gt; is1Of2 (|Odd|Even|) 1 val it : true </code></pre> <p>Something like this would work in your case, but it is far from being perfect.</p> <p>You can do a little better job if you declare multiple partial active patterns (but then you of course loose some nice aspects of full active patterns such as completeness checking):</p> <pre><code>let (|Odd|_|) n = if n % 2 = 0 then None else Some() let (|Even|_|) n = if n % 2 = 0 then Some() else None </code></pre> <p>Now you can write a function that checks whether a value matches pattern:</p> <pre><code>let matches pattern value = match pattern value with | Some _ -&gt; true | None -&gt; false &gt; matches (|Odd|_|) 1;; val it : bool = true &gt; matches (|Even|_|) 2;; val it : bool = true </code></pre> <p><strong>Summary</strong> While there may be some more or less elegant way to achieve what you need, I'd probably consider whether active patterns give you any big advantage over using standard functions. It may be a better idea to implenent the code using functions first and then decide which of the constructs would be useful as active patterns and add active patterns later. In this case, the usual code wouldn't look much worse:</p> <pre><code>type LegResult = LegIsSettled | LegIsConfirmed | LegIsUnsettled let getLegStatus (l: tradeLeg) = if Helper.exists l.actuals then LegIsSettled elif Helper.exists l.confirmedPrice then LegIsConfirmed else LegIsUnsettled // Later in the code you would use pattern matching match getLegStatus trade with | LegIsSettled -&gt; // ... | LegIsUnSettled -&gt; // ... // But you can still use higher-order functions too trades |&gt; List.exist (fun t -&gt; getLegStatus t = LegIsSettled) // Which can be rewritten (if you like point-free style): trades |&gt; List.exist (getLegStatus &gt;&gt; ((=) LegIsSettled)) // Or you can write helper function (which is more readable): let legStatusIs check trade = getLegStatus trade = check trades |&gt; List.exist (legStatusIs LegIsSettled) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload