From c26fb691882d71e60c420f3818898fe2717fb27d Mon Sep 17 00:00:00 2001 From: Sanchime Date: Sat, 14 May 2022 17:25:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Linq?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 27 ++++++++++++ .vscode/tasks.json | 41 +++++++++++++++++++ Sanchime.Functional/Extensions/Exceptional.cs | 27 ++++++++++++ Sanchime.Test/Program.cs | 20 ++++----- Sanchime.Toolkits/Basic.cs | 3 +- 5 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..71cf075 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,27 @@ +{ + "version": "0.2.0", + "configurations": [ + + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/Sanchime.Test/bin/Debug/net7.0/Sanchime.Test.dll", + "args": [], + "cwd": "${workspaceFolder}/Sanchime.Test", + // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console + "console": "internalConsole", + "stopAtEntry": false + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..308a063 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/Sanchime.Test/Sanchime.Test.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/Sanchime.Test/Sanchime.Test.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/Sanchime.Test/Sanchime.Test.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Sanchime.Functional/Extensions/Exceptional.cs b/Sanchime.Functional/Extensions/Exceptional.cs index 46dcabb..41a9b06 100644 --- a/Sanchime.Functional/Extensions/Exceptional.cs +++ b/Sanchime.Functional/Extensions/Exceptional.cs @@ -54,4 +54,31 @@ public static class ExceptionalException => @this.Map(CurryingExtension.CurryFirst).Apply(value); #endregion + + #region 单子 + + public static Exceptional Bind(this Exceptional @this, Func> binding) + => @this.Successful ? binding(@this.Value) : new Exceptional(@this.Exception); + + #endregion + + public static Exceptional ForEach(this Exceptional @this, Action action) + => @this.Map(action.ToFunc()); + + #region Linq + + public static Exceptional Select(this Exceptional @this, Func mapping) + => @this.Map(mapping); + + public static Exceptional SelectMany(this Exceptional @this, Func> binding, Func project) + { + if (@this.Exceptionally) + { + return new Exceptional(@this.Exception); + } + var bound = binding(@this.Value); + return bound.Exceptionally ? new Exceptional(bound.Exception) : project(@this.Value, bound.Value); + } + + #endregion } diff --git a/Sanchime.Test/Program.cs b/Sanchime.Test/Program.cs index 342b628..e76d641 100644 --- a/Sanchime.Test/Program.cs +++ b/Sanchime.Test/Program.cs @@ -1,5 +1,5 @@ -using Sanchime.Functional.Core.Products; -using Sanchime.Functional.Core.Extensions; +using Sanchime.Functional.Products; +using Sanchime.Functional.Extensions; using Sanchime.Toolkits; // Test test = new Test(); @@ -19,15 +19,15 @@ using Sanchime.Toolkits; // Option.Some(1).Map(x => (float)x + 1.2).Map(x => x.ToString()).Map(x => x.GetType().Name).WriteLine(); -// // 测试Option的Bind -// var parse = (string s) => Int32.TryParse(s, out int i) ? Option.Some(i) : Option.None; -// var foo = (string s) => s.Pipe(parse).Bind(Age.Of); +// 测试Option的Bind +var parse = (string s) => Int32.TryParse(s, out int i) ? Option.Some(i) : Option.None; +var foo = (string s) => s.Pipe(parse).Bind(Age.Of); -// foo("111").WriteLine(); -// foo("aaa").WriteLine(); -// foo("123").WriteLine(); -// // 管道 -// foo("1ab").Pipe(x => Console.WriteLine(x)); +foo("111").WriteLine(); +foo("aaa").WriteLine(); +foo("123").WriteLine(); +// 管道 +foo("1ab").Pipe(x => Console.WriteLine(x)); public struct Age { private int _value; diff --git a/Sanchime.Toolkits/Basic.cs b/Sanchime.Toolkits/Basic.cs index c0e4805..2c12695 100644 --- a/Sanchime.Toolkits/Basic.cs +++ b/Sanchime.Toolkits/Basic.cs @@ -1,3 +1,4 @@ +using System.Diagnostics; namespace Sanchime.Toolkits; public static class Basic @@ -5,7 +6,7 @@ public static class Basic public static void WriteLine(this T @this) { - System.Console.WriteLine(@this); + Debug.WriteLine(@this); } public static void WriteLine(this T @this, Func func)