/// <summary>
 /// By default, Some case is converted to 200 OK and None is 404.
 /// Optionally, provide functions to handle Some and None cases.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="option"></param>
 /// <returns></returns>
 public static Task <IActionResult> ToActionResult <T>(this OptionAsync <T> option, Func <T, IActionResult> some = null, Func <IActionResult> none = null) =>
 option.Match(some ?? Ok, none ?? NotFound);
Пример #2
0
 public OptionAsync <A> Append(OptionAsync <A> x, OptionAsync <A> y) =>
 Plus(x, y);
Пример #3
0
 public Func <Unit, Task <int> > Count(OptionAsync <A> ma) => async _ =>
 await ma.IsSome
         ? 1
         : 0;
Пример #4
0
 public OptionAsync <A> Optional(A value) =>
 OptionAsync <A> .Optional(value);
Пример #5
0
 public OptionAsync <A> BindReturn(Unit _, OptionAsync <A> mb) =>
 mb;
Пример #6
0
 public Task <bool> EqualsAsync(OptionAsync <A> x, OptionAsync <A> y) =>
 default(EqOptionAsync <EqDefault <A>, A>).EqualsAsync(x, y);
Пример #7
0
 public OptionAsync <A> Some(A value) =>
 isnull(value)
         ? throw new ArgumentNullException(nameof(value))
         : OptionAsync <A> .Some(value);
Пример #8
0
 public Task <bool> IsSome(OptionAsync <A> opt) =>
 opt.IsSome;
 public static OptionAsync <B> Apply <A, B>(this Func <A, B> fab, OptionAsync <A> fa) =>
 ApplOptionAsync <A, B> .Inst.Apply(fab, fa);
Пример #10
0
 public OptionAsync <A> ReturnAsync(Func <Unit, Task <A> > f) =>
 OptionAsync <A> .SomeAsync(f(unit));
Пример #11
0
 public Task <bool> IsNone(OptionAsync <A> opt) =>
 opt.IsNone;
Пример #12
0
 public OptionAsync <A> Plus(OptionAsync <A> a, OptionAsync <A> b) =>
 default(MOptionAsync <A>).RunAsync(async __ =>
                                    await a.IsSome.ConfigureAwait(false)
             ? a
             : b);
Пример #13
0
 public Func <Unit, Task <S> > Fold <S>(OptionAsync <A> ma, S state, Func <S, A, S> f) => async _ =>
Пример #14
0
 public MB Bind <MonadB, MB, B>(OptionAsync <A> ma, Func <A, MB> f) where MonadB : struct, MonadAsync <Unit, Unit, MB, B> =>
 default(MonadB).RunAsync(async _ =>
                          (await ma.IsSome.ConfigureAwait(false))
             ? f(await ma.Value.ConfigureAwait(false))
                          : default(MonadB).Fail(ValueIsNoneException.Default));
Пример #15
0
 public static OptionAsync <A> Subtract <NUM, A>(this OptionAsync <A> x, OptionAsync <A> y) where NUM : struct, Num <A> =>
 from a in x
 from b in y
 select subtract <NUM, A>(a, b);
Пример #16
0
 public static OptionAsync <C> Apply <A, B, C>(this Func <A, B, C> fabc, OptionAsync <A> fa, OptionAsync <B> fb) =>
 ApplOptionAsync <A, B, C> .Inst.Apply(curry(fabc), fa, fb);
Пример #17
0
 public static OptionAsync <A> Divide <NUM, A>(this OptionAsync <A> x, OptionAsync <A> y) where NUM : struct, Num <A> =>
 from a in x
 from b in y
 select divide <NUM, A>(a, b);
Пример #18
0
 public static OptionAsync <Func <B, C> > Apply <A, B, C>(this OptionAsync <Func <A, B, C> > fabc, OptionAsync <A> fa) =>
 from x in fabc
 from y in ApplOptionAsync <A, B, C> .Inst.Apply(curry(x), fa)
 select y;
Пример #19
0
 public Task <int> GetHashCodeAsync(OptionAsync <A> x) =>
 default(HashableOptionAsync <A>).GetHashCodeAsync(x);
Пример #20
0
 public static OptionAsync <Func <B, C> > Apply <A, B, C>(this Func <A, Func <B, C> > fabc, OptionAsync <A> fa) =>
 ApplOptionAsync <A, B, C> .Inst.Apply(fabc, fa);
Пример #21
0
 public OptionAsync <A> SomeAsync(Task <A> taskA) =>
 isnull(taskA)
         ? throw new ArgumentNullException(nameof(taskA))
         : OptionAsync <A> .SomeAsync(taskA);
Пример #22
0
 public static OptionAsync <B> Action <A, B>(this OptionAsync <A> fa, OptionAsync <B> fb) =>
 ApplOptionAsync <A, B> .Inst.Action(fa, fb);
Пример #23
0
 public OptionAsync <A> OptionalAsync(Task <A> taskA) =>
 OptionAsync <A> .OptionalAsync(taskA);
Пример #24
0
 public static async Task <A?> ToNullable <A>(this OptionAsync <A> ma) where A : struct =>
 (await ma.IsNone)
         ? (A?)null
 : await ma.Value;
Пример #25
0
 public MB BindAsync <MonadB, MB, B>(OptionAsync <A> ma, Func <A, Task <MB> > f) where MonadB : struct, MonadAsync <Unit, Unit, MB, B> =>
 default(MonadB).RunAsync(async _ =>
                          (await ma.IsSome)
             ? await f(await ma.Value)
                          : default(MonadB).Fail(ValueIsNoneException.Default));
Пример #26
0
 /// <summary>
 /// Sum the bound value
 /// </summary>
 /// <remarks>This is a legacy method for backwards compatibility</remarks>
 /// <param name="a">Option of int</param>
 /// <returns>The bound value or 0 if None</returns>
 public static Task <int> Sum(this OptionAsync <int> a) =>
 a.IfNone(0);
Пример #27
0
 public Func <Unit, Task <S> > FoldBackAsync <S>(OptionAsync <A> ma, S state, Func <S, A, Task <S> > f) =>
 FoldAsync(ma, state, f);
Пример #28
0
 /// <summary>
 /// Sum the bound value
 /// </summary>
 /// <remarks>This is a legacy method for backwards compatibility</remarks>
 /// <param name="self">Option of A that is from the type-class NUM</param>
 /// <returns>The bound value or 0 if None</returns>
 public static Task <A> Sum <NUM, A>(this OptionAsync <A> self)
     where NUM : struct, Num <A> =>
 sumAsync <NUM, MOptionAsync <A>, OptionAsync <A>, A>(self);
Пример #29
0
 public OptionAsync <A> Plus(OptionAsync <A> a, OptionAsync <A> b) =>
 default(MOptionAsync <A>).RunAsync(async __ =>
                                    await a.IsSome
             ? a
             : b);
 /// <summary>
 /// Some case is converted to Ok JsonResult. None case is 404.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="option"></param>
 /// <returns></returns>
 public static Task <IActionResult> ToActionResult <T>(this OptionAsync <T> option) =>
 option.Match(Ok, NotFound);