Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class SwitchExtensions
- {
- public static SwitchContext<T, TResult> Switch<T, TResult>(this T testValue, TResult defaultResult)
- {
- return new SwitchContext<T, TResult>(testValue, defaultResult);
- }
- public static SwitchContext<T, TResult> Switch<T, TResult>(this T testValue)
- {
- return new SwitchContext<T, TResult>(testValue, default(TResult));
- }
- public class SwitchContext<T, TResult>
- {
- private T _testValue;
- private TResult _result;
- private bool _matchFound = false;
- internal SwitchContext(T testValue, TResult defaultResult)
- {
- this._testValue = testValue;
- this._result = defaultResult;
- }
- public SwitchContext<T, TResult> Case(T caseValue, TResult caseResult)
- {
- return Case(v => caseValue.SafeEquals(v), v => caseResult);
- }
- public SwitchContext<T, TResult> Case(T caseValue, Func<T, TResult> caseEvaluator)
- {
- return Case(v => caseValue.SafeEquals(v), caseEvaluator);
- }
- public SwitchContext<T, TResult> Case(Func<T, bool> casePredicate, TResult caseResult)
- {
- return Case(casePredicate, v => caseResult);
- }
- public SwitchContext<T, TResult> Case(Func<T, bool> casePredicate, Func<T, TResult> caseEvaluator)
- {
- if (_matchFound)
- return this;
- if (casePredicate(_testValue))
- {
- _result = caseEvaluator(_testValue);
- _matchFound = true;
- }
- return this;
- }
- public SwitchContext<T, TResult> Else(TResult defaultResult)
- {
- return Else(v => defaultResult);
- }
- public SwitchContext<T, TResult> Else(Func<T, TResult> defaultEvaluator)
- {
- if (_matchFound)
- return this;
- _result = defaultEvaluator(_testValue);
- _matchFound = true;
- return this;
- }
- public SwitchContext<T, TResult> ElseThrow()
- {
- if (_matchFound)
- return this;
- throw new NoMatchFoundException();
- }
- public SwitchContext<T, TResult> ElseThrow(string message)
- {
- if (_matchFound)
- return this;
- throw new NoMatchFoundException(message);
- }
- public TResult Result
- {
- get { return _result; }
- }
- public static implicit operator TResult(SwitchContext<T, TResult> context)
- {
- return context._result;
- }
- [Serializable]
- public class NoMatchFoundException : Exception
- {
- public NoMatchFoundException() : base("No matching case was found.") { }
- public NoMatchFoundException(string message) : base(message) { }
- public NoMatchFoundException(string message, Exception inner) : base(message, inner) { }
- protected NoMatchFoundException(
- System.Runtime.Serialization.SerializationInfo info,
- System.Runtime.Serialization.StreamingContext context)
- : base(info, context) { }
- }
- }
- }
- public class PokerCard
- {
- public PokerCard(int value, PokerSuit suit)
- {
- this.Value = value;
- this.Suit = suit;
- }
- public int Value { get; private set; }
- public PokerSuit Suit { get; private set; }
- public static PokerCard Parse(string s)
- {
- char cValue = s[0];
- char cSuit = s[1];
- int value = cValue.Switch<char, int>()
- .Case('T', 10)
- .Case('J', 11)
- .Case('Q', 12)
- .Case('K', 13)
- .Case('A', 1)
- .Case(c => char.IsDigit(c), c => int.Parse(c.ToString()))
- .ElseThrow("Invalid card value");
- PokerSuit suit = cSuit.Switch<char, PokerSuit>()
- .Case('S', PokerSuit.Spades)
- .Case('H', PokerSuit.Hearts)
- .Case('D', PokerSuit.Diamonds)
- .Case('C', PokerSuit.Clubs)
- .ElseThrow("Invalid card suit");
- return new PokerCard(value, suit);
- }
- }
- public enum PokerSuit { Spades, Hearts, Diamonds, Clubs }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement