Skip to content

Instantly share code, notes, and snippets.

@kana
Created March 22, 2011 08:11
Show Gist options
  • Save kana/880918 to your computer and use it in GitHub Desktop.
Save kana/880918 to your computer and use it in GitHub Desktop.
static class ComposingTest
{
static private bool Not(bool x)
{
return !x;
}
static private bool Even(int x)
{
return x % 2 == 0;
}
static void Test()
{
var xs = new [] {1, 2, 3, 4};
Func<bool, bool> not = x => !x;
Func<int, bool> even = x => x % 2 == 0;
xs.Where(even); // OK
xs.Where(not.Compose(even)); // OK
xs.Where(Even); // OK
xs.Where(Not.Compose(Even)); // ERROR - Why?
}
}
static public class FunctionExtensions
{
static public Func<Ta, Tc> Compose<Ta, Tb, Tc>(this Func<Tb, Tc> f, Func<Ta, Tb> g)
{
return x => f(g(x));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment