Active patterns
I promised in Chapter 4, Basic Pattern Matching, that I would add to the subject by covering active patterns; now is a perfect time. Remember matching with guards? Guards provide a way to drill down into the matched pattern-expression
function by attaching an arbitrary calculation having the bool
result.
Guard mechanism adds a certain customization potential to the vanilla pattern matching, but it is kind of detached: regardless of how much data decomposition is required in order to complete the guard calculation, all this effort is discarded for both matching and non-matching possible calculation results. Wouldn't it be nice to have a fully customizable transition between the recognition and transformation phases of pattern matching? Active patterns aim exactly at this matter. Broadly speaking, active patterns represent a special kind of function allowed to be used inside pattern-expression
.
They allow you to implement some typical patterns of data transformations in a very...