添加Nullable转为Option的扩展

This commit is contained in:
Sanchime 2022-05-08 15:28:22 +08:00
parent 5e8849133f
commit 01633d1c4c
5 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,12 @@
using Sanchime.Functional.Core.Products;
namespace Sanchime.Functional.Core.Extensions;
public static class NullableExtension
{
/// <summary>
/// 将<see cref="Nullable"/>值转换为<see cref="Option"/>
/// </summary>
public static Option<T> ToOption<T>(this Nullable<T> @this) where T : struct
=> @this.HasValue ? Option.Some(@this.Value) : Option.None;
}

View File

@ -0,0 +1,9 @@
namespace Sanchime.Functional.Core.Products;
/// <summary>
/// 应用函子
/// </summary>
/// <typeparam name="TCategory"></typeparam>
public interface IApplicative<TCategory> : IFunctor<TCategory>
{
}

View File

@ -7,5 +7,15 @@ namespace Sanchime.Functional.Core.Products;
/// <typeparam name="TCategory"></typeparam>
public interface IFunctor<TCategory> : ICategory
{
IFunctor<TResult> Map<TResult>(Func<TCategory, IFunctor<TResult>> mapping);
TCategory Map(Func<TCategory, TCategory> mapping);
}
public struct Test<T> : IFunctor<Test<T>>
{
public T? Value {get; set; }
public Test<T> Map(Func<Test<T>, Test<T>> mapping)
{
return mapping(this);
}
}

View File

@ -0,0 +1,9 @@
namespace Sanchime.Functional.Core.Products;
/// <summary>
/// 单子
/// </summary>
/// <typeparam name="TCategory"></typeparam>
public interface IMonad<TCategory> : IApplicative<TCategory>
{
}

View File

@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanchime.Test", "Sanchime.T
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanchime.Functional", "Sanchime.Functional\Sanchime.Functional.csproj", "{DF48AFE2-CD1B-4BCE-83EF-DA8BDA104069}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanchime.Toolkits", "Sanchime.Toolkits\Sanchime.Toolkits.csproj", "{3A94EB2F-04F1-46F8-ADD8-DE091402635F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -24,5 +26,9 @@ Global
{DF48AFE2-CD1B-4BCE-83EF-DA8BDA104069}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF48AFE2-CD1B-4BCE-83EF-DA8BDA104069}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF48AFE2-CD1B-4BCE-83EF-DA8BDA104069}.Release|Any CPU.Build.0 = Release|Any CPU
{3A94EB2F-04F1-46F8-ADD8-DE091402635F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3A94EB2F-04F1-46F8-ADD8-DE091402635F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A94EB2F-04F1-46F8-ADD8-DE091402635F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A94EB2F-04F1-46F8-ADD8-DE091402635F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal