Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to not have to repeat this function invocation in pattern matching?

Tags:

f#

I have a string for which I want to use an active pattern to match against. I've noticed I have to repeat the same function invocation when the value is an input to a function. Is there a way to not have to keep calling the function?

let (|A|B|C|X|) stringValue =
        match stringValue with
        | value when compareCaseInsensitive stringValue "a" -> A
        | value when compareCaseInsensitive stringValue "b" -> B
        | value when compareCaseInsensitive stringValue "c" -> C
        | _ -> X stringValue
like image 281
moarboilerplate Avatar asked Oct 22 '15 14:10

moarboilerplate


1 Answers

You could define yet another active pattern for your black-box function:

let (|CCI|_|) (v: string) c =
    if v.Equals(c, System.StringComparison.InvariantCultureIgnoreCase) then Some()
    else None

let (|A|B|C|X|) stringValue =
        match stringValue with
        | CCI "a" -> A
        | CCI "b" -> B
        | CCI "c" -> C
        | _ -> X stringValue
like image 127
CaringDev Avatar answered Oct 14 '22 03:10

CaringDev