Created
December 4, 2017 09:44
-
-
Save ivanopagano/efffd4d7da0f9966f123524e99c09b04 to your computer and use it in GitHub Desktop.
desugaring of a for comprehension for the herding-cats either example
This file contains hidden or 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
//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