Active patterns
Dynamic patterns for powerful matching
F# has a special type of pattern matching called "active patterns" where the pattern can be parsed or detected dynamically. As with normal patterns, the matching and output are combined into a single step from the caller's point of view.
Here is an example of using active patterns to parse a string into an int or bool.
// create an active pattern
let (|Int|_|) str =
match System.Int32.TryParse(str) with
| (true,int) -> Some(int)
| _ -> None
// create an active pattern
let (|Bool|_|) str =
match System.Boolean.TryParse(str) with
| (true,bool) -> Some(bool)
| _ -> NoneYou don't need to worry about the complex syntax used to define the active pattern right now ? this is just an example so that you can see how they are used.
Once these patterns have been set up, they can be used as part of a normal "match..with" expression.
// create a function to call the patterns
let testParse str =
match str with
| Int i -> printfn "The value is an int '%i'" i
| Bool b -> printfn "The value is a bool '%b'" b
| _ -> printfn "The value '%s' is something else" str
// test
testParse "12"
testParse "true"
testParse "abc"You can see that from the caller's point of view, the matching with an Int or Bool is transparent, even though there is parsing going on behind the scenes.
A similar example is to use active patterns with regular expressions in order to both match on a regex pattern and return the matched value in a single step.
Again, once this pattern has been set up, it can be used transparently as part of a normal match expression.
And for fun, here's one more: the well-known FizzBuzz challenge written using active patterns.
Last updated
Was this helpful?