Skip to content

Instantly share code, notes, and snippets.

@panesofglass
Created January 20, 2010 16:00
Show Gist options
  • Select an option

  • Save panesofglass/281943 to your computer and use it in GitHub Desktop.

Select an option

Save panesofglass/281943 to your computer and use it in GitHub Desktop.
Extensions to Func<T, bool> to allow composing predicate functions.
using System;
namespace Fusion.Foundation.Specifications
{
/// <summary>
/// Adds extensions to allow composing predicates.
/// </summary>
public static class CompositePredicate
{
#region methods
/// <summary>
/// Composes the two predicate functions with an '&&'.
/// </summary>
/// <typeparam name="T">The type of the predicate function.</typeparam>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The composed predicate function.</returns>
public static Func<T, bool> And<T>(this Func<T, bool> left, Func<T, bool> right)
{
return target => left(target) && right(target);
}
/// <summary>
/// Reverses boolean of the specified predicate.
/// </summary>
/// <typeparam name="T">The type of the predicate.</typeparam>
/// <param name="predicate">The predicate.</param>
/// <returns>The reversed predicate.</returns>
public static Func<T, bool> Not<T>(this Func<T, bool> predicate)
{
return target => !predicate(target);
}
/// <summary>
/// Composes the two predicate functions with an '&&'.
/// </summary>
/// <typeparam name="T">The type of the predicate function.</typeparam>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The composed predicate function.</returns>
public static Func<T, bool> Or<T>(this Func<T, bool> left, Func<T, bool> right)
{
return target => left(target) || right(target);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment