# F Sharp Programming/Active Patterns

Active Patterns allow programmers to wrap arbitrary values in a union-like data structure for easy pattern matching. For example, its possible wrap objects with an active pattern, so that you can use objects in pattern matching as easily as any other union type.

## Defining Active Patterns

Active patterns look like functions with a funny name:
This function defines an ad hoc union data structure, where each union case namen is separated from the next by a | and the entire list is enclosed between (| and |) (humbly called "banana brackets"). In other words, the function does not have a simple name at all, it instead defines a series of union constructors.
A typical active pattern might look like this:
Even and Odd are union constructors, so our active pattern either returns an instance of Even or an instance of Odd. The code above is roughly equivalent to the following:
Active patterns can also define union constructors which take a set of parameters. For example, consider we can wrap a seq<'a> with an active pattern as follows:
This code is, of course, equivalent to the following:
You've probably noticed the immediate difference between active patterns and explicitly defined unions:
* Active patterns define an anonymous union, where the explicit union has a name (numKind, seqWrapper, etc.).
* Active patterns determine their constructor parameters using a kind of type-inference, whereas we need to explicitly define the constructor parameters for each case of our explicit union.

### Using Active Patterns

The syntax for using active patterns looks a little odd, but once you know what's going on, it's very easy to understand. Active patterns are used in pattern matching expressions, for example:
What's going on here? When the pattern matching function encounters Even, it calls (|Even|Odd|) with parameter in the match clause, it's as if you've written:
The parameter in the match clause is always passed as the last argument to the active pattern expression. Using our seq example from earlier, we can write the following:
Traditionally, seq's are resistant to pattern matching, but now we can operate on them just as easily as lists.

### Parameterizing Active Patterns

Its possible to pass arguments to active patterns, for example:
The single-case active pattern (|Contains|) wraps the String.Contains function. When we call Contains "kitty" true, F# passes "kitty" and the argument we're matching against to the (|Contains|) active pattern and tests the return value against the value true. The code above is equivalent to:
As you can see, the code using the active patterns is much cleaner and easier to read than the equivalent code using the explicitly defined union.
   Note: Single-case active patterns might not look terribly useful at first, but they can really help to clean up messy code. For example, the active pattern above wraps up the String.Contains method and allows us to invoke it in a pattern matching expression. Without the active pattern, pattern matching quickly becomes messy:

### Partial Active Patterns

A partial active pattern is a special class of single-case active patterns: it either returns Some or None. For example, a very handy active pattern for working with regex can be defined as follows:
This is equivalent to writing:
Using partial active patterns, we can test an input against any number of active patterns:
Partial active patterns don't constrain us to a finite set of cases like traditional unions do, we can use as many partial active patterns in match statement as we need.

## Additional Resources

* Introduction to Active Patterns (Internet Archive Wayback Machine) by Chris Smith
* Extensible Pattern Matching via Lightweight Language Extension by Don Syme