Skip to content

Instantly share code, notes, and snippets.

@qingwei91
Last active December 29, 2022 15:01
Show Gist options
  • Save qingwei91/0c939744a50b587ceee60ca5c28f406b to your computer and use it in GitHub Desktop.
Save qingwei91/0c939744a50b587ceee60ca5c28f406b to your computer and use it in GitHub Desktop.
import cats.effect._
import scala.concurrent.duration._
def addCoffeePowder(pot: Pot): IO[Pot] = ???
def addWater(pot: Pot): IO[Pot] = ???
def waitUntilBoil(heatedPot: Pot): StateT[IO, Unit, FiniteDuration] = ???
def makeCoffee(pot: Pot): IO[Coffee] = {
for {
withCoffeePwd <- addCoffeePowder(pot)
withWater <- addWater(withCoffeePwd)
heatedPot <- turnOnHeat(withWater)
_ <- waitUntilBoil(heatedPot) // wont compile
coffee <- pot.pourCoffee
} yield coffee
}
-----------
def addCoffeePowder[F[_]](pot: Pot): F[Pot] = ???
def addWater[F[_]](pot: Pot): F[Pot] = ???
type DurationState[F[_]] = Stateful[F, FiniteDuration]
def waitUntilBoil[F[_]: DurationState](heatedPot: Pot): F[Unit] = ???
def makeCoffee[F[_]: DurationState](pot: Pot): F[Coffee] = {
for {
withCoffeePwd <- addCoffeePowder(pot)
withWater <- addWater(withCoffeePwd)
heatedPot <- turnOnHeat(withWater)
_ <- waitUntilBoil(heatedPot) // compiles without addWater and addCoffeePowder knowing anything about State
coffee <- pot.pourCoffee
} yield coffee
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment