Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 6-Clojure/banking.clj @ 85:67e2bb3bcccd
Add bank account processing with refs and dosync calls
Also included a "transfer" function to atomically transfer money.
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 09 Jun 2019 19:47:01 +0100 |
parents | |
children |
rev | line source |
---|---|
85
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 (def accounts (ref [])) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 (defn add_account [accts] |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 (dosync (alter accts conj 0))) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 (defn credit [accts acct_num amount] |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 (dosync |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 (alter accts assoc acct_num (+ (accts acct_num) amount)) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 ) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 ) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 (defn debit [accts acct_num amount] |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 (dosync |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 (alter accts assoc acct_num (- (accts acct_num) amount)) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 ) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 ) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 (defn transfer [accts acct_from acct_to amount] |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 (dosync |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 (debit accts acct_from amount) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 (credit accts acct_to amount) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 ) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 ) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 (add_account accounts) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 (add_account accounts) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 (add_account accounts) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 (credit accounts 0 100) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 (credit accounts 1 50) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 (credit accounts 2 500) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 (transfer accounts 2 1 150) |
67e2bb3bcccd
Add bank account processing with refs and dosync calls
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 (println @accounts) ; We should get [100, 200, 350] |