调整Option

This commit is contained in:
sanchime 2023-01-11 21:07:38 +08:00
parent 06d1054c80
commit 79402fa1f8
3 changed files with 10 additions and 27 deletions

View File

@ -2,31 +2,10 @@ namespace Sanchime.Functional.Extensions;
public static class OptionExtension public static class OptionExtension
{ {
#region :
// public static Option<R> Map<T, R>(this None _, Func<T, R> mapping)
// => Option.None;
// public static Option<R> Map<T, R>(this Some<T> some, Func<T, R> mapping)
// => Option.Some(mapping(some.Value));
// public static Option<R> Map<T, R>(this Option<T> option, Func<T, R> mapping)
// => option.Match(() => Option.None, (val) => Option.Some(mapping(val)));
// public static IOption Map<T1, T2, R>(this Option<T1> option, Func<T1, T2, R> mapping)
// => option.Map(mapping.Curry());
// public static IOption Map<T1, T2, T3, R>(this Option<T1> option, Func<T1, T2, T3, R> mapping)
// => option.Map(mapping.CurryFirst());
#endregion
#region : #region :
public static Option<R> Bind<T, R>(this Option<T> option, Func<T, Option<R>> binding)
=> option.Match(() => new Option<R>(), binding);
public static IEnumerable<R> Bind<T1, T2, R>(this Option<T1> option, Func<T1, IEnumerable<R>> binding) public static IEnumerable<R> Bind<T1, T2, R>(this Option<T1> option, Func<T1, IEnumerable<R>> binding)
=> option.AsEnumerable().Bind(binding); => option.AsEnumerable().Bind(binding);

View File

@ -3,12 +3,13 @@ namespace Sanchime.Functional.Products;
/// <summary> /// <summary>
/// <see cref="Optional"/>的<see cref="None"/>状态 /// <see cref="Optional"/>的<see cref="None"/>状态
/// </summary> /// </summary>
public readonly partial struct None : IOption public readonly partial struct None : IOption<Unit>
{ {
/// <summary> public None()
/// 静态构造形成单例 {
/// </summary>
static None() { } }
public bool IsSome => false; public bool IsSome => false;
internal static readonly None Default = new(); internal static readonly None Default = new();
public Unit Value { get; } = Unit.Value;
} }

View File

@ -13,4 +13,7 @@ public readonly partial struct Option<TValue>
public static IOption Return(TValue value) public static IOption Return(TValue value)
=> Return<TValue>(value); => Return<TValue>(value);
public Option<TResult> Bind<TResult>(Func<TValue, Option<TResult>> binder)
=> this.Match(() => new Option<TResult>(), binder);
} }