Created
March 19, 2010 00:06
-
-
Save simoneb/337085 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
public static class Extensions | |
{ | |
public static bool And(this IEnumerable<bool> bools) | |
{ | |
return bools.Foldr(Expression.AndAlso, true); | |
} | |
public static T Foldr<T>(this IEnumerable<T> enumerable, Func<Expression, Expression, BinaryExpression> fold, T v) | |
{ | |
foreach(var b in enumerable) | |
{ | |
var h = Expression.Parameter(typeof(T), "h"); | |
var t = Expression.Parameter(typeof(T), "t"); | |
return Expression.Lambda<Func<T, T, T>>(fold(h, t), h, t) | |
.Compile() | |
.Invoke(b, Foldr(enumerable.Skip(1), fold, v)); | |
} | |
return v; | |
} | |
public static bool All_<T>(this IEnumerable<T> enumerable, Func<T, bool> p) | |
{ | |
return enumerable.Select(p).And(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment