Is there a way to encapsulate a template in F #?

Is there a way to encapsulate a template in F #?

For example, instead of writing this ...

let stringToMatch = "example1" match stringToMatch with | "example1" | "example2" | "example3" -> ... | "example4" | "example5" | "example6" -> ... | _ -> ... 

Is there a way to accomplish something in this direction ...

 let match1to3 = | "example1" | "example2" | "example3" let match4to6 = | "example4" | "example5" | "example6" match stringToMatch with | match1to3 -> ... | match4to6 -> ... | _ -> ... 
+5
source share
1 answer

You can do this with active templates:

 let (|Match1to3|_|) text = match text with | "example1" | "example2" | "example3" -> Some text | _ -> None let (|Match4to6|_|) text = match text with | "example4" | "example5" | "example6" -> Some text | _ -> None match stringToMatch with | Match1to3 text -> .... | Match4to6 text -> .... | _ -> ... 
+6
source

Source: https://habr.com/ru/post/1257477/


All Articles