Skip to content

Instantly share code, notes, and snippets.

@tobnee
Created June 24, 2014 12:31
Show Gist options
  • Save tobnee/2b0c64da4602c1a7c471 to your computer and use it in GitHub Desktop.
Save tobnee/2b0c64da4602c1a7c471 to your computer and use it in GitHub Desktop.
EitherFuture
type Result = String
type Error = String
sealed trait DError
case object NotFound extends DError
case class ValidationErrors(err: Set[Error]) extends DError
val id = "10"
val data = "data"
val b: Future[Either[DError, Result]] = for {
content <- loadA(id)
validatedContent <- validateAndLift(content)
res <- updateAndLift(content)
} yield content
def validateAndLift(c: Either[DError, Nothing]) =
c.right.map(content => validateAsEither(content))
.fold(err => failedValidation(err), a => a)
def validateAsEither(content: Nothing) = {
validate(data, content)
.map(err => if (err.isEmpty) Right(content) else Left(ValidationErrors(err)))
}
def updateAndLift(c: Either[DError, Nothing]) =
c.right.map(g => updateA(id, g))
.fold(err => failedValidation(err), a => a.map(res => Right(res)))
def failedValidation(err: DError) = Future.successful(Left(err))
def loadA[A](id: String): Future[Either[DError, A]] = ???
def validate[A](data: Any, stuff: A): Future[Set[Error]] = ???
def updateA(id: String, data: Any): Future[Result] = ???
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment