DotNext by Roman Sakno

<PackageReference Include="DotNext" Version="2.7.0" />

.NET API 303,616 bytes

 Predicate

public static class Predicate
Provides extension methods for type Predicate<T> and predefined predicates.
using System; using System.Runtime.CompilerServices; namespace DotNext { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class Predicate { [System.Runtime.CompilerServices.Nullable(0)] private static class TruePredicate<[System.Runtime.CompilerServices.Nullable(2)] T> { internal static readonly Predicate<T> Value = AlwaysTrue; private static bool AlwaysTrue(T value) { return true; } } [System.Runtime.CompilerServices.Nullable(0)] private static class FalsePredicate<[System.Runtime.CompilerServices.Nullable(2)] T> { internal static readonly Predicate<T> Value = AlwaysFalse; private static bool AlwaysFalse(T value) { return false; } } [System.Runtime.CompilerServices.Nullable(0)] private static class IsNullPredicate<T> where T : class { internal static readonly Predicate<T> Value = ObjectExtensions.IsNull; } [System.Runtime.CompilerServices.Nullable(0)] private static class IsNotNullPredicate<T> where T : class { internal static readonly Predicate<T> Value = ObjectExtensions.IsNotNull; } [System.Runtime.CompilerServices.NullableContext(0)] private static class HasValuePredicate<T> where T : struct { [System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] internal static readonly Predicate<T?> Value = HasValue; private static bool HasValue(T? nullable) { return nullable.HasValue; } } public static Predicate<T> IsNull<T>() where T : class { return IsNullPredicate<T>.Value; } public static Predicate<T> IsNotNull<T>() where T : class { return IsNotNullPredicate<T>.Value; } [System.Runtime.CompilerServices.NullableContext(0)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] public static Predicate<T?> HasValue<T>() where T : struct { return HasValuePredicate<T>.Value; } public static Predicate<T> True<[System.Runtime.CompilerServices.Nullable(2)] T>() { return TruePredicate<T>.Value; } public static Predicate<T> False<[System.Runtime.CompilerServices.Nullable(2)] T>() { return FalsePredicate<T>.Value; } public static Func<T, bool> AsFunc<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> predicate) { return predicate.ChangeType<Func<T, bool>>(); } public static Converter<T, bool> AsConverter<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> predicate) { return predicate.ChangeType<Converter<T, bool>>(); } private static bool Negate<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> predicate, T obj) { return !predicate(obj); } public static Predicate<T> Negate<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> predicate) { return predicate.Negate<T>; } public static Predicate<T> Or<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> left, Predicate<T> right) { return delegate(T input) { if (!left(input)) return right(input); return true; }; } public static Predicate<T> And<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> left, Predicate<T> right) { return delegate(T input) { if (left(input)) return right(input); return false; }; } public static Predicate<T> Xor<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> left, Predicate<T> right) { return (T input) => left(input) ^ right(input); } [return: System.Runtime.CompilerServices.Nullable(0)] public static Result<bool> TryInvoke<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> predicate, T obj) { try { return predicate(obj); } catch (Exception error) { return new Result<bool>(error); } } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static ValueFunc<T, bool> AsValueFunc<[System.Runtime.CompilerServices.Nullable(2)] T>(this Predicate<T> predicate, bool wrap = false) { return new ValueFunc<T, bool>(Unsafe.As<Func<T, bool>>(predicate), wrap); } } }