Skip to content
Open
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
20 changes: 12 additions & 8 deletions src/pages/monad-transformers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ and launch devastating attacks.
The message sending method looks like this:

```scala
def getPowerLevel(autobot: String): Response[Int] =
def getPowerLevel(autobot: String)(implicit ec: ExecutionContext): Response[Int] =
???
```

Expand Down Expand Up @@ -590,11 +590,12 @@ val powerLevels = Map(
```
```scala mdoc:silent
import cats.instances.future._ // for Monad
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContext.Implicits.global

type Response[A] = EitherT[Future, String, A]

def getPowerLevel(ally: String): Response[Int] = {
def getPowerLevel(ally: String)(implicit ec: ExecutionContext): Response[Int] = {
powerLevels.get(ally) match {
case Some(avg) => EitherT.right(Future(avg))
case None => EitherT.left(Future(s"$ally unreachable"))
Expand Down Expand Up @@ -624,6 +625,7 @@ and use `map` and `flatMap` to combine the results:
import cats.implicits._
import cats.data._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContext.Implicits.global

type Response[A] = EitherT[Future, String, A]
Expand All @@ -634,15 +636,15 @@ val powerLevels = Map(
"Hot Rod" -> 10
)

def getPowerLevel(ally: String): Response[Int] = {
def getPowerLevel(ally: String)(implicit ec: ExecutionContext): Response[Int] = {
powerLevels.get(ally) match {
case Some(avg) => EitherT.right(Future(avg))
case None => EitherT.left(Future(s"$ally unreachable"))
}
}
```
```scala mdoc:silent
def canSpecialMove(ally1: String, ally2: String): Response[Boolean] =
def canSpecialMove(ally1: String, ally2: String)(implicit ec: ExecutionContext): Response[Boolean] =
for {
power1 <- getPowerLevel(ally1)
power2 <- getPowerLevel(ally2)
Expand All @@ -655,7 +657,7 @@ takes two ally names and prints a message
saying whether they can perform a special move:

```scala mdoc:silent
def tacticalReport(ally1: String, ally2: String): String =
def tacticalReport(ally1: String, ally2: String)(implicit ec: ExecutionContext): String =
???
```

Expand All @@ -667,6 +669,7 @@ and `Await` and `fold` to unpack the `Future` and `Either`:
import cats.implicits._
import cats.data._
import scala.concurrent.Future
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContext.Implicits.global

type Response[A] = EitherT[Future, String, A]
Expand All @@ -677,7 +680,7 @@ val powerLevels = Map(
"Hot Rod" -> 10
)

def getPowerLevel(ally: String): Response[Int] = {
def getPowerLevel(ally: String)(implicit ec: ExecutionContext): Response[Int] = {
powerLevels.get(ally) match {
case Some(avg) => EitherT.right(Future(avg))
case None => EitherT.left(Future(s"$ally unreachable"))
Expand All @@ -686,16 +689,17 @@ def getPowerLevel(ally: String): Response[Int] = {
```
```scala mdoc:silent
import scala.concurrent.Await
import scala.concurrent.ExecutionContext
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

def canSpecialMove(ally1: String, ally2: String): Response[Boolean] =
def canSpecialMove(ally1: String, ally2: String)(implicit ec: ExecutionContext): Response[Boolean] =
for {
power1 <- getPowerLevel(ally1)
power2 <- getPowerLevel(ally2)
} yield (power1 + power2) > 15

def tacticalReport(ally1: String, ally2: String): String = {
def tacticalReport(ally1: String, ally2: String)(implicit ec: ExecutionContext): String = {
val stack = canSpecialMove(ally1, ally2).value

Await.result(stack, 1.second) match {
Expand Down