Advertisement
Guest User

Untitled

a guest
May 26th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.87 KB | None | 0 0
  1.     public static class SwitchExtensions
  2.     {
  3.  
  4.         public static SwitchContext<T, TResult> Switch<T, TResult>(this T testValue, TResult defaultResult)
  5.         {
  6.             return new SwitchContext<T, TResult>(testValue, defaultResult);
  7.         }
  8.  
  9.         public static SwitchContext<T, TResult> Switch<T, TResult>(this T testValue)
  10.         {
  11.             return new SwitchContext<T, TResult>(testValue, default(TResult));
  12.         }
  13.  
  14.         public class SwitchContext<T, TResult>
  15.         {
  16.             private T _testValue;
  17.             private TResult _result;
  18.             private bool _matchFound = false;
  19.  
  20.             internal SwitchContext(T testValue, TResult defaultResult)
  21.             {
  22.                 this._testValue = testValue;
  23.                 this._result = defaultResult;
  24.             }
  25.  
  26.             public SwitchContext<T, TResult> Case(T caseValue, TResult caseResult)
  27.             {
  28.                 return Case(v => caseValue.SafeEquals(v), v => caseResult);
  29.             }
  30.  
  31.             public SwitchContext<T, TResult> Case(T caseValue, Func<T, TResult> caseEvaluator)
  32.             {
  33.                 return Case(v => caseValue.SafeEquals(v), caseEvaluator);
  34.             }
  35.  
  36.             public SwitchContext<T, TResult> Case(Func<T, bool> casePredicate, TResult caseResult)
  37.             {
  38.                 return Case(casePredicate, v => caseResult);
  39.             }
  40.  
  41.             public SwitchContext<T, TResult> Case(Func<T, bool> casePredicate, Func<T, TResult> caseEvaluator)
  42.             {
  43.                 if (_matchFound)
  44.                     return this;
  45.  
  46.                 if (casePredicate(_testValue))
  47.                 {
  48.                     _result = caseEvaluator(_testValue);
  49.                     _matchFound = true;
  50.                 }
  51.                 return this;
  52.             }
  53.  
  54.             public SwitchContext<T, TResult> Else(TResult defaultResult)
  55.             {
  56.                 return Else(v => defaultResult);
  57.             }
  58.  
  59.             public SwitchContext<T, TResult> Else(Func<T, TResult> defaultEvaluator)
  60.             {
  61.                 if (_matchFound)
  62.                     return this;
  63.  
  64.                 _result = defaultEvaluator(_testValue);
  65.                 _matchFound = true;
  66.  
  67.                 return this;
  68.             }
  69.  
  70.             public SwitchContext<T, TResult> ElseThrow()
  71.             {
  72.                 if (_matchFound)
  73.                     return this;
  74.  
  75.                 throw new NoMatchFoundException();
  76.             }
  77.  
  78.             public SwitchContext<T, TResult> ElseThrow(string message)
  79.             {
  80.                 if (_matchFound)
  81.                     return this;
  82.  
  83.                 throw new NoMatchFoundException(message);
  84.             }
  85.  
  86.             public TResult Result
  87.             {
  88.                 get { return _result; }
  89.             }
  90.  
  91.             public static implicit operator TResult(SwitchContext<T, TResult> context)
  92.             {
  93.                 return context._result;
  94.             }
  95.  
  96.             [Serializable]
  97.             public class NoMatchFoundException : Exception
  98.             {
  99.                 public NoMatchFoundException() : base("No matching case was found.") { }
  100.                 public NoMatchFoundException(string message) : base(message) { }
  101.                 public NoMatchFoundException(string message, Exception inner) : base(message, inner) { }
  102.                 protected NoMatchFoundException(
  103.                   System.Runtime.Serialization.SerializationInfo info,
  104.                   System.Runtime.Serialization.StreamingContext context)
  105.                     : base(info, context) { }
  106.             }
  107.         }
  108.  
  109.     }
  110.  
  111.     public class PokerCard
  112.     {
  113.         public PokerCard(int value, PokerSuit suit)
  114.         {
  115.             this.Value = value;
  116.             this.Suit = suit;
  117.         }
  118.  
  119.         public int Value { get; private set; }
  120.         public PokerSuit Suit { get; private set; }
  121.  
  122.         public static PokerCard Parse(string s)
  123.         {
  124.             char cValue = s[0];
  125.             char cSuit = s[1];
  126.  
  127.             int value = cValue.Switch<char, int>()
  128.                         .Case('T', 10)
  129.                         .Case('J', 11)
  130.                         .Case('Q', 12)
  131.                         .Case('K', 13)
  132.                         .Case('A', 1)
  133.                         .Case(c => char.IsDigit(c), c => int.Parse(c.ToString()))
  134.                         .ElseThrow("Invalid card value");
  135.  
  136.             PokerSuit suit = cSuit.Switch<char, PokerSuit>()
  137.                             .Case('S', PokerSuit.Spades)
  138.                             .Case('H', PokerSuit.Hearts)
  139.                             .Case('D', PokerSuit.Diamonds)
  140.                             .Case('C', PokerSuit.Clubs)
  141.                             .ElseThrow("Invalid card suit");
  142.  
  143.             return new PokerCard(value, suit);
  144.         }
  145.     }
  146.    
  147.     public enum PokerSuit { Spades, Hearts, Diamonds, Clubs }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement