Created
August 20, 2020 04:17
-
-
Save justinhj/24c661e25e8bf0e6d45dea49e041a12b to your computer and use it in GitHub Desktop.
Fizz buzz using Zio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zio._ | |
import zio.console._ | |
import zio.clock._ | |
import zio.duration._ | |
object ZioScheduleExample extends App { | |
val zLayers = Clock.live ++ Console.live | |
val everySecond = Schedule.spaced(1.second) | |
val everyThreeSeconds = Schedule.spaced(3.seconds) | |
val everyFiveSeconds = Schedule.spaced(5.seconds) | |
def run(args: List[String]): URIO[ZEnv, Int] = { | |
val fizz = putStrLn("Fizz") | |
val buzz = putStrLn("Buzz") | |
val timeSecs = clock.nanoTime.map(_ / 1000000000L) | |
for ( | |
start <- timeSecs; | |
_ <- (ZIO.sleep(3.seconds) *> fizz.repeat(everyThreeSeconds)).fork; | |
_ <- (ZIO.sleep(5.seconds) *> buzz.repeat(everyFiveSeconds)).fork; | |
_ <- timeSecs.flatMap(time => putStrLn(s"time ${time - start}")).repeat(everySecond) | |
) yield 0 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment