Created
March 22, 2011 08:11
-
-
Save kana/880918 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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