Exemplo n.º 1
0
        public Maybe <TResult> SelectMany <TMiddle, TResult>(
            Func <T, Maybe <TMiddle> > selector,
            Func <T, TMiddle, TResult> resultSelector)
        {
            if (selector is null)
            {
                throw new Anexn(nameof(selector));
            }
            if (resultSelector is null)
            {
                throw new Anexn(nameof(resultSelector));
            }

            if (!_isSome)
            {
                return(Maybe <TResult> .None);
            }

            Maybe <TMiddle> middle = selector(_value);

            if (!middle._isSome)
            {
                return(Maybe <TResult> .None);
            }

            return(Maybe.Of(resultSelector(_value, middle._value)));
        }
Exemplo n.º 2
0
        public Maybe <TResult> Select <TResult>(Func <T, TResult> selector)
        {
            if (selector is null)
            {
                throw new Anexn(nameof(selector));
            }

            return(_isSome ? Maybe.Of(selector(_value)) : Maybe <TResult> .None);
        }
Exemplo n.º 3
0
        public static async Task <Maybe <TResult> > SelectAsync <T, TResult>(
            this Maybe <T> @this,
            Func <T, Task <TResult> > selector,
            bool continueOnCapturedContext)
        {
            if (selector is null)
            {
                throw new Anexn(nameof(selector));
            }

            return(@this.TryGetValue(out T? value)
                ? Maybe.Of(await selector(value).ConfigureAwait(continueOnCapturedContext))
                : Maybe <TResult> .None);
        }
Exemplo n.º 4
0
        private static Maybe <TResult> GroupJoinImpl <TOuter, TInner, TKey, TResult>(
            Maybe <TOuter> outer,
            Maybe <TInner> inner,
            Func <TOuter, TKey> outerKeySelector,
            Func <TInner, TKey> innerKeySelector,
            Func <TOuter, Maybe <TInner>, TResult> resultSelector,
            IEqualityComparer <TKey> comparer)
        {
            if (outer.TryGetValue(out TOuter? x) && inner.TryGetValue(out TInner? y))
            {
                TKey outerKey = outerKeySelector(x);
                TKey innerKey = innerKeySelector(y);

                if (comparer.Equals(outerKey, innerKey))
                {
                    return(Maybe.Of(resultSelector(x, inner)));
                }
            }

            return(Maybe <TResult> .None);
        }
Exemplo n.º 5
0
 private async Task <Maybe <TResult> > SelectAsyncImpl <TResult>(
     Func <T, Task <TResult> > selector)
 {
     return(_isSome ? Maybe.Of(await selector(_value).ConfigureAwait(false))
         : Maybe <TResult> .None);
 }