Skip to content

Instantly share code, notes, and snippets.

View qingwei91's full-sized avatar

Qing qingwei91

View GitHub Profile
@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 {
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 / 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)
@qingwei91
qingwei91 / io-high-compose.scala
Last active December 29, 2022 11:56
High level composition of IO
import cats.effect._
val a = IO(...)
val b = IO(...)
a.map(transformA) // transform output of effect
a >> b // a then b, in other words, FlatMap
(a, b).parTupled // a and b concurrently, then use the output
@qingwei91
qingwei91 / io-sample.scala
Last active December 29, 2022 11:38
Sample IO
import cats.effects._
val knockNeighborsDoor = IO(raiseHandAndKnock)
knockNeighborsDoor.unsafeRunSync == noResponse
knockNeighborsDoor.unsafeRunSync == neighborOpenDoor
@qingwei91
qingwei91 / rt.scala
Created December 1, 2022 10:40
referential transparent
/**
A piece of code can be replaced by a variable/function that represent itself, in all cases!
*/
val x = 1 + 2
val y = x + x
val z = (1 + 2) + (1+2)
assert(z == y == (x + x))
var mx = 20
#!/usr/bin/env sh
# This is a wrapper script, that automatically download ammonite from GitHub release pages
# You can give the required mill version with AMM_VERSION env variable
# If no version is given, it falls back to the value of DEFAULT_AMM_VERSION
DEFAULT_AMM_VERSION=2.0.4
SCALA_VERSION=2.12
set -e
@qingwei91
qingwei91 / stream-flattap.scala
Created February 27, 2020 12:36
flatTap is interesting
import $ivy.`org.typelevel::cats-effect:2.1.1`
import $ivy.`co.fs2::fs2-core:2.2.2`
import fs2._
import cats.effect._
import cats.implicits._
val inputStream = Stream.emits[IO, Int](0 to 10)
def sideEffect(i:Int): Stream[IO, Int] = Stream.emits(1 to 3)
// create non-terminating loop
val cancellable = cancellableLoop[IO, Int, Int](i => {println("a step");Left(i)})(0)
val fiber = cancellable.start.unsafeRunSync
fiber.cancel.unsafeRunSync
def cancellableLoop[F[_], LoopCtx, A](
step: LoopCtx => Either[LoopCtx, A]
)(init: LoopCtx)(implicit cs: ContextShift[F], monad: Monad[F]): F[A] = {
def inner(in: LoopCtx, i: Int): F[A] = {
if (i > 2000) {
cs.shift.flatMap(_ => inner(in, 0))
} else {
step(in) match {
case Left(cont) => inner(cont, i + 1)