-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Abhijit Sarkar
committed
Dec 26, 2024
1 parent
eb1bc3e
commit d684f6c
Showing
26 changed files
with
607 additions
and
104 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.12.3 | ||
0.12.4 |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,3 @@ class CodataSpec extends AnyFunSpec: | |
|
||
val product = list()(1, (a, b) => a * b) | ||
product shouldBe 6 | ||
|
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package ch10 | ||
|
||
import scala.concurrent.Future | ||
import cats.data.EitherT | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.Await | ||
import scala.concurrent.duration.* | ||
|
||
/* | ||
10.4 Exercise: Monads: Transform and Roll Out | ||
The Autobots, well-known robots in disguise, frequently send messages | ||
during battle requesting the power levels of their team mates. | ||
This helps them coordinate strategies and launch devastating attacks. | ||
Transmissions take time in Earth’s viscous atmosphere, and messages are | ||
occasionally lost due to satellite malfunction or sabotage by pesky Decepticons8. | ||
Optimus Prime is getting tired of the nested for comprehensions in his neural matrix. | ||
Help him by rewriting Response using a monad transformer. | ||
*/ | ||
object Lib: | ||
|
||
type Response[A] = EitherT[Future, String, A] | ||
|
||
val powerLevels = Map( | ||
"Jazz" -> 6, | ||
"Bumblebee" -> 8, | ||
"Hot Rod" -> 10 | ||
) | ||
|
||
/* | ||
Implement getPowerLevel to retrieve data from a set of imaginary allies. | ||
If an Autobot isn’t in the powerLevels map, return an error message reporting | ||
that they were unreachable. Include the name in the message for good effect. | ||
*/ | ||
def getPowerLevel(ally: String): Response[Int] = | ||
powerLevels.get(ally) match | ||
case Some(avg) => EitherT.right(Future(avg)) | ||
case None => EitherT.left(Future(s"$ally unreachable")) | ||
|
||
/* | ||
Two autobots can perform a special move if their combined power level is greater than 15. | ||
If either ally is unavailable, fail with an appropriate error message. | ||
*/ | ||
def canSpecialMove(ally1: String, ally2: String): Response[Boolean] = | ||
for | ||
lvl1 <- getPowerLevel(ally1) | ||
lvl2 <- getPowerLevel(ally2) | ||
yield (lvl1 + lvl2) > 15 | ||
|
||
/* | ||
Write a method tacticalReport that takes two ally names and prints | ||
a message saying whether they can perform a special move. | ||
*/ | ||
def tacticalReport(ally1: String, ally2: String): String = | ||
val stack: Future[Either[String, Boolean]] = | ||
canSpecialMove(ally1, ally2).value | ||
|
||
Await.result(stack, 1.second) match | ||
case Left(msg) => s"Comms error: $msg" | ||
case Right(true) => s"$ally1 and $ally2 are ready to roll out!" | ||
case Right(false) => s"$ally1 and $ally2 need a recharge." |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import cats.data.{EitherT, OptionT, Writer} | ||
import cats.syntax.applicative.catsSyntaxApplicativeId // pure | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import scala.concurrent.duration.* | ||
import scala.concurrent.{Await, Future} | ||
|
||
// 10.2 A Transformative Example | ||
/* | ||
we build ListOption from the inside out: we pass List, the type of the outer monad, | ||
as a parameter to OptionT, the transformer for the inner monad. | ||
*/ | ||
type ListOption[A] = OptionT[List, A] | ||
|
||
// We can create instances of ListOption using the OptionT constructor, or more conveniently using pure | ||
val result1: ListOption[Int] = OptionT(List(Option(10))) | ||
val result2: ListOption[Int] = 32.pure[ListOption] | ||
|
||
result1.flatMap { (x: Int) => | ||
result2.map { (y: Int) => | ||
x + y | ||
} | ||
} | ||
|
||
// 10.3.2 Building Monad Stacks | ||
|
||
// Alias Either to a type constructor with one parameter | ||
type ErrorOr[A] = Either[String, A] | ||
|
||
// Build our final monad stack using OptionT | ||
type ErrorOrOption[A] = OptionT[ErrorOr, A] | ||
|
||
val a = 10.pure[ErrorOrOption] | ||
val b = 32.pure[ErrorOrOption] | ||
|
||
val c = a.flatMap(x => b.map(y => x + y)) | ||
|
||
type FutureEither[A] = EitherT[Future, String, A] | ||
|
||
type FutureEitherOption[A] = OptionT[FutureEither, A] | ||
|
||
val futureEitherOr: FutureEitherOption[Int] = | ||
for | ||
a <- 10.pure[FutureEitherOption] | ||
b <- 32.pure[FutureEitherOption] | ||
yield a + b | ||
|
||
// 10.3.3 Constructing and Unpacking Instances | ||
val intermediate = futureEitherOr.value | ||
|
||
val stack = intermediate.value | ||
|
||
Await.result(stack, 1.second) | ||
|
||
// 10.3.5 Usage Patterns | ||
type Logged[A] = Writer[List[String], A] | ||
|
||
// Methods generally return untransformed stacks | ||
def parseNumber(str: String): Logged[Option[Int]] = | ||
util.Try(str.toInt).toOption match | ||
case Some(num) => Writer(List(s"Read $str"), Some(num)) | ||
case None => Writer(List(s"Failed on $str"), None) | ||
|
||
// Consumers use monad transformers locally to simplify composition | ||
def addAll(a: String, b: String, c: String): Logged[Option[Int]] = | ||
val result = for | ||
a <- OptionT(parseNumber(a)) | ||
b <- OptionT(parseNumber(b)) | ||
c <- OptionT(parseNumber(c)) | ||
yield a + b + c | ||
|
||
result.value | ||
|
||
// This approach doesn't force OptionT on other users' code: | ||
addAll("1", "2", "3") | ||
addAll("1", "a", "3") |
Oops, something went wrong.