-
Notifications
You must be signed in to change notification settings - Fork 8
/
examples.scala
87 lines (64 loc) · 1.8 KB
/
examples.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
package monadic
import scalaz.*
import Scalaz.*
import effect.*
import monadic.syntax.*
class ScalazMonadic[M[_]: Monad : BindRec] extends Monadic[M] {
def pure[A](a: A): M[A] = a.pure
def sequence[X, R](init: M[X])(f: X => Either[M[X], M[R]]): M[R] =
Monad[M].join(BindRec[M].tailrecM(init) { mx =>
Monad[M].map(mx) { x => \/.fromEither(f(x)) }
})
}
given [M[_]](using Monad[M], BindRec[M]): Monadic[M] = ScalazMonadic()
/**
* This example illustrates a basic integration of ZIO with monadic reflection.
*
* The interface presented here is very preliminary and has a lot of room for
* ergonomic improvement.
*/
object MyApp {
type IntReader[A] = Reader[Int, A]
type IntWriter[A] = Writer[Int, A]
def read(): Int in IntReader = Reader[Int, Int] { n => n }.reflect
def write(n: Int): Unit in IntWriter = n.tell.reflect
def main(args: Array[String]): Unit = {
def io = reify [IO] in {
IO.putStr("Hello, ").reflect
IO.putStrLn("world!").reflect
}
io.unsafePerformIO
val res: IntReader[Int] = reify [IntReader] in {
read() + read() + read()
}
println("\nReader Example:")
println { res.run(2) }
val res2 = reify [IntWriter] in {
write(1)
write(4)
write(3)
}
println("\nWriter Example:")
println { res2.run(0) }
val resBoth: IntReader[IntWriter[Unit]] = reify [IntReader] in {
reify [IntWriter] in {
write(read()) // +1
4.tell.reflect
write(read()) // +1
}
}
println("\nCombined Example:")
println { resBoth(1).run(0) }
def loop(): Unit = reify[IO] in {
var i = 100000
def dec() = IO { i = i - 1 }.reflect
def inc() = IO { i = i + 1 }.reflect
while (i > 0) do {
dec()
inc()
dec()
}
}
loop()
}
}