Skip to content

Instantly share code, notes, and snippets.

@noorulhaq
Last active January 21, 2017 13:10
Show Gist options
  • Save noorulhaq/d620f750eafb0e9cd8496ce99b52ac2e to your computer and use it in GitHub Desktop.
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.
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