view 6-Clojure/banking.clj @ 93:39084e2b8744

Add a function for word-aware text wrapping Potentially hugely inefficient because we iterate through the string character by character, but then splitting it first and iterating over words still needs to iterate over the string to know where to split.
author IBBoard <dev@ibboard.co.uk>
date Tue, 18 Jun 2019 21:05:00 +0100
parents 67e2bb3bcccd
children
line wrap: on
line source

(def accounts (ref []))

(defn add_account [accts]
    (dosync (alter accts conj 0)))

(defn credit [accts acct_num amount]
    (dosync
        (alter accts assoc acct_num (+ (accts acct_num) amount))
    )
)

(defn debit [accts acct_num amount]
    (dosync
        (alter accts assoc acct_num (- (accts acct_num) amount))
    )
)

(defn transfer [accts acct_from acct_to amount]
    (dosync
        (debit accts acct_from amount)
        (credit accts acct_to amount)
    )
)

(add_account accounts)
(add_account accounts)
(add_account accounts)
(credit accounts 0 100)
(credit accounts 1 50)
(credit accounts 2 500)
(transfer accounts 2 1 150)
(println @accounts) ; We should get [100, 200, 350]