-
Notifications
You must be signed in to change notification settings - Fork 29
/
cats.scala
132 lines (126 loc) · 4.94 KB
/
cats.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.thoughtworks.dsl
package domains
import _root_.cats.FlatMap
import com.thoughtworks.dsl.Dsl
import com.thoughtworks.dsl.Dsl.{!!, Keyword}
import _root_.cats.MonadError
import com.thoughtworks.Extractor._
import com.thoughtworks.dsl.keywords.Catch.CatchDsl
import com.thoughtworks.dsl.keywords.{Catch, Monadic}
import scala.language.higherKinds
import scala.language.implicitConversions
import scala.util.control.Exception.Catcher
import scala.util.control.{ControlThrowable, 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 `@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 cats.free.Trampoline
* import cats.instances.function._
* import com.thoughtworks.dsl.keywords.Monadic._
* import com.thoughtworks.dsl.domains.cats._
* import com.thoughtworks.dsl.Dsl.reset
*
* val trampoline3 = Trampoline.done(3)
*
* def dslSquare = Trampoline.delay {
* s"This string is produced by a trampoline: ${!trampoline3 * !trampoline3}"
* }: @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]]:
*
* {{{
*
* def catsSyntaxSquare = trampoline3.flatMap { tmp1 =>
* trampoline3.flatMap { tmp2 =>
* Trampoline.delay {
* s"This string is produced by a trampoline: ${tmp1 * tmp2}"
* }
* }
* }
*
* catsSyntaxSquare.run should be("This string is produced by a trampoline: 9")
* }}}
*
* <hr/>
*
* A `@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 `@reset` code block
* whose return type is [[cats.effect.IO]].
*
* {{{
* import cats.effect.IO
* val io0 = IO(0)
*
* def dslTryCatch: IO[String] = IO {
* try {
* s"Division result: ${!io0 / !io0}"
* } catch {
* case e: ArithmeticException =>
* s"Cannot divide ${!io0} by itself"
* }
* }: @reset
*
* dslTryCatch.unsafeRunSync() should be("Cannot divide 0 by itself")
* }}}
*
* The above `dslTryCatch` method is equivalent to the following code in [[cats.syntax]]:
*
* {{{
* def catsSyntaxTryCatch: IO[String] = {
* import cats.syntax.applicativeError._
* io0.flatMap { tmp0 =>
* io0.flatMap { tmp1 =>
* IO(s"Division result: ${tmp0 / tmp1}")
* }
* }.handleErrorWith {
* case e: ArithmeticException =>
* io0.flatMap { tmp2 =>
* IO(s"Cannot divide ${tmp2} by itself")
* }
* case e =>
* e.raiseError[IO, String]
* }
* }
*
* catsSyntaxTryCatch.unsafeRunSync() should be("Cannot divide 0 by itself")
* }}}
*
* @author 杨博 (Yang Bo)
*/
object cats {
protected type MonadThrowable[F[_]] = MonadError[F, Throwable]
implicit def catsCatchDsl[F[_], A, B](implicit monadError: MonadThrowable[F]): CatchDsl[F[A], F[B], A] =
new CatchDsl[F[A], F[B], A] {
def tryCatch(block: F[A] !! A, catcher: Catcher[F[A] !! A], handler: A => F[B]): F[B] = {
val fa = monadError.flatMap(monadError.pure(block))(_(monadError.pure(_)))
val protectedFa = monadError.handleErrorWith(fa) {
case catcher.extract(recovered) =>
recovered(monadError.pure(_))
case e =>
monadError.raiseError[A](e)
}
monadError.flatMap(protectedFa)(handler)
}
}
implicit def catsFlatMapDsl[F[_], A, B](implicit flatMap: FlatMap[F]): Dsl[Monadic[F, A], F[B], A] =
new Dsl[Monadic[F, A], F[B], A] {
def interpret(keyword: Monadic[F, A], handler: A => F[B]): F[B] = {
flatMap.flatMap(keyword.fa)(handler)
}
}
}