using System; namespace ZeroLevel { /// /// Сборник монад /// public static class Monades { #region With public static TResult With(this TInput o, Func evaluator) { if (null != o) return evaluator(o); return default(TResult); } #endregion #region Return public static TResult Return(this TInput o, Func evaluator, TResult failureValue) { if (null != o) return evaluator(o); return failureValue; } public static TResult Return(this TInput o, Func evaluator) { if (null != o) return evaluator(o); return default(TResult); } #endregion #region IsNotNull public static bool IsNotNull(this TInput o) { if (null != o) return true; return false; } #endregion #region If public static TInput If(this TInput o, Predicate evaluator) { if (null != o) return evaluator(o) ? o : default(TInput); return default(TInput); } public static TOutput Either(this TInput o, Func condition, Func ifTrue, Func ifFalse) => condition(o) ? ifTrue(o) : ifFalse(o); public static TOutput Either(this TInput o, Func ifTrue, Func ifFalse) => o.Either(x => x != null, ifTrue, ifFalse); #endregion #region Do public static TInput Do(this TInput o, Action action) { if (null != o) action(o); return o; } public static TInput Do(this TInput o, Action action, Action nullHandler) { if (null != o) { action(o); } else { nullHandler(); } return o; } #endregion } }