mirror of https://github.com/ogoun/Zero.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.2 KiB
84 lines
2.2 KiB
using System;
|
|
|
|
namespace ZeroLevel
|
|
{
|
|
public static class Monades
|
|
{
|
|
#region With
|
|
|
|
public static TResult With<TInput, TResult>(this TInput o, Func<TInput, TResult> evaluator)
|
|
{
|
|
if (null != o) return evaluator(o);
|
|
return default(TResult)!;
|
|
}
|
|
|
|
#endregion With
|
|
|
|
#region Return
|
|
|
|
public static TResult Return<TInput, TResult>(this TInput o, Func<TInput, TResult> evaluator, TResult failureValue)
|
|
{
|
|
if (null != o) return evaluator(o);
|
|
return failureValue;
|
|
}
|
|
|
|
public static TResult Return<TInput, TResult>(this TInput o, Func<TInput, TResult> evaluator)
|
|
{
|
|
if (null != o) return evaluator(o);
|
|
return default(TResult)!;
|
|
}
|
|
|
|
#endregion Return
|
|
|
|
#region IsNotNull
|
|
|
|
public static bool IsNotNull<TInput>(this TInput o)
|
|
{
|
|
if (null != o) return true;
|
|
return false;
|
|
}
|
|
|
|
#endregion IsNotNull
|
|
|
|
#region If
|
|
|
|
public static TInput If<TInput>(this TInput o, Predicate<TInput> evaluator)
|
|
{
|
|
if (null != o) return evaluator(o) ? o : default(TInput)!;
|
|
return default(TInput)!;
|
|
}
|
|
|
|
public static TOutput Either<TInput, TOutput>(this TInput o, Func<TInput, bool> condition,
|
|
Func<TInput, TOutput> ifTrue, Func<TInput, TOutput> ifFalse)
|
|
=> condition(o) ? ifTrue(o) : ifFalse(o);
|
|
|
|
public static TOutput Either<TInput, TOutput>(this TInput o, Func<TInput, TOutput> ifTrue,
|
|
Func<TInput, TOutput> ifFalse)
|
|
=> o.Either(x => x != null!, ifTrue, ifFalse);
|
|
|
|
#endregion If
|
|
|
|
#region Do
|
|
|
|
public static TInput Do<TInput>(this TInput o, Action<TInput> action)
|
|
{
|
|
if (null != o) action(o);
|
|
return o;
|
|
}
|
|
|
|
public static TInput Do<TInput>(this TInput o, Action<TInput> action, Action nullHandler)
|
|
{
|
|
if (null != o)
|
|
{
|
|
action(o);
|
|
}
|
|
else
|
|
{
|
|
nullHandler();
|
|
}
|
|
return o;
|
|
}
|
|
|
|
#endregion Do
|
|
}
|
|
} |