-
Notifications
You must be signed in to change notification settings - Fork 8
/
examples.scala
67 lines (55 loc) · 1.78 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
package monadic
import zio._
import zio.console._
import java.io.IOException
/**
* 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 extends zio.App {
type ZIOMonad[R, E] = [A] =>> ZIO[R, E, A]
class ZIOMonadic[R, E] extends Monadic[ZIOMonad[R, E]] {
type M[A] = ZIO[R, E, A]
def pure[A](a: A): ZIO[R, E, A] = ZIO.succeed(a)
def sequence[X, R](init: M[X])(f: X => Either[M[X], M[R]]): M[R] =
init.flatMap { x => f(x) match {
case Left(mx) => sequence(mx)(f)
case Right(res) => res
}}
}
type Zio[R, E, A] = CanReflect[ZIOMonad[R, E]] ?=> A
object Zio {
def reify[R, E, A](p: Zio[R, E, A]): ZIO[R, E, A] = new ZIOMonadic[R, E].reify {
p
}
def reflect[R, E, A](z: ZIO[R, E, A]): Zio[R, E, A] =
summon[CanReflect[ZIOMonad[R, E]]].reflect(z)
}
extension [R, E, A](self: ZIO[R, E, A])
def perform: Zio[R, E, A] = Zio.reflect(self)
def run(args: List[String]) =
Zio.reify { loop(); myAppLogic() }.exitCode
/**
* This is the getting started example from:
* https://zio.dev/docs/overview/overview_basic_operations
*
* translated to direct style.
*/
def myAppLogic(): Zio[Has[Console.Service], IOException, Unit] =
putStrLn("Hello! What is your name?").perform
val name = getStrLn.perform
putStrLn(s"Hello, ${name}, welcome to ZIO!").perform
/**
* Just a loop to test for stack consumption
*/
def loop(): Zio[Has[Console.Service], IOException, Unit] =
var i = 100000
def dec() = ZIO.effectTotal{ i = i - 1 }.perform
def inc() = ZIO.effectTotal{ i = i + 1 }.perform
while (i > 0) do
dec()
inc()
dec()
}