This is a neat trick that pops up from time to time. Instead of representing an algebraic data type as a bunch of cases that you can test with if-statements at runtime, you represent it as a pattern-matching function that receives a code block to run for each possibility.