PageRenderTime 1381ms CodeModel.GetById 1370ms RepoModel.GetById 0ms app.codeStats 0ms

/src/LinFu.Finders/PredicateExtensions.cs

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