コード例 #1
0
        public static Maybe <T> TryLast <T>(this IEnumerable <T> source, Func <T, bool> predicate)
        {
            var last = source.LastOrDefault(predicate);

            if (last != null)
            {
                return(Maybe <T> .From(last));
            }
            return(Maybe <T> .None);
        }
コード例 #2
0
        public static Maybe <T> TryFirst <T>(this IEnumerable <T> source, Func <T, bool> predicate)
        {
            var firstOrEmpty = source.Where(predicate).Take(1).ToList();

            if (firstOrEmpty.Any())
            {
                return(Maybe <T> .From(firstOrEmpty[0]));
            }
            return(Maybe <T> .None);
        }
コード例 #3
0
        /// <summary>
        /// Creates a new <see cref="Maybe{T}" /> if <paramref name="maybeTask" /> is empty, using the result of the supplied <paramref name="fallbackOperation" />, otherwise it returns <paramref name="maybeTask" />
        /// </summary>
        /// <param name="maybeTask"></param>
        /// <param name="fallbackOperation"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static async Task <Maybe <T> > Or <T>(this Task <Maybe <T> > maybeTask, Func <T> fallbackOperation)
        {
            Maybe <T> maybe = await maybeTask.DefaultAwait();

            if (maybe.HasNoValue)
            {
                return(Maybe <T> .From(fallbackOperation()));
            }

            return(maybe);
        }
コード例 #4
0
        /// <summary>
        /// Creates a new <see cref="Maybe{T}" /> if <paramref name="maybeTask" /> is empty, using the supplied <paramref name="fallback" />, otherwise it returns <paramref name="maybeTask" />
        /// </summary>
        /// <param name="maybeTask"></param>
        /// <param name="fallback"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static async Task <Maybe <T> > Or <T>(this Task <Maybe <T> > maybeTask, Task <T> fallback)
        {
            Maybe <T> maybe = await maybeTask.DefaultAwait();

            if (maybe.HasNoValue)
            {
                var value = await fallback.DefaultAwait();

                return(Maybe <T> .From(value));
            }

            return(maybe);
        }
コード例 #5
0
 /// <summary>
 /// When bool value is true returns Result.Success otherwise Result.Fail
 /// </summary>
 /// <param name="val"></param>
 /// <param name="error"></param>
 /// <returns></returns>
 public static Result ToResult(this bool val, string error)
 {
     return(Maybe <bool> .From(val).ToValidResult(error)
            .OnSuccess(x => x ? Result.Success() : Result.Fail(error)));
 }