Reputation: 57
I have a Future[List[Result]]
I need to run a logic for example if all items in the list are Result.Ok
then return Result.Ok
(or true), else return Result.BadRequest
(or false)
I've tried:
futureResultList.map(temp => temp.forall(_ == true))
But this code works only when the list contains booleans. It does not work if it contains Result
objects (When changing check to _ == Result.Ok
)
Upvotes: 2
Views: 1032
Reputation: 1482
The simplest way to overview such an example is mapping. If you have a List of Result, but need a List of Boolean, then map it. Don't be afraid of mapping multiple times, it's way more readable in the long run :)
val futureResultList: Future[List[Result]] = ???
val futureBooleanList: Future[List[Boolean]] = futureResultList.map(_.map(_ == Result.OK))
val result = futureBooleanList.map(temp => temp.forall(_ == true))
or slightly more concise:
val futureResultList: Future[List[Result]] = ???
val result = futureResultList.map(_.map(_ == Result.OK)
.forall(_ == true))
and of course as the other people suggest, go directly to checking the equality on the Result object. But mapping is often useful to navigate between types and getting a clearer and more readable code.
Upvotes: 0
Reputation: 27356
To return a Boolean
you just need to change the predicate in the forall
call:
futureResultList.map(_.forall(_ == Result.Ok))
Upvotes: 8