Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. public struct Maybe<A>
  2. {
  3. private const bool TAG_NOTHING = default(bool);
  4.  
  5. private const bool TAG_JUST = !default(bool);
  6.  
  7. private readonly bool Tag;
  8.  
  9. private readonly A Value;
  10.  
  11. public Maybe(A value)
  12. {
  13. Tag = TAG_JUST;
  14. Value = value;
  15. }
  16.  
  17. public bool IsJust { get { return Tag == TAG_JUST; } }
  18.  
  19. public bool IsNothing { get { return Tag == TAG_NOTHING; } }
  20.  
  21. public static bool operator ==(Maybe<A> a, Maybe<A> b)
  22. {
  23. return Equals(a, b);
  24. }
  25.  
  26. public static bool operator !=(Maybe<A> a, Maybe<A> b)
  27. {
  28. return !Equals(a, b);
  29. }
  30.  
  31. public Maybe<B> Map<B>(Func<A,B> f)
  32. {
  33. switch (Tag)
  34. {
  35. case TAG_JUST:
  36. return new Maybe<B>(f(Value));
  37. case TAG_NOTHING:
  38. return new Maybe<B>();
  39. default:
  40. throw new CaseException(Tag, typeof(Maybe<A>));
  41. }
  42. }
  43.  
  44. public Maybe<B> Bind<B>(Func<A,Maybe<B>> f)
  45. {
  46. switch (Tag)
  47. {
  48. case TAG_JUST:
  49. return f(Value);
  50. case TAG_NOTHING:
  51. return new Maybe<B>();
  52. default:
  53. throw new CaseException(Tag, typeof(Maybe<A>));
  54. }
  55. }
  56.  
  57. public B Cases<B>(Func<B> nothingCase, Func<A,B> justCase)
  58. {
  59. switch (Tag)
  60. {
  61. case TAG_JUST:
  62. return justCase(Value);
  63. case TAG_NOTHING:
  64. return nothingCase();
  65. default:
  66. throw new CaseException(Tag, typeof(Maybe<A>));
  67. }
  68. }
  69.  
  70. public override bool Equals(object obj)
  71. {
  72. if (!(obj is Maybe<A>))
  73. {
  74. return false;
  75. }
  76. var b = (Maybe<A>)obj;
  77. switch (Tag)
  78. {
  79. case TAG_JUST:
  80. switch (b.Tag)
  81. {
  82. case TAG_JUST:
  83. return Equals(Value, b.Value);
  84. case TAG_NOTHING:
  85. return false;
  86. default:
  87. throw new CaseException(Tag, typeof(Maybe<A>));
  88. }
  89. case TAG_NOTHING:
  90. switch (b.Tag)
  91. {
  92. case TAG_JUST:
  93. return false;
  94. case TAG_NOTHING:
  95. return true;
  96. default:
  97. throw new CaseException(Tag, typeof(Maybe<A>));
  98. }
  99. default:
  100. throw new CaseException(Tag, typeof(Maybe<A>));
  101. }
  102. }
  103.  
  104. public override int GetHashCode()
  105. {
  106. switch (Tag)
  107. {
  108. case TAG_JUST:
  109. return Value == null ? 0 : Value.GetHashCode();
  110. case TAG_NOTHING:
  111. return ~0;
  112. default:
  113. throw new CaseException(Tag, typeof(Maybe<A>));
  114. }
  115. }
  116.  
  117. public override string ToString()
  118. {
  119. switch (Tag)
  120. {
  121. case TAG_JUST:
  122. return $"Just({Value})";
  123. case TAG_NOTHING:
  124. return $"Nothing<{typeof(A).FullName}>()";
  125. default:
  126. throw new CaseException(Tag, typeof(Maybe<A>));
  127. }
  128. }
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement