Last active
January 21, 2017 13:10
-
-
Save noorulhaq/d620f750eafb0e9cd8496ce99b52ac2e to your computer and use it in GitHub Desktop.
This Gist demostrate a situation where Kleisli/ReaderT monad transformer is used to avoid extra mapping to extract account from Future monad.
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 { | |
def debit(no: String, amount: Amount): Kleisli[Future,AccountRepository, Account] = ??? | |
def credit(no: String, amount: Amount): ReaderT[Future,AccountRepository, Account] = ??? | |
def transfer(from: String, to: String, amount: Amount) : ReaderT[Future,AccountRepository, (Account,Account)] = for { | |
debitAcc <- debit(from, amount) | |
creditAcc <- credit(to, amount) | |
} yield (debitAcc, creditAcc) // If you do not use ReaderT/Kleisli here then you need extra mapping to extract accounts from Future | |
} | |
object AccountService extends AccountService | |
object AccountRepository extends AccountRepositoryInMemory | |
AccountService.transfer("from", "to", 10).run(AccountRepository) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment