calvinlfer / free-monads-functional-web-apps

Delving into Free Monads and using them to write pure functional web applications
17 stars 2 forks source link

Use Coproducts to combine instructions #1

Open calvinlfer opened 7 years ago

calvinlfer commented 7 years ago

Right now, we use a hackish way to compose interpreters wherein we use a big interpreter composed of little interpreters. The big interpreter dispatches to the little specific interpreters:

class AppInstructionInterpreter(logInterpreter: LogInterpreter, dbInterpreter: DatabaseInterpreter) {
    val interpreter: AppInstruction ~> Task = new (AppInstruction ~> Task) {
      override def apply[A](fa: AppInstruction[A]): Task[A] = fa match {
        case logInstruction: LogInstruction[A] =>
          logInterpreter.run(logInstruction)

        case dbInstruction: DatabaseInstruction[A] =>
          dbInterpreter.run(dbInstruction)
      }
    }

Source

The way to merge interpreters properly is to use Coproducts. See here:

calvinlfer commented 7 years ago

See here for an example

calvinlfer commented 7 years ago

I would have to make the switch to using the cats machinery to use FreeK