Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ragge-dev committed Apr 7, 2024
1 parent f99e91b commit e5eebd4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
6 changes: 3 additions & 3 deletions benchmark/src/main/scala/io/github/ragazoor/IOBenchmark.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class IOBenchmark {
await(futureFlatMapRec(recursion, StdFuture.successful(1)))

@tailrec private[this] final def resultFlatMapRec(i: Int, f: Attempt[Nothing, Int])(implicit
ec: ExecutionContext
ec: ExecutionContext
): Attempt[Nothing, Int] =
if (i > 0) resultFlatMapRec(i - 1, f.flatMap(Attempt.successful)(ec))(ec)
else f
Expand All @@ -61,7 +61,7 @@ class IOBenchmark {
await(futureMapRec(recursion, StdFuture.successful(1)))

@tailrec private[this] final def resultMapRec(i: Int, f: Attempt[Nothing, Int])(implicit
ec: ExecutionContext
ec: ExecutionContext
): Attempt[Nothing, Int] =
if (i > 0) resultMapRec(i - 1, f.map(identity)(ec))(ec)
else f
Expand All @@ -76,7 +76,7 @@ class IOBenchmark {
else f

@tailrec private[this] final def resultMapErrorRec(i: Int, f: Attempt[RuntimeException, Int])(implicit
ec: ExecutionContext
ec: ExecutionContext
): Attempt[RuntimeException, Int] =
if (i > 0) resultMapErrorRec(i - 1, f.mapError(identity)(ec))(ec)
else f
Expand Down
16 changes: 8 additions & 8 deletions result/src/main/scala/io/github/ragazoor/Attempt.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.github.ragazoor

import io.github.ragazoor.AttemptUtils.{failedFailure, zipWithTuple2Fun}
import io.github.ragazoor.AttemptUtils.{ failedFailure, zipWithTuple2Fun }

import scala.concurrent.ExecutionContext.parasitic
import scala.concurrent.duration.Duration
import scala.concurrent.{Awaitable, CanAwait, ExecutionContext, TimeoutException, Future => StdFuture}
import scala.concurrent.{ Awaitable, CanAwait, ExecutionContext, Future => StdFuture, TimeoutException }
import scala.util.control.NonFatal
import scala.util.{Failure, Success, Try}
import scala.util.{ Failure, Success, Try }

sealed trait Attempt[+E <: Throwable, +A] extends Awaitable[A] {
self =>
Expand Down Expand Up @@ -46,12 +46,12 @@ sealed trait Attempt[+E <: Throwable, +A] extends Awaitable[A] {
zipWith(that)(zipWithTuple2Fun)(parasitic)

def zipWith[E2 >: E <: Throwable, U, R](that: Attempt[E2, U])(f: (A, U) => R)(implicit
ec: ExecutionContext
ec: ExecutionContext
): Attempt[E2, R] =
Attempt(self.toFuture.zipWith(that.toFuture)(f), isFatal || that.isFatal)

def catchAll[E2 >: E <: Throwable, A2 >: A](f: E => Attempt[E2, A2])(implicit
ec: ExecutionContext
ec: ExecutionContext
): Attempt[E2, A2] = {
var isFutureFatal = isFatal
val transformedFuture = self.toFuture.transformWith {
Expand All @@ -65,7 +65,7 @@ sealed trait Attempt[+E <: Throwable, +A] extends Awaitable[A] {
}

def catchSome[E2 >: E <: Throwable, A2 >: A](pf: PartialFunction[E, Attempt[E2, A2]])(implicit
ec: ExecutionContext
ec: ExecutionContext
): Attempt[E2, A2] = {
val transformedFuture = self.toFuture.transformWith {
case Failure(e) if NonFatal(e) && pf.isDefinedAt(e.asInstanceOf[E]) && !isFatal =>
Expand Down Expand Up @@ -98,7 +98,7 @@ sealed trait Attempt[+E <: Throwable, +A] extends Awaitable[A] {
Attempt(self.toFuture.transform(f), isFatal)

def transformWith[E2 >: E <: Throwable, B](f: Try[A] => Attempt[E2, B])(implicit
executor: ExecutionContext
executor: ExecutionContext
): Attempt[E2, B] =
Attempt(self.toFuture.transformWith(f(_).toFuture), isFatal)

Expand Down Expand Up @@ -166,7 +166,7 @@ object Attempt {
}

final def sequence[E <: Throwable, A](results: Seq[Attempt[E, A]])(implicit
ec: ExecutionContext
ec: ExecutionContext
): Attempt[E, Seq[A]] =
Attempt(StdFuture.sequence(results.map(_.toFuture)), fatal = false)

Expand Down
2 changes: 1 addition & 1 deletion result/src/main/scala/io/github/ragazoor/Future.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.ragazoor

import scala.concurrent.{ExecutionContext, Future => StdFuture}
import scala.concurrent.{ ExecutionContext, Future => StdFuture }
import scala.util.Try

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.ragazoor.migration

import io.github.ragazoor.Attempt

import scala.concurrent.{Future => StdFuture}
import scala.concurrent.{ Future => StdFuture }
import scala.language.implicitConversions

trait IoToStdFuture {
Expand Down Expand Up @@ -244,7 +244,29 @@ trait IoToStdFuture {
X21,
B
](
f21: (X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X18, X19, X20, X21) => Attempt[E, B]
f21: (
X1,
X2,
X3,
X4,
X5,
X6,
X7,
X8,
X9,
X10,
X11,
X12,
X13,
X14,
X15,
X16,
X17,
X18,
X19,
X20,
X21
) => Attempt[E, B]
): (X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, X16, X17, X18, X19, X20, X21) => StdFuture[B] =
(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21) =>
f21(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21).toFuture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.ragazoor.migration

import io.github.ragazoor.Attempt

import scala.concurrent.{Future => StdFuture}
import scala.concurrent.{ Future => StdFuture }
import scala.language.implicitConversions

object implicits extends IoToStdFuture {
Expand Down
11 changes: 7 additions & 4 deletions result/src/test/scala/io/github/ragazoor/AttemptSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.ragazoor

import munit.FunSuite

import scala.concurrent.{Future => StdFuture}
import scala.concurrent.{ Future => StdFuture }
import scala.util.Try

class AttemptSpec extends FunSuite {
Expand Down Expand Up @@ -170,13 +170,15 @@ class AttemptSpec extends FunSuite {
}

test("Future.mapEither") {
Attempt.successful(1)
Attempt
.successful(1)
.mapEither(_ => Right(2))
.map(result => assert(result == 2))
}

test("Future.mapEither, with typed error") {
Attempt.successful(1)
Attempt
.successful(1)
.mapEither[RuntimeException, Int](_ => Left(new RuntimeException("Test message")))
.map(_ => assert(false))
.catchSome { case _: RuntimeException =>
Expand All @@ -185,7 +187,8 @@ class AttemptSpec extends FunSuite {
}

test("Future.mapTry") {
Attempt.successful(1)
Attempt
.successful(1)
.mapTry(_ => Try(2))
.map(result => assert(result == 2))
}
Expand Down

0 comments on commit e5eebd4

Please sign in to comment.