Skip to content

Instantly share code, notes, and snippets.

@ioleo
Last active January 23, 2019 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ioleo/f1a78b9e6a6435464b6e129a12e18ea2 to your computer and use it in GitHub Desktop.
Save ioleo/f1a78b9e6a6435464b6e129a12e18ea2 to your computer and use it in GitHub Desktop.
Freestyle free program example with multiple algebras composed into module
import cats.effect.IO
import freestyle.free._
import freestyle.free.implicits._
@free trait Logger {
def debug(message: String): FS[Unit]
}
@free trait Summer {
def sum(a: Int, b: Int): FS[Int]
}
@module trait FreeApp {
val log: Logger
val summer: Summer
def program[F[_]](a: Int, b: Int)(implicit app: FreeApp[F]): FreeS[F, Int] =
for {
sum <- app.summer.sum(a, b)
_ <- app.log.debug(s"$a + $b = $sum")
} yield sum
}
object FreeModulesExample extends App {
implicit val loggerHandler = new Logger.Handler[IO] {
def debug(message: String): IO[Unit] = IO { println(s"Logger debug: $message") }
}
implicit val summerHandler = new Summer.Handler[IO] {
override def sum(a: Int, b: Int): IO[Int] = IO { a + b }
}
val result = FreeApp.instance.program[FreeApp.Op](5, 3).interpret[IO].unsafeRunSync()
println(s"Result is: $result")
// Logger debug: 5 + 3 = 8
// Result is: 8
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment