更新Func扩展

This commit is contained in:
Sanchime 2022-05-04 20:58:08 +08:00
parent 92507c8c06
commit 49716bd884
7 changed files with 98 additions and 11 deletions

View File

@ -1,5 +1,5 @@
using Sanchime.Functional.Core.Products;
using Sanchime.Functional.Core.Eithers;
namespace Sanchime.Functional.Core.Extensions;
public static class EitherExtension

View File

@ -0,0 +1,94 @@
using Sanchime.Functional.Core.Products;
namespace Sanchime.Functional.Core.Extensions;
public static class FuncExtension
{
/// <summary>
/// 忽略入参<see cref="Unit"/>
/// </summary>
/// <param name="this"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Func<T> ToNullary<T>(this Func<Unit, T> @this)
=> () => @this(Unit.Value);
/// <summary>
/// 将两个函数组合
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <typeparam name="T1"></typeparam>
/// <typeparam name="T2"></typeparam>
/// <typeparam name="R"></typeparam>
/// <returns></returns>
public static Func<T1, R> Compose<T1, T2, R>(this Func<T2, R> left, Func<T1, T2> right)
=> x => left(right(x));
/// <summary>
/// 对谓词取反
/// </summary>
/// <param name="pred"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Func<T, bool> Negate<T>(this Func<T, bool> pred)
=> val => !pred(val);
#region
public static Func<R> Map<T, R>(this Func<T> @this, Func<T,R> mapping)
=> () => mapping(@this());
public static Func<T1, T2, R> Map<T1, T2, T3, R>(this Func<T1, T2, T3> @this, Func<T3, R> mapping)
=> (t1, t2) => mapping(@this(t1, t2));
#endregion
#region
public static Func<R> Bind<T, R>(this Func<T> @this, Func<T, Func<R>> binding)
=> () => binding(@this()).Invoke();
#endregion
#region
public static Func<T2, R> Apply<T1, T2, R>(this Func<T1, T2, R> @this, T1 t1)
=> (t2) => @this(t1, t2);
public static Func<T2, T3, R> Apply<T1, T2, T3, R>(this Func<T1, T2, T3, R> @this, T1 t1)
=> (t2, t3) => @this(t1, t2, t3);
public static Func<T2, T3, T4, R> Apply<T1, T2, T3, T4, R>(this Func<T1, T2, T3, T4, R> @this, T1 t1)
=> (t2, t3, t4) => @this(t1, t2, t3, t4);
public static Func<T2, T3, T4, T5, R> Apply<T1, T2, T3, T4, T5, R>(this Func<T1, T2, T3, T4, T5, R> @this, T1 t1)
=> (t2, t3, t4, t5) => @this(t1, t2, t3, t4, t5);
public static Func<T2, T3, T4, T5, T6, R> Apply<T1, T2, T3, T4, T5, T6, R>(this Func<T1, T2, T3, T4, T5, T6, R> @this, T1 t1)
=> (t2, t3, t4, t5, t6) => @this(t1, t2, t3, t4, t5, t6);
public static Func<T2, T3, T4, T5, T6, T7, R> Apply<T1, T2, T3, T4, T5, T6, T7, R>(this Func<T1, T2, T3, T4, T5, T6, T7, R> @this, T1 t1)
=> (t2, t3, t4, t5, t6, t7) => @this(t1, t2, t3, t4, t5, t6, t7);
public static Func<T2, T3, T4, T5, T6, T7, T8, R> Apply<T1, T2, T3, T4, T5, T6, T7, T8, R>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, R> @this, T1 t1)
=> (t2, t3, t4, t5, t6, t7, t8) => @this(t1, t2, t3, t4, t5, t6, t7, t8);
public static Func<T2, T3, T4, T5, T6, T7, T8, T9, R> Apply<T1, T2, T3, T4, T5, T6, T7, T8, T9, R>(this Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, R> @this, T1 t1)
=> (t2, t3, t4, t5, t6, t7, t8, t9) => @this(t1, t2, t3, t4, t5, t6, t7, t8, t9);
#endregion
#region Linq
public static Func<R> Select<T, R>(this Func<T> @this, Func<T,R> mapping)
=> @this.Map(mapping);
public static Func<P> SelectMany<T, R, P>(this Func<T> @this, Func<T,Func<R>> binding, Func<T, R, P> project)
=> () =>
{
var t = @this();
var r = binding(t).Invoke();
return project(t, r);
};
#endregion
}

View File

@ -1,5 +1,4 @@
using System.Collections.Immutable;
using Sanchime.Functional.Core.Options;
using Sanchime.Functional.Core.Products;
namespace Sanchime.Functional.Core.Extensions;

View File

@ -8,5 +8,5 @@ public static class IdentityExtension
=> () => mapping(@this());
public static Identity<R> Bind<T, R>(this Identity<T> @this, Func<T, Identity<R>> binding)
=> binding(@this()).Invoke();
=> () => binding(@this()).Invoke();
}

View File

@ -1,5 +1,3 @@
using System;
using Sanchime.Functional.Core.Options;
using Sanchime.Functional.Core.Products;
namespace Sanchime.Functional.Core.Extensions;

View File

@ -1,6 +1,4 @@
using Sanchime.Functional.Core.Products;
namespace Sanchime.Functional.Core.Eithers;
namespace Sanchime.Functional.Core.Products;
public static class Either
{

View File

@ -1,6 +1,4 @@
using Sanchime.Functional.Core.Products;
namespace Sanchime.Functional.Core.Options;
namespace Sanchime.Functional.Core.Products;
/// <summary>
/// <see cref="Option"/>的<see cref="None"/>状态