Not just a function : Generating functions

Every now and then, you hit some problem that a programming language helps solve very cleanly. Here’s the problem.

You are writing a series of functions that return a bool depending on some condition (predicates). Something like

bool Foo(State t);

Let’s also assume that State is defined as

class State  { Color c; ... }

You have a set of colors, known at compile time, for which Foo should return true. How would you go about doing it?

You could pack the set of colors into an array and write Foo so that at runtime, it searches the color array and returns true if the color passed is in the array. Something like

bool Foo(State t) { return colors.IndexOf(t.Color) != -1; }

Assume that, for design reasons, it isn’t really possible to allow access to colors from within Foo. Then what?

Anonymous methods and closures, of course. How about writing a function, that returns a Foo that returns true only if the color matches. Whew, that sounds confusing, but the code looks pretty simple.

Foo GenerateFoo(Color color) { return delegate(Color passedColor) { return color == passedColor; } }

You can then call GenerateFoo(Color.Red) to get a Foo that will return true only if it is called with Color.Red, call GenerateFoo(Color.Orange) to get one that returns true only if called with Color.Orange and so on.

Foo redFoo = GenerateFoo(Color.Red);
Foo orangeFoo = GenerateFoo(Color.Orange);
Console.WriteLine(redFoo(Color.Red)); // prints true
Console.WriteLine(redFoo(Color.Yellow)); // prints false

Nifty, eh?