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

Version Path
trackler-2.2.1.180 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.179 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.178 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.177 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.176 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.175 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.174 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.173 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.172 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.171 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.170 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.169 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.167 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.166 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.165 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.164 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.163 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.162 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.161 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.160 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs