@@ -26,10 +26,25 @@ module Monads
2626 Right (Nil , T ).new(value)
2727 end
2828
29- abstract def value_or (element : U ) forall U
30- abstract def value_or (lambda : E - > U ) forall U
31- abstract def or (monad : Either )
32- abstract def or (lambda : E - > U ) forall U
29+ # Fold/match the Either: applies right_fn if Right, left_fn if Left
30+ # Returns the result of whichever function was applied
31+ #
32+ # Example:
33+ # result.fold(
34+ # ->(account : Account) { json_response(account) },
35+ # ->(error : AuthError) { error_response(error) }
36+ # )
37+ abstract def fold (right_fn : T - > U , left_fn : E - > U ) forall U
38+
39+ # Block version of fold
40+ def fold (& block : T - > U ) forall U
41+ fold(block, - > (e : E ) { raise " Called fold on Left" })
42+ end
43+
44+ abstract def value_or (other : U ) forall U
45+ abstract def value_or (other : E - > U ) forall U
46+ abstract def or (other : Either )
47+ abstract def or (other : E - > U ) forall U
3348 abstract def <=> (other : Right )
3449 abstract def <=> (other : Left )
3550 abstract def map_or (default : U , lambda : T - > U ) forall U
@@ -51,6 +66,10 @@ module Monads
5166 @data
5267 end
5368
69+ def fold (right_fn : T - > U , left_fn : E - > U ) forall U
70+ right_fn.call(@data )
71+ end
72+
5473 def fmap (lambda : T - > U ) : Right (E , U ) forall U
5574 Right (E , U ).new(lambda.call(@data ))
5675 end
@@ -67,15 +86,19 @@ module Monads
6786 1
6887 end
6988
70- def value_or (element : _)
89+ def value_or (other : _)
90+ value!
91+ end
92+
93+ def value_or (other : E - > _)
7194 value!
7295 end
7396
74- def or (monad : Either )
97+ def or (other : Either )
7598 self
7699 end
77100
78- def or (lambda : _ - > _) : Right (E , T )
101+ def or (other : _ - > _) : Right (E , T )
79102 self
80103 end
81104
@@ -97,20 +120,20 @@ module Monads
97120 self .as(Leftable (E , U ))
98121 end
99122
100- def value_or (lambda : E - > _)
101- lambda .call(@data )
123+ def value_or (other : E - > _)
124+ other .call(@data )
102125 end
103126
104- def value_or (element : U ) forall U
105- element
127+ def value_or (other : U ) forall U
128+ other
106129 end
107130
108- def or (monad : Either )
109- monad
131+ def or (other : Either )
132+ other
110133 end
111134
112- def or (lambda : E - > _)
113- lambda .call(@data )
135+ def or (other : E - > _)
136+ other .call(@data )
114137 end
115138
116139 def bind (lambda : T - > _) : Leftable (E , T )
@@ -132,6 +155,10 @@ module Monads
132155 @data
133156 end
134157
158+ def fold (right_fn : T - > U , left_fn : E - > U ) forall U
159+ left_fn.call(@data )
160+ end
161+
135162 def <=> (other : LeftException )
136163 1
137164 end
@@ -152,20 +179,20 @@ module Monads
152179 Left (E , U ).new(@data )
153180 end
154181
155- def value_or (lambda : E - > _)
156- lambda .call(@data )
182+ def value_or (other : E - > _)
183+ other .call(@data )
157184 end
158185
159- def value_or (element : U ) forall U
160- element
186+ def value_or (other : U ) forall U
187+ other
161188 end
162189
163- def or (monad : Either )
164- monad
190+ def or (other : Either )
191+ other
165192 end
166193
167- def or (lambda : E - > _)
168- lambda .call(@data )
194+ def or (other : E - > _)
195+ other .call(@data )
169196 end
170197
171198 def bind (lambda : T - > _) : Left (E , T )
@@ -191,6 +218,10 @@ module Monads
191218 @data
192219 end
193220
221+ def fold (right_fn : T - > U , left_fn : Exception - > U ) forall U
222+ left_fn.call(@data )
223+ end
224+
194225 def <=> (other : LeftException )
195226 if @data .class == other.value!.class
196227 0
@@ -215,20 +246,20 @@ module Monads
215246 LeftException (U ).new(@data )
216247 end
217248
218- def value_or (lambda : Exception - > _)
219- lambda .call(@data )
249+ def value_or (other : Exception - > _)
250+ other .call(@data )
220251 end
221252
222- def value_or (element : U ) forall U
223- element
253+ def value_or (other : U ) forall U
254+ other
224255 end
225256
226- def or (monad : Either )
227- monad
257+ def or (other : Either )
258+ other
228259 end
229260
230- def or (lambda : Exception - > _)
231- lambda .call(@data )
261+ def or (other : Exception - > _)
262+ other .call(@data )
232263 end
233264
234265 def bind (lambda : T - > _) : LeftException (T )
0 commit comments