Skip to content

Instantly share code, notes, and snippets.

@Gryff
Created March 12, 2019 16:18
Show Gist options
  • Save Gryff/d04fd53d0d0f543ac3eaafc87972a46c to your computer and use it in GitHub Desktop.
Save Gryff/d04fd53d0d0f543ac3eaafc87972a46c to your computer and use it in GitHub Desktop.
-- 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