Navigate / search

Learning to think Functionally -> Why I still don’t understand Single Case Active Patterns

This entry is part 9 of 11 in the series Learning to think Functionally

I won’t lie to you, my burgeoning relationship with F# hit a bit of a rough patch recently.

While I’ve understood pattern matching from the outset, I’ve only had a vague idea about Active Patterns. Nothing I’ve read has really made them click for me. So, I decided to focus and try and get a better understanding.

What I managed to grasp almost immediately is that Active Patterns are functions and there are a few different types

  • Single Case
  • Multi-Case
  • Partial

This is where the trouble starts. Most explanations seem to begin with the Single Case Active Pattern on the basis that it is the “simplest”.

Here’s a typical example

let (|ToColor|) x =
    match x with
    | "red"   -> System.Drawing.Color.Red
    | "blue"  -> System.Drawing.Color.Blue
    | "white" -> System.Drawing.Color.White
    | _       -> failwith "Unknown Color"

This converts the value x to a color.

And here’s how we would use it

let (ToColor col) = "red"

These kinds of examples are everywhere and my overwhelming feeling on seeing them is WHY? Why not simply use a function?

let ToColor x =
    match x with
    | "red"   -> System.Drawing.Color.Red
    | "blue"  -> System.Drawing.Color.Blue
    | "white" -> System.Drawing.Color.White
    | _       -> failwith "Unknown Color"

The only difference seems to me to be the way the function is called.

Instead of

let (ToColor col) = "red"

we have

let col = ToColor "red"

Call me old fashioned but the regular function call looks better and more understandable to me. What on
earth is going on in the Active Pattern? A Function, with and out parameter, that accepts another parameter
using assignment?

It just seems daft compared to the more straightforward function call that we know and love. There has to be some scenario that makes the Active Pattern useful, but I’m pretty sure simple conversions like this aren’t it.

I did send out a cry for help on Twitter and I got a few replies showing cases where the Single Active Pattern is necessary, however the examples were quite complicated (to my novice eyes), leading me to think that far from being the “simplest” Active Pattern, the Single Case actually fulfils quite a niche purpose which is not straightforward at all. The Simplistic examples published on various blogs do nothing to illustrate where this feature is actually useful.

So, let’s park the Single Case, I’ll return to is when I’m able to explain it properly. For now, I still don’t understand it. In the Next post I’ll explain the Multiple Case Active Pattern, which I do understand.

Series Navigation<< Learning to think Functionally: MemoizationLearning To Think Functionally : Readability >>


Pattern Matching | Devjoy

[…] two posts here, and here complaining about the Single Case Active Pattern, it’s time to write the posts […]

Leave a comment


email* (not published)