Skip to content

Instantly share code, notes, and snippets.

Avatar

Dmitry Komanov dkomanov

View GitHub Profile
@dkomanov
dkomanov / Option2.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] option 2
View Option2.scala
for {
movie <- findMovie("Dark Waters") orFail new MovieNotFoundException("...")
} yield movie
@dkomanov
dkomanov / Option1.scala
Last active May 31, 2020
[writing-async-app-in-scala-part-1] option 1
View Option1.scala
implicit class FutureOfOptionExtensions[T](val v: Future[Option[T]]) extends AnyVal {
def orFail(e: => Throwable): Future[T] =
v.flatMap(_.fold[Future[T]](Future.failed(e), Future.successful)
}
@dkomanov
dkomanov / ForComprehensions7.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] for-comprehensions 7
View ForComprehensions7.scala
for {
_ <- hasPermissions orFail new PermissionDeniedException("...")
movie <- getMovie.filterOrFail(!_.hidden, new HiddenMovieException("..."))
actors <- getActors(movie)
plot <- getPlot(movie)
} yield MovieDescription(movie, actors, plot)
@dkomanov
dkomanov / ForComprehensions6.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] for-comprehensions 6
View ForComprehensions6.scala
implicit class FutureExtensions[T](val future: Future[T]) extends AnyVal {
def filterOrFail(f: T => Boolean, e: => Throwable): Future[T] =
future.flatMap(value => if (f(value)) Future.successful(value) else Future.failed(e))
}
@dkomanov
dkomanov / ForComprehensions5.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] for-comprehensions 5
View ForComprehensions5.scala
movie <- getMovie.filter(!_.hidden).recoverWith {
case _: NoSuchElementException => Future.failed(new HiddenMovieException("..."))
}
@dkomanov
dkomanov / ForComprehensions4.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] for-comprehensions 4
View ForComprehensions4.scala
for {
_ <- hasPermissions orFail new PermissionDeniedException("...")
movie <- getMovie
if !movie.hidden
actors <- getActors(movie)
plot <- getPlot(movie)
} yield MovieDescription(movie, actors, plot)
@dkomanov
dkomanov / ForComprehensions3.scala
Created May 22, 2020
[writing-async-app-in-scala-part-1] for-comprehensions 3
View ForComprehensions3.scala
implicit class BooleanFutureExtensions(val future: Future[Boolean]) extends AnyVal {
def orFail(e: => Throwable): Future[Boolean] =
future.transform(t => if (t.isSuccess && !t.get) Failure(e) else t)
}
@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)
@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 / 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)