/src/LinFu.Finders/PredicateExtensions.cs
C# | 52 lines | 19 code | 3 blank | 30 comment | 2 complexity | 400be02c37445fb1d5a694ada17bf56a MD5 | raw file
1using System; 2 3namespace LinFu.Finders 4{ 5 /// <summary> 6 /// A class that adds logical extensions to the <see cref="Func{T,TResult}" /> predicate 7 /// class. 8 /// </summary> 9 public static class PredicateExtensions 10 { 11 /// <summary> 12 /// Logically ORs two <see cref="Func{T,TResult}" /> predicates together. 13 /// </summary> 14 /// <typeparam name="TItem">The type of item being compared.</typeparam> 15 /// <param name="left">The left hand predicate.</param> 16 /// <param name="right">The right hand predicate.</param> 17 /// <returns> 18 /// A predicate that will return <c>true</c> if and only if one of the given predicates is <c>true</c>; otherwise, 19 /// it will return <c>false</c>. 20 /// </returns> 21 public static Func<TItem, bool> Or<TItem>(this Func<TItem, bool> left, Func<TItem, bool> right) 22 { 23 return item => left(item) || right(item); 24 } 25 26 /// <summary> 27 /// Logically ANDs two <see cref="Func{T,TResult}" /> predicates together. 28 /// </summary> 29 /// <typeparam name="TItem">The type of item being compared.</typeparam> 30 /// <param name="left">The left hand predicate.</param> 31 /// <param name="right">The right hand predicate.</param> 32 /// <returns> 33 /// A predicate that will return <c>true</c> if and only if both of the given predicates are <c>true</c>; 34 /// otherwise, it will return <c>false</c>. 35 /// </returns> 36 public static Func<TItem, bool> And<TItem>(this Func<TItem, bool> left, Func<TItem, bool> right) 37 { 38 return item => left(item) && right(item); 39 } 40 41 /// <summary> 42 /// Logically negates a single predicate. 43 /// </summary> 44 /// <typeparam name="TItem">The type of item being compared.</typeparam> 45 /// <param name="predicate">The predicate to negate.</param> 46 /// <returns>Returns <c>true</c> if the given predicate is <c>false</c>.</returns> 47 public static Func<TItem, bool> Inverse<TItem>(this Func<TItem, bool> predicate) 48 { 49 return item => !predicate(item); 50 } 51 } 52}