Skip to content
This repository was archived by the owner on Aug 17, 2019. It is now read-only.

Commit 4cd57d8

Browse files
committed
A few API enhancements
1 parent 3a56018 commit 4cd57d8

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

Diff for: src/main/scala/com/ckkloverdos/maybe/Maybe.scala

+43-7
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.ckkloverdos.maybe
1818

19-
import collection.Iterator
2019
import com.ckkloverdos.manifest.ManifestHelpers
20+
import collection.{Iterator}
2121

2222
/**
2323
* Inspired by Lift's `Box`, Haskell's `Maybe` and Scala's `Option`.
@@ -29,6 +29,7 @@ sealed abstract class Maybe[+A] extends Serializable {
2929
def toTraversable: Traversable[A]
3030
def toOption: Option[A]
3131
def toList: List[A]
32+
def toStream: Stream[A]
3233

3334
def isJust: Boolean
3435
def isNoVal: Boolean
@@ -46,14 +47,32 @@ sealed abstract class Maybe[+A] extends Serializable {
4647
final def mapJust[B >: A](f: Just[A] Maybe[B]): Maybe[B] =
4748
if(isJust) f(this.asInstanceOf[Just[A]]) else this
4849

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+
4956
@inline
5057
final def mapNoVal[B >: A](f: Maybe[B]): Maybe[B] =
5158
if(isNoVal) f else this
5259

60+
@inline
61+
final def forNoVal[U](f: U): this.type = {
62+
if(isNoVal) f
63+
this
64+
}
65+
5366
@inline
5467
final def mapFailed[B >: A](f: Failed Maybe[B]): Maybe[B] =
5568
if(isFailed) f(this.asInstanceOf[Failed]) else this
5669

70+
@inline
71+
final def forFailed[U](f: Failed U): this.type = {
72+
if(isFailed) f(this.asInstanceOf[Failed])
73+
this
74+
}
75+
5776
/**
5877
* Map or return the provided default value.
5978
*/
@@ -75,13 +94,13 @@ sealed abstract class Maybe[+A] extends Serializable {
7594

7695
def filter(f: A Boolean): Maybe[A]
7796

78-
def foreach(f: A Unit): Unit
97+
def foreach[U](f: A U): Unit
7998

8099
def fold[T](onJust: (A) T)(onNoVal: T)(onFailed: (Failed) T): T
81100

82101
@inline
83102
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)
85104

86105
@inline
87106
final def foldJust[T](onJust: (A) T)(onOther: T): T =
@@ -93,6 +112,15 @@ sealed abstract class Maybe[+A] extends Serializable {
93112
* Flattens two successive maybes to one.
94113
*/
95114
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
96124
}
97125

98126
/**
@@ -151,6 +179,7 @@ final case class Just[+A](get: A) extends MaybeOption[A] with MaybeEither[A] {
151179
def toTraversable = Traversable(get)
152180
def toOption = Some(get)
153181
def toList = List(get)
182+
def toStream = Stream.cons(get, Stream.Empty)
154183

155184
def isJust = true
156185
def isFailed = false
@@ -187,7 +216,7 @@ final case class Just[+A](get: A) extends MaybeOption[A] with MaybeEither[A] {
187216
case t: Throwable Failed(t)
188217
}
189218
}
190-
def foreach(f: A Unit) = f(get)
219+
def foreach[U](f: A U) = f(get)
191220

192221
def castTo[B : Manifest] = get match {
193222
case null
@@ -206,6 +235,8 @@ final case class Just[+A](get: A) extends MaybeOption[A] with MaybeEither[A] {
206235

207236
def flatten1[U](implicit ev: A <:< Maybe[U]): Maybe[U] = ev(get)
208237

238+
def throwMe = throw new Exception(toString)
239+
209240
override def equals(that: Any) =
210241
that.isInstanceOf[Just[_]] && that.asInstanceOf[Just[_]].get == this.get
211242
}
@@ -215,6 +246,7 @@ case object NoVal extends MaybeOption[Nothing] {
215246
def toTraversable = Maybe.MaybeEmptyTraversable
216247
def toOption = None
217248
def toList = Maybe.MaybeEmptyList
249+
def toStream = Stream.Empty
218250

219251
def isJust = false
220252
def isNoVal = true
@@ -229,7 +261,7 @@ case object NoVal extends MaybeOption[Nothing] {
229261
def map[B](f: (Nothing) B)= NoVal
230262
def flatMap[B](f: (Nothing) Maybe[B]) = NoVal
231263
def filter(f: (Nothing) Boolean) = NoVal
232-
def foreach(f: Nothing Unit) = {}
264+
def foreach[U](f: Nothing U) = {}
233265

234266
def finallyMap[B](_finally: (Nothing) Unit)(f: (Nothing) B) = this
235267

@@ -239,6 +271,8 @@ case object NoVal extends MaybeOption[Nothing] {
239271

240272
def flatten1[U](implicit ev: <:<[Nothing, Maybe[U]]) = this
241273

274+
def throwMe = throw new Exception(toString)
275+
242276
override def equals(that: Any) = that.asInstanceOf[AnyRef] eq NoVal
243277
}
244278

@@ -256,7 +290,7 @@ final case class Failed(exception: Throwable) extends MaybeEither[Nothing] {
256290
def toTraversable = Maybe.MaybeEmptyTraversable
257291
def toOption = None
258292
def toList = Maybe.MaybeEmptyList
259-
def toSet = Maybe.MaybeEmptySet
293+
def toStream = Stream.Empty
260294

261295
def getOr[B >: Nothing](b: B) = b
262296

@@ -267,7 +301,7 @@ final case class Failed(exception: Throwable) extends MaybeEither[Nothing] {
267301
def map[B](f: (Nothing) B) = this
268302
def flatMap[B](f: (Nothing) Maybe[B]) = this
269303
def filter(f: (Nothing) Boolean) = this
270-
def foreach(f: Nothing Unit) = {}
304+
def foreach[U](f: Nothing U) = {}
271305

272306
def finallyMap[B](_finally: (Nothing) Unit)(f: (Nothing) B) = this
273307

@@ -277,6 +311,8 @@ final case class Failed(exception: Throwable) extends MaybeEither[Nothing] {
277311

278312
def flatten1[U](implicit ev: <:<[Nothing, Maybe[U]]) = this
279313

314+
def throwMe = throw exception
315+
280316
override def equals(that: Any) = {
281317
that match {
282318
case failed: Failed

0 commit comments

Comments
 (0)