Logical logic in the future [Boolean]

In non-competitive programming, we usually have the following logic:

boolean canIMarryher(){
    return iLoveHer() && myParentsLoveHer() && shesHot() && sheSaidYes();
}

My question is, what if all of these (or some of these conditions) are Future [Boolean] in scala? Can I get a clear method like in the example above?

Update As you know, in logic at runtime we will have “optimization”: immediately return when using &&and meet falseor using ||and meet true. Can I use it in Future [Boolean]?

Regards, Drew

+4
source share
2 answers

"". , , , false.

, , . , . , , . , .

, :

def all(futures: Future[Boolean]*)(implicit executor: ExecutionContext): Future[Boolean] = {
  Future.find(futures) { !_ } map { _.isEmpty }
}

def canIMarryher = all(iLoveHer, myParentsLoveHer, shesHot, sheSaidYes)

, && || Future[Boolean]

+8

, Future.reduce , , , :

val isThereAFuture: Future[Boolean] = 
  for {
    iLoveHer <- doILoveHer()
    myParentsLoveHer <- doMyParentsLoveHer()
    sheLovesMe <- doesSheLoveMe()
  } yield iLoveHer && myParentsLoveHer && sheLovesMe
+4

Source: https://habr.com/ru/post/1606070/


All Articles