Skip to content

Instantly share code, notes, and snippets.

View qingwei91's full-sized avatar

Qing qingwei91

View GitHub Profile
@qingwei91
qingwei91 / low-compose-io.scala
Created December 29, 2022 11:54
Low level IO api
import cats.effect._
val a = IO(...)
// get access to underlying thread-like abstraction, where you can cancel, cancel is however asynchronous!
val aFiber = a.start
val cancel = aFiber.flatMap(_.cancel)
val join = aFiber.flatMap(_.join)
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] = {
@qingwei91
qingwei91 / io-resource.scala
Created January 27, 2023 08:54
Resource type
val fileR: Resource[IO, File] = ...
val socketR1: Resource[IO, Socket] = ...
val socketR2: Resource[IO, Socket] = ...
(for {
file <- fileR
sock1 <- socketR1
sock2 <- socketR2
} yield (file, sock1, sock2)).use {