16
16
17
17
package com .ckkloverdos .maybe
18
18
19
- import collection .Iterator
20
19
import com .ckkloverdos .manifest .ManifestHelpers
20
+ import collection .{Iterator }
21
21
22
22
/**
23
23
* Inspired by Lift's `Box`, Haskell's `Maybe` and Scala's `Option`.
@@ -29,6 +29,7 @@ sealed abstract class Maybe[+A] extends Serializable {
29
29
def toTraversable : Traversable [A ]
30
30
def toOption : Option [A ]
31
31
def toList : List [A ]
32
+ def toStream : Stream [A ]
32
33
33
34
def isJust : Boolean
34
35
def isNoVal : Boolean
@@ -46,14 +47,32 @@ sealed abstract class Maybe[+A] extends Serializable {
46
47
final def mapJust [B >: A ](f : Just [A ] ⇒ Maybe [B ]): Maybe [B ] =
47
48
if (isJust) f(this .asInstanceOf [Just [A ]]) else this
48
49
50
+ @ inline
51
+ final def forJust [U ](f : Just [A ] ⇒ U ): this .type = {
52
+ if (isJust) f(this .asInstanceOf [Just [A ]])
53
+ this
54
+ }
55
+
49
56
@ inline
50
57
final def mapNoVal [B >: A ](f : ⇒ Maybe [B ]): Maybe [B ] =
51
58
if (isNoVal) f else this
52
59
60
+ @ inline
61
+ final def forNoVal [U ](f : ⇒ U ): this .type = {
62
+ if (isNoVal) f
63
+ this
64
+ }
65
+
53
66
@ inline
54
67
final def mapFailed [B >: A ](f : Failed ⇒ Maybe [B ]): Maybe [B ] =
55
68
if (isFailed) f(this .asInstanceOf [Failed ]) else this
56
69
70
+ @ inline
71
+ final def forFailed [U ](f : Failed ⇒ U ): this .type = {
72
+ if (isFailed) f(this .asInstanceOf [Failed ])
73
+ this
74
+ }
75
+
57
76
/**
58
77
* Map or return the provided default value.
59
78
*/
@@ -75,13 +94,13 @@ sealed abstract class Maybe[+A] extends Serializable {
75
94
76
95
def filter (f : A ⇒ Boolean ): Maybe [A ]
77
96
78
- def foreach (f : A ⇒ Unit ): Unit
97
+ def foreach [ U ] (f : A ⇒ U ): Unit
79
98
80
99
def fold [T ](onJust : (A ) ⇒ T )(onNoVal : ⇒ T )(onFailed : (Failed ) ⇒ T ): T
81
100
82
101
@ inline
83
102
final def foldUnit (onJust : ⇒ Any )(onNoVal : ⇒ Any )(onFailed : ⇒ Any ): Unit =
84
- fold(a ⇒ onJust)(onNoVal)(f ⇒ onFailed)
103
+ fold(( a : A ) ⇒ onJust)(onNoVal)(f ⇒ onFailed)
85
104
86
105
@ inline
87
106
final def foldJust [T ](onJust : (A ) ⇒ T )(onOther : ⇒ T ): T =
@@ -93,6 +112,15 @@ sealed abstract class Maybe[+A] extends Serializable {
93
112
* Flattens two successive maybes to one.
94
113
*/
95
114
def flatten1 [U ](implicit ev : A <:< Maybe [U ]): Maybe [U ]
115
+
116
+ /**
117
+ * If this is a [[com.ckkloverdos.maybe.Failed ]], throw its exception. Otherwise throw an irrelevant exception.
118
+ *
119
+ * This is syntactic sugar for the cases we already know this is a [[com.ckkloverdos.maybe.Failed ]].
120
+ *
121
+ * @return `Nothing`
122
+ */
123
+ def throwMe : Nothing
96
124
}
97
125
98
126
/**
@@ -151,6 +179,7 @@ final case class Just[+A](get: A) extends MaybeOption[A] with MaybeEither[A] {
151
179
def toTraversable = Traversable (get)
152
180
def toOption = Some (get)
153
181
def toList = List (get)
182
+ def toStream = Stream .cons(get, Stream .Empty )
154
183
155
184
def isJust = true
156
185
def isFailed = false
@@ -187,7 +216,7 @@ final case class Just[+A](get: A) extends MaybeOption[A] with MaybeEither[A] {
187
216
case t : Throwable ⇒ Failed (t)
188
217
}
189
218
}
190
- def foreach (f : A ⇒ Unit ) = f(get)
219
+ def foreach [ U ] (f : A ⇒ U ) = f(get)
191
220
192
221
def castTo [B : Manifest ] = get match {
193
222
case null ⇒
@@ -206,6 +235,8 @@ final case class Just[+A](get: A) extends MaybeOption[A] with MaybeEither[A] {
206
235
207
236
def flatten1 [U ](implicit ev : A <:< Maybe [U ]): Maybe [U ] = ev(get)
208
237
238
+ def throwMe = throw new Exception (toString)
239
+
209
240
override def equals (that : Any ) =
210
241
that.isInstanceOf [Just [_]] && that.asInstanceOf [Just [_]].get == this .get
211
242
}
@@ -215,6 +246,7 @@ case object NoVal extends MaybeOption[Nothing] {
215
246
def toTraversable = Maybe .MaybeEmptyTraversable
216
247
def toOption = None
217
248
def toList = Maybe .MaybeEmptyList
249
+ def toStream = Stream .Empty
218
250
219
251
def isJust = false
220
252
def isNoVal = true
@@ -229,7 +261,7 @@ case object NoVal extends MaybeOption[Nothing] {
229
261
def map [B ](f : (Nothing ) ⇒ B )= NoVal
230
262
def flatMap [B ](f : (Nothing ) ⇒ Maybe [B ]) = NoVal
231
263
def filter (f : (Nothing ) ⇒ Boolean ) = NoVal
232
- def foreach (f : Nothing ⇒ Unit ) = {}
264
+ def foreach [ U ] (f : Nothing ⇒ U ) = {}
233
265
234
266
def finallyMap [B ](_finally : (Nothing ) ⇒ Unit )(f : (Nothing ) ⇒ B ) = this
235
267
@@ -239,6 +271,8 @@ case object NoVal extends MaybeOption[Nothing] {
239
271
240
272
def flatten1 [U ](implicit ev : <:< [Nothing , Maybe [U ]]) = this
241
273
274
+ def throwMe = throw new Exception (toString)
275
+
242
276
override def equals (that : Any ) = that.asInstanceOf [AnyRef ] eq NoVal
243
277
}
244
278
@@ -256,7 +290,7 @@ final case class Failed(exception: Throwable) extends MaybeEither[Nothing] {
256
290
def toTraversable = Maybe .MaybeEmptyTraversable
257
291
def toOption = None
258
292
def toList = Maybe .MaybeEmptyList
259
- def toSet = Maybe . MaybeEmptySet
293
+ def toStream = Stream . Empty
260
294
261
295
def getOr [B >: Nothing ](b : ⇒ B ) = b
262
296
@@ -267,7 +301,7 @@ final case class Failed(exception: Throwable) extends MaybeEither[Nothing] {
267
301
def map [B ](f : (Nothing ) ⇒ B ) = this
268
302
def flatMap [B ](f : (Nothing ) ⇒ Maybe [B ]) = this
269
303
def filter (f : (Nothing ) ⇒ Boolean ) = this
270
- def foreach (f : Nothing ⇒ Unit ) = {}
304
+ def foreach [ U ] (f : Nothing ⇒ U ) = {}
271
305
272
306
def finallyMap [B ](_finally : (Nothing ) ⇒ Unit )(f : (Nothing ) ⇒ B ) = this
273
307
@@ -277,6 +311,8 @@ final case class Failed(exception: Throwable) extends MaybeEither[Nothing] {
277
311
278
312
def flatten1 [U ](implicit ev : <:< [Nothing , Maybe [U ]]) = this
279
313
314
+ def throwMe = throw exception
315
+
280
316
override def equals (that : Any ) = {
281
317
that match {
282
318
case failed : Failed ⇒
0 commit comments