Init Scripts in new repo
This commit is contained in:
44
Functional/Some.cs
Normal file
44
Functional/Some.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace SJK.Functional;
|
||||
|
||||
public class Some<T> : IOption<T>
|
||||
{
|
||||
private readonly T _data;
|
||||
|
||||
private Some(T data)
|
||||
{
|
||||
_data = data;
|
||||
}
|
||||
public ref readonly T Value => ref _data;
|
||||
public static Some<T> Of(T data) => new(data);
|
||||
// [MethodImpl(MethodImplOptions.AggressiveInlining)]//TODO see if these inporve perfomce in gernral cases
|
||||
public TResult Match<TResult>(Func<T, TResult> onSome, Func<TResult> _) =>
|
||||
onSome(_data);
|
||||
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public IOption<TResult> Bind<TResult>(Func<T, IOption<TResult>> f) => f(_data);
|
||||
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public IOption<TResult> Map<TResult>(Func<T, TResult> f) => new Some<TResult>(f(_data));
|
||||
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public T Or(T _) => _data;
|
||||
public T Or(Func<T> aDefault) => _data;
|
||||
public bool HasValue([NotNullWhen(true)]out T? value) => (value = _data) is not null;
|
||||
|
||||
public bool HasValue() => true;
|
||||
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Match(Action<T> onSome, Action onNone) => onSome(_data);
|
||||
public override string ToString()
|
||||
{
|
||||
return nameof(Some<T>)+": "+ _data;
|
||||
}
|
||||
|
||||
public bool Equals(IOption<T>? other)
|
||||
{
|
||||
if (other is Some<T> some){
|
||||
return _data!.Equals(some._data);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user