Created
January 21, 2017 13:09
-
-
Save noorulhaq/48d7cdabbfa0de5dd175bbce47735620 to your computer and use it in GitHub Desktop.
This Gist demostrate a situation where OptionT monad transformer is used to avoid extra mapping to extract account from stacked Future[Option] monands within Kleisli
This file contains 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 AccountService { | |
type Valid[A] = OptionT[Future, A] | |
type AccountOperation[A] = Kleisli[Valid, AccountRepository, A] | |
def debit(no: String, amount: Amount): AccountOperation[Account] = ??? | |
def credit(no: String, amount: Amount): AccountOperation[Account] = ??? | |
def transfer(from: String, to: String, amount: Amount) : AccountOperation[(Account,Account)] = for { | |
debitAcc <- debit(from, amount) | |
creditAcc <- credit(to, amount) | |
} yield (debitAcc, creditAcc) // You need OptionT transformer inside Kleisli/ReaderT to avoid extra mapping to extract account from stacked Future[Option] monads. | |
} | |
object AccountService extends AccountService | |
object AccountRepository extends AccountRepositoryInMemory |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment