Tutorial by Examples

Active patterns are a special type of pattern matching where you can specify named categories that your data may fall into, and then use those categories in match statements. To define an active pattern that classifies numbers as positive, negative or zero: let (|Positive|Negative|Zero|) num = ...
Active patterns are just simple functions. Like functions you can define additional parameters: let (|HasExtension|_|) expected (uri : string) = let result = uri.EndsWith (expected, StringComparison.CurrentCultureIgnoreCase) match result with | true -> Some true | _ -> N...
An interesting but rather unknown usage of Active Patterns in F# is that they can be used to validate and transform function arguments. Consider the classic way to do argument validation: // val f : string option -> string option -> string let f v u = let v = defaultArg v "Hello&quo...
Active Patterns can be used to make calling some .NET API's feel more natural, particularly those that use an output parameter to return more than just the function return value. For example, you'd normally call the System.Int32.TryParse method as follows: let couldParse, parsedInt = System.Int32....
There are two types of Active Patterns that somewhat differ in usage - Complete and Partial. Complete Active Patterns can be used when you are able to enumerate all the outcomes, like "is a number odd or even?" let (|Odd|Even|) number = if number % 2 = 0 then Even else Odd N...

Page 1 of 1