using System; using System.Collections.Generic; using System.Linq; namespace MaybeMonad { /// /// Contains Extensions Methods to work with possible null values. /// public static class MonadExtensions { /// /// Returns the specified propertyvalue or null if the calling object is null. /// /// The type of the input. /// The type of the result. /// The object which may be null. /// A Function to access the property. /// The specified propertyvalue or null if the calling object is null public static TResult With(this TInput o, Func evaluator) where TResult : class where TInput : class { if (o == null) return null; return evaluator(o); } /// /// Returns the given collection or an empty collection if it is null. /// /// The type of the source. /// The source collection. /// The given collection or an empty collection if it is null. public static IList NullToEmpty(this IList source) { return source ?? new List(); } /// /// Returns the given collection or an empty collection if it is null. /// /// The type of the source. /// The source collection. /// The given collection or an empty collection if it is null. public static IEnumerable NullToEmpty(this IEnumerable source) { return source ?? Enumerable.Empty(); } /// /// Returns the specified propertyvalue or null if the calling object is null. /// /// The type of the input. /// The type of the result. /// The object which may be null. /// A Function to access the property. /// The specified propertyvalue or null if the calling object is null public static Nullable WithNullable(this TInput o, Func evaluator) where TResult : struct where TInput : class { if (o == null) return new Nullable(); return evaluator(o); } /// /// Returns the specified propertyvalue or null if the calling object is null. /// /// The type of the input. /// The type of the result. /// The object which may be null. /// A Function to access the property. /// /// The specified propertyvalue or null if the calling object is null /// public static Nullable WithNullable(this TInput o, Func> evaluator) where TResult : struct where TInput : class { if (o == null) return new Nullable(); return evaluator(o); } /// /// Returns the specified propertyvalue or null if the calling object is null. /// /// The type of the input. /// The type of the result. /// The object which may be null. /// A Function to access the property. /// /// The specified propertyvalue or null if the calling object is null /// public static TResult WithValue(this TInput o, Func evaluator) where TResult : struct where TInput : class { if (o == null) return default(TResult); return evaluator(o); } /// /// Returns the specified propertyvalue or the specified failureValue if the calling object is null. /// /// The type of the input. /// The type of the result. /// The object which may be null. /// The evaluator. /// The failure value. /// /// The specified propertyvalue or the specified failureValue if the calling object is null. /// public static TResult Return(this TInput o, Func evaluator, TResult failureValue) where TInput : class { if (o == null) return failureValue; return evaluator(o); } /// /// Returns the empty string if called on null, otherwise the same as ToString. /// /// The type of the input. /// The type of the result. /// The object which may be null. /// The evaluator. /// The format string. /// /// The empty string if called on null, otherwise the same as ToString. /// public static string ToStringSave(this TInput o, Func evaluator, string formatString = null) where TInput : class { if (o == null) return ""; var val = evaluator(o); if (val == null) return ""; if (formatString == null) return val.ToString(); return string.Format("{0:" + formatString + "}", val); } /// /// Returns the specified object if the expression is true or null if it is not. /// /// The type of the input. /// The o. /// The evaluator. /// The specified object if the expression is true or null if it is not. public static TInput If(this TInput o, Func evaluator) where TInput : class { if (o == null) return null; return evaluator(o) ? o : null; } /// /// Executes the specified Action if the object is not null. /// /// The type of the input. /// The o. /// The action. /// The calling object. public static TInput Do(this TInput o, Action action) where TInput : class { if (o == null) return null; action(o); return o; } /// /// Returns the Value or null if the key is not found. /// /// The type of the key. /// The type of the value. /// The dictionary. /// The key. /// public static TValue With (this IDictionary dictionary, TKey key) { TValue value; if (dictionary.TryGetValue(key, out value)) return value; return default(TValue); } } }