调整Option
This commit is contained in:
parent
06d1054c80
commit
79402fa1f8
|
@ -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);
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
Loading…
Reference in New Issue