-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclosures.clj
51 lines (44 loc) · 1.68 KB
/
closures.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
;; This file contains a conversion of an example in the LFE code base, that in
;; turn was a conversion of some of the Common Lisp code from the beginning of
;; Chapter 13 in Peter Norvig's "Paradigms of Artificial Intelligence
;; Programming: Case Studies in Common Lisp".
;;
;; For example usage, see the README file, in particular:
;; https://github.com/oubiwann/maintaining-state-in-clojure#state-via-closures
;;
;; You may also view the unit tests for example usage.
(ns state-examples.closures)
(defn new-account [name balance interest-rate]
(fn [message]
(case message
:name (fn [] name)
:balance (fn [] balance)
:interest (fn []
(new-account
name
(+ balance (* balance interest-rate))
interest-rate))
:deposit (fn [amt]
(new-account
name
(+ balance amt)
interest-rate))
:withdraw (fn [amt]
(cond (<= amt balance)
(new-account
name
(- balance amt)
interest-rate)
:else (throw (Exception. ": Insufficient funds.")))))))
(defn get-account-method [object command]
(apply object [command]))
(defn get-name [object]
(apply (get-account-method object :name) []))
(defn get-balance [object]
(apply (get-account-method object :balance) []))
(defn apply-interest [object]
(apply (get-account-method object :interest) []))
(defn withdraw [object amt]
(apply (get-account-method object :withdraw) [amt]))
(defn deposit [object amt]
(apply (get-account-method object :deposit) [amt]))