title: Avoiding "let mutable" cleanly with pattern matching and bitwise combining enum flags in fsharp
body: <p>Consider the following (mutable) example:</p>
  
  <pre><code>let getRegexFlax flags =
      let mutable res = RegexOptions.None
      for ch in flags do
          match ch with
          | 's' -&gt;  res &lt;- res ||| RegexOptions.Singleline
          | 'x' -&gt;  res &lt;- res ||| RegexOptions.IgnorePatternWhitespace
          | 'i' -&gt;  res &lt;- res ||| RegexOptions.IgnoreCase
          | 'm' -&gt;  res &lt;- res ||| RegexOptions.Multiline
          | _   -&gt;  raise (Exception("invalid flag"))
      res
  </code></pre>
  
  <p>I used this example to exemplify a situation we often encounter. The idea is simple: based on a string (or any complex condition), you need to combine zero or more enum flags.</p>
  
  <p>The easiest way to do that, I think, is like above, with mutable. If you do not use mutable, I can think of a myriad of other ways, but none seem to be very clean:</p>
  
  <ul>
  <li>recursive function combining the return flags (cumbersome)</li>
  <li><a href="http://hubfs.net/topic/None/56660" rel="nofollow"><code>enum.Combine</code></a> as Don Syme suggests, with an if-condition or pattern match in each array entry (ugly)</li>
  <li>a long range of <code>|||</code> combining expressions, each having a conditional (also ugly)</li>
  </ul>
  
  <p>I'm sure there is a simpler, more direct way, preferably with pattern matching and, of course, without mutability. It may be the late hour of day, but I simply can't think of it at the moment, anybody can show me the light?</p>