使用全局命名空间

This commit is contained in:
Sanchime 2022-05-14 10:53:03 +08:00
parent 198607f4ef
commit eec4cbc3b6
13 changed files with 34 additions and 30 deletions

View File

@ -1,5 +1,3 @@
using Sanchime.Functional.Products;
namespace Sanchime.Functional.Extensions;
public static class ActionExtension

View File

@ -1,5 +1,3 @@
using Sanchime.Functional.Products;
namespace Sanchime.Functional.Extensions;
/// <summary>

View File

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

View File

@ -1,5 +1,3 @@
using Sanchime.Functional.Products;
namespace Sanchime.Functional.Extensions;
public static class EnumExtension

View File

@ -1,5 +1,3 @@
using Sanchime.Functional.Products;
namespace Sanchime.Functional.Extensions;
/// <summary>
@ -10,6 +8,29 @@ public static class ExceptionalException
public static Func<T, Exceptional<T>> Return<T>()
=> val => val;
public static Unit Match<T>(this Exceptional<T> @this, Action<Exception> Exception, Action<T> Success)
=> @this.Match(Exception.ToFunc(), Success.ToFunc());
#region
public static Exceptional<R> Map<T, R>(this Exceptional<T> @this, Func<T, R> mapping)
=> @this.Successful ? mapping(@this.Value) : new Exceptional<R>(@this.Exception);
#endregion
#region
public static Exceptional<R> Apply<T, R>(this Exceptional<Func<T, R>> @this, Exceptional<T> value)
=> @this.Match(
Exception: e => e,
Success: apply => value.Match(
Exception: e => e,
Success: val => new Exceptional<R>(apply(val))
)
);
public static Exceptional<Func<T2, R>> Apply<T1, T2, R>(this Exceptional<Func<T1, T2, R>> @this, Exceptional<T1> value)
=> @this.Map(CurryingExtension.Curry).Apply(value);
#endregion
}

View File

@ -1,4 +1,3 @@
using Sanchime.Functional.Products;
namespace Sanchime.Functional.Extensions;
public static class FuncExtension

View File

@ -0,0 +1,3 @@
global using Sanchime.Functional.Products;
namespace Sanchime.Functional.Extensions;

View File

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

View File

@ -1,5 +1,3 @@
using Sanchime.Functional.Products;
namespace Sanchime.Functional.Extensions;
public static class IdentityExtension

View File

@ -1,5 +1,3 @@
using Sanchime.Functional.Products;
namespace Sanchime.Functional.Extensions;
public static class NullableExtension

View File

@ -1,5 +1,3 @@
using Sanchime.Functional.Products;
namespace Sanchime.Functional.Extensions;
public static class OptionExtension

View File

@ -1,7 +1,3 @@
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using Sanchime.Functional.Products;
namespace Sanchime.Functional.Extensions;
public static class TaskExtension

View File

@ -2,13 +2,13 @@ namespace Sanchime.Functional.Products;
public readonly struct Exceptional<TValue>
{
internal Exception _exception { get; }
internal Exception Exception { get; }
internal TValue _value { get; }
internal TValue Value { get; }
public bool Success => _exception is null;
public bool Successful => Exception is null;
public bool Exception => !Success;
public bool Exceptionally => !Successful;
internal Exceptional(Exception exception)
{
@ -16,11 +16,11 @@ public readonly struct Exceptional<TValue>
{
throw new ArgumentNullException(nameof(exception));
}
(_exception, _value) = (exception, default!);
(Exception, Value) = (exception, default!);
}
internal Exceptional(TValue right)
=> (_value, _exception) = (right, default!);
=> (Value, Exception) = (right, default!);
public static implicit operator Exceptional<TValue>(Exception left)
=> new(left);
@ -29,7 +29,7 @@ public readonly struct Exceptional<TValue>
=> new(right);
public TRight Match<TRight>(Func<Exception, TRight> Exception, Func<TValue, TRight> Success)
=> this.Exception ? Exception(_exception) : Success(_value);
=> Exceptionally ? Exception(this.Exception) : Success(Value);
public override string ToString()
=> Match(e => $"Exception({e.Message})", v => $"Success({v})");