tsunami

log in
history

Partial application

Luke Breuer
2008-01-07 21:21 UTC

Partial application is often very poorly explained. Here's a simple case that should demonstrate the concept quite clearly, assuming you can parse the C# 3.0.
static void Main(string[] args)
{
    if (Array.IndexOf(args, "foo") >= 0)
        Console.WriteLine("foo was specified");
    if (Array.IndexOf(args, "bar") >= 0)
        Console.WriteLine("bar was specified");
}
Partial application can remove some of the repeated elements in Array.IndexOf(args, "foo") >= 0
static void Main(string[] args)
{
    Func<string, bool> argExists = arg => Array.IndexOf(args, arg) >= 0;

    if (argExists("foo"))
        Console.WriteLine("foo was specified");
    if (argExists("bar"))
        Console.WriteLine("bar was specified");
}