on master help sign in
added bank account door example
commitf0371f226015898fdb4494e391b2b1ef8515d0e1 authorDavid Kerschner <dkerschner@hcoop.net> dateWed 22 May 2019, 14:47 (7 years ago) parent4e68eeb
1 file changed+79
learn/hoon/workbook/bank.md+79
learn/hoon/workbook/bank.mdnew file+79
@@ -0,0 +1,79 @@
1++++
2+title = "Bank Account"
3+weight = 0
4+template = "doc.html"
5++++
6+
7+Write a door that can act as a bank account with the ability to withdraw, deposit, and check the balance.
8+
9+```
10+:- %say
11+|= *
12+:- %noun
13+=< =~ new-account
14+ (deposit 100)
15+ (deposit 100)
16+ (withdraw 50)
17+ balance
18+ ==
19+|%
20+++ new-account
21+ |_ balance=@ud
22+ ++ deposit
23+ |= amount=@ud
24+ +>.$(balance (add balance amount))
25+ ++ withdraw
26+ |= amount=@ud
27+ +>.$(balance (sub balance amount))
28+ --
29+--
30+```
31+
32+We start with the three lines we have in every `%say` generator.
33+
34+```
35+:- %say
36+|= *
37+:- %noun
38+```
39+
40+We're creating a cell the head of which is a `%say` and the tail of which is a gate that produces a cell with a head of the mark of the kind of data we are going to produce.
41+
42+```
43+=< =~ new-account
44+ (deposit 100)
45+ (deposit 100)
46+ (withdraw 50)
47+ balance
48+ ==
49+```
50+
51+Here we're going to compose two runes using `=<` which has inverted arguments. We use this rune to keep the heaviest twig to the bottom of the code.
52+
53+`=~` is a rune that composes multiple expressions. We take `new-account` and use that as the subject for the call to `deposit`. `deposit` and `withdraw` both produce a new version of the door which is used in subsequent calls which is why we are able to chain them in this fashion, the final reference is to `balance` which is the balance contain in the core we are about to examine.
54+
55+```
56+|%
57+++ new-account
58+ |_ balance=@ud
59+ ++ deposit
60+ |= amount=@ud
61+ +>.$(balance (add balance amount))
62+ ++ withdraw
63+ |= amount=@ud
64+ +>.$(balance (sub balance amount))
65+ --
66+--
67+```
68+
69+We've chosen here to wrap our door in it's own core to emulate the style of programming that is used when creating libraries. `new-account` is the name of our door. Recall that a door is a core with one or more arms that has a sample. Here our door has a sample of one `@ud` with the face `balance` and two arms, `deposit` and `withdraw`.
70+
71+Each of these arms produces a gate which takes an `@ud` argument. Each of these gates has a similar bit of code inside.
72+
73+```
74++>.$(balance (add balance amount))
75+```
76+
77+With wing syntax remember that we're looking for `+>`, or the 3rd element, out of `$`, which is to say the subject of the gate we are in, which is the entire `new-account` door. We change `balance` to be the result of adding `balance` and `amount` and produce the door as the result. `withdraw` functions the same way only doing subtraction instead of addition.
78+
79+The important point to notice here is that the sample, `balance`, is stored as part of the door rather than existing outside of it.