Skip to content

Instantly share code, notes, and snippets.

@ivanopagano
Created December 4, 2017 09:44
Show Gist options
  • Save ivanopagano/efffd4d7da0f9966f123524e99c09b04 to your computer and use it in GitHub Desktop.
Save ivanopagano/efffd4d7da0f9966f123524e99c09b04 to your computer and use it in GitHub Desktop.
desugaring of a for comprehension for the herding-cats either example
//refers to http://eed3si9n.com/herding-cats/stacking-future-and-either.html
def isFriends1(user1: Long, user2: Long)
(implicit ec: ExecutionContext): Future[Either[Error, Boolean]] =
for {
a <- followers(user1)
b <- followers(user2)
} yield for {
x <- a.right
y <- b.right
} yield x.exists(_.id == user2) && y.exists(_.id == user1)
def isFriends1Desugared(user1: Long, user2: Long)
(implicit ec: ExecutionContext): Future[Either[Error, Boolean]] =
followers(user1).flatMap { a =>
followers(user2).map { b =>
a.right.flatMap { x =>
b.right.map { y =>
x.exists(_.id == user2) && y.exists(_.id == user1)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment