Skip to content

Instantly share code, notes, and snippets.

@cameronpresley
Created March 25, 2017 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cameronpresley/7c0ddaced32c07c37cb2ed284b7cf2aa to your computer and use it in GitHub Desktop.
Save cameronpresley/7c0ddaced32c07c37cb2ed284b7cf2aa to your computer and use it in GitHub Desktop.
public class Result<T, U>
{
private readonly T _left;
private readonly U _right;
private readonly bool _isLeft;
private Result(T left, U right, bool isLeft)
{
_left = left;
_right = right;
_isLeft = isLeft;
}
public static Result<T, U> CreateLeft(T t)
{
return new Result<T, U>(t,default(U), true);
}
public static Result<T, U> CreateRight(U u)
{
return new Result<T, U>(default(T), u, false);
}
public void Bind(Action leftAction, Action rightAction)
{
if (_isLeft)
{
leftAction();
}
else
{
rightAction();
}
}
public V Map<V>(Func<T, V> leftAction, Func<U, V> rightAction)
{
if (_isLeft)
{
return leftAction(_left);
}
return rightAction(_right);
}
public void Bind(Action<T> leftAction, Action<U> rightAction)
{
if (_isLeft)
{
leftAction(_left);
}
else
{
rightAction(_right);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment