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.
Zero/ZeroLevel/Services/Extensions/Monades.cs

84 lines
2.2 KiB

6 years ago
using System;
namespace ZeroLevel
{
public static class Monades
{
#region With
6 years ago
public static TResult With<TInput, TResult>(this TInput o, Func<TInput, TResult> evaluator)
{
if (null != o) return evaluator(o);
5 months ago
return default(TResult)!;
6 years ago
}
#endregion With
6 years ago
#region Return
6 years ago
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);
5 months ago
return default(TResult)!;
6 years ago
}
#endregion Return
6 years ago
#region IsNotNull
6 years ago
public static bool IsNotNull<TInput>(this TInput o)
{
if (null != o) return true;
return false;
}
#endregion IsNotNull
6 years ago
#region If
6 years ago
public static TInput If<TInput>(this TInput o, Predicate<TInput> evaluator)
{
5 months ago
if (null != o) return evaluator(o) ? o : default(TInput)!;
return default(TInput)!;
6 years ago
}
6 years ago
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)
5 months ago
=> o.Either(x => x != null!, ifTrue, ifFalse);
#endregion If
6 years ago
#region Do
6 years ago
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
6 years ago
}
}

Powered by TurnKey Linux.