Created
March 12, 2019 16:18
-
-
Save Gryff/d04fd53d0d0f543ac3eaafc87972a46c to your computer and use it in GitHub Desktop.
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
-- our Transaction type | |
data Transaction = Deposit Int | Withdrawal Int | |
-- our bank functions | |
deposit :: Monad m => Int -> StateT [Transaction] m () | |
deposit amount = modify $ \transactions -> transactions ++ [Deposit amount] | |
withdraw :: Monad m => Int -> StateT [Transaction] m () | |
withdraw amount = modify $ \transactions -> transactions ++ [Withdrawal amount] | |
printStatement :: (Monad m, MonadStatementPrinter m) => StateT [Transaction] m () | |
printStatement = do | |
transactions <- get | |
let statement = toStatement transactions | |
lift $ printSt statement | |
-- in our test file | |
it "deposits money" $ do | |
runIdentity (execStateT (deposit 100) newBank) `shouldBe` [Deposit 100] | |
it "withdraws money" $ do | |
runIdentity (execStateT (withdraw 100) newBank) `shouldBe` [Withdrawal 100] | |
it "prints a statement" $ do | |
execWriter (evalStateT printStatement [Deposit 100]) `shouldBe` "Desposited 100 | Balance 100\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment