Skip to content

Instantly share code, notes, and snippets.

@justinhj
Created August 20, 2020 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinhj/24c661e25e8bf0e6d45dea49e041a12b to your computer and use it in GitHub Desktop.
Save justinhj/24c661e25e8bf0e6d45dea49e041a12b to your computer and use it in GitHub Desktop.
Fizz buzz using Zio
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