Skip to content

Commit f99e91b

Browse files
committed
IO is rebranded to Attempt
1 parent 5e8132a commit f99e91b

File tree

10 files changed

+178
-177
lines changed

10 files changed

+178
-177
lines changed

benchmark/src/main/scala/io/github/ragazoor/IOBenchmark.scala

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class IOBenchmark {
2121
r.get.isInstanceOf[Success[T]]
2222
}
2323

24-
protected final def await[E <: Throwable, T](result: IO[E, T]): Boolean = {
24+
protected final def await[E <: Throwable, T](result: Attempt[E, T]): Boolean = {
2525
var r: Option[Try[T]] = None
2626
while (r eq None) r = result.value
2727
r.get.isInstanceOf[Success[T]]
@@ -31,7 +31,7 @@ class IOBenchmark {
3131
await(StdFuture.sequence(input.map(StdFuture.successful)))
3232

3333
@Benchmark def resultSequence: Boolean =
34-
await(IO.sequence(input.map(IO.successful)))
34+
await(Attempt.sequence(input.map(Attempt.successful)))
3535

3636
@tailrec private[this] final def futureFlatMapRec(i: Int, f: StdFuture[Int])(implicit
3737
ec: ExecutionContext
@@ -42,14 +42,14 @@ class IOBenchmark {
4242
@Benchmark final def futureFlatMap: Boolean =
4343
await(futureFlatMapRec(recursion, StdFuture.successful(1)))
4444

45-
@tailrec private[this] final def resultFlatMapRec(i: Int, f: IO[Nothing, Int])(implicit
46-
ec: ExecutionContext
47-
): IO[Nothing, Int] =
48-
if (i > 0) resultFlatMapRec(i - 1, f.flatMap(IO.successful)(ec))(ec)
45+
@tailrec private[this] final def resultFlatMapRec(i: Int, f: Attempt[Nothing, Int])(implicit
46+
ec: ExecutionContext
47+
): Attempt[Nothing, Int] =
48+
if (i > 0) resultFlatMapRec(i - 1, f.flatMap(Attempt.successful)(ec))(ec)
4949
else f
5050

5151
@Benchmark final def resultFlatMap: Boolean =
52-
await(resultFlatMapRec(recursion, IO.successful(1)))
52+
await(resultFlatMapRec(recursion, Attempt.successful(1)))
5353

5454
@tailrec private[this] final def futureMapRec(i: Int, f: StdFuture[Int])(implicit
5555
ec: ExecutionContext
@@ -60,30 +60,30 @@ class IOBenchmark {
6060
@Benchmark final def futureMap: Boolean =
6161
await(futureMapRec(recursion, StdFuture.successful(1)))
6262

63-
@tailrec private[this] final def resultMapRec(i: Int, f: IO[Nothing, Int])(implicit
64-
ec: ExecutionContext
65-
): IO[Nothing, Int] =
63+
@tailrec private[this] final def resultMapRec(i: Int, f: Attempt[Nothing, Int])(implicit
64+
ec: ExecutionContext
65+
): Attempt[Nothing, Int] =
6666
if (i > 0) resultMapRec(i - 1, f.map(identity)(ec))(ec)
6767
else f
6868

6969
@Benchmark final def resultMap: Boolean =
70-
await(resultMapRec(recursion, IO.successful(1)))
70+
await(resultMapRec(recursion, Attempt.successful(1)))
7171

7272
@tailrec private[this] final def futureRecoverWithRec(i: Int, f: StdFuture[Int])(implicit
7373
ec: ExecutionContext
7474
): StdFuture[Int] =
7575
if (i > 0) futureRecoverWithRec(i - 1, f.recoverWith(e => StdFuture.failed(e))(ec))(ec)
7676
else f
7777

78-
@tailrec private[this] final def resultMapErrorRec(i: Int, f: IO[RuntimeException, Int])(implicit
79-
ec: ExecutionContext
80-
): IO[RuntimeException, Int] =
78+
@tailrec private[this] final def resultMapErrorRec(i: Int, f: Attempt[RuntimeException, Int])(implicit
79+
ec: ExecutionContext
80+
): Attempt[RuntimeException, Int] =
8181
if (i > 0) resultMapErrorRec(i - 1, f.mapError(identity)(ec))(ec)
8282
else f
8383

8484
@Benchmark final def futureRecover: Boolean =
8585
await(futureRecoverWithRec(recursion, StdFuture.failed[Int](new RuntimeException("Future error"))))
8686

8787
@Benchmark final def resultMapError: Boolean =
88-
await(resultMapErrorRec(recursion, IO.failed(new RuntimeException("Result error"))))
88+
await(resultMapErrorRec(recursion, Attempt.failed(new RuntimeException("Result error"))))
8989
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
package io.github.ragazoor
22

3-
import io.github.ragazoor.IOUtils.{ failedFailure, zipWithTuple2Fun }
3+
import io.github.ragazoor.AttemptUtils.{failedFailure, zipWithTuple2Fun}
44

55
import scala.concurrent.ExecutionContext.parasitic
66
import scala.concurrent.duration.Duration
7-
import scala.concurrent.{ Awaitable, CanAwait, ExecutionContext, Future => StdFuture, TimeoutException }
7+
import scala.concurrent.{Awaitable, CanAwait, ExecutionContext, TimeoutException, Future => StdFuture}
88
import scala.util.control.NonFatal
9-
import scala.util.{ Failure, Success, Try }
9+
import scala.util.{Failure, Success, Try}
1010

11-
sealed trait IO[+E <: Throwable, +A] extends Awaitable[A] {
11+
sealed trait Attempt[+E <: Throwable, +A] extends Awaitable[A] {
1212
self =>
1313
def toFuture: StdFuture[A]
1414
val isFatal: Boolean
1515

1616
def value: Option[Try[A]] = self.toFuture.value
1717

18-
def map[B](f: A => B)(implicit ec: ExecutionContext): IO[E, B] =
19-
IO(self.toFuture.transform(_ map f), isFatal)
18+
def map[B](f: A => B)(implicit ec: ExecutionContext): Attempt[E, B] =
19+
Attempt(self.toFuture.transform(_ map f), isFatal)
2020

21-
def mapTry[B](f: A => Try[B])(implicit ec: ExecutionContext): IO[Throwable, B] =
22-
IO(self.toFuture.transform(_ flatMap f), isFatal)
21+
def mapTry[B](f: A => Try[B])(implicit ec: ExecutionContext): Attempt[Throwable, B] =
22+
Attempt(self.toFuture.transform(_ flatMap f), isFatal)
2323

24-
def mapEither[E2 >: E <: Throwable, B](f: A => Either[E2, B])(implicit ec: ExecutionContext): IO[E2, B] =
25-
IO(self.toFuture.transform(_ flatMap (f(_).toTry)), isFatal)
24+
def mapEither[E2 >: E <: Throwable, B](f: A => Either[E2, B])(implicit ec: ExecutionContext): Attempt[E2, B] =
25+
Attempt(self.toFuture.transform(_ flatMap (f(_).toTry)), isFatal)
2626

27-
def flatMap[E2 >: E <: Throwable, B](f: A => IO[E2, B])(implicit ec: ExecutionContext): IO[E2, B] =
28-
IO(self.toFuture.flatMap(f(_).toFuture), isFatal)
27+
def flatMap[E2 >: E <: Throwable, B](f: A => Attempt[E2, B])(implicit ec: ExecutionContext): Attempt[E2, B] =
28+
Attempt(self.toFuture.flatMap(f(_).toFuture), isFatal)
2929

30-
def flatten[E2 >: E <: Throwable, B](implicit ev: A <:< IO[E2, B]): IO[E2, B] =
30+
def flatten[E2 >: E <: Throwable, B](implicit ev: A <:< Attempt[E2, B]): Attempt[E2, B] =
3131
flatMap(ev)(parasitic)
3232

33-
def mapError[E2 <: Throwable](f: E => E2)(implicit ec: ExecutionContext): IO[E2, A] = {
33+
def mapError[E2 <: Throwable](f: E => E2)(implicit ec: ExecutionContext): Attempt[E2, A] = {
3434
var isFutureFatal = isFatal
3535
val transformedFuture = self.toFuture.transform {
3636
case Failure(e) if NonFatal(e) && !isFatal => Failure(f(e.asInstanceOf[E]))
@@ -39,20 +39,20 @@ sealed trait IO[+E <: Throwable, +A] extends Awaitable[A] {
3939
Failure(e)
4040
case success => success
4141
}
42-
IO[E2, A](transformedFuture, isFutureFatal)
42+
Attempt[E2, A](transformedFuture, isFutureFatal)
4343
}
4444

45-
def zip[E2 >: E <: Throwable, B](that: IO[E2, B]): IO[E2, (A, B)] =
45+
def zip[E2 >: E <: Throwable, B](that: Attempt[E2, B]): Attempt[E2, (A, B)] =
4646
zipWith(that)(zipWithTuple2Fun)(parasitic)
4747

48-
def zipWith[E2 >: E <: Throwable, U, R](that: IO[E2, U])(f: (A, U) => R)(implicit
49-
ec: ExecutionContext
50-
): IO[E2, R] =
51-
IO(self.toFuture.zipWith(that.toFuture)(f), isFatal || that.isFatal)
48+
def zipWith[E2 >: E <: Throwable, U, R](that: Attempt[E2, U])(f: (A, U) => R)(implicit
49+
ec: ExecutionContext
50+
): Attempt[E2, R] =
51+
Attempt(self.toFuture.zipWith(that.toFuture)(f), isFatal || that.isFatal)
5252

53-
def catchAll[E2 >: E <: Throwable, A2 >: A](f: E => IO[E2, A2])(implicit
54-
ec: ExecutionContext
55-
): IO[E2, A2] = {
53+
def catchAll[E2 >: E <: Throwable, A2 >: A](f: E => Attempt[E2, A2])(implicit
54+
ec: ExecutionContext
55+
): Attempt[E2, A2] = {
5656
var isFutureFatal = isFatal
5757
val transformedFuture = self.toFuture.transformWith {
5858
case Failure(e) if NonFatal(e) && !isFatal => f(e.asInstanceOf[E]).toFuture
@@ -61,19 +61,19 @@ sealed trait IO[+E <: Throwable, +A] extends Awaitable[A] {
6161
self.toFuture
6262
case _ => self.toFuture
6363
}
64-
IO[E2, A2](transformedFuture, isFutureFatal)
64+
Attempt[E2, A2](transformedFuture, isFutureFatal)
6565
}
6666

67-
def catchSome[E2 >: E <: Throwable, A2 >: A](pf: PartialFunction[E, IO[E2, A2]])(implicit
68-
ec: ExecutionContext
69-
): IO[E2, A2] = {
67+
def catchSome[E2 >: E <: Throwable, A2 >: A](pf: PartialFunction[E, Attempt[E2, A2]])(implicit
68+
ec: ExecutionContext
69+
): Attempt[E2, A2] = {
7070
val transformedFuture = self.toFuture.transformWith {
7171
case Failure(e) if NonFatal(e) && pf.isDefinedAt(e.asInstanceOf[E]) && !isFatal =>
7272
pf(e.asInstanceOf[E]).toFuture
7373
case _ =>
7474
self.toFuture
7575
}
76-
IO[E2, A2](transformedFuture, isFatal)
76+
Attempt[E2, A2](transformedFuture, isFatal)
7777
}
7878

7979
private final def failedFun[B](v: Try[B]): Try[E] =
@@ -83,8 +83,8 @@ sealed trait IO[+E <: Throwable, +A] extends Awaitable[A] {
8383
case Success(_) => failedFailure
8484
}
8585

86-
def failed: IO[NoSuchElementException, E] =
87-
transform(failedFun)(parasitic).asInstanceOf[IO[NoSuchElementException, E]]
86+
def failed: Attempt[NoSuchElementException, E] =
87+
transform(failedFun)(parasitic).asInstanceOf[Attempt[NoSuchElementException, E]]
8888

8989
def foreach[U](f: A => U)(implicit executor: ExecutionContext): Unit =
9090
onComplete(_ foreach f)
@@ -94,13 +94,13 @@ sealed trait IO[+E <: Throwable, +A] extends Awaitable[A] {
9494

9595
def isCompleted: Boolean = self.toFuture.isCompleted
9696

97-
def transform[B](f: Try[A] => Try[B])(implicit executor: ExecutionContext): IO[Throwable, B] =
98-
IO(self.toFuture.transform(f), isFatal)
97+
def transform[B](f: Try[A] => Try[B])(implicit executor: ExecutionContext): Attempt[Throwable, B] =
98+
Attempt(self.toFuture.transform(f), isFatal)
9999

100-
def transformWith[E2 >: E <: Throwable, B](f: Try[A] => IO[E2, B])(implicit
101-
executor: ExecutionContext
102-
): IO[E2, B] =
103-
IO(self.toFuture.transformWith(f(_).toFuture), isFatal)
100+
def transformWith[E2 >: E <: Throwable, B](f: Try[A] => Attempt[E2, B])(implicit
101+
executor: ExecutionContext
102+
): Attempt[E2, B] =
103+
Attempt(self.toFuture.transformWith(f(_).toFuture), isFatal)
104104

105105
@throws(classOf[TimeoutException])
106106
@throws(classOf[InterruptedException])
@@ -114,61 +114,61 @@ sealed trait IO[+E <: Throwable, +A] extends Awaitable[A] {
114114
def result(atMost: Duration)(implicit permit: CanAwait): A =
115115
self.toFuture.result(atMost)
116116

117-
def recover[B >: A](pf: PartialFunction[E, B])(implicit executor: ExecutionContext): IO[E, B] =
117+
def recover[B >: A](pf: PartialFunction[E, B])(implicit executor: ExecutionContext): Attempt[E, B] =
118118
transform(_.recover(pf.asInstanceOf[PartialFunction[Throwable, B]]))
119-
.asInstanceOf[IO[E, B]]
119+
.asInstanceOf[Attempt[E, B]]
120120
}
121121

122-
object IO {
123-
def unapply[E <: Throwable, A](result: IO[E, A]): Option[(StdFuture[A], Boolean)] =
122+
object Attempt {
123+
def unapply[E <: Throwable, A](result: Attempt[E, A]): Option[(StdFuture[A], Boolean)] =
124124
Some((result.toFuture, result.isFatal))
125125

126-
private[ragazoor] final def apply[E <: Throwable, A](future: StdFuture[A], fatal: Boolean): IO[E, A] =
127-
new IO[E, A] {
126+
private[ragazoor] final def apply[E <: Throwable, A](future: StdFuture[A], fatal: Boolean): Attempt[E, A] =
127+
new Attempt[E, A] {
128128
override def toFuture: StdFuture[A] = future
129129
override val isFatal: Boolean = fatal
130130
}
131131

132-
final def apply[A](body: => A)(implicit ec: ExecutionContext): IO[Throwable, A] =
133-
IO[Throwable, A](StdFuture(body), fatal = false)
132+
final def apply[A](body: => A)(implicit ec: ExecutionContext): Attempt[Throwable, A] =
133+
Attempt[Throwable, A](StdFuture(body), fatal = false)
134134

135-
final def fromFuture[A](future: StdFuture[A]): IO[Throwable, A] =
136-
IO(future, fatal = false)
135+
final def fromFuture[A](future: StdFuture[A]): Attempt[Throwable, A] =
136+
Attempt(future, fatal = false)
137137

138-
final def fromEither[E <: Throwable, A](either: Either[E, A]): IO[E, A] =
139-
IO(StdFuture.fromTry(either.toTry), fatal = false)
138+
final def fromEither[E <: Throwable, A](either: Either[E, A]): Attempt[E, A] =
139+
Attempt(StdFuture.fromTry(either.toTry), fatal = false)
140140

141-
final def fromTry[A](result: Try[A]): IO[Throwable, A] =
142-
IO[Throwable, A](StdFuture.fromTry(result), fatal = false)
141+
final def fromTry[A](result: Try[A]): Attempt[Throwable, A] =
142+
Attempt[Throwable, A](StdFuture.fromTry(result), fatal = false)
143143

144-
final def successful[A](result: A): IO[Nothing, A] = {
144+
final def successful[A](result: A): Attempt[Nothing, A] = {
145145
val future = StdFuture.successful(result)
146-
new IO[Nothing, A] {
146+
new Attempt[Nothing, A] {
147147
override def toFuture: StdFuture[A] = future
148148
override val isFatal: Boolean = false
149149
}
150150
}
151151

152-
final def failed[E <: Exception](exception: E): IO[E, Nothing] = {
152+
final def failed[E <: Exception](exception: E): Attempt[E, Nothing] = {
153153
val future = StdFuture.failed(exception)
154-
new IO[E, Nothing] {
154+
new Attempt[E, Nothing] {
155155
override def toFuture: StdFuture[Nothing] = future
156156
override val isFatal: Boolean = false
157157
}
158158
}
159159

160-
final def fatal(exception: Exception): IO[Nothing, Nothing] = {
160+
final def fatal(exception: Exception): Attempt[Nothing, Nothing] = {
161161
val future = StdFuture.failed(exception)
162-
new IO[Nothing, Nothing] {
162+
new Attempt[Nothing, Nothing] {
163163
override def toFuture: StdFuture[Nothing] = future
164164
override val isFatal: Boolean = true
165165
}
166166
}
167167

168-
final def sequence[E <: Throwable, A](results: Seq[IO[E, A]])(implicit
169-
ec: ExecutionContext
170-
): IO[E, Seq[A]] =
171-
IO(StdFuture.sequence(results.map(_.toFuture)), fatal = false)
168+
final def sequence[E <: Throwable, A](results: Seq[Attempt[E, A]])(implicit
169+
ec: ExecutionContext
170+
): Attempt[E, Seq[A]] =
171+
Attempt(StdFuture.sequence(results.map(_.toFuture)), fatal = false)
172172

173-
val unit = IO.successful(())
173+
val unit = Attempt.successful(())
174174
}

result/src/main/scala/io/github/ragazoor/IOUtils.scala renamed to result/src/main/scala/io/github/ragazoor/AttemptUtils.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package io.github.ragazoor
33
import scala.util.Failure
44
import scala.util.control.NoStackTrace
55

6-
object IOUtils {
6+
object AttemptUtils {
77
private[ragazoor] final val failedFailure =
88
Failure[Nothing](
99
new NoSuchElementException("Future.failed not completed with error E.") with NoStackTrace
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
package io.github.ragazoor
22

3-
import scala.concurrent.{ ExecutionContext, Future => StdFuture }
3+
import scala.concurrent.{ExecutionContext, Future => StdFuture}
44
import scala.util.Try
55

66
/*
77
* Testing this to see if it is possible to make it completely drop in
88
*/
99
object Future {
1010

11-
def unapply[E <: Throwable, A](result: IO[E, A]): Option[StdFuture[A]] =
12-
IO.unapply(result).map(_._1)
11+
def unapply[E <: Throwable, A](result: Attempt[E, A]): Option[StdFuture[A]] =
12+
Attempt.unapply(result).map(_._1)
1313

14-
final def apply[A](body: => A)(implicit ec: ExecutionContext): IO[Throwable, A] =
15-
IO[Throwable, A](StdFuture(body), fatal = false)
14+
final def apply[A](body: => A)(implicit ec: ExecutionContext): Attempt[Throwable, A] =
15+
Attempt[Throwable, A](StdFuture(body), fatal = false)
1616

17-
final def fromFuture[A](future: StdFuture[A]): IO[Throwable, A] =
18-
IO.fromFuture(future)
17+
final def fromFuture[A](future: StdFuture[A]): Attempt[Throwable, A] =
18+
Attempt.fromFuture(future)
1919

20-
final def fromEither[E <: Throwable, A](either: Either[E, A]): IO[E, A] =
21-
IO.fromEither(either)
20+
final def fromEither[E <: Throwable, A](either: Either[E, A]): Attempt[E, A] =
21+
Attempt.fromEither(either)
2222

23-
final def fromTry[A](body: Try[A]): IO[Throwable, A] =
24-
IO.fromTry(body)
23+
final def fromTry[A](body: Try[A]): Attempt[Throwable, A] =
24+
Attempt.fromTry(body)
2525

26-
final def successful[A](value: A): IO[Nothing, A] =
27-
IO.successful(value)
26+
final def successful[A](value: A): Attempt[Nothing, A] =
27+
Attempt.successful(value)
2828

29-
final def failed[E <: Exception](exception: E): IO[E, Nothing] =
30-
IO.failed(exception)
29+
final def failed[E <: Exception](exception: E): Attempt[E, Nothing] =
30+
Attempt.failed(exception)
3131

32-
final def sequence[E <: Throwable, A](results: Seq[IO[E, A]])(implicit
32+
final def sequence[E <: Throwable, A](results: Seq[Attempt[E, A]])(implicit
3333
ec: ExecutionContext
34-
): IO[E, Seq[A]] =
35-
IO.sequence(results)
34+
): Attempt[E, Seq[A]] =
35+
Attempt.sequence(results)
3636

37-
val unit = IO.unit
37+
val unit = Attempt.unit
3838

3939
}

result/src/main/scala/io/github/ragazoor/implicits.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import scala.concurrent.{ Future => StdFuture }
44

55
object implicits {
66
implicit class StdFutureToIo[A](val future: StdFuture[A]) {
7-
def io: IO[Throwable, A] = IO.fromFuture(future)
7+
def io: Attempt[Throwable, A] = Attempt.fromFuture(future)
88
}
99

1010
}

0 commit comments

Comments
 (0)