Skip to content

Commit

Permalink
Reformat with scalafmt 3.7.17
Browse files Browse the repository at this point in the history
Executed command: scalafmt --non-interactive
  • Loading branch information
scala-steward committed Nov 18, 2023
1 parent 540b318 commit 574654f
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 62 deletions.
115 changes: 72 additions & 43 deletions domains-cats/src/main/scala/com/thoughtworks/dsl/domains/cats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ import scala.language.implicitConversions
import scala.util.control.Exception.Catcher
import scala.util.control.NonFatal

/** Contains interpreters to enable [[Dsl.Keyword#unary_$bang !-notation]]
* for [[keywords.Monadic]] and other keywords
* in code blocks whose type support [[cats.FlatMap]] and [[cats.MonadError]].
*
* @example [[cats.free.Trampoline Trampoline]] is a monadic data type that performs tail call optimization.
* It can be built from a `@[[Dsl.reset reset]]` code block within some [[Dsl.Keyword#unary_$bang !-notation]],
* similar to the [[com.thoughtworks.each.Monadic.EachOps#each each]] method in
* [[https://github.com/ThoughtWorksInc/each ThoughtWorks Each]].
*
* {{{
/** Contains interpreters to enable [[Dsl.Keyword#unary_$bang !-notation]] for
* [[keywords.Monadic]] and other keywords in code blocks whose type support
* [[cats.FlatMap]] and [[cats.MonadError]].
*
* @example
* [[cats.free.Trampoline Trampoline]] is a monadic data type that performs
* tail call optimization. It can be built from a `@[[Dsl.reset reset]]` code
* block within some [[Dsl.Keyword#unary_$bang !-notation]], similar to the
* [[com.thoughtworks.each.Monadic.EachOps#each each]] method in
* [[https://github.com/ThoughtWorksInc/each ThoughtWorks Each]].
*
* {{{
* import _root_.cats.free.Trampoline
* import _root_.cats.instances.function._
* import com.thoughtworks.dsl.keywords.Monadic._
Expand All @@ -37,13 +39,13 @@ import scala.util.control.NonFatal
* }: @reset
*
* dslSquare.run should be("This string is produced by a trampoline: 9")
* }}}
* }}}
*
* `!trampoline3` is a shortcut of `!Monadic(trampoline3)`,
* which will be converted to `flatMap` calls by our DSL interpreter.
* Thus, the method `dslSquare` is equivalent to the following code in [[cats.syntax]]:
* `!trampoline3` is a shortcut of `!Monadic(trampoline3)`, which will be
* converted to `flatMap` calls by our DSL interpreter. Thus, the method
* `dslSquare` is equivalent to the following code in [[cats.syntax]]:
*
* {{{
* {{{
*
* def catsSyntaxSquare = trampoline3.flatMap { tmp1 =>
* trampoline3.flatMap { tmp2 =>
Expand All @@ -54,15 +56,17 @@ import scala.util.control.NonFatal
* }
*
* catsSyntaxSquare.run should be("This string is produced by a trampoline: 9")
* }}}
* @example A `@[[Dsl.reset reset]]` code block can contain `try` / `catch` / `finally`
* if the monadic data type supports [[cats.MonadError]].
* }}}
* @example
* A `@[[Dsl.reset reset]]` code block can contain `try` / `catch` /
* `finally` if the monadic data type supports [[cats.MonadError]].
*
* For example, [[cats.effect.IO]] is a monadic data type that supports [[cats.MonadError]],
* therefore `try` / `catch` / `finally` expressions can be used inside a `@[[Dsl.reset reset]]` code block
* whose return type is [[cats.effect.IO]].
* For example, [[cats.effect.IO]] is a monadic data type that supports
* [[cats.MonadError]], therefore `try` / `catch` / `finally` expressions can
* be used inside a `@[[Dsl.reset reset]]` code block whose return type is
* [[cats.effect.IO]].
*
* {{{
* {{{
* import com.thoughtworks.dsl.keywords.Monadic._
* import com.thoughtworks.dsl.domains.cats._
* import _root_.cats.effect.IO
Expand All @@ -80,11 +84,12 @@ import scala.util.control.NonFatal
* }: @reset
*
* dslTryCatch.unsafeRunSync() should be("Cannot divide 0 by itself")
* }}}
* }}}
*
* The above `dslTryCatch` method is equivalent to the following code in [[cats.syntax]]:
* The above `dslTryCatch` method is equivalent to the following code in
* [[cats.syntax]]:
*
* {{{
* {{{
* def catsSyntaxTryCatch: IO[String] = {
* import _root_.cats.syntax.applicativeError._
* io0.flatMap { tmp0 =>
Expand All @@ -102,19 +107,23 @@ import scala.util.control.NonFatal
* }
*
* catsSyntaxTryCatch.unsafeRunSync() should be("Cannot divide 0 by itself")
* }}}
* @author 杨博 (Yang Bo)
* }}}
* @author
* 杨博 (Yang Bo)
*/
object cats {
protected type MonadThrowable[F[_]] = MonadError[F, Throwable]

implicit def catsReturnDsl[F[_], A, B](implicit applicative: Applicative[F],
restReturnDsl: Dsl[Return[A], B, Nothing]): Dsl[Return[A], F[B], Nothing] = {
implicit def catsReturnDsl[F[_], A, B](implicit
applicative: Applicative[F],
restReturnDsl: Dsl[Return[A], B, Nothing]
): Dsl[Return[A], F[B], Nothing] = {
(keyword: Return[A], handler: Nothing => F[B]) =>
applicative.pure(restReturnDsl.cpsApply(keyword, identity))
}
@inline private def catchNativeException[F[_], A](continuation: F[A] !! A)(
implicit monadThrowable: MonadThrowable[F]): F[A] = {
@inline private def catchNativeException[F[_], A](
continuation: F[A] !! A
)(implicit monadThrowable: MonadThrowable[F]): F[A] = {
try {
continuation(monadThrowable.pure(_))
} catch {
Expand All @@ -123,27 +132,41 @@ object cats {
}
}

implicit def catsTryFinally[F[_], A, B](
implicit monadError: MonadThrowable[F]): TryFinally[A, F[B], F[A], F[Unit]] = {
(block: F[A] !! A, finalizer: F[Unit] !! Unit, outerSuccessHandler: A => F[B]) =>
implicit def catsTryFinally[F[_], A, B](implicit
monadError: MonadThrowable[F]
): TryFinally[A, F[B], F[A], F[Unit]] = {
(
block: F[A] !! A,
finalizer: F[Unit] !! Unit,
outerSuccessHandler: A => F[B]
) =>
@inline
def injectFinalizer[A](f: Unit => F[A]): F[A] = {
monadError.flatMap(catchNativeException(finalizer))(f)
}

monadError.flatMap(monadError.handleErrorWith(catchNativeException(block)) { e: Throwable =>
injectFinalizer { _: Unit =>
monadError.raiseError(e)
monadError.flatMap(
monadError.handleErrorWith(catchNativeException(block)) {
e: Throwable =>
injectFinalizer { _: Unit =>
monadError.raiseError(e)
}
}
}) { a =>
) { a =>
injectFinalizer { _: Unit =>
outerSuccessHandler(a)
}
}
}

implicit def catsTryCatch[F[_], A, B](implicit monadError: MonadThrowable[F]): TryCatch[A, F[B], F[A]] = {
(block: F[A] !! A, catcher: Catcher[F[A] !! A], outerSuccessHandler: A => F[B]) =>
implicit def catsTryCatch[F[_], A, B](implicit
monadError: MonadThrowable[F]
): TryCatch[A, F[B], F[A]] = {
(
block: F[A] !! A,
catcher: Catcher[F[A] !! A],
outerSuccessHandler: A => F[B]
) =>
def errorHandler(e: Throwable): F[A] = {
(try {
catcher.lift(e)
Expand All @@ -157,10 +180,14 @@ object cats {
catchNativeException(recovered)
}
}
monadError.flatMap(monadError.handleErrorWith(catchNativeException(block))(errorHandler))(outerSuccessHandler)
monadError.flatMap(
monadError.handleErrorWith(catchNativeException(block))(errorHandler)
)(outerSuccessHandler)
}

private[dsl] def catsCatchDsl[F[_], A, B](implicit monadError: MonadThrowable[F]): CatchDsl[F[A], F[B], A] = {
private[dsl] def catsCatchDsl[F[_], A, B](implicit
monadError: MonadThrowable[F]
): CatchDsl[F[A], F[B], A] = {
(block: F[A] !! A, catcher: Catcher[F[A] !! A], handler: A => F[B]) =>
val fa = monadError.flatMap(monadError.pure(block))(_(monadError.pure))
val protectedFa = monadError.handleErrorWith(fa) {
Expand All @@ -172,7 +199,9 @@ object cats {
monadError.flatMap(protectedFa)(handler)
}

implicit def catsMonadicDsl[F[_], A, B](implicit flatMap: FlatMap[F]): Dsl[Monadic[F, A], F[B], A] = {
implicit def catsMonadicDsl[F[_], A, B](implicit
flatMap: FlatMap[F]
): Dsl[Monadic[F, A], F[B], A] = {
(keyword: Monadic[F, A], handler: A => F[B]) =>
flatMap.flatMap(keyword.fa)(handler)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import com.thoughtworks.dsl.domains.cats._
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers

/**
* @author 杨博 (Yang Bo)
/** @author
* 杨博 (Yang Bo)
*/
class catsSpec extends AnyFreeSpec with Matchers {

Expand All @@ -34,23 +34,56 @@ class catsSpec extends AnyFreeSpec with Matchers {
generator should be(
Seq(
/**/ "Entering generator",
/****/ "Fork thread 0",
/******/ "Entering asyncFunction",
/********/ "Fork sub-thread 0",
/**********/ "Leaving asyncFunction",
/**********/ "Leaving generator",
/********/ "Fork sub-thread 1",
/**********/ "Leaving asyncFunction",
/**********/ "Leaving generator",
/****/ "Fork thread 1",
/******/ "Entering asyncFunction",
/********/ "Fork sub-thread 0",
/**********/ "Leaving asyncFunction",
/**********/ "Leaving generator",
/********/ "Fork sub-thread 1",
/**********/ "Leaving asyncFunction",
/**********/ "Leaving generator"
))
/** *
*/
"Fork thread 0",
/** ***
*/
"Entering asyncFunction",
/** *****
*/
"Fork sub-thread 0",
/** *******
*/
"Leaving asyncFunction",
/** *******
*/
"Leaving generator",
/** *****
*/
"Fork sub-thread 1",
/** *******
*/
"Leaving asyncFunction",
/** *******
*/
"Leaving generator",
/** *
*/
"Fork thread 1",
/** ***
*/
"Entering asyncFunction",
/** *****
*/
"Fork sub-thread 0",
/** *******
*/
"Leaving asyncFunction",
/** *******
*/
"Leaving generator",
/** *****
*/
"Fork sub-thread 1",
/** *******
*/
"Leaving asyncFunction",
/** *******
*/
"Leaving generator"
)
)
}

}
Expand Down

0 comments on commit 574654f

Please sign in to comment.