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
public interface ArticleService { | |
ArticleDetails getArticleDetails(Long articleId) throws ArticleNotFoundException; | |
} |
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
trait ArticleService { | |
def getArticleDetails(articleId: Long): ArticleDetails | |
} |
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
trait ArticleService { | |
def getArticleDetails(articleId: Long): Either[ArticleNotFound, ArticleDetails] | |
} |
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
trait ArticleService { | |
def getArticleDetails(articleId: Long): Future[Either[ArticleNotFound, ArticleDetails]] | |
} |
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
case object ArticleNotFound | |
trait ArticleService { | |
def getArticleDetails(articleId: Long): Future[Either[ArticleNotFound.type, ArticleDetails]] | |
def getArticleIdByUniqueName(articleName: String): Future[Either[ArticleNotFound.type, Long]] | |
} |
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
def getArticleDetails(articleName: String): Future[Either[ArticleNotFound.type, ArticleDetails]] = { | |
for { | |
articleId <- articleService.getArticleIdByUniqueName(articleName).right | |
article <- articleService.getArticleDetails(articleId).right | |
} yield article | |
} |
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
case object ArticleNotFound | |
trait ArticleService { | |
def getArticleDetails(articleId: Long): Future[\/[ArticleNotFound.type, ArticleDetails]] | |
def getArticleIdByUniqueName(articleName: String): Future[\/[ArticleNotFound.type, Long]] | |
} | |
def getArticleDetails(articleName: String): Future[\/[ArticleNotFound.type, ArticleDetails]] = { | |
for { | |
articleId <- articleService.getArticleIdByUniqueName(articleName) |
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
implicit val FutureFunctor = new Functor[Future] { | |
def map[A, B](a: Future[A])(f: A => B): Future[B] = a map f | |
} | |
implicit val FutureMonad = new Monad[Future] { | |
def point[A](a: => A): Future[A] = Future(a) | |
def bind[A, B](fa: Future[A])(f: (A) => Future[B]): Future[B] = fa flatMap f | |
} |
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
def getArticleDetails(articleName: String): Future[\/[ArticleNotFound.type, ArticleDetails]] = { | |
val r = for { | |
articleId <- EitherT(articleService.getArticleIdByUniqueName(articleName)) | |
article <- EitherT(articleService.getArticleDetails(articleId)) | |
} yield article | |
r.run | |
} |
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
import com.twitter.util.Future | |
import scalaz.{EitherT, Monad, Functor, \/} | |
object EitherTest { | |
val articleService = new ArticleService { | |
def getArticleDetails(articleId: Long): Future[\/[ArticleNotFound.type, ArticleDetails]] = ??? | |
def getArticleIdByUniqueName(articleName: String): Future[\/[ArticleNotFound.type, Long]] = ??? | |
} |
OlderNewer