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.159 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.158 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.157 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.156 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.155 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.154 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.153 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.152 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.151 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.150 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.149 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.148 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.147 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.146 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.145 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.144 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.143 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.142 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.141 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.140 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs