Created
June 14, 2014 17:26
-
-
Save snipsnipsnip/de10f02506e7db8ffba9 to your computer and use it in GitHub Desktop.
BankAccount example in J (via http://sumim.no-ip.com/wiki/849)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NB. SELF's BankAccount example in J | |
NB. http://sumim.no-ip.com/wiki/849 | |
bankAccount =. conew 'z' | |
dollars__bankAccount =: 200 | |
NB. Not present in the original code. Defined a getter to make it customizable. | |
getDollars__bankAccount =: verb define | |
dollars | |
) | |
NB. Not present in the original code. Defined a setter to make it customizable. | |
setDollars__bankAccount =: verb define | |
dollars =: y | |
) | |
deposit__bankAccount =: monad define | |
setDollars (getDollars '') + y | |
) | |
withdraw__bankAccount =: monad define | |
setDollars 0 >. (getDollars '') - y | |
) | |
echo getDollars__bankAccount '' NB. ==> 200 | |
echo deposit__bankAccount 50 NB. ==> 250 | |
withdraw__bankAccount 100 | |
echo getDollars__bankAccount '' NB. ==> 150 | |
withdraw__bankAccount 200 | |
echo getDollars__bankAccount '' NB. ==> 0 | |
account =. conew > bankAccount | |
deposit__account 500 | |
echo getDollars__account ''NB. ==> 500 | |
echo getDollars__bankAccount '' NB. ==> 0 # プロトタイプのスロットには影響なし。 | |
stockAccount =. conew > bankAccount | |
numShares__stockAccount =: 10 | |
pricePerShare__stockAccount =: 30 | |
getNumShares__stockAccount =: verb define | |
numShares | |
) | |
getDollars__stockAccount =: verb define | |
numShares * pricePerShare | |
) | |
setDollars__stockAccount =: monad define | |
numShares =: y % pricePerShare | |
) | |
echo getDollars__stockAccount '' NB. ==> 300 | |
setDollars__stockAccount 150 | |
echo getDollars__stockAccount '' NB. ==> 150 | |
echo getNumShares__stockAccount '' NB. ==> 5 # 株数値が変更されている。 | |
stock =. conew > stockAccount | |
setDollars__stock 600 | |
echo getNumShares__stock '' NB. ==> 20 | |
deposit__stock 60 | |
echo getDollars__stock '' NB. ==> 660 | |
echo getNumShares__stock '' NB. ==> 22 | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment