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
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)) | |
} |
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
movie <- getMovie.filter(!_.hidden).recoverWith { | |
case _: NoSuchElementException => Future.failed(new HiddenMovieException("...")) | |
} |
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
for { | |
_ <- hasPermissions orFail new PermissionDeniedException("...") | |
movie <- getMovie | |
if !movie.hidden | |
actors <- getActors(movie) | |
plot <- getPlot(movie) | |
} yield MovieDescription(movie, actors, plot) |
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
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) | |
} |
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
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) |
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
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) |
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
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) |
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
def condition1: Boolean = ... | |
def condition2: Boolean = ... | |
def condition3: Future[Boolean] = ... | |
val condition = condition1 && (condition2 || condition3) | |
condition.flatMap( | |
if (_) | |
... | |
else | |
... |
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
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 | |
} |
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
def condition1: Future[Boolean] = ... | |
def condition2: Future[Boolean] = ... | |
def condition3: Future[Boolean] = ... | |
val condition = condition1 && (condition2 || condition3) | |
condition.flatMap( | |
if (_) | |
... | |
else | |
... |