Created
March 25, 2017 15:35
-
-
Save cameronpresley/7c0ddaced32c07c37cb2ed284b7cf2aa to your computer and use it in GitHub Desktop.
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
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