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.139 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.138 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.137 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.136 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.135 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.134 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.133 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.132 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.131 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.130 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.129 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.128 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.127 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.126 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.125 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.124 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.123 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.122 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.121 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs
trackler-2.2.1.120 tracks/haskell/exercises/bank-account/examples/success-standard/src/BankAccount.hs