caloggins
10/3/2014 - 2:42 PM

Maybe.cs

    public static class MaybeExtensions
    {
        public static TInput With<TInput>(TInput input)
            where TInput : class
        {
            return input;
        }

        public static TResult With<TInput, TResult>(this TInput i, Func<TInput, TResult> expression)
            where TResult : class
            where TInput : class
        {
            return i == null ? null : expression(i);
        }

        public static TResult Return<TInput, TResult>(this TInput input, Func<TInput, TResult> expression, TResult ifNull)
            where TResult : class
            where TInput : class
        {
            return expression(input) ?? ifNull;
        }

        public static TInput Do<TInput>(this TInput input, Action<TInput> action)
            where TInput : class
        {
            if (input != null)
                action(input);
            return input;
        }
        public static TInput If<TInput>(this TInput o, Func<TInput, bool> evaluator)
               where TInput : class
        {
            if (o == null) return null;
            return evaluator(o) ? o : null;
        }

        public static TInput Unless<TInput>(this TInput o, Func<TInput, bool> evaluator)
               where TInput : class
        {
            if (o == null) return null;
            return evaluator(o) ? null : o;
        }
    }