A peer-to-peer stack for code collaboration
;; Account stuff. Should be moved elsewhere when we have an import mechanism
(def create-currency (fn []
;; The map of all accounts
(def accounts (ref (dict)))
;; Create an account with 10 coins.
(def new-account (fn [name]
(modify-ref accounts (fn [acc]
(insert name 10 acc)))))
;; Get an account's balance
(def account-balance (fn [name]
(lookup name (read-ref accounts))))
;; Transfer money
(def transfer (fn [from to amount]
(def acc (read-ref accounts))
(write-ref accounts
(if (> amount (lookup from acc))
(print! "not enough balance")
(modify-map
to
(fn [x] (+ x amount))
(modify-map from (fn [x] (- x amount)) acc))))))
(def dispatch (fn [m]
(cond
(eq? m 'new-account) new-account
(eq? m 'account-balance) account-balance
(eq? m 'transfer) transfer
:else (print! "error: currency- unknown message"))))
dispatch))
(def currency (create-currency))
(def new-eval (fn [expr]
(cond
(eq? (head expr) 'new-account) ((currency 'new-account) (nth 1 expr))
(eq? (head expr) 'account-balance) ((currency 'account-balance) (nth 2 expr))
(eq? (head expr) 'transfer) ((currency 'transfer) (nth 1 expr) (nth 2 expr) (nth 3 expr)))
else: (print! "transaction must be new-account, transfer, or account-balance")))