From 49716bd884c3963bf9a441c4ed9ae7c16ed538e7 Mon Sep 17 00:00:00 2001 From: Sanchime Date: Wed, 4 May 2022 20:58:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0Func=E6=89=A9=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sanchime.Functional/Core/Extensions/Either.cs | 2 +- Sanchime.Functional/Core/Extensions/Func.cs | 94 +++++++++++++++++++ .../Core/Extensions/IEnumerable.cs | 1 - .../Core/Extensions/Identity.cs | 2 +- Sanchime.Functional/Core/Extensions/Option.cs | 2 - .../Core/{Eithers => Products}/Either.cs | 4 +- .../Core/{Options => Products}/Option.cs | 4 +- 7 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 Sanchime.Functional/Core/Extensions/Func.cs rename Sanchime.Functional/Core/{Eithers => Products}/Either.cs (96%) rename Sanchime.Functional/Core/{Options => Products}/Option.cs (97%) diff --git a/Sanchime.Functional/Core/Extensions/Either.cs b/Sanchime.Functional/Core/Extensions/Either.cs index 2817727..508712a 100644 --- a/Sanchime.Functional/Core/Extensions/Either.cs +++ b/Sanchime.Functional/Core/Extensions/Either.cs @@ -1,5 +1,5 @@ using Sanchime.Functional.Core.Products; -using Sanchime.Functional.Core.Eithers; + namespace Sanchime.Functional.Core.Extensions; public static class EitherExtension diff --git a/Sanchime.Functional/Core/Extensions/Func.cs b/Sanchime.Functional/Core/Extensions/Func.cs new file mode 100644 index 0000000..17a4f22 --- /dev/null +++ b/Sanchime.Functional/Core/Extensions/Func.cs @@ -0,0 +1,94 @@ +using Sanchime.Functional.Core.Products; +namespace Sanchime.Functional.Core.Extensions; + +public static class FuncExtension +{ + /// + /// 忽略入参 + /// + /// + /// + /// + public static Func ToNullary(this Func @this) + => () => @this(Unit.Value); + + /// + /// 将两个函数组合 + /// + /// + /// + /// + /// + /// + /// + public static Func Compose(this Func left, Func right) + => x => left(right(x)); + + /// + /// 对谓词取反 + /// + /// + /// + /// + public static Func Negate(this Func pred) + => val => !pred(val); + + #region 函子 + public static Func Map(this Func @this, Func mapping) + => () => mapping(@this()); + + public static Func Map(this Func @this, Func mapping) + => (t1, t2) => mapping(@this(t1, t2)); + + #endregion + + #region 单子 + public static Func Bind(this Func @this, Func> binding) + => () => binding(@this()).Invoke(); + + #endregion + + #region 应用子 + + public static Func Apply(this Func @this, T1 t1) + => (t2) => @this(t1, t2); + + public static Func Apply(this Func @this, T1 t1) + => (t2, t3) => @this(t1, t2, t3); + + public static Func Apply(this Func @this, T1 t1) + => (t2, t3, t4) => @this(t1, t2, t3, t4); + + public static Func Apply(this Func @this, T1 t1) + => (t2, t3, t4, t5) => @this(t1, t2, t3, t4, t5); + + public static Func Apply(this Func @this, T1 t1) + => (t2, t3, t4, t5, t6) => @this(t1, t2, t3, t4, t5, t6); + + public static Func Apply(this Func @this, T1 t1) + => (t2, t3, t4, t5, t6, t7) => @this(t1, t2, t3, t4, t5, t6, t7); + + public static Func Apply(this Func @this, T1 t1) + => (t2, t3, t4, t5, t6, t7, t8) => @this(t1, t2, t3, t4, t5, t6, t7, t8); + + public static Func Apply(this Func @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 Select(this Func @this, Func mapping) + => @this.Map(mapping); + + public static Func

SelectMany(this Func @this, Func> binding, Func project) + => () => + { + var t = @this(); + var r = binding(t).Invoke(); + return project(t, r); + }; + + #endregion +} \ No newline at end of file diff --git a/Sanchime.Functional/Core/Extensions/IEnumerable.cs b/Sanchime.Functional/Core/Extensions/IEnumerable.cs index e757fa2..1bf9a10 100644 --- a/Sanchime.Functional/Core/Extensions/IEnumerable.cs +++ b/Sanchime.Functional/Core/Extensions/IEnumerable.cs @@ -1,5 +1,4 @@ using System.Collections.Immutable; -using Sanchime.Functional.Core.Options; using Sanchime.Functional.Core.Products; namespace Sanchime.Functional.Core.Extensions; diff --git a/Sanchime.Functional/Core/Extensions/Identity.cs b/Sanchime.Functional/Core/Extensions/Identity.cs index dc7901d..df70ba3 100644 --- a/Sanchime.Functional/Core/Extensions/Identity.cs +++ b/Sanchime.Functional/Core/Extensions/Identity.cs @@ -8,5 +8,5 @@ public static class IdentityExtension => () => mapping(@this()); public static Identity Bind(this Identity @this, Func> binding) - => binding(@this()).Invoke(); + => () => binding(@this()).Invoke(); } diff --git a/Sanchime.Functional/Core/Extensions/Option.cs b/Sanchime.Functional/Core/Extensions/Option.cs index d44b6cb..0c838b1 100644 --- a/Sanchime.Functional/Core/Extensions/Option.cs +++ b/Sanchime.Functional/Core/Extensions/Option.cs @@ -1,5 +1,3 @@ -using System; -using Sanchime.Functional.Core.Options; using Sanchime.Functional.Core.Products; namespace Sanchime.Functional.Core.Extensions; diff --git a/Sanchime.Functional/Core/Eithers/Either.cs b/Sanchime.Functional/Core/Products/Either.cs similarity index 96% rename from Sanchime.Functional/Core/Eithers/Either.cs rename to Sanchime.Functional/Core/Products/Either.cs index 8de5b80..669ea9c 100644 --- a/Sanchime.Functional/Core/Eithers/Either.cs +++ b/Sanchime.Functional/Core/Products/Either.cs @@ -1,6 +1,4 @@ -using Sanchime.Functional.Core.Products; - -namespace Sanchime.Functional.Core.Eithers; +namespace Sanchime.Functional.Core.Products; public static class Either { diff --git a/Sanchime.Functional/Core/Options/Option.cs b/Sanchime.Functional/Core/Products/Option.cs similarity index 97% rename from Sanchime.Functional/Core/Options/Option.cs rename to Sanchime.Functional/Core/Products/Option.cs index d3cd9a9..e33f560 100644 --- a/Sanchime.Functional/Core/Options/Option.cs +++ b/Sanchime.Functional/Core/Products/Option.cs @@ -1,6 +1,4 @@ -using Sanchime.Functional.Core.Products; - -namespace Sanchime.Functional.Core.Options; +namespace Sanchime.Functional.Core.Products; ///

/// 状态