-
-
Save anonymous/47a5fe5b6fe7b6912863 to your computer and use it in GitHub Desktop.
Maybe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// "Maybe" monad extension that make working with long chains of null | |
/// checks easier. From http://www.codeproject.com/KB/cs/maybemonads.aspx. | |
/// </summary> | |
public static class MaybeExtensions | |
{ | |
/// <summary> | |
/// Executes the specified action if the input object is not null; otherwise, the action is not invoked. | |
/// </summary> | |
/// <typeparam name="TInput">The type of the object that is tested against null.</typeparam> | |
/// <param name="obj">The object that is tested against null.</param> | |
/// <param name="action">The action that is performed if the object is not null.</param> | |
/// <returns>The object that was passed in.</returns> | |
public static TInput Do<TInput>(this TInput obj, Action<TInput> action) | |
{ | |
if (obj == null) | |
{ | |
return default(TInput); | |
} | |
action(obj); | |
return obj; | |
} | |
/// <summary> | |
/// Returns the result of the evaluator expression if the input object | |
/// is not null; otherwise, returns the failureValue. | |
/// </summary> | |
/// <typeparam name="TInput">The type of the object that is tested against null.</typeparam> | |
/// <typeparam name="TResult">The type of the object that is returned.</typeparam> | |
/// <param name="obj">The object that is tested against null.</param> | |
/// <param name="evaluator">The evaluator function that is applied against the object if it is not null.</param> | |
/// <param name="failureValue">The value that is returned instead if the object is null.</param> | |
/// <returns>The result of the evaluator function or the failureValue as is appropriate.</returns> | |
public static TResult Return<TInput, TResult>( | |
this TInput obj, | |
Func<TInput, TResult> evaluator, | |
TResult failureValue) | |
{ | |
if (obj == null) | |
{ | |
return failureValue; | |
} | |
return evaluator(obj); | |
} | |
/// <summary> | |
/// Returns the result of the evaluator function if the object to be | |
/// tested is not null; otherwise, null. | |
/// </summary> | |
/// <typeparam name="TInput">The type of the object that is tested against null.</typeparam> | |
/// <typeparam name="TResult">The type of the object that is returned.</typeparam> | |
/// <param name="obj">The object that is tested against null.</param> | |
/// <param name="evaluator">The evaluator function that is applied against the object if it is not null.</param> | |
/// <returns>The result of the evaluator function if the object is not null; otherwise, null.</returns> | |
public static TResult With<TInput, TResult>( | |
this TInput obj, | |
Func<TInput, TResult> evaluator) | |
{ | |
if (obj == null) | |
{ | |
return default(TResult); | |
} | |
return evaluator(obj); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment