Skip to content

Instantly share code, notes, and snippets.

@simoneb
Created March 19, 2010 00:06
Show Gist options
  • Save simoneb/337085 to your computer and use it in GitHub Desktop.
Save simoneb/337085 to your computer and use it in GitHub Desktop.
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