Skip to content

Instantly share code, notes, and snippets.

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