添加Task扩展

This commit is contained in:
Sanchime 2022-05-02 22:38:05 +08:00
parent a9ba5c8093
commit 824e672b48
26 changed files with 296 additions and 3 deletions

View File

@ -1,3 +1,4 @@
using System;
using Sanchime.Functional.Core.Options;
using Sanchime.Functional.Core.Products;
@ -49,12 +50,33 @@ public static class OptionExtension
#region
public static Option<R> Apply<T, R>(this Option<T> option, Func<Option<T>, R> apply, Option<T> value)
public static Option<R> Apply<T, R>(this Option<Func<T, R>> option, Option<T> value)
=> option.Match(
None: () => Option.None,
Some: (func) => value.Match(
Some: (apply) => value.Match(
None: () => Option.None,
Some: (val) => Option.Some(apply(value))));
Some: (val) => Option.Some(apply(val))));
public static Option<Func<T2, R>> Apply<T1, T2, R>(this Option<Func<T1, T2, R>> option, Option<T1> value)
=> option.Map(CurryingExtension.Curry).Apply(value);
#endregion
public static T GetOrElse<T>(this Option<T> option, T @default)
=> option.Match(() => @default, (val) => val);
public static T GetOrElse<T>(this Option<T> option, Func<T> fallback)
=> option.Match(() => fallback(), (val) => val);
public static Task<T> GetOrElse<T>(this Option<T> option, Func<Task<T>> fallback)
=> option.Match(() => fallback(), (val) => TaskExtension.Async(val));
public static Option<T> OrElse<T>(this Option<T> left, Option<T> right)
=> left.Match(() => right, (_) => left);
public static Option<T> OrElse<T>(this Option<T> left, Func<Option<T>> right)
=> left.Match(() => right(), (_) => left);
}

View File

@ -0,0 +1,37 @@
using System;
namespace Sanchime.Functional.Core.Extensions;
public static class TaskExtension
{
public static Task<T> Async<T>(T value)
=> Task.FromResult(value);
#region
public static Task<R> Map<T, R>(this Task<T> @this, Func<Exception, R> Faulted, Func<T, R> Completed)
=> @this.ContinueWith(val => val.Status == TaskStatus.Faulted ? Faulted(val.Exception!) : Completed(val.Result));
public static async Task<R> Map<T, R>(this Task<T> @this, Func<T, R> mapping)
=> mapping(await @this.ConfigureAwait(false));
public static async Task<R> Map<R>(this Task @this, Func<R> mapping)
{
await @this;
return mapping();
}
public static Task<Func<T2, R>> Map<T1, T2, R>(this Task<T1> @this, Func<T1, T2, R> mapping)
=> @this.Map(mapping.Curry());
#endregion
public static async Task<R> Apply<T, R>(this Task<Func<T, R>> @this, Task<T> task)
=> (await @this.ConfigureAwait(false))(await task.ConfigureAwait(false));
public static Task<Func<T2, R>> Apply<T1, T2, R>(this Task<Func<T1, T2, R>> @this, Task<T1> task)
=> @this.Map(CurryingExtension.Curry).Apply(task);
}

View File

@ -82,6 +82,7 @@ public readonly struct Option<TValue> : IEquatable<None>, IEquatable<Option<TVal
/// <returns></returns>
public TResult Match<TResult>(Func<TResult> None, Func<TValue, TResult> Some)
=> _isSome ? Some(_value) : None();
public bool Equals(Option<TValue> other)
=> this._isSome == other._isSome
&& (this._isNone || this._value!.Equals(other._value));

View File

@ -0,0 +1,23 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v7.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v7.0": {
"Sanchime.Functional/1.0.0": {
"runtime": {
"Sanchime.Functional.dll": {}
}
}
}
},
"libraries": {
"Sanchime.Functional/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}

View File

@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v7.0", FrameworkDisplayName = "")]

View File

@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("Sanchime.Functional")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyProductAttribute("Sanchime.Functional")]
[assembly: System.Reflection.AssemblyTitleAttribute("Sanchime.Functional")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
// 由 MSBuild WriteCodeFragment 类生成。

View File

@ -0,0 +1 @@
1132520a32de11ff302b7fe56da971f09e88fb78

View File

@ -0,0 +1,10 @@
is_global = true
build_property.TargetFramework = net7.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = Sanchime.Functional
build_property.ProjectDir = /home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/

View File

@ -0,0 +1,8 @@
// <auto-generated/>
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

View File

@ -0,0 +1 @@
14ff64154b5d7a38f0fe0b94f6fa9545005d4e6f

View File

@ -0,0 +1,12 @@
/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/obj/Debug/net7.0/Sanchime.Functional.csproj.AssemblyReference.cache
/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/obj/Debug/net7.0/Sanchime.Functional.GeneratedMSBuildEditorConfig.editorconfig
/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/obj/Debug/net7.0/Sanchime.Functional.AssemblyInfoInputs.cache
/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/obj/Debug/net7.0/Sanchime.Functional.AssemblyInfo.cs
/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/obj/Debug/net7.0/Sanchime.Functional.csproj.CoreCompileInputs.cache
/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/bin/Debug/net7.0/Sanchime.Functional.deps.json
/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/bin/Debug/net7.0/Sanchime.Functional.dll
/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/bin/Debug/net7.0/Sanchime.Functional.pdb
/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/obj/Debug/net7.0/Sanchime.Functional.dll
/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/obj/Debug/net7.0/refint/Sanchime.Functional.dll
/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/obj/Debug/net7.0/Sanchime.Functional.pdb
/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/obj/Debug/net7.0/ref/Sanchime.Functional.dll

View File

@ -0,0 +1,60 @@
{
"format": 1,
"restore": {
"/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/Sanchime.Functional.csproj": {}
},
"projects": {
"/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/Sanchime.Functional.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/Sanchime.Functional.csproj",
"projectName": "Sanchime.Functional",
"projectPath": "/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/Sanchime.Functional.csproj",
"packagesPath": "/home/sanchime/.nuget/packages/",
"outputPath": "/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/sanchime/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net7.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net7.0": {
"targetAlias": "net7.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net7.0": {
"targetAlias": "net7.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/home/sanchime/dotnet/sdk/7.0.100-preview.3.22179.4/RuntimeIdentifierGraph.json"
}
}
}
}
}

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(ExcludeRestorePackageImports)' != 'true'">
<RestoreSuccess Condition="'$(RestoreSuccess)' == ''">True</RestoreSuccess>
<RestoreTool Condition="'$(RestoreTool)' == ''">NuGet</RestoreTool>
<ProjectAssetsFile Condition="'$(ProjectAssetsFile)' == ''">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition="'$(NuGetPackageRoot)' == ''">/home/sanchime/.nuget/packages/</NuGetPackageRoot>
<NuGetPackageFolders Condition="'$(NuGetPackageFolders)' == ''">/home/sanchime/.nuget/packages/</NuGetPackageFolders>
<NuGetProjectStyle Condition="'$(NuGetProjectStyle)' == ''">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition="'$(NuGetToolVersion)' == ''">6.2.0</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition="'$(ExcludeRestorePackageImports)' != 'true'">
<SourceRoot Include="/home/sanchime/.nuget/packages/" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />

View File

@ -0,0 +1,65 @@
{
"version": 3,
"targets": {
"net7.0": {}
},
"libraries": {},
"projectFileDependencyGroups": {
"net7.0": []
},
"packageFolders": {
"/home/sanchime/.nuget/packages/": {}
},
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/Sanchime.Functional.csproj",
"projectName": "Sanchime.Functional",
"projectPath": "/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/Sanchime.Functional.csproj",
"packagesPath": "/home/sanchime/.nuget/packages/",
"outputPath": "/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/obj/",
"projectStyle": "PackageReference",
"configFilePaths": [
"/home/sanchime/.nuget/NuGet/NuGet.Config"
],
"originalTargetFrameworks": [
"net7.0"
],
"sources": {
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net7.0": {
"targetAlias": "net7.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
}
},
"frameworks": {
"net7.0": {
"targetAlias": "net7.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "/home/sanchime/dotnet/sdk/7.0.100-preview.3.22179.4/RuntimeIdentifierGraph.json"
}
}
}
}

View File

@ -0,0 +1,8 @@
{
"version": 2,
"dgSpecHash": "SFlPh821qi4GkLKqdQvlF3w70bJfiHdxPTcWjQz6sHNJrETehv6TShnFcPyYahMOhHW73n97cu5NcnOrJNHiSw==",
"success": true,
"projectFilePath": "/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/Sanchime.Functional.csproj",
"expectedPackageFiles": [],
"logs": []
}

View File

@ -0,0 +1 @@
"restore":{"projectUniqueName":"/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/Sanchime.Functional.csproj","projectName":"Sanchime.Functional","projectPath":"/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/Sanchime.Functional.csproj","outputPath":"/home/sanchime/桌面/Program/C#/Sanchime/Sanchime.Functional/obj/","projectStyle":"PackageReference","originalTargetFrameworks":["net7.0"],"sources":{"https://api.nuget.org/v3/index.json":{}},"frameworks":{"net7.0":{"targetAlias":"net7.0","projectReferences":{}}},"warningProperties":{"warnAsError":["NU1605"]}}"frameworks":{"net7.0":{"targetAlias":"net7.0","imports":["net461","net462","net47","net471","net472","net48"],"assetTargetFallback":true,"warn":true,"frameworkReferences":{"Microsoft.NETCore.App":{"privateAssets":"all"}},"runtimeIdentifierGraphPath":"/home/sanchime/dotnet/sdk/7.0.100-preview.3.22179.4/RuntimeIdentifierGraph.json"}}

View File

@ -0,0 +1 @@
16514907543498126