Last active
February 19, 2021 19:04
-
-
Save joshuakfarrar/3d08db611492a9c35ac4a55c37280390 to your computer and use it in GitHub Desktop.
use monad transformers for great elegance!
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 cats.data.OptionT | |
| import cats.implicits._ | |
| /* | |
| * in my domain, the confirmation algebra provides Confirmable, | |
| * which is parameterized over the type of id of the thing, probably | |
| * a user, to be confirmed, and the type of the confirmation token | |
| */ | |
| type C = Confirmable[UUID, SecureRandomId] | |
| def getConfirmableFromToken(token: SecureRandomId): OptionT[F, C] = for { | |
| (userId, confirmation) <- confirmationService.getByToken(token) | |
| user <- userService.find(userId) | |
| } yield ConfirmableUser(user, confirmation) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OptionT[F[_], A]is a monad transformer, representingF[Option[A]], where F represents an arbitrary functor (or monad, because monads are just composable functors!), and A is some type of thing wrapped by that functor, e.g.OptionT[IO, User]encodesIO[Option[User]]. Monad transformers compose neatly and with them you can avoid some boilerplate around monad composition with.flatMapand.mapFor more information on this particular implementation of
OptionT, see the Cats documentation: https://typelevel.org/cats/datatypes/optiont.html