f# - Avoiding "let mutable" cleanly with pattern matching and bitwise combining enum flags in fsharp -



f# - Avoiding "let mutable" cleanly with pattern matching and bitwise combining enum flags in fsharp -

consider next (mutable) example:

let getregexflax flags = allow mutable res = regexoptions.none ch in flags match ch | 's' -> res <- res ||| regexoptions.singleline | 'x' -> res <- res ||| regexoptions.ignorepatternwhitespace | 'i' -> res <- res ||| regexoptions.ignorecase | 'm' -> res <- res ||| regexoptions.multiline | _ -> raise (exception("invalid flag")) res

i used illustration exemplify situation encounter. thought simple: based on string (or complex condition), need combine 0 or more enum flags.

the easiest way that, think, above, mutable. if not utilize mutable, can think of myriad of other ways, none seem clean:

recursive function combining homecoming flags (cumbersome) enum.combine don syme suggests, if-condition or pattern match in each array entry (ugly) a long range of ||| combining expressions, each having conditional (also ugly)

i'm sure there simpler, more direct way, preferably pattern matching and, of course, without mutability. may late hr of day, can't think of @ moment, can show me light?

i like:

let chartoflag ch= match ch | 's' -> regexoptions.singleline | 'x' -> regexoptions.ignorepatternwhitespace | 'i' -> regexoptions.ignorecase | 'm' -> regexoptions.multiline | _ -> raise (exception("invalid flag")) flags |> seq.map chartoflag |> seq.fold (|||) regexoptions.none

enums f# pattern-matching mutable bitwise-or

Comments

Popular posts from this blog

maven fortify plugin : Unable to load build session with ID XXXXX .. See log file for more details -

c# - Primavera WebServices does not return any data -

android - Display emoji panel with genymotion - keyboard/touch input? -