Skip to content

Instantly share code, notes, and snippets.

View longliveenduro's full-sized avatar

Chris Wewerka longliveenduro

View GitHub Profile
public interface ArticleService {
ArticleDetails getArticleDetails(Long articleId) throws ArticleNotFoundException;
}
trait ArticleService {
def getArticleDetails(articleId: Long): ArticleDetails
}
trait ArticleService {
def getArticleDetails(articleId: Long): Either[ArticleNotFound, ArticleDetails]
}
trait ArticleService {
def getArticleDetails(articleId: Long): Future[Either[ArticleNotFound, ArticleDetails]]
}
case object ArticleNotFound
trait ArticleService {
def getArticleDetails(articleId: Long): Future[Either[ArticleNotFound.type, ArticleDetails]]
def getArticleIdByUniqueName(articleName: String): Future[Either[ArticleNotFound.type, Long]]
}
def getArticleDetails(articleName: String): Future[Either[ArticleNotFound.type, ArticleDetails]] = {
for {
articleId <- articleService.getArticleIdByUniqueName(articleName).right
article <- articleService.getArticleDetails(articleId).right
} yield article
}
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)
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
}
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
}
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]] = ???
}