Guest User

Untitled

a guest
Oct 24th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. /// <summary>
  2. /// Interface definition of Either
  3. /// </summary>
  4. /// <typeparam name="Tl">type of the Left value</typeparam>
  5. /// <typeparam name="Tr">type of the Right value</typeparam>
  6. public interface IEither<out Tl, out Tr>
  7. {
  8. /// <summary>
  9. /// Check the type of the value held and invoke the matching handler function
  10. /// </summary>
  11. /// <typeparam name="U">the return type of the handler functions</typeparam>
  12. /// <param name="ofLeft">handler for the Left type</param>
  13. /// <param name="ofRight">handler for the Right type</param>
  14. /// <returns>the value returned by the invoked handler function</returns>
  15. U Case<U>(Func<Tl, U> ofLeft, Func<Tr, U> ofRight);
  16.  
  17. /// <summary>
  18. /// Check the type of the value held and invoke the matching handler function
  19. /// </summary>
  20. /// <param name="ofLeft">handler for the Left type</param>
  21. /// <param name="ofRight">handler for the Right type</param>
  22. void Case(Action<Tl> ofLeft, Action<Tr> ofRight);
  23. }
  24.  
  25. /// <summary>
  26. /// Static helper class for Either
  27. /// </summary>
  28. public static class Either
  29. {
  30. private sealed class LeftImpl<Tl, Tr> : IEither<Tl, Tr>
  31. {
  32. private readonly Tl value;
  33.  
  34. public LeftImpl(Tl value)
  35. {
  36. this.value = value;
  37. }
  38.  
  39. public U Case<U>(Func<Tl, U> ofLeft, Func<Tr, U> ofRight)
  40. {
  41. if (ofLeft == null)
  42. throw new ArgumentNullException("ofLeft");
  43.  
  44. return ofLeft(value);
  45. }
  46.  
  47. public void Case(Action<Tl> ofLeft, Action<Tr> ofRight)
  48. {
  49. if (ofLeft == null)
  50. throw new ArgumentNullException("ofLeft");
  51.  
  52. ofLeft(value);
  53. }
  54. }
  55.  
  56. private sealed class RightImpl<Tl, Tr> : IEither<Tl, Tr>
  57. {
  58. private readonly Tr value;
  59.  
  60. public RightImpl(Tr value)
  61. {
  62. this.value = value;
  63. }
  64.  
  65. public U Case<U>(Func<Tl, U> ofLeft, Func<Tr, U> ofRight)
  66. {
  67. if (ofRight == null)
  68. throw new ArgumentNullException("ofRight");
  69.  
  70. return ofRight(value);
  71. }
  72.  
  73. public void Case(Action<Tl> ofLeft, Action<Tr> ofRight)
  74. {
  75. if (ofRight == null)
  76. throw new ArgumentNullException("ofRight");
  77.  
  78. ofRight(value);
  79. }
  80. }
  81.  
  82. /// <summary>
  83. /// Create an Either with Left value
  84. /// </summary>
  85. /// <typeparam name="Tl">type of the Left value</typeparam>
  86. /// <typeparam name="Tr">type of the Right value</typeparam>
  87. /// <param name="value">the value to hold</param>
  88. /// <returns>an Either with the specified Left value</returns>
  89. public static IEither<Tl, Tr> Left<Tl, Tr>(Tl value)
  90. {
  91. return new LeftImpl<Tl, Tr>(value);
  92. }
  93.  
  94. /// <summary>
  95. /// Create an Either with Right value
  96. /// </summary>
  97. /// <typeparam name="Tl">type of the Left value</typeparam>
  98. /// <typeparam name="Tr">type of the Right value</typeparam>
  99. /// <param name="value">the value to hold</param>
  100. /// <returns>an Either with the specified Right value</returns>
  101. public static IEither<Tl, Tr> Right<Tl, Tr>(Tr value)
  102. {
  103. return new RightImpl<Tl, Tr>(value);
  104. }
  105.  
  106. /// <summary>
  107. /// Create an Either with the specified value
  108. /// </summary>
  109. /// <typeparam name="Tl">type of the Left value</typeparam>
  110. /// <typeparam name="Tr">type of the Right value</typeparam>
  111. /// <param name="value">the value to hold</param>
  112. /// <returns>an Either with the specified value</returns>
  113. public static IEither<Tl, Tr> Create<Tl, Tr>(Tl value)
  114. {
  115. return new LeftImpl<Tl, Tr>(value);
  116. }
  117.  
  118. /// <summary>
  119. /// Create an Either with the specified value
  120. /// </summary>
  121. /// <typeparam name="Tl">type of the Left value</typeparam>
  122. /// <typeparam name="Tr">type of the Right value</typeparam>
  123. /// <param name="value">the value to hold</param>
  124. /// <returns>an Either with the specified value</returns>
  125. public static IEither<Tl, Tr> Create<Tl, Tr>(Tr value)
  126. {
  127. return new RightImpl<Tl, Tr>(value);
  128. }
  129. }
Add Comment
Please, Sign In to add comment