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

Commit 047ba90

Browse files
committed
Cleanup API
1 parent dd3db71 commit 047ba90

File tree

2 files changed

+25
-57
lines changed

2 files changed

+25
-57
lines changed

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

+24-56
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package com.ckkloverdos.maybe
1919
import collection.Iterator
2020

2121
/**
22-
* Inspired by Lift's `Box`, Haskell's `Maybe` and Scala's `Option`.
22+
* Inspired by Lift's `Box`, Haskell's `Maybe` and Scala's [[scala.Option]].
2323
*
2424
* @author Christos KK Loverdos <loverdos@gmail.com>
2525
*/
@@ -29,6 +29,8 @@ sealed abstract class Maybe[+A] extends Serializable {
2929
def toOption: Option[A]
3030
def toList: List[A]
3131
def toStream: Stream[A]
32+
def toMaybeOption: MaybeOption[A]
33+
def toMaybeEither: MaybeEither[A]
3234

3335
def isJust: Boolean
3436
def isNoVal: Boolean
@@ -42,27 +44,21 @@ sealed abstract class Maybe[+A] extends Serializable {
4244

4345
def flatMap[B](f: A Maybe[B]): Maybe[B]
4446

45-
def mapJust[B >: A](f: Just[A] Maybe[B]): Maybe[B]
46-
47-
def mapNoVal[B >: A](f: Maybe[B]): Maybe[B]
48-
49-
def mapFailed[B >: A](f: Failed Maybe[B]): Maybe[B]
50-
5147
/**
5248
* Map or return the provided default value.
5349
*/
5450
@inline
55-
def defaultMap[B](default: B)(f: A B): B = map(f) getOr default
51+
def mapDefault[B](default: B)(f: A B): B = map(f) getOr default
5652

5753
/**
5854
* Use this value of type `A` to provide another one of type `B` and then
5955
* do some cleanup on the original value of type `A`.
6056
*
6157
* Use case: Get a DB cursor, perform calculations based on that and then close the cursor.
6258
*/
63-
def finallyMap[B](_finally: A Unit)(f: A B): Maybe[B]
59+
def mapFinally[B](_finally: A Unit)(f: A B): Maybe[B]
6460

65-
def finallyFlatMap[B](_finally: A Unit)(f: A Maybe[B]): Maybe[B]
61+
def flatMapFinally[B](_finally: A Unit)(f: A Maybe[B]): Maybe[B]
6662

6763
@inline
6864
final def >>[B](f: A Maybe[B]): Maybe[B] = this flatMap f
@@ -89,7 +85,7 @@ sealed abstract class Maybe[+A] extends Serializable {
8985
/**
9086
* If this is a [[com.ckkloverdos.maybe.Failed]], throw its exception. Otherwise do nothing.
9187
*/
92-
def throwMe: Unit
88+
def throwMe(): Unit
9389
}
9490

9591
/**
@@ -131,6 +127,8 @@ object Maybe {
131127
/**
132128
* This is a polymorphic constructor for Maybes.
133129
* Use it if you are not sure what to expect from `x`.
130+
*
131+
* All exceptions but [[java.lang.Error]]s are caught.
134132
*/
135133
def apply[A](x: A): Maybe[A] = {
136134
try {
@@ -155,6 +153,8 @@ final case class Just[+A](get: A) extends MaybeOption[A] with MaybeEither[A] {
155153
def toOption = Some(get)
156154
def toList = List(get)
157155
def toStream = Stream.cons(get, Stream.Empty)
156+
def toMaybeOption = this
157+
def toMaybeEither = this
158158

159159
def toEither: Either[Throwable, A] = Right(get)
160160

@@ -164,27 +164,12 @@ final case class Just[+A](get: A) extends MaybeOption[A] with MaybeEither[A] {
164164

165165
def getOr[B >: A](b: B) = get
166166

167-
def mapJust[B >: A](f: (Just[A]) => Maybe[B]) = {
168-
try f(this)
169-
catch {
170-
case e: Error
171-
throw e
172-
173-
case e: Throwable
174-
Failed(e)
175-
}
176-
}
177-
178-
def mapNoVal[B >: A](f: => Maybe[B]) = this
179-
180-
def mapFailed[B >: A](f: (Failed) => Maybe[B]) = this
181-
182-
def finallyMap[B](_finally: A Unit)(f: A B): Maybe[B] = {
167+
def mapFinally[B](_finally: A Unit)(f: A B): Maybe[B] = {
183168
try this.map(f)
184169
finally { safeUnit(_finally(get)) }
185170
}
186171

187-
def finallyFlatMap[B](_finally: A Unit)(f: A Maybe[B]): Maybe[B] = {
172+
def flatMapFinally[B](_finally: A Unit)(f: A Maybe[B]): Maybe[B] = {
188173
try this.flatMap(f)
189174
finally { safeUnit(_finally(get)) }
190175
}
@@ -221,7 +206,7 @@ final case class Just[+A](get: A) extends MaybeOption[A] with MaybeEither[A] {
221206

222207
def flatten1[U](implicit ev: A <:< Maybe[U]): Maybe[U] = ev(get)
223208

224-
def throwMe = {}
209+
def throwMe() = {}
225210

226211
override def equals(that: Any) =
227212
that.isInstanceOf[Just[_]] && that.asInstanceOf[Just[_]].get == this.get
@@ -233,19 +218,15 @@ case object NoVal extends MaybeOption[Nothing] {
233218
def toOption = None
234219
def toList = Maybe.MaybeEmptyList
235220
def toStream = Stream.Empty
221+
def toMaybeOption = this
222+
def toMaybeEither = Failed(new Exception("No value"))
236223

237224
def isJust = false
238225
def isNoVal = true
239226
def isFailed = false
240227

241228
def getOr[B >: Nothing](b: B) = b
242229

243-
def mapJust[B >: Nothing](f: (Just[Nothing]) => Maybe[B]) = this
244-
245-
def mapNoVal[B >: Nothing](f: => Maybe[B]) = f
246-
247-
def mapFailed[B >: Nothing](f: (Failed) => Maybe[B]) = this
248-
249230
def fold[T](onJust: (Nothing) T)(onNoVal: T)(onFailed: (Failed) T) = onNoVal
250231

251232
def ||[B >: Nothing](f: Maybe[B]) = f
@@ -255,13 +236,13 @@ case object NoVal extends MaybeOption[Nothing] {
255236
def filter(f: (Nothing) Boolean) = NoVal
256237
def foreach[U](f: Nothing U) = {}
257238

258-
def finallyMap[B](_finally: (Nothing) Unit)(f: (Nothing) B) = this
239+
def mapFinally[B](_finally: (Nothing) Unit)(f: (Nothing) B) = this
259240

260-
def finallyFlatMap[B](_finally: (Nothing) Unit)(f: (Nothing) Maybe[B]) = this
241+
def flatMapFinally[B](_finally: (Nothing) Unit)(f: (Nothing) Maybe[B]) = this
261242

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

264-
def throwMe = {}
245+
def throwMe() = {}
265246

266247
override def equals(that: Any) = that.asInstanceOf[AnyRef] eq NoVal
267248
}
@@ -281,26 +262,13 @@ final case class Failed(exception: Throwable) extends MaybeEither[Nothing] {
281262
def toOption = None
282263
def toList = Maybe.MaybeEmptyList
283264
def toStream = Stream.Empty
265+
def toMaybeOption = NoVal
266+
def toMaybeEither = this
284267

285268
def toEither: Either[Throwable, Nothing] = Left(exception)
286269

287270
def getOr[B >: Nothing](b: B) = b
288271

289-
def mapJust[B >: Nothing](f: (Just[Nothing]) => Maybe[B]) = this
290-
291-
def mapNoVal[B >: Nothing](f: => Maybe[B]) = this
292-
293-
def mapFailed[B >: Nothing](f: (Failed) => Maybe[B]) = {
294-
try f(this)
295-
catch {
296-
case e: Error
297-
throw e
298-
299-
case e: Throwable
300-
Failed(e)
301-
}
302-
}
303-
304272
def fold[T](onJust: (Nothing) T)(onNoVal: T)(onFailed: (Failed) T) = onFailed(this)
305273

306274
def ||[B >: Nothing](f: Maybe[B]) = f
@@ -310,13 +278,13 @@ final case class Failed(exception: Throwable) extends MaybeEither[Nothing] {
310278
def filter(f: (Nothing) Boolean) = this
311279
def foreach[U](f: Nothing U) = {}
312280

313-
def finallyMap[B](_finally: (Nothing) Unit)(f: (Nothing) B) = this
281+
def mapFinally[B](_finally: (Nothing) Unit)(f: (Nothing) B) = this
314282

315-
def finallyFlatMap[B](_finally: (Nothing) Unit)(f: (Nothing) Maybe[B]) = this
283+
def flatMapFinally[B](_finally: (Nothing) Unit)(f: (Nothing) Maybe[B]) = this
316284

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

319-
def throwMe = throw exception
287+
def throwMe() = throw exception
320288

321289
override def equals(that: Any) = {
322290
that match {

Diff for: src/test/scala/com/ckkloverdos/maybe/MaybeTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class MaybeTest {
111111
def newCursor = new TesterCursor
112112
}
113113

114-
Maybe(new Tester).map(_.newCursor).finallyMap(_.close())(_.doit())
114+
Maybe(new Tester).map(_.newCursor).mapFinally(_.close())(_.doit())
115115

116116
Assert.assertEquals(true, _flag1)
117117
Assert.assertEquals(true, _flag2)

0 commit comments

Comments
 (0)