Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ContextShift.apply in the tutorial #489

Merged
merged 1 commit into from
Apr 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions site/src/main/tut/tutorial/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ version := "1.0"

scalaVersion := "2.12.8"

libraryDependencies += "org.typelevel" %% "cats-effect" % "1.0.0" withSources() withJavadoc()
libraryDependencies += "org.typelevel" %% "cats-effect" % "1.2.0" withSources() withJavadoc()

scalacOptions ++= Seq(
"-feature",
Expand Down Expand Up @@ -1139,13 +1139,11 @@ import cats.effect._
import cats.implicits._
import scala.concurrent.ExecutionContext

def doHeavyStuffInADifferentThreadPool[F[_]: ContextShift: Sync](implicit ec: ExecutionContext): F[Unit] = {
val csf = implicitly[ContextShift[F]]
def doHeavyStuffInADifferentThreadPool[F[_]: ContextShift: Sync](implicit ec: ExecutionContext): F[Unit] =
for {
_ <- csf.evalOn(ec)(Sync[F].delay(println("Hi!"))) // Swapping to thread pool of given ExecutionContext
_ <- ContextShift[F].evalOn(ec)(Sync[F].delay(println("Hi!"))) // Swapping to thread pool of given ExecutionContext
_ <- Sync[F].delay(println("Welcome!")) // Running back in default thread pool
} yield ()
}
```

#### Exercise: using a custom thread pool in echo server
Expand All @@ -1164,11 +1162,9 @@ connected client. So the beginning of the `echoProtocol` function would look lik
```scala
def echoProtocol[F[_]: Sync: ContextShift](clientSocket: Socket, stopFlag: MVar[F, Unit])(implicit clientsExecutionContext: ExecutionContext): F[Unit] = {

val csf = implicitly[ContextShift[F]]

def loop(reader: BufferedReader, writer: BufferedWriter, stopFlag: MVar[F, Unit]): F[Unit] =
for {
lineE <- csf.evalOn(clientsExecutionContext)(Sync[F].delay(reader.readLine()).attempt)
lineE <- ContextShift[F].evalOn(clientsExecutionContext)(Sync[F].delay(reader.readLine()).attempt)
// ...
```

Expand Down