Skip to content

Instantly share code, notes, and snippets.

Avatar

Dmitry Komanov dkomanov

View GitHub Profile
@dkomanov
dkomanov / IfElseSync.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] sync if-statement
View IfElseSync.scala
def condition: Boolean = ...
def action1(): Unit = ...
def action2(): Unit = ...
if (condition) {
action1()
} else {
action2()
}
@dkomanov
dkomanov / IfElseAsync.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] async if-statement
View IfElseAsync.scala
def condition: Future[Boolean] = ...
def action1(): Future[Unit] = ...
def action2(): Future[Unit] = ...
condition.flatMap(
if (_)
action1()
else
action2()
@dkomanov
dkomanov / IfElseMultipleConditions.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] multiple statement
View IfElseMultipleConditions.scala
def condition1: Boolean = ...
def condition2: Boolean = ...
def condition3: Boolean = ...
if (condition1 && (condition2 || condition3)) ...
@dkomanov
dkomanov / BooleanOps1.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] BooleanOps1
View BooleanOps1.scala
implicit class FutureOfBooleanExtensions(val future: Future[Boolean]) extends AnyVal {
def &&(other: => Future[Boolean]): Future[Boolean] =
future.flatMap(value => if (!value) Future.successful(false) else other)
def ||(other: => Future[Boolean]): Future[Boolean] =
future.flatMap(value => if (value) Future.successful(true) else other)
}
@dkomanov
dkomanov / IfElseAsyncMultipleOps.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] multiple statement
View IfElseAsyncMultipleOps.scala
def condition1: Future[Boolean] = ...
def condition2: Future[Boolean] = ...
def condition3: Future[Boolean] = ...
val condition = condition1 && (condition2 || condition3)
condition.flatMap(
if (_)
...
else
...
@dkomanov
dkomanov / BooleanOps2.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] BooleanOps2
View BooleanOps2.scala
implicit class BooleanToFutureExtensions(val v: Boolean) extends AnyVal {
def &&(other: => Future[Boolean]): Future[Boolean] =
if (v) other else Future.successful(false)
def ||(other: => Future[Boolean]): Future[Boolean] =
if (v) Future.successful(true) else other
}
@dkomanov
dkomanov / IfElseAsyncMultipleOps2.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] multiple statements mixed Boolean and Future[Boolean]
View IfElseAsyncMultipleOps2.scala
def condition1: Boolean = ...
def condition2: Boolean = ...
def condition3: Future[Boolean] = ...
val condition = condition1 && (condition2 || condition3)
condition.flatMap(
if (_)
...
else
...
@dkomanov
dkomanov / BooleanOps3.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] BooleanOps3
View BooleanOps3.scala
implicit class FutureOfBooleanExtensions(val future: Future[Boolean]) extends AnyVal {
def &&(other: => BooleanOrFutureOfBoolean): Future[Boolean] =
v.flatMap(
if (_)
other match {
case Bool(otherValue) => Future.successful(otherValue)
case Fut(future) => future
}
else
Future.successful(false)
@dkomanov
dkomanov / ForComprehensions1.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] for-comprehensions 1
View ForComprehensions1.scala
def getMovie: Future[Movie]
def getActors(movie: Movie): Future[Seq[Actor]]
def getPlot(movie: Movie): Future[MoviePlot]
for {
movie <- getMovie
actors <- getActors(movie)
plot <- getPlot(movie)
} yield MovieDescription(movie, actors, plot)
@dkomanov
dkomanov / ForComprehensions2.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] for-comprehensions 2
View ForComprehensions2.scala
def hasPermissions: Future[Boolean]
for {
isPermitted <- hasPermissions
if isPermitted
movie <- getMovie
if !movie.hidden
actors <- getActors(movie)
plot <- getPlot(movie)
} yield MovieDescription(movie, actors, plot)