Skip to content

Instantly share code, notes, and snippets.

@3v0k4
Last active December 21, 2019 00:17
Show Gist options
  • Save 3v0k4/4073a0d7eeb13a515b82f286fc4f5cfd to your computer and use it in GitHub Desktop.
Save 3v0k4/4073a0d7eeb13a515b82f286fc4f5cfd to your computer and use it in GitHub Desktop.
data Transaction
= Deposit Info
| Withdraw Info
type Info =
{ timestamp :: DateTime
, amount :: Int
}
deposit :: Int -> StateT (Array Transaction) Effect Unit
deposit amount = do
ts <- lift nowDateTime
let t = Deposit { timestamp: ts, amount: amount }
modify_ \ts -> ts <> [t]
withdraw :: Int -> StateT (Array Transaction) Effect Unit
withdraw amount = do
ts <- lift nowDateTime
let t = Withdraw { timestamp: ts, amount: amount }
modify_ \ts -> ts <> [t]
printStatement :: StateT (Array Transaction) Effect Unit
printStatement = do
s <- gets toStatement
lift $ log s
toStatement :: Array Transaction -> String
toStatement =
fst <<< foldl fnc (Tuple "" 0)
where
fnc (Tuple s i) (Deposit d) =
Tuple (s <> "\n" <> joinWith " " [ show d.timestamp, show d.amount, show $ i + d.amount]) (i + d.amount)
fnc (Tuple s i) (Withdraw w) =
Tuple (s <> "\n" <> joinWith " " [ show w.timestamp, "-" <> show w.amount, show $ i - w.amount]) (i - w.amount)
main :: Effect Unit
main = do
flip evalStateT [] do
deposit 500
withdraw 100
printStatement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment