Created
January 20, 2010 16:00
-
-
Save panesofglass/281943 to your computer and use it in GitHub Desktop.
Extensions to Func<T, bool> to allow composing predicate functions.
This file contains hidden or 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
| 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