This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.effects._ | |
val knockNeighborsDoor = IO(raiseHandAndKnock) | |
knockNeighborsDoor.unsafeRunSync == noResponse | |
knockNeighborsDoor.unsafeRunSync == neighborOpenDoor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder