Sha256: 17185e6382652d2483a99ff3a1d576107c1f398d09a6e188955c982b3369735c
Contents?: true
Size: 784 Bytes
Versions: 68
Compression:
Stored size: 784 Bytes
Contents
module BankAccount ( BankAccount , openAccount, closeAccount , getBalance, incrementBalance) where import Control.Concurrent.STM (TVar, atomically, newTVar, readTVar, writeTVar) newtype BankAccount = BankAccount { unBankAccount :: TVar (Maybe Int) } openAccount :: IO BankAccount openAccount = atomically $ BankAccount <$> newTVar (Just 0) closeAccount :: BankAccount -> IO () closeAccount = atomically . flip writeTVar Nothing . unBankAccount getBalance :: BankAccount -> IO (Maybe Int) getBalance = atomically . readTVar . unBankAccount incrementBalance :: BankAccount -> Int -> IO (Maybe Int) incrementBalance acct delta = atomically $ do let b = unBankAccount acct bal <- fmap (delta +) <$> readTVar b writeTVar b bal return bal
Version data entries
68 entries across 68 versions & 1 rubygems