-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add example on composability of direct style
- Loading branch information
Showing
5 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
commons/src/main/scala/io/github/tassiLuca/dse/examples/gears/ShowcasingDirectStyle.scala
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,55 @@ | ||
package io.github.tassiLuca.dse.examples.gears | ||
|
||
import scala.reflect.ClassTag | ||
|
||
object ShowcasingDirectStyle: | ||
|
||
object Monadic: | ||
import scala.concurrent.duration.Duration | ||
import scala.concurrent.{Await, ExecutionContext, Future} | ||
|
||
def transform[E, T]( | ||
xs: Seq[Future[Either[E, T]]], | ||
)(using ExecutionContext): Future[Either[E, Seq[T]]] = | ||
import cats.implicits._ | ||
Future.sequence(xs) // Future[Seq[Either[E, T]] | ||
.map(_.sequence) // equivalent to: _.traverse(identity) | ||
|
||
def transform2[E, T]( | ||
xs: Seq[Future[Either[E, T]]], | ||
)(using ec: ExecutionContext): Future[Either[E, Seq[T]]] = | ||
val initial: Future[Either[E, List[T]]] = Future.successful(Right(List.empty[T])) | ||
xs.foldRight(initial): (future, acc) => | ||
for | ||
f <- future | ||
a <- acc | ||
yield a.flatMap(lst => f.map(_ :: lst)) | ||
|
||
@main def useMonadicTransform(): Unit = | ||
given ExecutionContext = ExecutionContext.global | ||
val xs = Seq(Future(Right("I")), Future(Right("Love")), Future(Right("Monads"))) | ||
val result = transform(xs) // transform 2 works as well | ||
println(Await.result(result, Duration.Inf)) | ||
val ys = Seq(Future(Right("Yes")), Future(Left("Nope!"))) | ||
val result2 = transform(ys) // transform 2 works as well | ||
println(Await.result(result2, Duration.Inf)) | ||
|
||
object Direct: | ||
import gears.async.default.given | ||
import gears.async.{Future, Async} | ||
import io.github.tassiLuca.dse.boundaries.either | ||
import io.github.tassiLuca.dse.boundaries.either.? | ||
import io.github.tassiLuca.dse.boundaries.EitherConversions.given | ||
|
||
def transform[E, T](xs: Seq[Future[Either[E, T]]])(using Async.Spawn): Future[Either[E, Seq[T]]] = | ||
Future: | ||
either: | ||
xs.map(_.await.?) | ||
|
||
@main def useDirectTransform(): Unit = Async.blocking: | ||
val xs = Seq(Future(Right("I")), Future(Right("Love")), Future(Right("Monads"))) | ||
val result = transform(xs) | ||
println(result.await) | ||
val ys = Seq(Future(Right("Yes")), Future(Left("Nope!"))) | ||
val result2 = transform(ys) | ||
println(result2.await) |
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,6 @@ | ||
def transform[E, T]( | ||
xs: Seq[Future[Either[E, T]]] | ||
)(using Async.Spawn): Future[Either[E, Seq[T]]] = | ||
Future: | ||
either: | ||
xs.map(_.await.?) |
6 changes: 6 additions & 0 deletions
6
docs/presentation/listings/intro/IdiomaticMonadicComposability.scala
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,6 @@ | ||
def transform[E, T]( | ||
xs: Seq[Future[Either[E, T]]], | ||
)(using ExecutionContext): Future[Either[E, Seq[T]]] = | ||
import cats.implicits._ | ||
Future.sequence(xs) // Future[Seq[Either[E, T]] | ||
.map(_.sequence) // equivalent to: _.traverse(identity) |
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,9 @@ | ||
def transform2[E, T]( | ||
xs: Seq[Future[Either[E, T]]], | ||
)(using ec: ExecutionContext): Future[Either[E, Seq[T]]] = | ||
val initial: Future[Either[E, List[T]]] = Future.successful(Right(List.empty[T])) | ||
xs.foldRight(initial): (future, acc) => | ||
for | ||
f <- future | ||
a <- acc | ||
yield a.flatMap(lst => f.map(_ :: lst)) |